LAL  7.5.0.1-ec27e42
LALHashTbl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Karl Wette
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with with program; see the file COPYING. If not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  */
19 
20 #ifndef _LALHASHTBL_H
21 #define _LALHASHTBL_H
22 
23 #include <lal/LALStdlib.h>
24 #include <lal/LALHashFunc.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * \defgroup LALHashTbl_h Header LALHashTbl.h
32  * \ingroup lal_utilities
33  * \author Karl Wette
34  * \brief Implementation of a generic hash table, following Chapter 5.2 of \cite open-data-structs .
35  */
36 /** @{ */
37 
38 /**
39  * Generic hash table with elements of type <tt>void *</tt>
40  */
41 typedef struct tagLALHashTbl LALHashTbl;
42 
43 /**
44  * Function which free memory associated with hash table element <tt>x</tt>
45  */
46 typedef void ( *LALHashTblDtorFcn )( void *x );
47 
48 /**
49  * Hash function for the hash table element <tt>x</tt>
50  */
51 typedef UINT8( *LALHashTblHashFcn )( const void *x );
52 
53 /**
54  * Hash function for the hash table element <tt>x</tt>, with a parameter \c param
55  */
56 typedef UINT8( *LALHashTblHashParamFcn )( void *param, const void *x );
57 
58 /**
59  * Function which compares hash table elements <tt>x</tt> and <tt>y</tt>
60  */
61 typedef int ( *LALHashTblCmpFcn )( const void *x, const void *y );
62 
63 /**
64  * Function which compares hash table elements <tt>x</tt> and <tt>y</tt>, with a parameter \c param
65  */
66 typedef int ( *LALHashTblCmpParamFcn )( void *param, const void *x, const void *y );
67 
68 /**
69  * Create a hash table
70  */
71 LALHashTbl *XLALHashTblCreate(
72  LALHashTblDtorFcn dtor, /**< [in] Function to free memory of elements of hash, if required */
73  LALHashTblHashFcn hash, /**< [in] Hash function for hash table elements */
74  LALHashTblCmpFcn cmp /**< [in] Hash table element comparison function */
75  );
76 
77 /**
78  * Create a hash table with parameterised hash and comparison functions
79  */
80 LALHashTbl *XLALHashTblCreate2(
81  LALHashTblDtorFcn dtor, /**< [in] Function to free memory of elements of hash, if required */
82  LALHashTblHashParamFcn hash, /**< [in] Parameterised hash function for hash table elements */
83  void *hash_param, /**< [in] Parameter to pass to hash function */
84  LALHashTblCmpParamFcn cmp, /**< [in] Parameterised hash table element comparison function */
85  void *cmp_param /**< [in] Parameter to pass to comparison function */
86  );
87 
88 /**
89  * Destroy a hash table and its elements
90  */
92  LALHashTbl *ht /**< [in] Pointer to hash table */
93  );
94 
95 /**
96  * Clear a hash table
97  */
99  LALHashTbl *ht /**< [in] Pointer to hash table */
100  );
101 
102 /**
103  * Return the size of a hash table
104  */
105 int XLALHashTblSize(
106  const LALHashTbl *ht /**< [in] Pointer to hash table */
107  );
108 
109 /**
110  * Find the element matching <tt>x</tt> in a hash table; if found, return in <tt>*y</tt>
111  */
112 int XLALHashTblFind(
113  const LALHashTbl *ht, /**< [in] Pointer to hash table */
114  const void *x, /**< [in] Hash element to match */
115  const void **y /**< [out] Pointer to matched hash element, or NULL if not found */
116  );
117 
118 /**
119  * Add an element to a hash table
120  */
121 int XLALHashTblAdd(
122  LALHashTbl *ht, /**< [in] Pointer to hash table */
123  void *x /**< [in] Hash element to add */
124  );
125 
126 /**
127  * Find the element matching <tt>x</tt> in a hash table; if found, remove it and return in <tt>*y</tt>
128  */
130  LALHashTbl *ht, /**< [in] Pointer to hash table */
131  const void *x, /**< [in] Hash element to match */
132  void **y /**< [out] Pointer to matched hash element, which has been removed from
133  the hash table, or NULL if not found */
134  );
135 
136 /**
137  * Find the element matching <tt>x</tt> in a hash table; if found, remove and destroy it
138  */
140  LALHashTbl *ht, /**< [in] Pointer to hash table */
141  const void *x /**< [in] Hash element to match */
142  );
143 
144 /** @} */
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif // _LALHASHTBL_H
static size_t hash(const char *s)
Definition: LALDict.c:51
static int cmp(REAL4Sequence *a, REAL4Sequence *b)
Definition: SequenceTest.c:62
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
int XLALHashTblFind(const LALHashTbl *ht, const void *x, const void **y)
Find the element matching x in a hash table; if found, return in *y
Definition: LALHashTbl.c:185
LALHashTbl * XLALHashTblCreate2(LALHashTblDtorFcn dtor, LALHashTblHashParamFcn hash, void *hash_param, LALHashTblCmpParamFcn cmp, void *cmp_param)
Create a hash table with parameterised hash and comparison functions.
Definition: LALHashTbl.c:101
void(* LALHashTblDtorFcn)(void *x)
Function which free memory associated with hash table element x
Definition: LALHashTbl.h:46
UINT8(* LALHashTblHashParamFcn)(void *param, const void *x)
Hash function for the hash table element x, with a parameter param.
Definition: LALHashTbl.h:56
LALHashTbl * XLALHashTblCreate(LALHashTblDtorFcn dtor, LALHashTblHashFcn hash, LALHashTblCmpFcn cmp)
Create a hash table.
Definition: LALHashTbl.c:86
int(* LALHashTblCmpParamFcn)(void *param, const void *x, const void *y)
Function which compares hash table elements x and y, with a parameter param.
Definition: LALHashTbl.h:66
int XLALHashTblSize(const LALHashTbl *ht)
Return the size of a hash table.
Definition: LALHashTbl.c:177
int XLALHashTblAdd(LALHashTbl *ht, void *x)
Add an element to a hash table.
Definition: LALHashTbl.c:215
UINT8(* LALHashTblHashFcn)(const void *x)
Hash function for the hash table element x
Definition: LALHashTbl.h:51
int XLALHashTblExtract(LALHashTbl *ht, const void *x, void **y)
Find the element matching x in a hash table; if found, remove it and return in *y
Definition: LALHashTbl.c:252
int XLALHashTblRemove(LALHashTbl *ht, const void *x)
Find the element matching x in a hash table; if found, remove and destroy it.
Definition: LALHashTbl.c:287
void XLALHashTblDestroy(LALHashTbl *ht)
Destroy a hash table and its elements.
Definition: LALHashTbl.c:129
int XLALHashTblClear(LALHashTbl *ht)
Clear a hash table.
Definition: LALHashTbl.c:148
int(* LALHashTblCmpFcn)(const void *x, const void *y)
Function which compares hash table elements x and y
Definition: LALHashTbl.h:61
Generic hash table with elements of type void *
Definition: LALHashTbl.c:35
void * cmp_param
Definition: LALHashTbl.c:44
void * hash_param
Definition: LALHashTbl.c:42
LALHashTblDtorFcn dtor
Definition: LALHashTbl.c:40