-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbits.cpp
More file actions
807 lines (753 loc) · 26.7 KB
/
Copy pathbits.cpp
File metadata and controls
807 lines (753 loc) · 26.7 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
#include "bits.h"
#include "bits_iterator.h"
#include "buffer.h"
#include "class.h"
#include "file.h"
#include "string.h"
#include "tuple.h"
Value next_bits_construct_empty(const Value *args, int numargs) {
(void)numargs;
(void)args;
Bits *b = Bits::create(0);
b->bytes[0] = 0;
return Value(b);
}
Value next_bits_construct(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "(number_of_bits)", 1, Integer);
int64_t numbits = args[1].toInteger();
if(numbits < 1) {
RERR("Number of bits in a bit array should be > 0!");
}
Bits2 ba = Bits::create(numbits);
std::memset(ba->bytes, 0, ba->chunkcount * Bits::ChunkSizeByte);
return Value(ba);
}
Value next_bits_construct_value(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "(number_of_bits, default_bit)", 1, Integer);
EXPECT(bits, "(number_of_bits, default_bit)", 2, Bit);
int64_t numbits = args[1].toInteger();
if(numbits < 1) {
RERR("Number of bits in a bit array should be > 0!");
}
Bits::ChunkType defbit = args[2].toInteger();
if(defbit) {
defbit = Bits::ChunkFull;
}
Bits2 ba = Bits::create(numbits);
std::memset(ba->bytes, defbit, ba->chunkcount * Bits::ChunkSizeByte);
if(defbit && (numbits & Bits::ChunkRemainderAnd)) {
// in the last chunk, set the remaining bits to zero
ba->bytes[ba->chunkcount - 1] =
Bits::ChunkFull >>
(Bits::ChunkSize - (numbits & Bits::ChunkRemainderAnd));
}
return Value(ba);
}
Value next_bits_from(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "from(value)", 1, Integer);
int64_t val = args[1].toInteger();
size_t lastOne = 0;
for(int64_t bak = val; bak != 0; bak >>= 1, lastOne++)
;
Bits2 ba = Bits::create(lastOne);
std::memcpy(ba->bytes, &val, sizeof(int64_t));
return Value(ba);
}
Value next_bits_set(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "set(bit)", 1, Integer);
int64_t val = args[1].toInteger();
Bits * ba = args[0].toBits();
if(val < 0 || val >= ba->size) {
RERR("Invalid bit index!");
}
ba->bytes[val >> Bits::ChunkCountShift] |=
((Bits::ChunkType)1 << (val & Bits::ChunkRemainderAnd));
return ValueNil;
}
Value next_bits_setall(const Value *args, int numargs) {
(void)numargs;
Bits *ba = args[0].toBits();
std::memset(ba->bytes, -1, ba->chunkcount * Bits::ChunkSizeByte);
int64_t lastbits = ba->size & Bits::ChunkRemainderAnd;
if(lastbits) {
ba->bytes[ba->chunkcount - 1] =
Bits::ChunkFull >> (Bits::ChunkSize - lastbits);
}
return ValueNil;
}
Value next_bits_setinrange(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "set(from, to)", 1, Integer);
EXPECT(bits, "set(from, to)", 2, Integer);
Bits * ba = args[0].toBits();
int64_t from = args[1].toInteger();
int64_t to = args[2].toInteger();
if(from < 0 || from >= ba->size || to < 0 || to >= ba->size || from > to) {
RERR("Invalid range specified!");
}
int64_t chunkfrom = from >> Bits::ChunkCountShift;
int64_t chunkto = to >> Bits::ChunkCountShift;
if(chunkfrom + 1 < chunkto - 1) {
std::memset(&ba->bytes[chunkfrom + 1], -1, (chunkto - 1 + chunkfrom));
}
int64_t fromrem = from & Bits::ChunkRemainderAnd;
int64_t torem = to & Bits::ChunkRemainderAnd;
if(torem)
torem = Bits::ChunkSize - torem - 1;
Bits::ChunkType mask = Bits::ChunkFull << fromrem;
if(chunkfrom == chunkto) {
mask &= (Bits::ChunkFull >> torem);
ba->bytes[chunkfrom] |= mask;
} else {
ba->bytes[chunkfrom] |= mask;
ba->bytes[chunkto] |= (Bits::ChunkFull >> torem);
}
return ValueNil;
}
Value next_bits_reset(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "reset(bit)", 1, Integer);
int64_t val = args[1].toInteger();
Bits * ba = args[0].toBits();
if(val < 0 || val >= ba->size) {
RERR("Invalid bit index!");
}
Bits::ChunkType reset_pattern =
~((Bits::ChunkType)1 << (val & Bits::ChunkRemainderAnd));
ba->bytes[val >> Bits::ChunkCountShift] &= reset_pattern;
return ValueNil;
}
Value next_bits_resetall(const Value *args, int numargs) {
(void)numargs;
Bits *ba = args[0].toBits();
std::memset(ba->bytes, 0, ba->chunkcount * Bits::ChunkSizeByte);
return ValueNil;
}
Value next_bits_resetinrange(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "reset(from, to)", 1, Integer);
EXPECT(bits, "reset(from, to)", 2, Integer);
Bits * ba = args[0].toBits();
int64_t from = args[1].toInteger();
int64_t to = args[2].toInteger();
if(from < 0 || from >= ba->size || to < 0 || to >= ba->size || from > to) {
RERR("Invalid range specified!");
}
int64_t chunkfrom = from >> Bits::ChunkCountShift;
int64_t chunkto = to >> Bits::ChunkCountShift;
if(chunkfrom + 1 < chunkto - 1) {
std::memset(&ba->bytes[chunkfrom + 1], 0, (chunkto - 1 + chunkfrom));
}
int64_t fromrem = from & Bits::ChunkRemainderAnd;
int64_t torem = to & Bits::ChunkRemainderAnd;
if(torem)
torem = Bits::ChunkSize - torem - 1;
Bits::ChunkType mask = Bits::ChunkFull << fromrem;
if(chunkfrom == chunkto) {
mask &= (Bits::ChunkFull >> torem);
ba->bytes[chunkfrom] &= ~mask;
} else {
ba->bytes[chunkfrom] &= ~mask;
ba->bytes[chunkto] &= ~(Bits::ChunkFull >> torem);
}
return ValueNil;
}
Value next_bits_toggle(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "toggle(bit)", 1, Integer);
int64_t val = args[1].toInteger();
Bits * ba = args[0].toBits();
if(val < 0 || val >= ba->size) {
RERR("Invalid bit index!");
}
Bits::ChunkType pattern =
((Bits::ChunkType)1 << (val & Bits::ChunkRemainderAnd));
ba->bytes[val >> Bits::ChunkCountShift] ^= pattern;
return ValueNil;
}
Value next_bits_toggleall(const Value *args, int numargs) {
(void)numargs;
Bits *ba = args[0].toBits();
for(int64_t i = 0; i < ba->chunkcount; i++) {
ba->bytes[i] = ~ba->bytes[i];
}
if(ba->size & Bits::ChunkRemainderAnd) {
ba->bytes[ba->chunkcount - 1] &=
(Bits::ChunkFull >>
(Bits::ChunkSize - (ba->size & Bits::ChunkRemainderAnd)));
}
return ValueNil;
}
Value next_bits_toggleinrange(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "toggle(from, to)", 1, Integer);
EXPECT(bits, "toggle(from, to)", 2, Integer);
Bits * ba = args[0].toBits();
int64_t from = args[1].toInteger();
int64_t to = args[2].toInteger();
if(from < 0 || from >= ba->size || to < 0 || to >= ba->size || from > to) {
RERR("Invalid range specified!");
}
int64_t chunkfrom = from >> Bits::ChunkCountShift;
int64_t chunkto = to >> Bits::ChunkCountShift;
for(int64_t i = chunkfrom + 1; i < chunkto - 1; i++) {
ba->bytes[i] = ~ba->bytes[i];
}
int64_t fromrem = from & Bits::ChunkRemainderAnd;
int64_t torem = to & Bits::ChunkRemainderAnd;
if(torem)
torem = Bits::ChunkSize - torem - 1;
Bits::ChunkType mask = Bits::ChunkFull << fromrem;
if(chunkfrom == chunkto) {
mask &= (Bits::ChunkFull >> torem);
ba->bytes[chunkfrom] ^= mask;
} else {
ba->bytes[chunkfrom] ^= mask;
ba->bytes[chunkto] ^= (Bits::ChunkFull >> torem);
}
return ValueNil;
}
Value next_bits_bit(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "bit(index)", 1, Integer);
int64_t val = args[1].toInteger();
Bits * ba = args[0].toBits();
if(val < 0 || val >= ba->size) {
RERR("Invalid bit index!");
}
Bits::ChunkType pattern =
((Bits::ChunkType)1 << (val & Bits::ChunkRemainderAnd));
Bits::ChunkType res = ba->bytes[val >> Bits::ChunkCountShift] & pattern;
return Value((int)(res != 0));
}
#define ALPHAZERO1 '0'
#define ALPHAZERO2 ALPHAZERO1, ALPHAZERO1
#define ALPHAZERO4 ALPHAZERO2, ALPHAZERO2
#define ALPHAZERO8 ALPHAZERO4, ALPHAZERO4
#define ALPHAZERO16 ALPHAZERO8, ALPHAZERO8
#define ALPHAZERO32 ALPHAZERO16, ALPHAZERO16
#define ALPHAZERO64 ALPHAZERO32, ALPHAZERO32
Value next_bits_str(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "str(_)", 1, File);
File *f = args[1].toFile();
if(!f->stream->isWritable()) {
return FileError::sete("File is not writable!");
}
Bits *ba = args[0].toBits();
if(ba->size == 0) {
f->writableStream()->write('0');
return ValueTrue;
}
// for the last chunk, print only what is required
Bits::ChunkType lastChunk = ba->bytes[ba->chunkcount - 1];
size_t upto = 0;
if((ba->size & Bits::ChunkRemainderAnd) == 0)
upto = Bits::ChunkSize;
else
upto = ba->size & Bits::ChunkRemainderAnd;
size_t uptobak = upto;
char lastSeq[Bits::ChunkSize] = {ALPHAZERO64};
size_t idx = upto - 1;
while(lastChunk) {
lastSeq[idx--] += lastChunk & 1;
lastChunk >>= 1;
}
f->writableStream()->writebytes(lastSeq, uptobak);
if(ba->chunkcount == 1)
return ValueTrue;
size_t remChunk = ba->chunkcount - 1;
while(remChunk--) {
Bits::ChunkType c = ba->bytes[remChunk];
char print[Bits::ChunkSize] = {ALPHAZERO64};
size_t i = Bits::ChunkSize - 1;
while(c) {
print[i--] += c & 1;
c >>= 1;
}
f->writableStream()->write(print);
}
return ValueTrue;
}
#undef ALPHAZERO1
#undef ALPHAZERO2
#undef ALPHAZERO4
#undef ALPHAZERO8
#undef ALPHAZERO16
#undef ALPHAZERO32
#undef ALPHAZERO64
#define NEXT_BITS_BINARY(name, op) \
Value next_bits_##name(const Value *args, int numargs) { \
(void)numargs; \
EXPECT(bits, #op "(bits)", 1, Bits); \
Bits * b1 = args[0].toBits(); \
Bits * b2 = args[1].toBits(); \
int64_t size = b1->size > b2->size ? b1->size : b2->size; \
Bits * b3 = Bits::create(size); \
int64_t i = 0; \
while(i < b1->chunkcount && i < b2->chunkcount) { \
b3->bytes[i] = b1->bytes[i] op b2->bytes[i]; \
i++; \
} \
while(i < b1->chunkcount) { \
b3->bytes[i] = b1->bytes[i]; \
i++; \
} \
while(i < b2->chunkcount) { \
b3->bytes[i] = b2->bytes[i]; \
i++; \
} \
return Value(b3); \
}
// size of or/xor is that of the largest sequence
NEXT_BITS_BINARY(or, |)
NEXT_BITS_BINARY(xor, ^)
Value next_bits_and(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "&(value)", 1, Bits);
Bits *a = args[0].toBits();
Bits *b = args[1].toBits();
// size of AND is that of the smallest sequence
int64_t size = a->size > b->size ? b->size : a->size;
Bits * c = Bits::create(size);
int64_t chunks = 0;
while(chunks < a->chunkcount && chunks < b->chunkcount) {
c->bytes[chunks] = a->bytes[chunks] & b->bytes[chunks];
chunks++;
}
return Value(c);
}
Value next_bits_not(const Value *args, int numargs) {
(void)numargs;
Bits *a = args[0].toBits();
Bits *b = Bits::create(a->size);
for(int64_t i = 0; i < a->chunkcount; i++) {
b->bytes[i] = ~a->bytes[i];
}
if(a->size & Bits::ChunkRemainderAnd) {
// if we have uneven last byte, set the unused bits to zero
b->bytes[a->chunkcount - 1] &=
(Bits::ChunkFull >>
(Bits::ChunkSize - (a->size & Bits::ChunkRemainderAnd)));
}
return Value(b);
}
Value next_bits_lshift(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "<<(shift)", 1, Integer);
int64_t shift = args[1].toInteger();
if(shift < 0) {
RERR("Bit shift expected to be > 0!");
}
Bits *a = args[0].toBits();
Bits *b = Bits::create(a->size + shift);
// find out the shift in chunk
int64_t chunkshift = shift >> Bits::ChunkCountShift;
// find out the shift in each byte
int64_t bitshift = shift & Bits::ChunkRemainderAnd;
// find out the number of bits that is _lost_ after a shift on the byte
int64_t remshift = Bits::ChunkSize - bitshift;
Bits::ChunkType lastbyte = 0;
// set the initial bytes to 0
std::memset(b->bytes, 0, chunkshift * Bits::ChunkSizeByte);
for(int64_t i = 0; i < a->chunkcount; i++) {
// shift the present byte, and or the remaining of the last byte
b->bytes[i + chunkshift] =
(a->bytes[i] << bitshift) | (lastbyte >> remshift);
lastbyte = a->bytes[i];
}
// whatever is left, dump that on the last byte
b->bytes[b->chunkcount - 1] |= (lastbyte >> remshift);
return Value(b);
}
Value next_bits_rshift(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, ">>(shift)", 1, Integer);
int64_t shift = args[1].toInteger();
if(shift < 0) {
RERR("Invalid shift value!");
}
Bits *a = args[0].toBits();
if(shift >= a->size) {
Bits *b = Bits::create(0);
b->bytes[0] = 0;
return Value(b);
}
// right shift shrinks the bits
Bits *b = Bits::create(a->size - shift);
// clear the last byte
b->bytes[b->chunkcount - 1] = 0;
int64_t chunkcount = a->chunkcount;
// find out the shift in chunk
int64_t chunkshift = shift >> Bits::ChunkCountShift;
// find out the shift in each byte
int64_t bitshift = shift & Bits::ChunkRemainderAnd;
// find out the number of bits that is _lost_ after a shift on the byte
int64_t remshift = Bits::ChunkSize - bitshift;
Bits::ChunkType mask = Bits::ChunkFull >> remshift;
Bits::ChunkType lastbyte = 0;
for(int64_t i = chunkcount - 1 - chunkshift; i >= 0; --i) {
// shift the present byte, and or the part of the last byte that was
// lost, after shifting them back to the left
b->bytes[i] = (a->bytes[i + chunkshift] >> bitshift) |
((lastbyte & mask) << remshift);
lastbyte = a->bytes[i + chunkshift];
}
return Value(b);
}
Value next_bits_size(const Value *args, int numargs) {
(void)numargs;
return Value(args[0].toBits()->size);
}
#define NEXT_BITS_BINARY_INPLACE(name, op) \
Value next_bits_##name(const Value *args, int numargs) { \
(void)numargs; \
EXPECT(bits, #name "(bits)", 1, Bits); \
Bits *source = args[0].toBits(); \
Bits *with = args[1].toBits(); \
\
if(source->size < with->size) { \
int64_t oldchunk = source->chunkcount; \
source->resize(with->size); \
for(int64_t i = oldchunk; i < with->chunkcount; i++) { \
source->bytes[i] = with->bytes[i]; \
} \
for(int64_t i = 0; i < oldchunk; i++) { \
source->bytes[i] op## = with->bytes[i]; \
} \
return source; \
} \
\
for(int64_t i = 0; i < with->chunkcount; i++) { \
source->bytes[i] op## = with->bytes[i]; \
} \
\
return source; \
}
NEXT_BITS_BINARY_INPLACE(or_with, |)
NEXT_BITS_BINARY_INPLACE(xor_with, ^)
Value next_bits_and_with(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "and_with(bits)", 1, Bits);
Bits *source = args[0].toBits();
Bits *with = args[1].toBits();
if(with->size < source->size) {
source->resize(with->size);
}
for(int64_t i = 0; i < source->chunkcount; i++) {
source->bytes[i] &= with->bytes[i];
}
return Value(source);
}
Value next_bits_shift_left(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "shift_left(shift)", 1, Integer);
int64_t shift = args[1].toInteger();
if(shift < 0) {
RERR("Bit shift expected to be > 0!");
}
Bits *a = args[0].toBits();
a->resize(a->size + shift);
// find out the shift in chunk
int64_t chunkshift = shift >> Bits::ChunkCountShift;
// find out the shift in each byte
int64_t bitshift = shift & Bits::ChunkRemainderAnd;
// find out the number of bits that is _lost_ after a shift on the byte
int64_t remshift = Bits::ChunkSize - bitshift;
Bits::ChunkType lastbyte = 0;
for(int64_t i = 0; i < a->chunkcount; i++) {
// shift the present byte, and or the remaining of the last byte
a->bytes[i + chunkshift] =
(a->bytes[i] << bitshift) | (lastbyte >> remshift);
lastbyte = a->bytes[i];
}
// whatever is left, dump that on the last byte
a->bytes[a->chunkcount - 1] |= (lastbyte >> remshift);
if(chunkshift) {
// clear up initial chunks
std::memset(a->bytes, 0, chunkshift * Bits::ChunkSizeByte);
}
if(bitshift) {
// clear up the initial bits of the leftmost chunk
a->bytes[chunkshift] &= (Bits::ChunkFull << bitshift);
}
return Value(a);
}
Value next_bits_shift_right(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "shift_right(shift)", 1, Integer);
int64_t shift = args[1].toInteger();
if(shift < 0) {
RERR("Invalid shift value!");
}
Bits *a = args[0].toBits();
if(shift >= a->size) {
a->resize(0);
a->bytes[0] = 0;
return Value(a);
}
int64_t chunkcount = a->chunkcount;
// find out the shift in chunk
int64_t chunkshift = shift >> Bits::ChunkCountShift;
// find out the shift in each byte
int64_t bitshift = shift & Bits::ChunkRemainderAnd;
// find out the number of bits that is _lost_ after a shift on the byte
int64_t remshift = Bits::ChunkSize - bitshift;
Bits::ChunkType mask = Bits::ChunkFull >> remshift;
Bits::ChunkType lastbyte = 0;
for(int64_t i = 0; i < chunkcount - chunkshift; i++) {
// shift the present byte, and or the part of the last byte that was
// lost, after shifting them back to the left
if(i + chunkshift + 1 < chunkcount)
lastbyte = a->bytes[i + chunkshift + 1];
else
lastbyte = 0;
a->bytes[i] = (a->bytes[i + chunkshift] >> bitshift) |
((lastbyte & mask) << remshift);
}
a->resize(a->size - shift);
// clear the extra part of the first chunk
a->bytes[a->chunkcount - 1] &= Bits::ChunkFull >> bitshift;
return Value(a);
}
Value next_bits_subs_get(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "[](_)", 1, Integer);
int64_t idx = args[1].toInteger();
Bits * b = args[0].toBits();
if(idx < 0 || idx >= b->size) {
RERR("Invalid bit index!");
}
int64_t chunk = idx >> Bits::ChunkCountShift;
int64_t bit = idx & Bits::ChunkRemainderAnd;
return Value((int)((b->bytes[chunk] & (1 << bit)) != 0));
}
Value next_bits_subs_set(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "[](_,_)", 1, Integer);
EXPECT(bits, "[](_,_)", 2, Bit);
int64_t idx = args[1].toInteger();
Bits * b = args[0].toBits();
if(idx < 0 || idx >= b->size) {
RERR("Invalid bit index!");
}
int64_t val = args[2].toInteger();
int64_t chunk = idx >> Bits::ChunkCountShift;
int64_t bit = idx & Bits::ChunkRemainderAnd;
b->bytes[chunk] &= ~((Bits::ChunkType)1 << bit);
b->bytes[chunk] |= ((Bits::ChunkType)val << bit);
return Value(val);
}
Value next_bits_iterate(const Value *args, int numargs) {
(void)numargs;
return Value(
BitsIterator::from(args[0].toBits(), BitsIterator::TraversalType::BIT));
}
Value next_bits_iterate_bytes(const Value *args, int numargs) {
(void)numargs;
return Value(BitsIterator::from(args[0].toBits(),
BitsIterator::TraversalType::BYTE));
}
Value next_bits_iterate_ints(const Value *args, int numargs) {
(void)numargs;
return Value(BitsIterator::from(args[0].toBits(),
BitsIterator::TraversalType::CHUNK));
}
Value next_bits_to_bytes(const Value *args, int numargs) {
(void)numargs;
Bits * b = args[0].toBits();
Tuple *t = Tuple::create((b->size >> 3) + ((b->size & 7) != 0));
for(int64_t i = 0, j = 0; i < b->size; i += 8, j++) {
int64_t chunk = i >> Bits::ChunkCountShift;
int64_t bit = i & Bits::ChunkRemainderAnd;
int64_t val = (b->bytes[chunk] >> bit) & 0xff;
t->values()[j] = Value(val);
}
return t;
}
Value next_bits_to_ints(const Value *args, int numargs) {
(void)numargs;
Bits * b = args[0].toBits();
Tuple *t = Tuple::create(b->chunkcount);
for(int64_t i = 0, j = 0; i < b->chunkcount; i++, j++) {
// hardcoded for 64bit chunks
int64_t val = b->bytes[i];
t->values()[j] = Value(val);
}
return t;
}
Value next_bits_insert(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "insert(bit)", 1, Bit);
int64_t b = args[1].toInteger();
Bits * a = args[0].toBits();
int64_t oldsize = a->size;
a->resize(a->size + 1);
a->bytes[oldsize >> Bits::ChunkCountShift] |=
(b << (oldsize & Bits::ChunkRemainderAnd));
return ValueNil;
}
Value next_bits_insert_byte(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "insert_byte(byte)", 1, Integer);
int64_t b = args[1].toInteger() & 0xff;
Bits * a = args[0].toBits();
int64_t oldsize = a->size;
int64_t oldcc = a->chunkcount;
a->resize(a->size + 8);
if(a->chunkcount == oldcc) {
a->bytes[oldsize >> Bits::ChunkCountShift] |=
(b << (oldsize & Bits::ChunkRemainderAnd));
} else {
int64_t space = (oldsize & Bits::ChunkRemainderAnd);
if(space) {
a->bytes[a->chunkcount - 2] |= (b << space);
}
a->bytes[a->chunkcount - 1] = (b >> space);
}
return ValueNil;
}
Value next_bits_insert_int(const Value *args, int numargs) {
(void)numargs;
EXPECT(bits, "insert_int(value)", 1, Integer);
int64_t b = args[1].toInteger();
Bits * a = args[0].toBits();
int64_t oldsize = a->size;
int64_t oldcc = a->chunkcount;
a->resize(a->size + 64);
if(a->chunkcount == oldcc) {
a->bytes[oldsize >> Bits::ChunkCountShift] |=
(b << (oldsize & Bits::ChunkRemainderAnd));
} else {
int64_t space = (oldsize & Bits::ChunkRemainderAnd);
if(space) {
a->bytes[a->chunkcount - 2] |= (b << space);
}
a->bytes[a->chunkcount - 1] = (b >> space);
}
return ValueNil;
}
Value next_bits_equal(const Value *args, int numargs) {
(void)numargs;
if(args[1].isBits()) {
Bits *a = args[0].toBits();
Bits *b = args[1].toBits();
if(a->size != b->size) {
return ValueFalse;
}
return Value(std::memcmp(a->bytes, b->bytes,
Bits::ChunkSizeByte * a->chunkcount) == 0);
}
return ValueFalse;
}
Value next_bits_unequal(const Value *args, int numargs) {
(void)numargs;
if(args[1].isBits()) {
Bits *a = args[0].toBits();
Bits *b = args[1].toBits();
if(a->size != b->size) {
return ValueTrue;
}
return Value(std::memcmp(a->bytes, b->bytes,
Bits::ChunkSizeByte * a->chunkcount) != 0);
}
return ValueTrue;
}
Bits *Bits::create(int64_t number_of_bits) {
Bits *b = Gc::alloc<Bits>();
b->size = number_of_bits;
b->chunkcount = (number_of_bits >> Bits::ChunkCountShift) +
((number_of_bits & Bits::ChunkRemainderAnd) != 0);
b->chunkcapacity = b->chunkcount * Bits::ChunkSize;
if(b->chunkcapacity == 0)
b->chunkcapacity = Bits::ChunkSize;
b->bytes = (Bits::ChunkType *)Gc_malloc(
(b->chunkcapacity >> Bits::ChunkCountShift) * Bits::ChunkSizeByte);
return b;
}
void Bits::resize(int64_t ns) {
int64_t oldchunkcount = chunkcount;
// readjust at all times
chunkcount =
(ns >> Bits::ChunkCountShift) + ((ns & Bits::ChunkRemainderAnd) != 0);
size = ns;
// extend the capacity only if we are smaller
if(ns > chunkcapacity) {
bytes = (ChunkType *)Gc_realloc(
bytes, oldchunkcount * ChunkSizeByte, chunkcount * ChunkSizeByte);
chunkcapacity = ns;
}
// if we are shrinking, clear the extra chunks
if(oldchunkcount > chunkcount) {
std::memset(&bytes[chunkcount], 0,
(oldchunkcount - chunkcount) * Bits::ChunkSizeByte);
}
if(size & ChunkRemainderAnd) {
// clear the extra part of the last chunk
bytes[chunkcount - 1] &= ~(ChunkFull << (size & ChunkRemainderAnd));
}
}
void Bits::init(Class *BitsClass) {
BitsClass->add_builtin_fn("()", 0, next_bits_construct_empty);
BitsClass->add_builtin_fn("(_)", 1,
next_bits_construct); // number of bits
BitsClass->add_builtin_fn(
"(_,_)", 1,
next_bits_construct_value); // number of bits, default value
BitsClass->add_builtin_fn("from(_)", 1, next_bits_from, false,
true); // integer
// planned API
// also, all file *bytes operations should operate on bytes
BitsClass->add_builtin_fn("set()", 0, next_bits_setall);
BitsClass->add_builtin_fn("set(_)", 1, next_bits_set);
BitsClass->add_builtin_fn("set(_,_)", 2, next_bits_setinrange);
BitsClass->add_builtin_fn("reset()", 0, next_bits_resetall);
BitsClass->add_builtin_fn("reset(_)", 1, next_bits_reset);
BitsClass->add_builtin_fn("reset(_,_)", 2, next_bits_resetinrange);
BitsClass->add_builtin_fn("toggle()", 0, next_bits_toggleall);
BitsClass->add_builtin_fn("toggle(_)", 1, next_bits_toggle);
BitsClass->add_builtin_fn("toggle(_,_)", 2, next_bits_toggleinrange);
BitsClass->add_builtin_fn("size()", 0, next_bits_size);
BitsClass->add_builtin_fn("bit(_)", 1, next_bits_bit);
BitsClass->add_builtin_fn("str(_)", 1, next_bits_str);
// bitwise operations, each of them produces
// a new Bits
BitsClass->add_builtin_fn("|(_)", 1, next_bits_or);
BitsClass->add_builtin_fn("&(_)", 1, next_bits_and);
BitsClass->add_builtin_fn("^(_)", 1, next_bits_xor);
// leftshift expands, rightshift shrinks the size
BitsClass->add_builtin_fn("<<(_)", 1, next_bits_lshift);
BitsClass->add_builtin_fn(">>(_)", 1, next_bits_rshift);
BitsClass->add_builtin_fn("not()", 0, next_bits_not);
// bitwise operations, but they are in place,
// modifies the source Bits, and returns that.
// in place not() can be done by toggle()
BitsClass->add_builtin_fn("or_with(_)", 1, next_bits_or_with);
BitsClass->add_builtin_fn("and_with(_)", 1, next_bits_and_with);
BitsClass->add_builtin_fn("xor_with(_)", 1, next_bits_xor_with);
BitsClass->add_builtin_fn("shift_left(_)", 1, next_bits_shift_left);
BitsClass->add_builtin_fn("shift_right(_)", 1, next_bits_shift_right);
// subscript operators, only accept and return bit
BitsClass->add_builtin_fn("[](_)", 1, next_bits_subs_get);
BitsClass->add_builtin_fn("[](_,_)", 2, next_bits_subs_set);
// iterators
BitsClass->add_builtin_fn("iterate()", 0, next_bits_iterate);
BitsClass->add_builtin_fn("as_bytes()", 0, next_bits_iterate_bytes);
BitsClass->add_builtin_fn("as_ints()", 0, next_bits_iterate_ints);
// converters, return tuple of values
BitsClass->add_builtin_fn("to_bytes()", 0, next_bits_to_bytes);
BitsClass->add_builtin_fn("to_ints()", 0, next_bits_to_ints);
// insertion
BitsClass->add_builtin_fn("insert(_)", 1, next_bits_insert);
BitsClass->add_builtin_fn("insert_byte(_)", 1, next_bits_insert_byte);
BitsClass->add_builtin_fn("insert_int(_)", 1, next_bits_insert_int);
// equality and inequality
BitsClass->add_builtin_fn("==(_)", 1, next_bits_equal);
BitsClass->add_builtin_fn("!=(_)", 1, next_bits_unequal);
// replace
// BitsClass->add_builtin_fn("replace(_,_)", 2, next_bits_replace);
}