Mercurial > hg > Members > masakoha > testcode
comparison c/regexParser/bitVector.cc @ 137:c292c67b3100 pairPro
add generateTransitionList function
author | masa |
---|---|
date | Fri, 04 Dec 2015 20:32:09 +0900 |
parents | 166136236891 |
children | ea2810db8d87 |
comparison
equal
deleted
inserted
replaced
136:15815fcb6c2f | 137:c292c67b3100 |
---|---|
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include <string.h> | 3 #include <string.h> |
4 #include "bitVector.h" | 4 #include "bitVector.h" |
5 extern BitVectorListPtr allocateBitVectorList(); | |
6 const BitVectorPtr allocateBitVector(); | 5 const BitVectorPtr allocateBitVector(); |
7 | 6 |
8 BitVectorListPtr createBitVector(NodePtr n,BitVectorListPtr bvl) { | 7 BitVectorListPtr createBitVector(NodePtr n) { |
9 BitVectorListPtr nextBvl = allocateBitVectorList(); | 8 BitVectorListPtr nextBvl = allocateBitVectorList(); |
10 nextBvl->bi = bitSet(n->nodeNumber); | 9 nextBvl->bi = bitSet(nextBvl,n->nodeNumber); |
11 return nextBvl; | 10 return nextBvl; |
12 } | 11 } |
13 | 12 |
14 const BitVectorPtr allocateBitVector() { | 13 const BitVectorPtr allocateBitVector() { |
15 | |
16 BitVectorPtr bi = (BitVectorPtr)malloc(sizeof(BitVector)); | 14 BitVectorPtr bi = (BitVectorPtr)malloc(sizeof(BitVector)); |
17 if (bi == NULL) { | 15 bi->bitContainer = 0; |
18 fprintf(stderr, "Failed to allocate memory.\n"); | |
19 exit(-1); | |
20 } | |
21 | |
22 bi->bitContainer = (unsigned long*)malloc(sizeof(unsigned long)*bi->arrayNum); | |
23 if (bi->bitContainer == NULL) { | |
24 fprintf(stderr, "Failed to allocate memory.\n"); | |
25 exit(-1); | |
26 } | |
27 | |
28 for (int i = 0; i < bi->arrayNum; i++) { | |
29 bi->bitContainer[i] = 0; | |
30 } | |
31 | |
32 return bi; | 16 return bi; |
33 } | 17 } |
34 | 18 |
35 BitVectorPtr bitSet(int bitSetPosition) { | 19 void bitSet(BitVectorPtr bi, int bitSetPosition) { |
36 | |
37 BitVectorPtr bi = allocateBitVector(); | |
38 | |
39 bi->arrayNum = (bitSetPosition + BITBLOCK) / BITBLOCK; | |
40 | |
41 int arrayPosition = bitSetPosition / BITBLOCK; | |
42 | |
43 unsigned long tmp = 1 << (bitSetPosition % BITBLOCK); | 20 unsigned long tmp = 1 << (bitSetPosition % BITBLOCK); |
44 bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp; | 21 bi->bitContainer = bi->bitContainer | tmp; |
45 | 22 return; |
46 return bi; | |
47 } | 23 } |
48 | 24 |
49 void bitPrint(BitVectorPtr bi) { | 25 void bitPrint(BitVectorPtr bi) { |
50 for (int i = 0; i < bi->arrayNum ; i++) { | 26 unsigned long vec = bi->bitContainer; |
51 unsigned long vec = bi->bitContainer[i]; | 27 for (int j = 0; j < BITBLOCK; j++) { |
52 for (int j = 0; j < BITBLOCK; j++) { | 28 putchar((vec & 1)?'1:'0'); |
53 printf( "%lu", vec & 1 ); | 29 vec >>= 1; |
54 vec >>= 1; | |
55 } | |
56 printf(" "); | |
57 } | 30 } |
58 puts(""); | 31 printf("\n"); |
59 } | 32 } |