~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to src/core/bitpackarray.h

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2007 Thomas Jahns <Thomas.Jahns@gmx.net>
 
3
 
 
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.
 
7
 
 
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.
 
15
*/
 
16
 
 
17
#ifndef BITPACKARRAY_H
 
18
#define BITPACKARRAY_H
 
19
 
 
20
#ifndef S_SPLINT_S
 
21
/**
 
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>
 
27
 */
 
28
 
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include "core/assert_api.h"
 
32
#include "core/bitpackstring.h"
 
33
#include "core/error.h"
 
34
#include "core/ma.h"
 
35
 
 
36
struct BitPackArray
 
37
{
 
38
  BitString store;
 
39
  BitOffset numElems;
 
40
  unsigned bitsPerElem;
 
41
};
 
42
 
 
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 */
 
46
 
 
47
#define BITPACKARRAYSTOREVAR(BPA) (BPA)->store
 
48
 
 
49
typedef struct BitPackArray BitPackArray;
 
50
 
 
51
/**
 
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
 
55
 * @return size
 
56
 */
 
57
 
 
58
static inline size_t sizeofbitarray(unsigned bits, BitOffset numValues)
 
59
{
 
60
  return bitElemsAllocSize(bits * numValues) * sizeof (BitElem);
 
61
}
 
62
 
 
63
/**
 
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
 
68
 */
 
69
static inline BitPackArray *
 
70
bitpackarray_new(unsigned bits, BitOffset numValues, bool withstorealloc)
 
71
{
 
72
  BitPackArray *newBPA = gt_malloc(sizeof (*newBPA));
 
73
  if (newBPA)
 
74
  {
 
75
    if (withstorealloc)
 
76
    {
 
77
      if (!(newBPA->store = gt_calloc(bitElemsAllocSize(bits*numValues),
 
78
                                      sizeof (BitElem))))
 
79
      {
 
80
        gt_free(newBPA);
 
81
        return NULL;
 
82
      }
 
83
    } else
 
84
    {
 
85
      newBPA->store = NULL;
 
86
    }
 
87
    newBPA->bitsPerElem = bits;
 
88
    newBPA->numElems = numValues;
 
89
  }
 
90
  return newBPA;
 
91
}
 
92
 
 
93
static inline void
 
94
bitpackarray_delete(BitPackArray *bpa)
 
95
{
 
96
  if (!bpa) return;
 
97
  gt_free(bpa->store);
 
98
  gt_free(bpa);
 
99
}
 
100
 
 
101
/**
 
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).
 
107
 */
 
108
static inline void
 
109
bitpackarray_store_uint32(BitPackArray *array, BitOffset index, uint32_t val)
 
110
{
 
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);
 
115
}
 
116
 
 
117
/**
 
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).
 
123
 */
 
124
static inline uint32_t
 
125
bitpackarray_get_uint32(const BitPackArray *array, BitOffset index)
 
126
{
 
127
  gt_assert(array && index < array->numElems
 
128
         && array->bitsPerElem <= sizeof (uint32_t)*CHAR_BIT);
 
129
  return gt_bsGetUInt32(array->store, array->bitsPerElem * index,
 
130
                     array->bitsPerElem);
 
131
}
 
132
 
 
133
/**
 
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).
 
139
 */
 
140
static inline void
 
141
bitpackarray_store_uint64(BitPackArray *array, BitOffset index, uint64_t val)
 
142
{
 
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);
 
147
}
 
148
 
 
149
/**
 
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).
 
155
 */
 
156
static inline uint64_t
 
157
bitpackarray_get_uint64(const BitPackArray *array, BitOffset index)
 
158
{
 
159
  gt_assert(array && index < array->numElems
 
160
         && array->bitsPerElem <= sizeof (uint64_t)*CHAR_BIT);
 
161
  return gt_bsGetUInt64(array->store, array->bitsPerElem * index,
 
162
                     array->bitsPerElem);
 
163
}
 
164
 
 
165
/**
 
166
 * Unit test function for BitPackArray.
 
167
 * @return 0 on success, -1 on error.
 
168
 */
 
169
int gt_bitpackarray_unit_test(GtError*);
 
170
 
 
171
/*
 
172
static inline void showbitpackarray(const BitPackArray *bitpackarray)
 
173
{
 
174
  unsigned long numofunits, idx;
 
175
 
 
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++)
 
182
  {
 
183
    printf("%lu: %u\n",idx,(unsigned int) bitpackarray->store[idx]);
 
184
    fflush(stdout);
 
185
  }
 
186
}
 
187
*/
 
188
 
 
189
#else
 
190
 
 
191
/* some minimal set of declaration to satisfy splint. We cannot use the
 
192
   original implementation, as this produces many errors when fed into
 
193
   splint
 
194
*/
 
195
 
 
196
typedef unsigned char BitElem;
 
197
typedef BitElem *BitString;
 
198
 
 
199
typedef struct
 
200
{
 
201
  BitString store;
 
202
  /* and others */
 
203
} BitPackArray;
 
204
 
 
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,
 
211
                               uint32_t val);
 
212
void bitpackarray_store_uint64(BitPackArray *array, BitOffset index,
 
213
                               uint64_t val);
 
214
uint32_t bitpackarray_get_uint32(const BitPackArray *array, BitOffset index);
 
215
uint64_t bitpackarray_get_uint64(const BitPackArray *array, BitOffset index);
 
216
#endif
 
217
 
 
218
#endif