view c/regexParser/bitVector.cc @ 115:ca30f8334741 pairPro

rename createRegexParser.cc to regexParser.cc
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Tue, 24 Nov 2015 14:38:26 +0900
parents a3adc5c24e19
children 66c633575b53
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitVector.h"
#include "regexParser.h"

extern BitVectorListPtr allocateBitVectorList();
BitVectorListPtr createBitVector(NodePtr,BitVectorListPtr);
const BitVectorPtr allocateBitVector();
BitVectorPtr bitSet(int);
void bitPrint(BitVectorPtr);

int bitBlock = sizeof(unsigned long) * 8;

BitVectorListPtr createBitVector(NodePtr n,BitVectorListPtr bvl) {
    BitVectorListPtr nextBvl = allocateBitVectorList();
    nextBvl->bi = bitSet(n->nodeNumber);
    return nextBvl;
}

const BitVectorPtr allocateBitVector() {

    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(int bitSetPosition) {

    BitVectorPtr bi = allocateBitVector();

    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("");
}