forked from zorkian/dtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitfield.cpp
369 lines (320 loc) · 6.69 KB
/
bitfield.cpp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "config.h"
#include "bitfield.h"
#ifdef WINDOWS
#include <io.h>
#include <memory.h>
#else
#include <unistd.h>
#include <sys/param.h>
#endif
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "bttime.h"
#ifndef HAVE_RANDOM
#include "compat.h"
#endif
const unsigned char BIT_HEX[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
#define _isset(idx) (b[(idx) / 8 ] & BIT_HEX[(idx) % 8])
#define _isempty() (nset == 0)
#define _isempty_sp(sp) ((sp).nset == 0)
#define _isfull() (nset >= nbits)
#define _isfull_sp(sp) ((sp).nset >= nbits)
bt_index_t Bitfield::nbytes = 0;
bt_index_t Bitfield::nbits = 0;
Bitfield::Bitfield()
{
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
memset(b, 0, nbytes);
nset = 0;
}
Bitfield::Bitfield(bt_index_t npcs)
{
nbits = npcs;
nbytes = nbits / 8;
if( nbits % 8 ) nbytes++;
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
memset(b, 0, nbytes);
nset = 0;
}
Bitfield::Bitfield(const Bitfield &bf)
{
nset = bf.nset;
if( _isfull_sp(bf) ) b = (unsigned char *)0;
else{
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
memcpy(b, bf.b, nbytes);
}
}
void Bitfield::operator=(const Bitfield &bf)
{
nset = bf.nset;
if( _isfull_sp(bf) ){
if( b ){
delete []b;
b = (unsigned char *)0;
}
}else{
if( !b ){
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
}
memcpy(b, bf.b, nbytes);
}
}
/* _set() sets the bit but doesn't increment nset or set the isfull case.
Use instead of Set() when you know nset is incorrect and will be corrected
afterward (as in Invert or by _recalc),
and either bitfield won't get full or you'll _recalc() afterward to fix it.
*/
inline void Bitfield::_set(bt_index_t idx)
{
if( idx < nbits && !_isfull() && !_isset(idx) )
b[idx / 8] |= BIT_HEX[idx % 8];
}
inline void Bitfield::_setall(unsigned char *buf)
{
memset(buf, 0xff, nbytes - 1);
if( nbits % 8 ){
buf[nbytes - 1] = ~(BIT_HEX[nbits % 8 - 1] - 1);
}else
buf[nbytes - 1] = (unsigned char) 0xFF;
}
// Compute a new value for nset from the actual bitfield data.
inline void Bitfield::_recalc()
{
static unsigned char BITS[256] = {0xff};
bt_index_t i;
if( BITS[0] ){ // initialize bitcounts
bt_index_t j, exp, x;
BITS[0] = 0;
x = 0;
for( i = 0; i < 8; i++ ){
exp = 1<<i;
for( j = 0; j < exp; j++ )
BITS[++x] = BITS[j] + 1;
}
}
for( nset = 0, i = 0; i < nbytes; i++ )
nset += BITS[b[i]];
if( _isfull() && b ){
delete []b;
b = (unsigned char *)0;
}
}
void Bitfield::SetAll()
{
if( b ){
delete []b;
b = (unsigned char *)0;
}
nset = nbits;
}
void Bitfield::Clear()
{
if( _isfull() ){
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
}
memset(b, 0, nbytes);
nset = 0;
}
int Bitfield::IsSet(bt_index_t idx) const
{
if( idx >= nbits ) return 0;
return _isfull() ? 1 : _isset(idx);
}
void Bitfield::Set(bt_index_t idx)
{
if( idx >= nbits ) return;
if( !_isfull() && !_isset(idx) ){
b[idx / 8] |= BIT_HEX[idx % 8];
nset++;
if( _isfull() && b ){
delete []b;
b = (unsigned char *)0;
}
}
}
void Bitfield::UnSet(bt_index_t idx)
{
if( idx >= nbits ) return;
if( _isfull() ){
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
_setall(b);
b[idx / 8] &= (~BIT_HEX[idx % 8]);
nset = nbits - 1;
}else{
if( _isset(idx) ){
b[idx / 8] &= (~BIT_HEX[idx % 8]);
nset--;
}
}
}
void Bitfield::Invert()
{
if( _isempty() ){
SetAll();
}else if( _isfull() ){
Clear();
}else{
bt_index_t i = 0;
bt_index_t s = nset;
for( ; i < nbytes - 1; i++ ) b[i] = ~b[i];
if( nbits % 8 ){
for( i = 8 * (nbytes - 1); i < nbits; i++ ){
if( _isset(i) ) UnSet(i);
else _set(i);
}
}else b[nbytes - 1] = ~b[nbytes - 1];
nset = nbits - s;
}
}
void Bitfield::Or(const Bitfield &bf)
{
if( !_isempty_sp(bf) && !_isfull() ){
if( _isfull_sp(bf) ){
SetAll();
}else if( _isempty() ){
memcpy(b, bf.b, nbytes);
nset = bf.nset;
}else{
bt_index_t i;
for( i = 0; i < nbytes; i++ )
b[i] |= bf.b[i];
_recalc();
}
}
}
void Bitfield::Except(const Bitfield &bf)
{
if( !_isempty_sp(bf) && !_isempty() ){
if( _isfull_sp(bf) ){
Clear();
}else{
if( _isfull() ){
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
_setall(b);
}
for( bt_index_t i = 0; i < nbytes; i++ )
b[i] &= ~bf.b[i];
_recalc();
}
}
}
void Bitfield::And(const Bitfield &bf)
{
if( !_isfull_sp(bf) && !_isempty() ){
if( _isempty_sp(bf) ){
Clear();
}else{
if( _isfull() ){
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
memcpy(b, bf.b, nbytes);
nset = bf.nset;
}else{
for( bt_index_t i = 0; i < nbytes; i++ )
b[i] &= bf.b[i];
_recalc();
}
}
}
}
bt_index_t Bitfield::Random() const
{
bt_index_t idx;
if( _isfull() ) idx = random() % nbits;
else{
bt_index_t i = random() % nset + 1;
for( idx = 0; idx < nbits && i; idx++ )
if( _isset(idx) ) i--;
idx--;
}
return idx;
}
void Bitfield::SetReferBuffer(const char *buf)
{
if( !b ){
b = new unsigned char[nbytes];
#ifndef WINDOWS
if( !b ) throw 9;
#endif
}
memcpy(b, buf, nbytes);
if( nbits % 8 )
b[nbytes - 1] &= ~(BIT_HEX[nbits % 8 - 1] - 1);
_recalc();
}
void Bitfield::WriteToBuffer(char *buf)
{
if( _isfull() )
_setall((unsigned char *)buf);
else
memcpy(buf, (char *)b, nbytes);
}
int Bitfield::SetReferFile(const char *fname)
{
int result = -1;
FILE *fp = (FILE *)0;
struct stat sb;
char *bitbuf = (char *)0;
if( stat(fname, &sb) < 0 ) goto done;
if( sb.st_size != nbytes ) goto done;
fp = fopen(fname, "r");
if( !fp ) goto done;
bitbuf = new char[nbytes];
#ifndef WINDOWS
if( !bitbuf ) goto done;
#endif
if( fread(bitbuf, nbytes, 1, fp) != 1 ) goto done;
SetReferBuffer(bitbuf);
result = 0;
done:
if( bitbuf ) delete []bitbuf;
if( fp ) fclose(fp);
DiskAccess();
return result;
}
int Bitfield::WriteToFile(const char *fname)
{
int result = -1;
FILE *fp = (FILE *)0;
char *bitbuf = (char *)0;
fp = fopen(fname, "w");
if( !fp ) goto done;
bitbuf = new char[nbytes];
#ifndef WINDOWS
if( !bitbuf ) goto done;
#endif
WriteToBuffer(bitbuf);
if( fwrite(bitbuf, nbytes, 1, fp) != 1 ) goto done;
result = 0;
done:
if( bitbuf ) delete []bitbuf;
if( fp ) fclose(fp);
DiskAccess();
return result;
}