comparison c/regexParser/bitVector.cc @ 110:a3adc5c24e19 pairPro

start branch
author masa
date Fri, 20 Nov 2015 21:02:00 +0900
parents 70069d4647a0
children ca30f8334741
comparison
equal deleted inserted replaced
109:6401c708f5dd 110:a3adc5c24e19
5 5
6 void bitPrint(BitVectorPtr bi); 6 void bitPrint(BitVectorPtr bi);
7 7
8 int bitBlock = sizeof(unsigned long) * 8; 8 int bitBlock = sizeof(unsigned long) * 8;
9 9
10 BitVectorPtr bitSet(int bitSetPosition) { 10 BitVectorListPtr createBitVector(NodePtr n,BitVectorListPtr bvl) {
11 BitVectorListPtr nextBvl = allocateBitVectorList();
12 nextBvl->bi = bitSet(n->nodeNumber);
13 nextBvl->initBvl = initBvl;
14 return nextBvl;
15 }
16
17 BitVectorPtr createBitVector(int bitSetPosition) {
11 18
12 BitVectorPtr bi = (BitVectorPtr)malloc(sizeof(BitVector)); 19 BitVectorPtr bi = (BitVectorPtr)malloc(sizeof(BitVector));
13 if (bi == NULL) { 20 if (bi == NULL) {
14 fprintf(stderr, "Failed to allocate memory.\n"); 21 fprintf(stderr, "Failed to allocate memory.\n");
15 exit(-1); 22 exit(-1);
19 if (bi->bitContainer == NULL) { 26 if (bi->bitContainer == NULL) {
20 fprintf(stderr, "Failed to allocate memory.\n"); 27 fprintf(stderr, "Failed to allocate memory.\n");
21 exit(-1); 28 exit(-1);
22 } 29 }
23 30
24 bi->arrayNum = (bitSetPosition + 1) / bitBlock;
25 if (((bitSetPosition + 1) % bitBlock) != 0) bi->arrayNum++;
26
27 for (int i = 0; i < bi->arrayNum; i++) { 31 for (int i = 0; i < bi->arrayNum; i++) {
28 bi->bitContainer[i] = 0; 32 bi->bitContainer[i] = 0;
29 } 33 }
30 unsigned long tmp = 1;
31 int arrayPosition = 0;
32 34
33 arrayPosition = bitSetPosition / bitBlock; 35 return bi;
34 bitSetPosition = bitSetPosition % bitBlock; 36 }
35 37
36 tmp = tmp << (bitBlock - 1 - bitSetPosition); 38 BitVectorPtr bitSet(BitVectorPtr bi, int bitSetPosition) {
39
40 bi->arrayNum = (bitSetPosition + bitBlock - 1) / bitBlock;
41
42 int arrayPosition = bitSetPosition / bitBlock;
43
44 unsigned long tmp = 1 << (bitSetPosition % bitBlock);
37 bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp; 45 bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp;
38 46
39 return bi; 47 return bi;
40 } 48 }
41 49
42 void bitPrint(BitVectorPtr bi) { 50 void bitPrint(BitVectorPtr bi) {
43 for (int i = 0; i < bi->arrayNum ; i++) { 51 for (int i = 0; i < bi->arrayNum ; i++) {
44 for (int j = bitBlock - 1; j >= 0; j--) { 52 unsigned long vec = bi->bitContainer[i];
45 printf( "%lu", ( bi->bitContainer[i] >> j ) & 1 ); 53 for (int j = 0; j < bitBlock; j++) {
54 printf( "%lu", vec & 1 );
55 vec >>= 1;
46 } 56 }
47 printf(" "); 57 printf(" ");
48 } 58 }
49 puts(""); 59 puts("");
50 } 60 }