2
Copyright (C) 2007 Thomas Jahns <Thomas.Jahns@gmx.net>
4
Permission to use, copy, modify, and distribute this software for any
5
purpose with or without fee is hereby granted, provided that the above
6
copyright notice and this permission notice appear in all copies.
8
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
#ifndef BITPACKARRAY_H
18
#define BITPACKARRAY_H
22
* \file bitpackarray.h
23
* \brief The class presented in this file encapsulates a bitstring in
24
* a data structure to store/retrieve integers at fixed bitlength and
25
* integrate well with genome tools library.
26
* \author Thomas Jahns <Thomas.Jahns@gmx.net>
31
#include "core/assert_api.h"
32
#include "core/bitpackstring.h"
33
#include "core/error.h"
43
/* SK added the following macro, as we need the structure component,
44
to store a pointer to the address, which is used when mapping the
45
bitpackarray as part of a larger memory map */
47
#define BITPACKARRAYSTOREVAR(BPA) (BPA)->store
49
typedef struct BitPackArray BitPackArray;
52
* determine size of BitPackArray structure.
53
* @param bits number of bits to encode each value stored with
54
* @param numValues number of values to store
58
static inline size_t sizeofbitarray(unsigned bits, BitOffset numValues)
60
return bitElemsAllocSize(bits * numValues) * sizeof (BitElem);
64
* Create new BitPackArray structure.
65
* @param bits number of bits to encode each value stored with
66
* @param numValues number of values to store
67
* @return pointer to new BitPackArray structure or NULL on failure
69
static inline BitPackArray *
70
bitpackarray_new(unsigned bits, BitOffset numValues, bool withstorealloc)
72
BitPackArray *newBPA = gt_malloc(sizeof (*newBPA));
77
if (!(newBPA->store = gt_calloc(bitElemsAllocSize(bits*numValues),
87
newBPA->bitsPerElem = bits;
88
newBPA->numElems = numValues;
94
bitpackarray_delete(BitPackArray *bpa)
102
* Stores unsigned integer in BitPackArray at given index.
103
* @param bparray array to use
104
* @param index index to store value at.
105
* @param val value to store (only as many bits as specified on
106
* BitPackArray construction are stored).
109
bitpackarray_store_uint32(BitPackArray *array, BitOffset index, uint32_t val)
111
gt_assert(array && index < array->numElems
112
&& array->bitsPerElem <= sizeof (val)*CHAR_BIT);
113
gt_bsStoreUInt32(array->store, array->bitsPerElem * index,
114
array->bitsPerElem, val);
118
* Stores unsigned integer in BitPackArray at given index.
119
* @param bparray array to use
120
* @param index index to store value at.
121
* @param val value to store (only as many bits as specified on
122
* BitPackArray construction are stored).
124
static inline uint32_t
125
bitpackarray_get_uint32(const BitPackArray *array, BitOffset index)
127
gt_assert(array && index < array->numElems
128
&& array->bitsPerElem <= sizeof (uint32_t)*CHAR_BIT);
129
return gt_bsGetUInt32(array->store, array->bitsPerElem * index,
134
* Stores unsigned integer in BitPackArray at given index.
135
* @param bparray array to use
136
* @param index index to store value at.
137
* @param val value to store (only as many bits as specified on
138
* BitPackArray construction are stored).
141
bitpackarray_store_uint64(BitPackArray *array, BitOffset index, uint64_t val)
143
gt_assert(array && index < array->numElems
144
&& array->bitsPerElem <= sizeof (val)*CHAR_BIT);
145
gt_bsStoreUInt64(array->store, array->bitsPerElem * index,
146
array->bitsPerElem, val);
150
* Stores unsigned integer in BitPackArray at given index.
151
* @param bparray array to use
152
* @param index index to store value at.
153
* @param val value to store (only as many bits as specified on
154
* BitPackArray construction are stored).
156
static inline uint64_t
157
bitpackarray_get_uint64(const BitPackArray *array, BitOffset index)
159
gt_assert(array && index < array->numElems
160
&& array->bitsPerElem <= sizeof (uint64_t)*CHAR_BIT);
161
return gt_bsGetUInt64(array->store, array->bitsPerElem * index,
166
* Unit test function for BitPackArray.
167
* @return 0 on success, -1 on error.
169
int gt_bitpackarray_unit_test(GtError*);
172
static inline void showbitpackarray(const BitPackArray *bitpackarray)
174
unsigned long numofunits, idx;
176
gt_assert(bitpackarray != NULL);
177
gt_assert(bitpackarray->store != NULL);
178
numofunits = (unsigned long) sizeofbitarray(bitpackarray->bitsPerElem,
179
bitpackarray->numElems);
180
printf("numofunits=%lu\n",numofunits);
181
for (idx=0; idx < numofunits; idx++)
183
printf("%lu: %u\n",idx,(unsigned int) bitpackarray->store[idx]);
191
/* some minimal set of declaration to satisfy splint. We cannot use the
192
original implementation, as this produces many errors when fed into
196
typedef unsigned char BitElem;
197
typedef BitElem *BitString;
205
typedef unsigned long long BitOffset;
206
size_t sizeofbitarray(unsigned bits, BitOffset numValues);
207
void bitpackarray_delete(BitPackArray *bpa);
208
BitPackArray *bitpackarray_new(unsigned bits, BitOffset numValues,
209
bool withstorealloc);
210
void bitpackarray_store_uint32(BitPackArray *array, BitOffset index,
212
void bitpackarray_store_uint64(BitPackArray *array, BitOffset index,
214
uint32_t bitpackarray_get_uint32(const BitPackArray *array, BitOffset index);
215
uint64_t bitpackarray_get_uint64(const BitPackArray *array, BitOffset index);