-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBitSet.ecl
1341 lines (1162 loc) · 47.6 KB
/
BitSet.ecl
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The BitSet module provides structures and algorithms for creating and
* manipulating bit arrays (called bitsets here) of arbitrary size
* (up to 4GB of RAM). A bitset is stored in a compact manner, with
* eight bits represented per byte.
*
* Bits within a bitset are referenced with a zero-based offset. The first
* bit is at position zero and, if the bitset was printed as a binary string
* of zeros and ones, its position would be at the far right of the string.
*
* Attributes exported by this module (detailed descriptions are inlined with
* each exported symbol):
*
* // Typedefs
* BitSet_t := DATA;
* Footprint_t := UNSIGNED4;
* BitCapacity_t := UNSIGNED6;
* BitPosition_t := UNSIGNED6;
*
* // Record definitions
* BitPositionsRec := {BitPosition_t bitPos};
*
* // Max values
* MAX_FOOTPRINT := (Footprint_t)-1;
* MAX_BIT_CAPACITY := MAX_FOOTPRINT * 8;
* HIGHEST_BIT_POSITION := MAX_BIT_CAPACITY - 1;
* LOWEST_BIT_POSITION := 0;
*
* // Sizes of a particular bitset
* Footprint_t Footprint(CONST BitSet_t b);
* BitCapacity_t Capacity(CONST BitSet_t b);
*
* // Exporting a bitset as a different format
* STRING AsHexString(CONST BitSet_t b);
* STRING AsBinaryString(CONST BitSet_t b);
* LITTLE_ENDIAN UNSIGNED8 AsUnsigned(CONST BitSet_t b);
*
* // Creating new bitsets
* BitSet_t New(BitCapacity_t bit_capacity);
* BitSet_t NewFromIntValue(LITTLE_ENDIAN UNSIGNED8 n, BitCapacity_t bit_capacity = 64);
* BitSet_t NewFromStrValue(STRING n, BitCapacity_t bit_capacity = 0);
* BitSet_t NewFromBitPositions(DATASET(BitPositionsRec) positions, BitCapacity_t bit_capacity = 0);
*
* // Manipulating single bits
* BitSet_t SetAllBits(CONST BitSet_t b, BOOLEAN on);
* BitSet_t ResetBits(CONST BitSet_t b);
* BitSet_t SetBit(CONST BitSet_t b, BitPosition_t position, BOOLEAN on = TRUE);
* BitSet_t FlipBit(CONST BitSet_t b, BitPosition_t position);
*
* // Bit shifting
* BitSet_t ShiftLeft(CONST BitSet_t b, BitCapacity_t num_bits = 1);
* BitSet_t ShiftRight(CONST BitSet_t b, BitCapacity_t num_bits = 1);
*
* // Bitwise operations
* BitSet_t BitwiseAND(CONST BitSet_t b1, CONST BitSet_t b2);
* BitSet_t BitwiseOR(CONST BitSet_t b1, CONST BitSet_t b2);
* BitSet_t BitwiseXOR(CONST BitSet_t b1, CONST BitSet_t b2);
* BitSet_t BitwiseNOT(CONST BitSet_t b);
* BitSet_t BitwiseDIFF(CONST BitSet_t b1, CONST BitSet_t b2);
*
* // Testing bits, individually and as a group
* BOOLEAN TestBit(CONST BitSet_t b, BitPosition_t position);
* BOOLEAN TestBits(CONST BitSet_t b1, CONST BitSet_t b2);
* BOOLEAN TestAnyBitsSet(CONST BitSet_t b);
* BOOLEAN TestNoBitsSet(CONST BitSet_t b);
* BOOLEAN TestAllBitsSet(CONST BitSet_t b);
* BOOLEAN TestBitSetsEqual(CONST BitSet_t b1, CONST BitSet_t b2);
*
* // Inspection
* BitCapacity_t CountBitsSet(CONST BitSet_t b);
* DATASET(BitPositionsRec) BitsSetPositions(CONST BitSet_t b);
*
* Self tests are available. To execute them, submit a job that contains
* the following line:
*
* BitSet._Tests.TestAll;
*
* All tests pass if the workunit completes successfully (there is no output).
*
* Origin: https://github.com/hpccsystems-solutions-lab/Useful_ECL
*/
EXPORT BitSet := MODULE, FORWARD
// Typedefs
EXPORT BitSet_t := DATA;
EXPORT Footprint_t := UNSIGNED4;
EXPORT BitCapacity_t := UNSIGNED6;
EXPORT BitPosition_t := UNSIGNED6;
// Record defining bit positions
EXPORT BitPositionsRec := RECORD
BitPosition_t bitPos;
END;
// Maximum number of bytes a single bitset can consume
EXPORT MAX_FOOTPRINT := (Footprint_t)-1;
// The maximum number of bits that can be referenced within one bitset
EXPORT MAX_BIT_CAPACITY := MAX_FOOTPRINT * 8;
// The highest-numbered bit position that can be referenced
EXPORT HIGHEST_BIT_POSITION := MAX_BIT_CAPACITY - 1;
// The lowest-numbered bit position that can be referenced
EXPORT LOWEST_BIT_POSITION := 0;
//--------------------------------------------------------------------------
/**
* Return the amount of memory used by a bitset.
*
* @param b A bitset; REQUIRED
*
* @return A non-negative integer representing the number of bytes
* used by the given bitset
*
* @see Capacity
*/
EXPORT Footprint_t Footprint(CONST BitSet_t b) := (Footprint_t)LENGTH(b);
/**
* Return the maximum number of bits that can be referenced within a
* bitset. This number may be up to seven bits higher than the number of
* bits originally requested due to memory allocation.
*
* @param b A bitset; REQUIRED
*
* @return A non-negative integer representing the number of bits that
* can be referenced by the given bitset
*
* @see Footprint
*/
EXPORT BitCapacity_t Capacity(CONST BitSet_t b) := (BitCapacity_t)(Footprint(b) * 8);
/**
* Return a hexadecimal representation of a bitset as a string. Basically,
* every byte used by the bitset is returned as its hexadecimal number and
* all such numbers are concatenated.
*
* @param b A bitset; REQUIRED
*
* @return A new STRING containing the hexadecimal representation of the
* given bitset. The returned value will consume twice the
* RAM of the bitset, as defined by Footprint(b), so care should
* taken when calling this function with extremely large bitsets.
*
* @see AsBinaryString
* @see AsUnsigned
*/
EXPORT STRING AsHexString(CONST BitSet_t b) := EMBED(C++)
#option pure;
__lenResult = lenB * 2;
__result = static_cast<char*>(rtlMalloc(__lenResult));
char* outPtr = __result;
const byte* inData = static_cast<const byte*>(b);
const char hexchar[] = "0123456789ABCDEF";
for (__int64 x = lenB - 1; x >= 0; x--)
{
*outPtr++ = hexchar[inData[x] >> 4];
*outPtr++ = hexchar[inData[x] & 0x0F];
}
ENDEMBED;
/**
* Return a binary representation of a bitset as a string of ones and
* zeros. Note that the result's length will always be a multiple of
* eight, rounded up from the number of bits requested for the bitset if
* necessary.
*
* @param b A bitset; REQUIRED
*
* @return A new STRING containing the binary representation of the
* given bitset. The returned value will consume eight times the
* RAM of the bitset, as defined by Footprint(b), so care should
* taken when calling this function with extremely large bitsets.
*
* @see AsHexString
* @see AsUnsigned
*/
EXPORT STRING AsBinaryString(CONST BitSet_t b) := EMBED(C++)
#option pure;
__lenResult = lenB * 8;
__result = static_cast<char*>(rtlMalloc(__lenResult));
char* outPtr = __result;
for (uint32_t x = lenB; x > 0; x--)
{
const byte sourceByte = static_cast<const byte*>(b)[x - 1];
for (uint32_t position = 8; position > 0; position--)
{
const byte testValue = 1 << (position - 1);
*outPtr++ = ((sourceByte & testValue) == testValue ? '1' : '0');
}
}
ENDEMBED;
/**
* Return the unsigned integer representation of the bitset. Note that
* only the first 64 bits of a bitset can be accurately represented this
* way; any bits beyond the first 64 will be ignored.
*
* @param b A bitset; REQUIRED
*
* @return An UNSIGNED8 value containing the integer representation of the
* given bitset. Only the first 64 bits of the bitset are used
* to build the returned value.
*
* @see AsHexString
* @see AsBinaryString
*/
EXPORT LITTLE_ENDIAN UNSIGNED8 AsUnsigned(CONST BitSet_t b) := EMBED(C++)
#option pure;
unsigned __int64 result = 0;
const uint32_t numBytesToCopy = (lenB < 8 ? lenB : 8);
memcpy(&result, b, numBytesToCopy);
return result;
ENDEMBED;
/**
* Create a new bitset.
*
* @param bit_capacity The maximum number of bits needed in the new
* bitset; REQUIRED
*
* @return A new BitSet_t value that can track at least the number of bits
* cited in the argument. The actual number of bits within the
* bitset may be slightly higher due to the use of bytes to pack
* the bits. All bits within the new bitset will be set to zero.
*
* @see NewFromIntValue
* @see NewFromStrValue
* @see NewFromBitPositions
*/
EXPORT BitSet_t New(BitCapacity_t bit_capacity) := FUNCTION
BitSet_t _New(BitCapacity_t _bit_capacity, BitCapacity_t _max_capacity = MAX_BIT_CAPACITY) := EMBED(C++)
#option pure;
const unsigned __int64 bitsRequested = (_bit_capacity < _max_capacity ? _bit_capacity : _max_capacity);
const uint32_t bytesNeeded = bitsRequested / 8 + (bitsRequested % 8 != 0 ? 1 : 0);
// Create empty result bitset
__lenResult = bytesNeeded;
__result = rtlMalloc(__lenResult);
memset(__result, 0, __lenResult);
ENDEMBED;
RETURN _New(bit_capacity);
END;
/**
* Create a new bitset initialized with an integer value. The optional
* <bit_capacity> argument provides control over how many bits are actually
* allocated in the bitset.
*
* @param n The integer value containing the bits that will
* be copied into the new bitset; REQUIRED
* @param bit_capacity The maximum number of bits needed in the new
* bitset; OPTIONAL, defaults to 64
*
* @return A new BitSet_t value preinitialized with the bits taken from
* the <n> argument. If <bit_capacity> is smaller the number of
* bits needed to represent <n> then only first few bytes of <n>
* will be used to seed the new bitset.
*
* @see New
* @see NewFromStrValue
* @see NewFromBitPositions
*/
EXPORT BitSet_t NewFromIntValue(LITTLE_ENDIAN UNSIGNED8 n, BitCapacity_t bit_capacity = 64) := EMBED(C++)
#option pure;
const uint32_t bytesNeeded = bit_capacity / 8 + (bit_capacity % 8 != 0 ? 1 : 0);
const uint32_t numBytesToCopy = (bytesNeeded < sizeof(n) ? bytesNeeded : sizeof(n));
// Create empty result bitset
__lenResult = bytesNeeded;
__result = rtlMalloc(__lenResult);
memset(__result, 0, __lenResult);
// Little endian integers have the same binary pattern that we are
// using, so a simple byte-wise copy is sufficient
memcpy(__result, &n, numBytesToCopy);
ENDEMBED;
/**
* Create a new bitset initialized with a binary string value. The optional
* <bit_capacity> argument provides control over how many bits are actually
* allocated in the bitset.
*
* @param s A STRING containing only ones and zeros; REQUIRED
* @param bit_capacity The minimum number of bits needed in the new
* bitset; use zero to indicate the number of bits
* should be derived from the length of <s>;
* OPTIONAL, defaults to zero
*
* @return A new BitSet_t value preinitialized with the characters read
* from the <s> argument. If <bit_capacity> is smaller than the
* number of characters in <s> then <bit_capacity> will be ignored.
*
* @see New
* @see NewFromIntValue
* @see NewFromBitPositions
*/
EXPORT BitSet_t NewFromStrValue(STRING s, BitCapacity_t bit_capacity = 0) := FUNCTION
BitSet_t _NewFromStrValue(STRING _s, BitCapacity_t _bit_capacity, BitCapacity_t _max_capacity = MAX_BIT_CAPACITY) := EMBED(C++)
#option pure;
const unsigned __int64 bitsRequested = (len_s > _bit_capacity ? len_s : _bit_capacity);
const unsigned __int64 actualBitCount = (bitsRequested < _max_capacity ? bitsRequested : _max_capacity);
const uint32_t bytesToAllocate = actualBitCount / 8 + (actualBitCount % 8 != 0 ? 1 : 0);
// Create empty result bitset
__lenResult = bytesToAllocate;
__result = rtlMalloc(__lenResult);
memset(__result, 0, __lenResult);
for (uint32_t resultBytePos = 0; resultBytePos < bytesToAllocate && resultBytePos * 8 < len_s; resultBytePos++)
{
__int64 incomingCharOffset = static_cast<__int64>(len_s) - static_cast<__int64>((resultBytePos + 1) * 8);
for (int x = 7; x >= 0; x--)
{
if (incomingCharOffset >= 0)
{
static_cast<byte*>(__result)[resultBytePos] |= ((_s[incomingCharOffset] == '0' ? 0 : 1) << x);
}
++incomingCharOffset;
}
}
ENDEMBED;
RETURN _NewFromStrValue(s, bit_capacity);
END;
/**
* Create a new bitset initialized from a list of bit positions. The
* <bit_capacity> argument provides control over how many bits are actually
* allocated in the bitset.
*
* @param positions A DATASET(BitPositionsRec) containing the
* zero-based bit positions to set in the
* new bitset; REQUIRED
* @param bit_capacity The minimum number of bits needed in the new
* bitset; use zero to indicate the number of bits
* should be derived from the highest bit position
* found within <positions>; OPTIONAL,
* defaults to zero
*
* @return A new BitSet_t value with bits set from the positions cited
* with the <positions> argument. If <bit_capacity> is smaller than
* highest position referenced in <positions> then it will be
* ignored.
*
* @see New
* @see NewFromIntValue
* @see NewFromStrValue
* @see BitsSetPositions
*/
EXPORT BitSet_t NewFromBitPositions(DATASET(BitPositionsRec) positions, BitCapacity_t bit_capacity = 0) := FUNCTION
BitSet_t _NewFromBitPositions(DATASET(BitPositionsRec) _positions, BitCapacity_t _bit_capacity, BitCapacity_t _max_capacity = MAX_BIT_CAPACITY, UNSIGNED1 _element_size = SIZEOF(bit_capacity)) := EMBED(C++)
#option pure;
const uint32_t numElements = len_positions / _element_size;
unsigned __int64 aPosition = 0;
unsigned __int64 highestPosition = 0;
// Find the highest referenced position
for (uint32_t x = 0; x < numElements; x++)
{
memcpy(&aPosition, static_cast<const byte*>(_positions) + (x * _element_size), _element_size);
if (aPosition > highestPosition)
{
highestPosition = aPosition;
}
}
const unsigned __int64 bitsRequested = (highestPosition > _bit_capacity ? highestPosition : _bit_capacity);
const unsigned __int64 actualBitCount = (bitsRequested < _max_capacity ? bitsRequested : _max_capacity);
const uint32_t bytesToAllocate = actualBitCount / 8 + (actualBitCount % 8 != 0 ? 1 : 0);
// Create empty result bitset
__lenResult = bytesToAllocate;
__result = rtlMalloc(__lenResult);
memset(__result, 0, __lenResult);
for (uint32_t x = 0; x < numElements; x++)
{
memcpy(&aPosition, static_cast<const byte*>(_positions) + (x * _element_size), _element_size);
uint32_t bytePos = aPosition / 8;
uint32_t bitPos = aPosition % 8;
byte newValue = 1 << bitPos;
static_cast<byte*>(__result)[bytePos] |= newValue;
}
ENDEMBED;
RETURN _NewFromBitPositions(positions, bit_capacity);
END;
/**
* Makes sure the given bitset can hold <bit_capacity> bits. A new bitset
* is always returned, even if the given bitset is large enough.
*
* @param b A bitset; REQUIRED
* @param bit_capacity The minimum number of bits needed in the
* bitset; REQUIRED
*
* @return A new BitSet_t value of at least size <bit_capacity>, with the
* bits from <b> copied over.
*/
EXPORT BitSet_t ReserveCapacity(CONST BitSet_t b, BitCapacity_t bit_capacity) := FUNCTION
BitSet_t _ReserveCapacity(CONST BitSet_t _b, BitCapacity_t _bit_capacity, BitCapacity_t _max_capacity = MAX_BIT_CAPACITY) := EMBED(C++)
#option pure;
const unsigned __int64 bitsRequested = (_bit_capacity < _max_capacity ? _bit_capacity : _max_capacity);
const uint32_t bytesNeeded = bitsRequested / 8 + (bitsRequested % 8 != 0 ? 1 : 0);
const uint32_t bytesToAllocate = (bytesNeeded > len_b ? bytesNeeded : len_b);
__lenResult = bytesToAllocate;
__result = rtlMalloc(__lenResult);
memcpy(__result, _b, len_b);
if (__lenResult > len_b)
{
memset(&(static_cast<byte*>(__result)[len_b]), 0, __lenResult - len_b);
}
ENDEMBED;
RETURN _ReserveCapacity(b, bit_capacity);
END;
/**
* Set every bit in a bitset to zero or one.
*
* @param b A bitset; REQUIRED
* @param on If TRUE, set every bit to one; if FALSE, set every
* bit to zero; REQUIRED
*
* @return A new BitSet_t, of the same size as <b>, with all bits set
* as indicated by the <on> argument.
*
* @see ResetBits
*/
EXPORT BitSet_t SetAllBits(CONST BitSet_t b, BOOLEAN on) := EMBED(C++)
#option pure;
// Create empty result bitset
__lenResult = lenB;
__result = rtlMalloc(__lenResult);
// Determine byte value to copy everywhere
const byte byteValue = (on ? 255 : 0);
// Copy byte value to result
memset(__result, byteValue, __lenResult);
ENDEMBED;
/**
* Set every bit in a bitset to zero.
*
* @param b A bitset; REQUIRED
*
* @return A new BitSet_t, of the same size as <b>, with all bits set
* to zero.
*
* @see SetAllBits
*/
EXPORT BitSet_t ResetBits(CONST BitSet_t b) := SetAllBits(b, FALSE);
/**
* Set a bit in a bitset to either zero or one.
*
* @param b A bitset; REQUIRED
* @param position The zero-based position of the bit to set; REQUIRED
* @param on If TRUE, set the bit to one; if FALSE, set the
* bit to zero; OPTIONAL, defaults to TRUE
*
* @return A new BitSet_t containing the bits from <b> and with bit
* <position> set as indicated by the <on> argument. If <position>
* is greater than the number of bits referenced by the bitset,
* an unchanged copy of <b> is returned.
*/
EXPORT BitSet_t SetBit(CONST BitSet_t b, BitPosition_t position, BOOLEAN on = TRUE) := FUNCTION
BitSet_t _SetBit(CONST BitSet_t b, BitPosition_t position, BOOLEAN on, BitPosition_t _max_position = HIGHEST_BIT_POSITION) := EMBED(C++)
#option pure;
// Create a copy of our bitset
__lenResult = lenB;
__result = rtlMalloc(__lenResult);
memcpy(__result, b, lenB);
const unsigned __int64 requestedPosition = (position < _max_position ? position : _max_position);
const uint32_t bytePos = requestedPosition / 8;
if (bytePos < lenB)
{
const byte bitValue = 1 << (requestedPosition % 8);
if (on)
{
static_cast<byte*>(__result)[bytePos] |= bitValue;
}
else
{
static_cast<byte*>(__result)[bytePos] &= ~bitValue;
}
}
ENDEMBED;
RETURN _SetBit(b, position, on);
END;
/**
* Flip a single bit from one to zero, or zero to one, depending on its
* original value.
*
* @param b A bitset; REQUIRED
* @param position The zero-based position of the bit to flip; REQUIRED
*
* @return A new BitSet_t containing the bits from <b> and with bit
* <position> flipped from zero to one or one to zero. If
* <position> is greater than the number of bits referenced by the
* bitset, an unchanged copy of <b> is returned.
*/
EXPORT BitSet_t FlipBit(CONST BitSet_t b, BitPosition_t position) := EMBED(C++)
#option pure;
// Create a copy of our bitset
__lenResult = lenB;
__result = rtlMalloc(__lenResult);
memcpy(__result, b, lenB);
const uint32_t bytePos = position / 8;
if (bytePos < lenB)
{
const byte bitValue = 1 << (position % 8);
static_cast<byte*>(__result)[bytePos] ^= bitValue;
}
ENDEMBED;
/**
* Return a boolean indicating if a single bit within a bitset is one or
* not. This is the equivalent of this test:
*
* (b >> position) & 1 == 1
*
* @param b A bitset; REQUIRED
* @param position The zero-based position of the bit to test; REQUIRED
*
* @return TRUE if the indicated bit's value is one, FALSE otherwise. If
* <position> is greater than the number of bits referenced by the
* bitset, FALSE is returned.
*
* @see TestBits
* @see TestAnyBitsSet
* @see TestNoBitsSet
* @see TestAllBitsSet
* @see TestBitSetsEqual
*/
EXPORT BOOLEAN TestBit(CONST BitSet_t b, BitPosition_t position) := EMBED(C++)
#option pure;
bool isSet = false;
const uint32_t bytePos = position / 8;
if (bytePos < lenB)
{
const byte testValue = 1 << (position % 8);
isSet = ((static_cast<const byte*>(b)[bytePos] & testValue) == testValue);
}
return isSet;
ENDEMBED;
/**
* Return a boolean indicating if every set bit in one bitset is also set
* in another bitset. This is the equivalent of this test:
*
* (b1 & b2) == b2
*
* @param b1 A bitset that you want to test against; REQUIRED
* @param b2 A bitset containing the bits to test; REQUIRED
*
* @return TRUE if every set bit in <b2> is also set in <b1>, FALSE
* otherwise.
*
* @see TestBit
* @see TestAnyBitsSet
* @see TestNoBitsSet
* @see TestAllBitsSet
* @see TestBitSetsEqual
*/
EXPORT BOOLEAN TestBits(CONST BitSet_t b1, CONST BitSet_t b2) := EMBED(C++)
#option pure;
for (uint32_t x = 0; x < lenB2; x++)
{
if ((x < lenB1 && ((static_cast<const byte*>(b1)[x] & static_cast<const byte*>(b2)[x]) != static_cast<const byte*>(b2)[x]))
|| (x >= lenB1 && (static_cast<const byte*>(b2)[x]) != 0))
{
return false;
}
}
return true;
ENDEMBED;
/**
* Return a boolean indicating if any bit in a bitset is set to one.
*
* @param b A bitset; REQUIRED
*
* @return TRUE if any bit in <b> is set to one, FALSE otherwise.
*
* @see TestBit
* @see TestBits
* @see TestNoBitsSet
* @see TestAllBitsSet
* @see TestBitSetsEqual
*/
EXPORT BOOLEAN TestAnyBitsSet(CONST BitSet_t b) := EMBED(C++)
#option pure;
for (uint32_t bytePos = 0; bytePos < lenB; bytePos++)
{
const byte val = static_cast<const byte*>(b)[bytePos];
if (val > 0)
{
return true;
}
}
return false;
ENDEMBED;
/**
* Return a boolean indicating if no bit in a bitset is set to one.
*
* @param b A bitset; REQUIRED
*
* @return TRUE if no bit in <b> is set to one, FALSE otherwise.
*
* @see TestBit
* @see TestBits
* @see TestAnyBitsSet
* @see TestAllBitsSet
* @see TestBitSetsEqual
*/
EXPORT BOOLEAN TestNoBitsSet(CONST BitSet_t b) := EMBED(C++)
#option pure;
for (uint32_t bytePos = 0; bytePos < lenB; bytePos++)
{
const byte val = static_cast<const byte*>(b)[bytePos];
if (val > 0)
{
return false;
}
}
return true;
ENDEMBED;
/**
* Return a boolean indicating if every bit in a bitset is set to one.
*
* @param b A bitset; REQUIRED
*
* @return TRUE if every bit in <b> is set to one, FALSE otherwise.
*
* @see TestBit
* @see TestBits
* @see TestAnyBitsSet
* @see TestNoBitsSet
* @see TestBitSetsEqual
*/
EXPORT BOOLEAN TestAllBitsSet(CONST BitSet_t b) := EMBED(C++)
#option pure;
for (uint32_t bytePos = 0; bytePos < lenB; bytePos++)
{
const byte val = static_cast<const byte*>(b)[bytePos];
if (val != 255)
{
return false;
}
}
return true;
ENDEMBED;
/**
* Return a boolean indicating if two bitsets are completely identical.
*
* @param b1 A bitset; REQUIRED
* @param b2 Another bitset; REQUIRED
*
* @return TRUE <b1> and <b2> have both the same size and combination of
* set and unset bits, FALSE otherwise.
*
* @see TestBit
* @see TestBits
*/
EXPORT BOOLEAN TestBitSetsEqual(CONST BitSet_t b1, CONST BitSet_t b2) := EMBED(C++)
#option pure;
return lenB1 == lenB2 && memcmp(b1, b2, lenB1) == 0;
ENDEMBED;
/**
* Count the number of set bits within a bitset.
*
* @param b A bitset; REQUIRED
*
* @return A non-negative integer representing the number of set bits
* within <b>.
*/
EXPORT BitCapacity_t CountBitsSet(CONST BitSet_t b) := EMBED(C++)
#option pure;
unsigned __int64 numBitsSet = 0;
for (uint32_t bytePos = 0; bytePos < lenB; bytePos++)
{
byte val = static_cast<const byte*>(b)[bytePos];
while (val > 0)
{
if ((val & 1) == 1)
{
++numBitsSet;
}
val >>= 1;
}
}
return numBitsSet;
ENDEMBED;
/**
* Collect the positions of all set bits within a bitset.
*
* @param b A bitset; REQUIRED
*
* @return A new DATASET(BitPositionsRec) containing the zero-based
* positions of all set bits within the bitset. If no bits are
* set then the resulting dataset will be empty.
*
* @see NewFromBitPositions
*/
EXPORT STREAMED DATASET(BitPositionsRec) BitsSetPositions(CONST BitSet_t b) := EMBED(C++)
#option pure;
class StreamDataset : public RtlCInterface, implements IRowStream
{
public:
StreamDataset(IEngineRowAllocator* _resultAllocator, size32_t _dataLength, const void* _dataPtr)
: resultAllocator(_resultAllocator), dataLength(_dataLength), dataPtr(static_cast<const byte*>(_dataPtr))
{
isStopped = false;
currentByte = 0;
currentBit = 0;
}
RTLIMPLEMENT_IINTERFACE
virtual const void* nextRow()
{
if (isStopped)
{
return NULL;
}
// Find next set bit
while (currentByte < dataLength)
{
while (currentBit < 8)
{
byte testValue = 1 << currentBit;
if ((dataPtr[currentByte] & testValue) == testValue)
{
RtlDynamicRowBuilder rowBuilder(resultAllocator);
unsigned int len = 6;
byte* row = rowBuilder.ensureCapacity(len, NULL);
unsigned __int64 position = currentByte * 8 + currentBit;
// Copy the position to the output record
memcpy(row, &position, len);
// Increment bit position for next call
++currentBit;
return rowBuilder.finalizeRowClear(len);
}
else
{
++currentBit;
}
}
currentBit = 0;
++currentByte;
}
isStopped = true;
return NULL;
}
virtual void stop()
{
isStopped = true;
}
protected:
Linked<IEngineRowAllocator> resultAllocator;
private:
size32_t dataLength;
const byte* dataPtr;
bool isStopped;
size32_t currentByte;
size32_t currentBit;
};
#body
return new StreamDataset(_resultAllocator, lenB, b);
ENDEMBED;
/**
* Shift all bits in a bitset left a given number of positions. This is
* the equivalent of:
*
* b << num_bits
*
* @param b A bitset; REQUIRED
* @param num_bits The number of positions to shift all bits to the
* left; OPTIONAL, defaults to one.
*
* @return A new BitSet_t bitset with the contents of <b> left-shifted by
* the number of positions indicated by <num_bits>. The bit
* positions that have been vacated by the shift operation are
* zero-filled. Bits that are shifted off the end are discarded.
*/
EXPORT BitSet_t ShiftLeft(CONST BitSet_t b, BitCapacity_t num_bits = 1) := EMBED(C++)
#option pure;
// Create empty result bitset
__lenResult = lenB;
__result = rtlMalloc(__lenResult);
if (num_bits > 0)
{
const uint32_t shift = num_bits / 8;
const uint32_t offset = num_bits % 8;
memset(__result, 0, __lenResult);
if (shift < lenB)
{
if (offset == 0)
{
for (size_t x = lenB - 1; x >= shift; x--)
{
static_cast<byte*>(__result)[x] = static_cast<const byte*>(b)[x - shift];
}
}
else
{
const size_t subOffset = (8 - offset);
for (size_t x = lenB - 1; x > shift; x--)
{
static_cast<byte*>(__result)[x] = ((static_cast<const byte*>(b)[x - shift] << offset) | (static_cast<const byte*>(b)[x - shift - 1] >> subOffset));
}
static_cast<byte*>(__result)[shift] = static_cast<const byte*>(b)[0] << offset;
}
}
}
else
{
// No shifting, so just copy
memcpy(__result, b, __lenResult);
}
ENDEMBED;
/**
* Shift all bits in a bitset right a given number of positions. This is
* the equivalent of:
*
* b >> num_bits
*
* @param b A bitset; REQUIRED
* @param num_bits The number of positions to shift all bits to the
* right; OPTIONAL, defaults to one.
*
* @return A new BitSet_t bitset with the contents of <b> right-shifted by
* the number of positions indicated by <num_bits>. The bit
* positions that have been vacated by the shift operation are
* zero-filled. Bits that are shifted off the end are discarded.
*/
EXPORT BitSet_t ShiftRight(CONST BitSet_t b, BitCapacity_t num_bits) := EMBED(C++)
#option pure;
// Create empty result bitset
__lenResult = lenB;
__result = rtlMalloc(__lenResult);
if (num_bits > 0)
{
const uint32_t shift = num_bits / 8;
const uint32_t offset = num_bits % 8;
const size_t limit = lenB - shift - 1;
memset(__result, 0, __lenResult);
if (shift < lenB)
{
if (offset == 0)
{
for (size_t x = 0; x <= limit; x++)
{
static_cast<byte*>(__result)[x] = static_cast<const byte*>(b)[x + shift];
}
}
else
{
const size_t subOffset = (8 - offset);
for (size_t x = 0; x < limit; x++)
{
static_cast<byte*>(__result)[x] = ((static_cast<const byte*>(b)[x + shift] >> offset) | (static_cast<const byte*>(b)[x + shift + 1] << subOffset));
}
static_cast<byte*>(__result)[limit] = static_cast<const byte*>(b)[lenB - 1] >> offset;
}
}
}
else
{
// No shifting, so just copy
memcpy(__result, b, __lenResult);
}
ENDEMBED;
/**
* Perform a bitwise AND operation on two bitsets. This is the equivalent
* of this function:
*
* b1 & b2
*
* @param b1 A bitset; REQUIRED
* @param b2 A bitset; REQUIRED
*
* @return A new BitSet_t containing the result of a bitwise AND operation
* between <b1> and <b2>. The new bitset will be as large as the
* larger of <b1> and <b2>.
*
* @see BitwiseOR
* @see BitwiseXOR
* @see BitwiseNOT
*/
EXPORT BitSet_t BitwiseAND(CONST BitSet_t b1, CONST BitSet_t b2) := EMBED(C++)
#option pure;
const bool b1Receives = (lenB1 >= lenB2);
uint32_t largerNumBytes = 0;
uint32_t smallerNumBytes = 0;
const byte* largerPtr = NULL;
const byte* smallerPtr = NULL;
if (b1Receives)
{
largerNumBytes = lenB1;
smallerNumBytes = lenB2;