-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitVector.c
3919 lines (3562 loc) · 116 KB
/
BitVector.c
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
#ifndef MODULE_BIT_VECTOR
#define MODULE_BIT_VECTOR
/*****************************************************************************/
/* MODULE NAME: BitVector.c MODULE TYPE: (adt) */
/*****************************************************************************/
/* MODULE IMPORTS: */
/*****************************************************************************/
#include <stdlib.h> /* MODULE TYPE: (sys) */
#include <limits.h> /* MODULE TYPE: (sys) */
#include <string.h> /* MODULE TYPE: (sys) */
#include <ctype.h> /* MODULE TYPE: (sys) */
#include "ToolBox.h" /* MODULE TYPE: (dat) */
/*****************************************************************************/
/* MODULE INTERFACE: */
/*****************************************************************************/
typedef enum
{
BV_ErrCode_Ok = 0, /* everything went allright */
BV_ErrCode_Type, /* types word and size_t have incompatible sizes */
BV_ErrCode_Bits, /* bits of word and sizeof(word) are inconsistent */
BV_ErrCode_Word, /* size of word is less than 16 bits */
BV_ErrCode_Powr, /* number of bits of word is not a power of two */
BV_ErrCode_Loga, /* error in calculation of logarithm */
BV_ErrCode_Lpwr, /* number of bits of long is not a power of two */
BV_ErrCode_WgtL, /* size of word is greater than size of long */
BV_ErrCode_Null, /* unable to allocate memory */
BV_ErrCode_Indx, /* index out of range */
BV_ErrCode_Ordr, /* minimum > maximum index */
BV_ErrCode_Size, /* bit vector size mismatch */
BV_ErrCode_Pars, /* input string syntax error */
BV_ErrCode_Ovfl, /* numeric overflow error */
BV_ErrCode_Same, /* operands must be distinct */
BV_ErrCode_Expo, /* exponent must be positive */
BV_ErrCode_Zero, /* division by zero error */
BV_ErrCode_Oops /* unexpected error (contact author) */
}
BV_ErrCode;
typedef
wordptr *bv_listptr;
/* ===> MISCELLANEOUS BASIC FUNCTIONS: <=== */
charptr BitVector_Error (BV_ErrCode error); /* map errcode to string */
BV_ErrCode BitVector_Boot (void); /* 0 = ok, 1..17 = error */
N_word BitVector_Size (N_int bits); /* bit vec. size (# of words) */
N_word BitVector_Mask (N_int bits); /* bit vec. mask (unused bits) */
/* ===> CLASS METHODS: <=== */
charptr BitVector_Version (void); /* return version string */
N_int BitVector_Word_Bits (void); /* return # of bits in machine word */
N_int BitVector_Long_Bits (void); /* return # of bits in unsigned long */
/* ===> CONSTRUCTOR METHODS: <=== */
wordptr BitVector_Create (N_int bits, boolean clear); /* malloc */
bv_listptr BitVector_Create_List(N_int bits, boolean clear, N_int count);
wordptr BitVector_Resize (wordptr oldaddr, N_int bits); /* realloc */
wordptr BitVector_Shadow (wordptr addr); /* make same size but empty */
wordptr BitVector_Clone (wordptr addr); /* make exact duplicate */
wordptr BitVector_Concat (wordptr X, wordptr Y); /* return concat'd */
/* ===> DESTRUCTOR METHODS: <=== */
void BitVector_Dispose (charptr string); /* string */
void BitVector_Destroy (wordptr addr); /* bitvec */
void BitVector_Destroy_List (bv_listptr list, N_int count); /* list */
/* ===> OBJECT METHODS: <=== */
/* ===> bit vector copy function: */
void BitVector_Copy (wordptr X, wordptr Y); /* X = Y */
/* ===> bit vector initialization: */
void BitVector_Empty (wordptr addr); /* X = {} */
void BitVector_Fill (wordptr addr); /* X = ~{} */
void BitVector_Flip (wordptr addr); /* X = ~X */
void BitVector_Primes (wordptr addr);
/* ===> miscellaneous functions: */
void BitVector_Reverse (wordptr X, wordptr Y);
/* ===> bit vector interval operations and functions: */
void BitVector_Interval_Empty (wordptr addr, N_int lower, N_int upper);
void BitVector_Interval_Fill (wordptr addr, N_int lower, N_int upper);
void BitVector_Interval_Flip (wordptr addr, N_int lower, N_int upper);
void BitVector_Interval_Reverse (wordptr addr, N_int lower, N_int upper);
boolean BitVector_interval_scan_inc (wordptr addr, N_int start,
N_intptr min, N_intptr max);
boolean BitVector_interval_scan_dec (wordptr addr, N_int start,
N_intptr min, N_intptr max);
void BitVector_Interval_Copy (wordptr X, wordptr Y, N_int Xoffset,
N_int Yoffset, N_int length);
wordptr BitVector_Interval_Substitute(wordptr X, wordptr Y,
N_int Xoffset, N_int Xlength,
N_int Yoffset, N_int Ylength);
/* ===> bit vector test functions: */
boolean BitVector_is_empty (wordptr addr); /* X == {} ? */
boolean BitVector_is_full (wordptr addr); /* X == ~{} ? */
boolean BitVector_equal (wordptr X, wordptr Y); /* X == Y ? */
Z_int BitVector_Lexicompare(wordptr X, wordptr Y); /* X <,=,> Y ? */
Z_int BitVector_Compare (wordptr X, wordptr Y); /* X <,=,> Y ? */
/* ===> bit vector string conversion functions: */
charptr BitVector_to_Hex (wordptr addr);
BV_ErrCode BitVector_from_Hex (wordptr addr, charptr string);
charptr BitVector_to_Bin (wordptr addr);
BV_ErrCode BitVector_from_Bin (wordptr addr, charptr string);
charptr BitVector_to_Dec (wordptr addr);
BV_ErrCode BitVector_from_Dec (wordptr addr, charptr string);
charptr BitVector_to_Enum (wordptr addr);
BV_ErrCode BitVector_from_Enum (wordptr addr, charptr string);
/* ===> bit vector bit operations, functions & tests: */
void BitVector_Bit_Off (wordptr addr, N_int index); /* X = X \ {x} */
void BitVector_Bit_On (wordptr addr, N_int index); /* X = X + {x} */
boolean BitVector_bit_flip (wordptr addr, N_int index); /* (X+{x})\(X*{x}) */
boolean BitVector_bit_test (wordptr addr, N_int index); /* {x} in X ? */
void BitVector_Bit_Copy (wordptr addr, N_int index, boolean bit);
/* ===> bit vector bit shift & rotate functions: */
void BitVector_LSB (wordptr addr, boolean bit);
void BitVector_MSB (wordptr addr, boolean bit);
boolean BitVector_lsb_ (wordptr addr);
boolean BitVector_msb_ (wordptr addr);
boolean BitVector_rotate_left (wordptr addr);
boolean BitVector_rotate_right (wordptr addr);
boolean BitVector_shift_left (wordptr addr, boolean carry_in);
boolean BitVector_shift_right (wordptr addr, boolean carry_in);
void BitVector_Move_Left (wordptr addr, N_int bits);
void BitVector_Move_Right (wordptr addr, N_int bits);
/* ===> bit vector insert/delete bits: */
void BitVector_Insert (wordptr addr, N_int offset, N_int count,
boolean clear);
void BitVector_Delete (wordptr addr, N_int offset, N_int count,
boolean clear);
/* ===> bit vector arithmetic: */
boolean BitVector_increment (wordptr addr); /* X++ */
boolean BitVector_decrement (wordptr addr); /* X-- */
boolean BitVector_compute (wordptr X, wordptr Y, wordptr Z, boolean minus,
boolean *carry);
boolean BitVector_add (wordptr X, wordptr Y, wordptr Z, boolean *carry);
boolean BitVector_sub (wordptr X, wordptr Y, wordptr Z, boolean *carry);
boolean BitVector_inc (wordptr X, wordptr Y);
boolean BitVector_dec (wordptr X, wordptr Y);
void BitVector_Negate (wordptr X, wordptr Y);
void BitVector_Absolute (wordptr X, wordptr Y);
Z_int BitVector_Sign (wordptr addr);
BV_ErrCode BitVector_Mul_Pos (wordptr X, wordptr Y, wordptr Z, boolean strict);
BV_ErrCode BitVector_Multiply (wordptr X, wordptr Y, wordptr Z);
BV_ErrCode BitVector_Div_Pos (wordptr Q, wordptr X, wordptr Y, wordptr R);
BV_ErrCode BitVector_Divide (wordptr Q, wordptr X, wordptr Y, wordptr R);
BV_ErrCode BitVector_GCD (wordptr X, wordptr Y, wordptr Z);
BV_ErrCode BitVector_GCD2 (wordptr U, wordptr V, wordptr W, /* O */
wordptr X, wordptr Y); /* I */
BV_ErrCode BitVector_Power (wordptr X, wordptr Y, wordptr Z);
/* ===> direct memory access functions: */
void BitVector_Block_Store(wordptr addr, charptr buffer, N_int length);
charptr BitVector_Block_Read (wordptr addr, N_intptr length);
/* ===> word array functions: */
void BitVector_Word_Store (wordptr addr, N_int offset, N_int value);
N_int BitVector_Word_Read (wordptr addr, N_int offset);
void BitVector_Word_Insert(wordptr addr, N_int offset, N_int count,
boolean clear);
void BitVector_Word_Delete(wordptr addr, N_int offset, N_int count,
boolean clear);
/* ===> arbitrary size chunk functions: */
void BitVector_Chunk_Store(wordptr addr, N_int chunksize,
N_int offset, N_long value);
N_long BitVector_Chunk_Read (wordptr addr, N_int chunksize,
N_int offset);
/* ===> set operations: */
void Set_Union (wordptr X, wordptr Y, wordptr Z); /* X = Y + Z */
void Set_Intersection (wordptr X, wordptr Y, wordptr Z); /* X = Y * Z */
void Set_Difference (wordptr X, wordptr Y, wordptr Z); /* X = Y \ Z */
void Set_ExclusiveOr (wordptr X, wordptr Y, wordptr Z); /*(Y+Z)\(Y*Z)*/
void Set_Complement (wordptr X, wordptr Y); /* X = ~Y */
/* ===> set functions: */
boolean Set_subset (wordptr X, wordptr Y); /* X in Y */
N_int Set_Norm (wordptr addr); /* = |X| */
N_int Set_Norm2 (wordptr addr); /* = |X| */
N_int Set_Norm3 (wordptr addr); /* = |X| */
Z_long Set_Min (wordptr addr); /* = min(X) */
Z_long Set_Max (wordptr addr); /* = max(X) */
/* ===> matrix-of-booleans operations: */
void Matrix_Multiplication(wordptr X, N_int rowsX, N_int colsX,
wordptr Y, N_int rowsY, N_int colsY,
wordptr Z, N_int rowsZ, N_int colsZ);
void Matrix_Product (wordptr X, N_int rowsX, N_int colsX,
wordptr Y, N_int rowsY, N_int colsY,
wordptr Z, N_int rowsZ, N_int colsZ);
void Matrix_Closure (wordptr addr, N_int rows, N_int cols);
void Matrix_Transpose (wordptr X, N_int rowsX, N_int colsX,
wordptr Y, N_int rowsY, N_int colsY);
/*****************************************************************************/
/* MODULE RESOURCES: */
/*****************************************************************************/
#define BV_BITS_(BitVector) *(BitVector-3)
#define BV_SIZE_(BitVector) *(BitVector-2)
#define BV_MASK_(BitVector) *(BitVector-1)
#define BV_ERRCODE_TYPE "sizeof(word) > sizeof(size_t)"
#define BV_ERRCODE_BITS "bits(word) != sizeof(word)*8"
#define BV_ERRCODE_WORD "bits(word) < 16"
#define BV_ERRCODE_POWR "bits(word) is not a power of two"
#define BV_ERRCODE_LOGA "bits(word) != 2^ld(bits(word))"
#define BV_ERRCODE_LPWR "bits(long) is not a power of two"
#define BV_ERRCODE_WGTL "bits(word) > bits(long)"
#define BV_ERRCODE_NULL "unable to allocate memory"
#define BV_ERRCODE_INDX "index out of range"
#define BV_ERRCODE_ORDR "minimum > maximum index"
#define BV_ERRCODE_SIZE "bit vector size mismatch"
#define BV_ERRCODE_PARS "input string syntax error"
#define BV_ERRCODE_OVFL "numeric overflow error"
#define BV_ERRCODE_SAME "result vector(s) must be distinct"
#define BV_ERRCODE_EXPO "exponent must be positive"
#define BV_ERRCODE_ZERO "division by zero error"
#define BV_ERRCODE_OOPS "unexpected internal error - please contact author"
const N_int BV_ByteNorm[256] =
{
0x00, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03,
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, /* 0x00 */
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, /* 0x10 */
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, /* 0x20 */
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0x30 */
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, /* 0x40 */
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0x50 */
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0x60 */
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, /* 0x70 */
0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, /* 0x80 */
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0x90 */
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0xA0 */
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, /* 0xB0 */
0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06, /* 0xC0 */
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, /* 0xD0 */
0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07, /* 0xE0 */
0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
0x05, 0x06, 0x06, 0x07, 0x06, 0x07, 0x07, 0x08 /* 0xF0 */
};
/*****************************************************************************/
/* MODULE IMPLEMENTATION: */
/*****************************************************************************/
/**********************************************/
/* global implementation-intrinsic constants: */
/**********************************************/
#define BV_HIDDENWORDS 3
/*****************************************************************/
/* global machine-dependent constants (set by "BitVector_Boot"): */
/*****************************************************************/
static N_word BV_WordBits; /* = # of bits in a machine word (power of 2) */
static N_word BV_ModMask; /* = BV_WordBits-1 (for "modulo BV_WordBits") */
static N_word BV_LogBits; /* = ld(BV_WordBits) (logarithmus dualis) */
static N_word BV_Factor; /* = ld(BV_WordBits / 8) (ld of # of bytes) */
static N_word BV_LSB = 1; /* = mask for least significant bit */
static N_word BV_MSB; /* = mask for most significant bit */
static N_word BV_LongBits; /* = # of bits in unsigned long */
static N_word BV_Log10; /* = logarithm to base 10 of BV_WordBits - 1 */
static N_word BV_Exp10; /* = largest possible power of 10 in signed int */
/********************************************************************/
/* global bit mask table for fast access (set by "BitVector_Boot"): */
/********************************************************************/
#define BV_MASKTABSIZE (sizeof(N_word) << 3)
static N_word BV_BitMaskTab[BV_MASKTABSIZE];
/*****************************/
/* global macro definitions: */
/*****************************/
#define BIT_VECTOR_ZERO_WORDS(target,count) \
while (count-- > 0) *target++ = 0;
#define BIT_VECTOR_FILL_WORDS(target,fill,count) \
while (count-- > 0) *target++ = fill;
#define BIT_VECTOR_FLIP_WORDS(target,flip,count) \
while (count-- > 0) *target++ ^= flip;
#define BIT_VECTOR_COPY_WORDS(target,source,count) \
while (count-- > 0) *target++ = *source++;
#define BIT_VECTOR_BACK_WORDS(target,source,count) \
{ target += count; source += count; while (count-- > 0) *--target = *--source; }
#define BIT_VECTOR_CLR_BIT(address,index) \
*(address+(index>>BV_LogBits)) &= NOT BV_BitMaskTab[index AND BV_ModMask];
#define BIT_VECTOR_SET_BIT(address,index) \
*(address+(index>>BV_LogBits)) |= BV_BitMaskTab[index AND BV_ModMask];
#define BIT_VECTOR_TST_BIT(address,index) \
((*(address+(index>>BV_LogBits)) AND BV_BitMaskTab[index AND BV_ModMask]) != 0)
#define BIT_VECTOR_FLP_BIT(address,index,mask) \
(mask = BV_BitMaskTab[index AND BV_ModMask]), \
(((*(addr+(index>>BV_LogBits)) ^= mask) AND mask) != 0)
#define BIT_VECTOR_DIGITIZE(type,value,digit) \
value = (type) ((digit = value) / 10); \
digit -= value * 10; \
digit += (type) '0';
/*********************************************************/
/* private low-level functions (potentially dangerous!): */
/*********************************************************/
static N_word BIT_VECTOR_power10(N_word x)
{
N_word y = 1;
while (x-- > 0) y *= 10;
return(y);
}
static void BIT_VECTOR_zro_words(wordptr addr, N_word count)
{
BIT_VECTOR_ZERO_WORDS(addr,count)
}
static void BIT_VECTOR_cpy_words(wordptr target, wordptr source, N_word count)
{
BIT_VECTOR_COPY_WORDS(target,source,count)
}
static void BIT_VECTOR_mov_words(wordptr target, wordptr source, N_word count)
{
if (target != source)
{
if (target < source) BIT_VECTOR_COPY_WORDS(target,source,count)
else BIT_VECTOR_BACK_WORDS(target,source,count)
}
}
static void BIT_VECTOR_ins_words(wordptr addr, N_word total, N_word count,
boolean clear)
{
N_word length;
if ((total > 0) and (count > 0))
{
if (count > total) count = total;
length = total - count;
if (length > 0) BIT_VECTOR_mov_words(addr+count,addr,length);
if (clear) BIT_VECTOR_zro_words(addr,count);
}
}
static void BIT_VECTOR_del_words(wordptr addr, N_word total, N_word count,
boolean clear)
{
N_word length;
if ((total > 0) and (count > 0))
{
if (count > total) count = total;
length = total - count;
if (length > 0) BIT_VECTOR_mov_words(addr,addr+count,length);
if (clear) BIT_VECTOR_zro_words(addr+length,count);
}
}
static void BIT_VECTOR_reverse(charptr string, N_word length)
{
charptr last;
N_char temp;
if (length > 1)
{
last = string + length - 1;
while (string < last)
{
temp = *string;
*string = *last;
*last = temp;
string++;
last--;
}
}
}
static N_word BIT_VECTOR_int2str(charptr string, N_word value)
{
N_word length;
N_word digit;
charptr work;
work = string;
if (value > 0)
{
length = 0;
while (value > 0)
{
BIT_VECTOR_DIGITIZE(N_word,value,digit)
*work++ = (N_char) digit;
length++;
}
BIT_VECTOR_reverse(string,length);
}
else
{
length = 1;
*work++ = (N_char) '0';
}
return(length);
}
static N_word BIT_VECTOR_str2int(charptr string, N_word *value)
{
N_word length;
N_word digit;
*value = 0;
length = 0;
digit = (N_word) *string++;
/* separate because isdigit() is likely a macro! */
while (isdigit((int)digit) != 0)
{
length++;
digit -= (N_word) '0';
if (*value) *value *= 10;
*value += digit;
digit = (N_word) *string++;
}
return(length);
}
/********************************************/
/* routine to convert error code to string: */
/********************************************/
charptr BitVector_Error(BV_ErrCode error)
{
switch (error)
{
case BV_ErrCode_Ok: return( (charptr) NULL ); break;
case BV_ErrCode_Type: return( (charptr) BV_ERRCODE_TYPE ); break;
case BV_ErrCode_Bits: return( (charptr) BV_ERRCODE_BITS ); break;
case BV_ErrCode_Word: return( (charptr) BV_ERRCODE_WORD ); break;
case BV_ErrCode_Powr: return( (charptr) BV_ERRCODE_POWR ); break;
case BV_ErrCode_Loga: return( (charptr) BV_ERRCODE_LOGA ); break;
case BV_ErrCode_Lpwr: return( (charptr) BV_ERRCODE_LPWR ); break;
case BV_ErrCode_WgtL: return( (charptr) BV_ERRCODE_WGTL ); break;
case BV_ErrCode_Null: return( (charptr) BV_ERRCODE_NULL ); break;
case BV_ErrCode_Indx: return( (charptr) BV_ERRCODE_INDX ); break;
case BV_ErrCode_Ordr: return( (charptr) BV_ERRCODE_ORDR ); break;
case BV_ErrCode_Size: return( (charptr) BV_ERRCODE_SIZE ); break;
case BV_ErrCode_Pars: return( (charptr) BV_ERRCODE_PARS ); break;
case BV_ErrCode_Ovfl: return( (charptr) BV_ERRCODE_OVFL ); break;
case BV_ErrCode_Same: return( (charptr) BV_ERRCODE_SAME ); break;
case BV_ErrCode_Expo: return( (charptr) BV_ERRCODE_EXPO ); break;
case BV_ErrCode_Zero: return( (charptr) BV_ERRCODE_ZERO ); break;
default: return( (charptr) BV_ERRCODE_OOPS ); break;
}
}
/*****************************************/
/* automatic self-configuration routine: */
/*****************************************/
/*******************************************************/
/* */
/* MUST be called once prior to any other function */
/* to initialize the machine dependent constants */
/* of this package! */
/* */
/*******************************************************/
BV_ErrCode BitVector_Boot(void)
{
N_word sample;
N_long longsample;
if (sizeof(N_word) > sizeof(size_t)) return(BV_ErrCode_Type);
BV_WordBits = 0;
sample = ~0;
while (sample) /* determine # of bits in a machine word */
{
sample &= sample - 1;
BV_WordBits++;
}
if (BV_WordBits != (sizeof(N_word) << 3)) return(BV_ErrCode_Bits);
if (BV_WordBits < 16) return(BV_ErrCode_Word);
BV_LongBits = 0;
longsample = ~0L;
while (longsample) /* determine # of bits in an unsigned long */
{
longsample &= longsample - 1L;
BV_LongBits++;
}
BV_LogBits = 0;
sample = BV_ModMask = BV_WordBits - 1;
if (sample AND BV_WordBits) return(BV_ErrCode_Powr); /* not a power of 2 */
while (sample)
{
sample &= sample - 1;
BV_LogBits++;
}
if (BV_WordBits != (BV_LSB << BV_LogBits)) return(BV_ErrCode_Loga); /* ld wrong */
longsample = BV_LongBits - 1;
if ((longsample AND BV_LongBits) or (BV_LongBits != (sizeof(N_long) << 3)))
{
BV_LongBits = (sizeof(N_long) << 3);
longsample = BV_LongBits - 1;
if (longsample AND BV_LongBits) return(BV_ErrCode_Lpwr);
}
if (BV_WordBits > BV_LongBits) return(BV_ErrCode_WgtL);
if (BV_WordBits > BV_MASKTABSIZE) return(BV_ErrCode_Oops);
for ( sample = 0; sample < BV_WordBits; sample++ )
{
BV_BitMaskTab[sample] = (BV_LSB << sample);
}
BV_Factor = BV_LogBits - 3; /* ld(Bits/8) = ld(Bits)-ld(8) = ld(Bits)-3 */
BV_MSB = (BV_LSB << BV_ModMask);
BV_Log10 = (N_word) (BV_ModMask * 0.30103); /* (Bits-1) * ln(2) / ln(10) */
BV_Exp10 = BIT_VECTOR_power10(BV_Log10);
return(BV_ErrCode_Ok);
}
N_word BitVector_Size(N_int bits) /* bit vector size (# of words) */
{
N_word size;
size = bits >> BV_LogBits;
if (bits AND BV_ModMask) size++;
return(size);
}
N_word BitVector_Mask(N_int bits) /* bit vector mask (unused bits) */
{
N_word mask;
mask = bits AND BV_ModMask;
if (mask) mask = (N_word) ~(~0L << mask); else mask = (N_word) ~0L;
return(mask);
}
charptr BitVector_Version(void)
{
return((charptr)"7.4");
}
N_int BitVector_Word_Bits(void)
{
return(BV_WordBits);
}
N_int BitVector_Long_Bits(void)
{
return(BV_LongBits);
}
/********************************************************************/
/* */
/* WARNING: Do not "free()" constant character strings, i.e., */
/* don't call "BitVector_Dispose()" for strings returned */
/* by "BitVector_Error()" or "BitVector_Version()"! */
/* */
/* ONLY call this function for strings allocated with "malloc()", */
/* i.e., the strings returned by the functions "BitVector_to_*()" */
/* and "BitVector_Block_Read()"! */
/* */
/********************************************************************/
void BitVector_Dispose(charptr string) /* free string */
{
if (string != NULL) free((voidptr) string);
}
void BitVector_Destroy(wordptr addr) /* free bitvec */
{
if (addr != NULL)
{
addr -= BV_HIDDENWORDS;
free((voidptr) addr);
}
}
void BitVector_Destroy_List(bv_listptr list, N_int count) /* free list */
{
bv_listptr slot;
if (list != NULL)
{
slot = list;
while (count-- > 0)
{
BitVector_Destroy(*slot++);
}
free((voidptr) list);
}
}
wordptr BitVector_Create(N_int bits, boolean clear) /* malloc */
{
N_word size;
N_word mask;
N_word bytes;
wordptr addr;
wordptr zero;
size = BitVector_Size(bits);
mask = BitVector_Mask(bits);
bytes = (size + BV_HIDDENWORDS) << BV_Factor;
addr = (wordptr) malloc((size_t) bytes);
if (addr != NULL)
{
*addr++ = bits;
*addr++ = size;
*addr++ = mask;
if (clear)
{
zero = addr;
BIT_VECTOR_ZERO_WORDS(zero,size)
}
}
return(addr);
}
bv_listptr BitVector_Create_List(N_int bits, boolean clear, N_int count)
{
bv_listptr list = NULL;
bv_listptr slot;
wordptr addr;
N_int i;
if (count > 0)
{
list = (bv_listptr) malloc(sizeof(wordptr) * count);
if (list != NULL)
{
slot = list;
for ( i = 0; i < count; i++ )
{
addr = BitVector_Create(bits,clear);
if (addr == NULL)
{
BitVector_Destroy_List(list,i);
return(NULL);
}
*slot++ = addr;
}
}
}
return(list);
}
wordptr BitVector_Resize(wordptr oldaddr, N_int bits) /* realloc */
{
N_word bytes;
N_word oldsize;
N_word oldmask;
N_word newsize;
N_word newmask;
wordptr newaddr;
wordptr source;
wordptr target;
oldsize = BV_SIZE_(oldaddr);
oldmask = BV_MASK_(oldaddr);
newsize = BitVector_Size(bits);
newmask = BitVector_Mask(bits);
if (oldsize > 0) *(oldaddr+oldsize-1) &= oldmask;
if (newsize <= oldsize)
{
newaddr = oldaddr;
BV_BITS_(newaddr) = bits;
BV_SIZE_(newaddr) = newsize;
BV_MASK_(newaddr) = newmask;
if (newsize > 0) *(newaddr+newsize-1) &= newmask;
}
else
{
bytes = (newsize + BV_HIDDENWORDS) << BV_Factor;
newaddr = (wordptr) malloc((size_t) bytes);
if (newaddr != NULL)
{
*newaddr++ = bits;
*newaddr++ = newsize;
*newaddr++ = newmask;
target = newaddr;
source = oldaddr;
newsize -= oldsize;
BIT_VECTOR_COPY_WORDS(target,source,oldsize)
BIT_VECTOR_ZERO_WORDS(target,newsize)
}
BitVector_Destroy(oldaddr);
}
return(newaddr);
}
wordptr BitVector_Shadow(wordptr addr) /* makes new, same size but empty */
{
return( BitVector_Create(BV_BITS_(addr),true) );
}
wordptr BitVector_Clone(wordptr addr) /* makes exact duplicate */
{
N_word bits;
wordptr twin;
bits = BV_BITS_(addr);
twin = BitVector_Create(bits,false);
if ((twin != NULL) and (bits > 0))
BIT_VECTOR_cpy_words(twin,addr,BV_SIZE_(addr));
return(twin);
}
wordptr BitVector_Concat(wordptr X, wordptr Y) /* returns concatenation */
{
/* BEWARE that X = most significant part, Y = least significant part! */
N_word bitsX;
N_word bitsY;
N_word bitsZ;
wordptr Z;
bitsX = BV_BITS_(X);
bitsY = BV_BITS_(Y);
bitsZ = bitsX + bitsY;
Z = BitVector_Create(bitsZ,false);
if ((Z != NULL) and (bitsZ > 0))
{
BIT_VECTOR_cpy_words(Z,Y,BV_SIZE_(Y));
BitVector_Interval_Copy(Z,X,bitsY,0,bitsX);
*(Z+BV_SIZE_(Z)-1) &= BV_MASK_(Z);
}
return(Z);
}
void BitVector_Copy(wordptr X, wordptr Y) /* X = Y */
{
N_word sizeX = BV_SIZE_(X);
N_word sizeY = BV_SIZE_(Y);
N_word maskX = BV_MASK_(X);
N_word maskY = BV_MASK_(Y);
N_word fill = 0;
wordptr lastX;
wordptr lastY;
if ((X != Y) and (sizeX > 0))
{
lastX = X + sizeX - 1;
if (sizeY > 0)
{
lastY = Y + sizeY - 1;
if ( (*lastY AND (maskY AND NOT (maskY >> 1))) == 0 ) *lastY &= maskY;
else
{
fill = (N_word) ~0L;
*lastY |= NOT maskY;
}
while ((sizeX > 0) and (sizeY > 0))
{
*X++ = *Y++;
sizeX--;
sizeY--;
}
*lastY &= maskY;
}
while (sizeX-- > 0) *X++ = fill;
*lastX &= maskX;
}
}
void BitVector_Empty(wordptr addr) /* X = {} clr all */
{
N_word size = BV_SIZE_(addr);
BIT_VECTOR_ZERO_WORDS(addr,size)
}
void BitVector_Fill(wordptr addr) /* X = ~{} set all */
{
N_word size = BV_SIZE_(addr);
N_word mask = BV_MASK_(addr);
N_word fill = (N_word) ~0L;
if (size > 0)
{
BIT_VECTOR_FILL_WORDS(addr,fill,size)
*(--addr) &= mask;
}
}
void BitVector_Flip(wordptr addr) /* X = ~X flip all */
{
N_word size = BV_SIZE_(addr);
N_word mask = BV_MASK_(addr);
N_word flip = (N_word) ~0L;
if (size > 0)
{
BIT_VECTOR_FLIP_WORDS(addr,flip,size)
*(--addr) &= mask;
}
}
void BitVector_Primes(wordptr addr)
{
N_word bits = BV_BITS_(addr);
N_word size = BV_SIZE_(addr);
wordptr work;
N_word temp;
N_word i,j;
if (size > 0)
{
temp = 0xAAAA;
i = BV_WordBits >> 4;
while (--i > 0)
{
temp <<= 16;
temp |= 0xAAAA;
}
i = size;
work = addr;
*work++ = temp XOR 0x0006;
while (--i > 0) *work++ = temp;
for ( i = 3; (j = i * i) < bits; i += 2 )
{
for ( ; j < bits; j += i ) BIT_VECTOR_CLR_BIT(addr,j)
}
*(addr+size-1) &= BV_MASK_(addr);
}
}
void BitVector_Reverse(wordptr X, wordptr Y)
{
N_word bits = BV_BITS_(X);
N_word mask;
N_word bit;
N_word value;
if (bits > 0)
{
if (X == Y) BitVector_Interval_Reverse(X,0,bits-1);
else if (bits == BV_BITS_(Y))
{
/* mask = BV_MASK_(Y); */
/* mask &= NOT (mask >> 1); */
mask = BV_BitMaskTab[(bits-1) AND BV_ModMask];
Y += BV_SIZE_(Y) - 1;
value = 0;
bit = BV_LSB;
while (bits-- > 0)
{
if ((*Y AND mask) != 0)
{
value |= bit;
}
if (not (mask >>= 1))
{
Y--;
mask = BV_MSB;
}
if (not (bit <<= 1))
{
*X++ = value;
value = 0;
bit = BV_LSB;
}
}
if (bit > BV_LSB) *X = value;
}
}
}
void BitVector_Interval_Empty(wordptr addr, N_int lower, N_int upper)
{ /* X = X \ [lower..upper] */
N_word bits = BV_BITS_(addr);
N_word size = BV_SIZE_(addr);
wordptr loaddr;
wordptr hiaddr;
N_word lobase;
N_word hibase;
N_word lomask;
N_word himask;
N_word diff;
if ((size > 0) and (lower < bits) and (upper < bits) and (lower <= upper))
{
lobase = lower >> BV_LogBits;
hibase = upper >> BV_LogBits;
diff = hibase - lobase;
loaddr = addr + lobase;
hiaddr = addr + hibase;
lomask = (N_word) (~0L << (lower AND BV_ModMask));
himask = (N_word) ~((~0L << (upper AND BV_ModMask)) << 1);
if (diff == 0)
{
*loaddr &= NOT (lomask AND himask);
}
else
{
*loaddr++ &= NOT lomask;
while (--diff > 0)
{
*loaddr++ = 0;
}
*hiaddr &= NOT himask;
}
}
}
void BitVector_Interval_Fill(wordptr addr, N_int lower, N_int upper)