Mercurial > hg > Applications > Grep
view c/regexParser/bitVector.cc @ 110:a3adc5c24e19 pairPro
start branch
author | masa |
---|---|
date | Fri, 20 Nov 2015 21:02:00 +0900 |
parents | 70069d4647a0 |
children | ca30f8334741 |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "bitVector.h" void bitPrint(BitVectorPtr bi); int bitBlock = sizeof(unsigned long) * 8; BitVectorListPtr createBitVector(NodePtr n,BitVectorListPtr bvl) { BitVectorListPtr nextBvl = allocateBitVectorList(); nextBvl->bi = bitSet(n->nodeNumber); nextBvl->initBvl = initBvl; return nextBvl; } BitVectorPtr createBitVector(int bitSetPosition) { BitVectorPtr bi = (BitVectorPtr)malloc(sizeof(BitVector)); if (bi == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); exit(-1); } bi->bitContainer = (unsigned long*)malloc(sizeof(unsigned long)*bi->arrayNum); if (bi->bitContainer == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); exit(-1); } for (int i = 0; i < bi->arrayNum; i++) { bi->bitContainer[i] = 0; } return bi; } BitVectorPtr bitSet(BitVectorPtr bi, int bitSetPosition) { bi->arrayNum = (bitSetPosition + bitBlock - 1) / bitBlock; int arrayPosition = bitSetPosition / bitBlock; unsigned long tmp = 1 << (bitSetPosition % bitBlock); bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp; return bi; } void bitPrint(BitVectorPtr bi) { for (int i = 0; i < bi->arrayNum ; i++) { unsigned long vec = bi->bitContainer[i]; for (int j = 0; j < bitBlock; j++) { printf( "%lu", vec & 1 ); vec >>= 1; } printf(" "); } puts(""); }