view c/regexParser/bitVector.cc @ 116:66c633575b53 pairPro

remove error and warning
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Tue, 24 Nov 2015 17:07:08 +0900
parents ca30f8334741
children 166136236891
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitVector.h"
extern BitVectorListPtr allocateBitVectorList();
const BitVectorPtr allocateBitVector();

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