Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LALBitset.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017 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#include <limits.h>
21
22#include <lal/LALBitset.h>
23#include <lal/LALHashTbl.h>
24
25#define BITS_PER_ELEM (sizeof(UINT8) * CHAR_BIT)
26
28 LALHashTbl *ht; /* Hash table which stores bits in UINT8s */
29};
30
31typedef struct {
34} elem;
35
36static UINT8 hash_elem( const void *x )
37{
38 const elem *ex = ( const elem * ) x;
39 UINT2 hval = 0;
40 XLALPearsonHash( &hval, sizeof( hval ), &ex->key, sizeof( ex->key ) );
41 return hval;
42}
43
44static int cmp_elem( const void *x, const void *y )
45{
46 const elem *ex = ( const elem * ) x;
47 const elem *ey = ( const elem * ) y;
48 return ex->key - ey->key;
49}
50
52 void
53 )
54{
55
56 /* Allocate memory for bitset struct */
57 LALBitset *bs = XLALCalloc( 1, sizeof( *bs ) );
58 XLAL_CHECK_NULL( bs != NULL, XLAL_ENOMEM );
59
60 /* Create hash table */
62 XLAL_CHECK_NULL( bs->ht != NULL, XLAL_EFUNC );
63
64 return bs;
65
66}
67
69 LALBitset *bs
70 )
71{
72 if ( bs != NULL ) {
73 XLALHashTblDestroy( bs->ht );
74 XLALFree( bs );
75 }
76}
77
79 LALBitset *bs
80 )
81{
82
83 /* Check input */
84 XLAL_CHECK( bs != NULL, XLAL_EFAULT );
85
86 /* Clear hash table */
88
89 return XLAL_SUCCESS;
90
91}
92
94 LALBitset *bs,
95 const UINT8 idx,
96 const BOOLEAN is_set
97 )
98{
99
100 /* Check input */
101 XLAL_CHECK( bs != NULL, XLAL_EFAULT );
102
103 /* Compute key and bit index */
104 const UINT8 key = idx / BITS_PER_ELEM;
105 const UINT8 bitidx = idx % BITS_PER_ELEM;
106
107 /* Extract element corresponding to key, or else create new element */
108 const elem x = { .key = key };
109 elem *y = NULL;
110 XLAL_CHECK( XLALHashTblExtract( bs->ht, &x, ( void ** ) &y ) == XLAL_SUCCESS, XLAL_EFUNC );
111 if ( y == NULL ) {
112 y = XLALCalloc( 1, sizeof( *y ) );
113 XLAL_CHECK( y != NULL, XLAL_ENOMEM );
114 y->key = key;
115 }
116
117 /* Set/unset bit in element */
118 if ( is_set ) {
119 y->bits |= ( ( (UINT8) 1 ) << bitidx );
120 } else {
121 y->bits &= ~( ( (UINT8) 1 ) << bitidx );
122 }
123
124 /* Add element back into hash */
126
127 return XLAL_SUCCESS;
128
129}
130
132 const LALBitset *bs,
133 const UINT8 idx,
134 BOOLEAN *is_set
135 )
136{
137
138 /* Check input */
139 XLAL_CHECK( bs != NULL, XLAL_EFAULT );
140 XLAL_CHECK( is_set != NULL, XLAL_EFAULT );
141
142 /* Compute key and bit index */
143 const UINT8 key = idx / BITS_PER_ELEM;
144 const UINT8 bitidx = idx % BITS_PER_ELEM;
145
146 /* Find element corresponding to key */
147 const elem x = { .key = key };
148 const elem *y = NULL;
149 XLAL_CHECK( XLALHashTblFind( bs->ht, &x, ( const void ** ) &y ) == XLAL_SUCCESS, XLAL_EFUNC );
150 if ( y == NULL ) {
151 *is_set = 0;
152 } else {
153
154 /* Check if bit is set in element */
155 *is_set = ( y->bits & ( ( (UINT8) 1 ) << bitidx ) ) ? 1 : 0;
156
157 }
158
159 return XLAL_SUCCESS;
160
161}
#define BITS_PER_ELEM
Definition: LALBitset.c:25
static UINT8 hash_elem(const void *x)
Definition: LALBitset.c:36
static int cmp_elem(const void *x, const void *y)
Definition: LALBitset.c:44
LALBitset * XLALBitsetCreate(void)
Create a bitset.
Definition: LALBitset.c:51
int XLALBitsetClear(LALBitset *bs)
Clear a bitset.
Definition: LALBitset.c:78
void XLALBitsetDestroy(LALBitset *bs)
Destroy a bitset and its elements.
Definition: LALBitset.c:68
int XLALBitsetGet(const LALBitset *bs, const UINT8 idx, BOOLEAN *is_set)
Get whether a bit in the bitset is set.
Definition: LALBitset.c:131
int XLALBitsetSet(LALBitset *bs, const UINT8 idx, const BOOLEAN is_set)
Set/unset a bit in the bitset.
Definition: LALBitset.c:93
unsigned char BOOLEAN
Boolean logical type, see Headers LAL(Atomic)Datatypes.h for more details.
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
uint16_t UINT2
Two-byte unsigned integer.
int XLALPearsonHash(void *hval, const size_t hval_len, const void *data, const size_t data_len)
Compute a arbitrary-sized Pearson hash value for the given arbitrary data.
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
int XLALHashTblAdd(LALHashTbl *ht, void *x)
Add an element to a hash table.
Definition: LALHashTbl.c:215
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
LALHashTbl * XLALHashTblCreate(LALHashTblDtorFcn dtor, LALHashTblHashFcn hash, LALHashTblCmpFcn cmp)
Create a hash table.
Definition: LALHashTbl.c:86
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
#define XLALCalloc(m, n)
Definition: LALMalloc.h:45
#define XLALFree(p)
Definition: LALMalloc.h:47
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
Definition: XLALError.h:810
#define XLAL_CHECK_NULL(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns a pointe...
Definition: XLALError.h:825
@ XLAL_ENOMEM
Memory allocation error.
Definition: XLALError.h:407
@ XLAL_SUCCESS
Success return value (not an error number)
Definition: XLALError.h:401
@ XLAL_EFAULT
Invalid pointer.
Definition: XLALError.h:408
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
Definition: LALBitset.c:31
UINT8 bits
Definition: LALBitset.c:33
UINT8 key
Definition: LALBitset.c:32
Arbitrary-size bitset.
Definition: LALBitset.c:27
LALHashTbl * ht
Definition: LALBitset.c:28