Mercurial > hg > Applications > Grep
annotate c/regexParser/bitVector.cc @ 108:70069d4647a0 impl-bitvector
implement malloc error checking
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 19 Nov 2015 17:48:36 +0900 |
parents | d0d2262d4edf |
children | a3adc5c24e19 |
rev | line source |
---|---|
86
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include <stdio.h> |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <stdlib.h> |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 #include <string.h> |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include "bitVector.h" |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 |
94
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
6 void bitPrint(BitVectorPtr bi); |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
7 |
86
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 int bitBlock = sizeof(unsigned long) * 8; |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 |
94
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
10 BitVectorPtr bitSet(int bitSetPosition) { |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
11 |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
12 BitVectorPtr bi = (BitVectorPtr)malloc(sizeof(BitVector)); |
108
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
13 if (bi == NULL) { |
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
14 fprintf(stderr, "Failed to allocate memory.\n"); |
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
15 exit(-1); |
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
16 } |
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
17 |
94
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
18 bi->bitContainer = (unsigned long*)malloc(sizeof(unsigned long)*bi->arrayNum); |
108
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
19 if (bi->bitContainer == NULL) { |
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
20 fprintf(stderr, "Failed to allocate memory.\n"); |
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
21 exit(-1); |
70069d4647a0
implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
22 } |
86
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 |
94
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
24 bi->arrayNum = (bitSetPosition + 1) / bitBlock; |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
25 if (((bitSetPosition + 1) % bitBlock) != 0) bi->arrayNum++; |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
26 |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
27 for (int i = 0; i < bi->arrayNum; i++) { |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
28 bi->bitContainer[i] = 0; |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
29 } |
86
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 unsigned long tmp = 1; |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 int arrayPosition = 0; |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 arrayPosition = bitSetPosition / bitBlock; |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 bitSetPosition = bitSetPosition % bitBlock; |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 tmp = tmp << (bitBlock - 1 - bitSetPosition); |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp; |
94
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
38 |
43b807f88961
implement bitvector (suspend)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
86
diff
changeset
|
39 return bi; |
86
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 } |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 void bitPrint(BitVectorPtr bi) { |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 for (int i = 0; i < bi->arrayNum ; i++) { |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 for (int j = bitBlock - 1; j >= 0; j--) { |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 printf( "%lu", ( bi->bitContainer[i] >> j ) & 1 ); |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 } |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 printf(" "); |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 } |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 puts(""); |
1d7ca366b199
add bitVector
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 } |