forked from PolMine/RcppCWB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitfields.c
More file actions
322 lines (282 loc) · 8.32 KB
/
Copy pathbitfields.c
File metadata and controls
322 lines (282 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* IMS Open Corpus Workbench (CWB)
* Copyright (C) 1993-2006 by IMS, University of Stuttgart
* Copyright (C) 2007- by the respective contributers (see file AUTHORS)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details (in the file "COPYING", or available via
* WWW at http://www.gnu.org/copyleft/gpl.html).
*/
#include <limits.h>
#include "globals.h"
#include "bitfields.h"
/**
* Size of the actual field of a Bitfield (in bytes).
*/
static int BaseTypeSize = sizeof(BFBaseType);
/**
* Size of the actual field of a Bitfield (in bits).
*/
static int BaseTypeBits = sizeof(BFBaseType) * CHAR_BIT;
void Rprintf(const char *, ...);
/**
* Create a new Bitfield object.
*
* @param nr_of_elements The number of bits the field should contain.
* @return The new Bitfield object.
*/
Bitfield
create_bitfield(int nr_of_elements)
{
int full_cells;
Bitfield bf = (Bitfield)cl_malloc(sizeof(BFBuf));
full_cells = nr_of_elements / BaseTypeBits;
if (nr_of_elements % BaseTypeBits != 0)
full_cells++;
bf->bytes = full_cells * BaseTypeSize;
bf->elements = nr_of_elements;
bf->nr_bits_set = 0;
bf->field = (BFBaseType *)cl_malloc(bf->bytes);
memset((char *)bf->field, '\0', bf->bytes);
return bf;
}
/**
* Copies a Bitfield.
*
* Creates a new bitfield object whihc is an exact duplicate of
* the source Bitfield.
*
* @param source The Bitfield to copy
* @return The duplicate Bitfield (or NULL if it was passed NULL).
*/
Bitfield
copy_bitfield(Bitfield source)
{
if (!source)
return NULL;
{
Bitfield target = create_bitfield(source->elements);
target->nr_bits_set = source->nr_bits_set;
memcpy(target->field, source->field, source->bytes);
return target;
}
}
/**
* Deletes a bitfield object.
*/
void
destroy_bitfield(Bitfield *bptr)
{
if (!bptr || !*bptr)
return;
cl_free((*bptr)->field);
cl_free(*bptr);
}
/**
* Sets a bit in a Bitfield to 1.
*
* The number of bits set is increased by 1 iff the setting
* of the specified bit has resulted in a change in the bitfield.
*
* If the offset specified does not exist within this Bitfield,
* the function returns false (plus an error message is printed).
* Otherwise it returns true.
*
* @param bitfield The Bitfield object to work with.
* @param element The offset of the bit to set.
* @return Boolean (see description).
*/
int
set_bit(Bitfield bitfield, int element)
{
if (bitfield && element < bitfield->elements) {
BFBaseType v1 = bitfield->field[element/BaseTypeBits];
bitfield->field[element/BaseTypeBits] |= (1<<(element % BaseTypeBits));
if (bitfield->field[element/BaseTypeBits] != v1)
bitfield->nr_bits_set++;
return 1;
}
Rprintf("Illegal offset %d in set_bit\n", element);
return 0;
}
/**
* Sets a bit in a Bitfield to 0 (clears it).
*
* The number of bits set is decreased by 1 iff the clearing
* of the specified bit has resulted in a change in the
* bitfield.
*
* If the offset specified does not exist within this Bitfield,
* the function returns false (plus an error message is printed).
* Otherwise it returns true.
*
* @param bitfield The Bitfield object to work with.
* @param element The offset of the bit to set.
* @return Boolean (see description).
*/
int
clear_bit(Bitfield bitfield, int element)
{
if (bitfield && element < bitfield->elements) {
BFBaseType v1 = bitfield->field[element/BaseTypeBits];
bitfield->field[element/BaseTypeBits] &= ~(1<<(element % BaseTypeBits));
if (bitfield->field[element/BaseTypeBits] != v1)
bitfield->nr_bits_set--;
return 1;
}
Rprintf("Illegal offset %d in clear_bit\n", element);
return 0;
}
/**
* Clears an entire Bitfield (ie sets all bits to 0).
*
* @return False if passed a NULL pointer; otherwise true.
*/
int
clear_all_bits(Bitfield bitfield)
{
if (bitfield) {
memset((char *)bitfield->field, '\0', bitfield->bytes);
bitfield->nr_bits_set = 0;
return 1;
}
return 0;
}
/**
* Sets an entire Bitfield (ie sets all bits to 1, all bytes to 0xff).
*
* @return False if passed a NULL pointer; otherwise true.
*/
int
set_all_bits(Bitfield bitfield)
{
if (bitfield) {
memset((char *)bitfield->field, 0xff, bitfield->bytes);
bitfield->nr_bits_set = bitfield->elements;
return 1;
}
return 0;
}
/**
* Gets the value of the bit at the specified offset in the Bitfield.
*
* @param bitfield The Bitfield to work with.
* @param element Offset of the desired bit.
* @return 1 if the bit is set; 0 if it isn't; -1 if
* element is not a legal offset (ie if it's
* outside the bounds of the bitfield).
*/
int
get_bit(Bitfield bitfield, int element)
{
if (bitfield && element < bitfield->elements)
return ( 0 == (bitfield->field[element/BaseTypeBits] & (1<<(element % BaseTypeBits))) ) ? 0 : 1;
Rprintf("Illegal offset %d in get_bit\n", element);
return -1;
}
/**
* Switches the value of the specified bit in a Bitfield.
*
* The number of set bits is either incremented or decremented,
* as appropriate.
*
* If the offset specified does not exist within this Bitfield,
* the function returns false (plus an error message is printed).
* Otherwise it returns true.
*
* @param bitfield The Bitfield to work with.
* @param element Offset of the bit to flip.
* @return Boolean (see description).
*/
int
toggle_bit(Bitfield bitfield, int element)
{
if (bitfield && element < bitfield->elements) {
if ((bitfield->field[element/BaseTypeBits]
& (1<<(element % BaseTypeBits))) == 0)
bitfield->nr_bits_set++;
else
bitfield->nr_bits_set--;
bitfield->field[element/BaseTypeBits] ^= (1<<(element % BaseTypeBits));
return 1;
}
Rprintf("Illegal offset %d in toggle_bit\n", element);
return 0;
}
/**
* Checks two Bitfield objects for equality of all bits.
*
* The Bitfields compared must have the same number of elements!
*
* @return Boolean: false if the two bitfields are different, true if they are the same.
*/
int
bf_equal(Bitfield bf1, Bitfield bf2)
{
int i, items, last_item_bits_used, mask;
assert(bf1->elements == bf2->elements);
assert(bf1->bytes == bf2->bytes);
items = bf1->bytes / BaseTypeSize;
last_item_bits_used = bf1->elements % BaseTypeBits;
if (0 != last_item_bits_used) {
items--; /* check last, partially used item (i.e. field[items-1]) separately */
mask = (1 << last_item_bits_used) - 1; /* should set first <last_item_bits_used> bits in mask */
if (((bf1->field[items] ^ bf2->field[items]) & mask) != 0)
return 0;
}
for (i = 0; i < items; i++)
if (bf1->field[i] != bf2->field[i])
return 0;
return 1;
}
/**
* Compares two Bitfield objects.
*
* Comparison is done from the start of the bitfield onwards.
* The Bitfields compared must have the same number of elements!
*
* @return 0 if the two are the same; 1 if bf1 is less; -1 if bf2 is less.
*/
int
bf_compare(Bitfield bf1, Bitfield bf2)
{
int i, items, last_item_bits_used, mask;
signed long diff;
assert(bf1->elements == bf2->elements);
assert(bf1->bytes == bf2->bytes);
items = bf1->bytes / BaseTypeSize;
last_item_bits_used = bf1->elements % BaseTypeBits;
if (last_item_bits_used != 0)
items--; /* check last, partially used item (i.e. field[items-1]) separately (below) */
for (i = 0; i < items; i++) {
diff = ((signed long) bf1->field[i]) - ((signed long) bf2->field[i]);
if (0 > diff)
return -1;
else if (diff > 0)
return 1;
}
if (0 != last_item_bits_used) {
mask = (1 << last_item_bits_used) - 1; /* should set first <last_item_bits_used> bits in mask */
diff = ((signed long) (bf1->field[i] & mask)) - ((signed long) (bf2->field[i] & mask));
if (diff < 0)
return -1;
else if (diff > 0)
return 1;
}
return 0;
}
/**
* Gets the number of bits set to 1 in the given Bitfield.
*/
int
nr_bits_set(Bitfield bitfield)
{
return bitfield->nr_bits_set;
}