From: Kim Nguyễn Date: Fri, 6 Apr 2012 12:01:26 +0000 (+0200) Subject: Change get_bit/set_bit to access the vector in big endian (to match X-Git-Url: http://git.nguyen.vg/gitweb/?p=SXSI%2FXMLTree.git;a=commitdiff_plain;h=cd438c6db9deacac203891fd76a49a768ba53e70 Change get_bit/set_bit to access the vector in big endian (to match libbp). --- diff --git a/bit-vector.cpp b/bit-vector.cpp index d69c177..5450989 100644 --- a/bit-vector.cpp +++ b/bit-vector.cpp @@ -1,5 +1,6 @@ #include #include "bit-vector.hpp" +#include static inline uint8_t pow2_mult8(uint8_t x){ return (~((x & (x-1))==0) + 1) & (x >> 3); @@ -11,10 +12,9 @@ static inline size_t uint32bits(size_t bits){ } - -void array_copy(const uint32_t * src, uint32_t * dst, size_t len) +static void array_copy(const uint32_t * src, uint32_t * dst, size_t len) { - while (len--) *dst++ = *src++; + for(size_t i = 0; i < len; i++) dst[i] = src[i]; } size_t bit_vector::allocforsize(size_t bits) @@ -135,23 +135,24 @@ void bit_vector::pack() size_t nalloc = uint32bits(_size); uint32_t * nvector = new uint32_t[nalloc]; - array_copy(_vector,nvector,nalloc); - if (!_leaked) delete [] _vector; + array_copy(_vector, nvector, nalloc); + if (!_leaked && _vector) delete [] _vector; _alloc = nalloc; - _vector = nvector; - _leaked = false; + this->_vector = nvector; + this->_leaked = false; } //sets the idxth bit to b void bit_vector::set(size_t idx, bool b) { - size_t i = idx / sizeof(uint32_t); - size_t j = idx % sizeof(uint32_t); + + size_t i = idx / W; + size_t j = idx % W; grow(allocforsize(idx+1)); if (idx >= _size) _size = idx+1; - uint32_t mask = 1 << j; + uint32_t mask = 1 << (W - 1 - j); if (b) _vector[i] |= mask; else diff --git a/bit-vector.hpp b/bit-vector.hpp index cd6382e..697581d 100644 --- a/bit-vector.hpp +++ b/bit-vector.hpp @@ -117,7 +117,9 @@ private: */ inline bool bit_vector::unsafe_get(size_t idx) const { - return (_vector[idx / W] >> (idx % W)) & true; + size_t i = idx / W; + size_t j = idx % W; + return (_vector[i] >> (W - 1 - j)) & true; }; /** Unsafe version of get_field. Does not perform bound checking. @@ -125,6 +127,7 @@ inline bool bit_vector::unsafe_get(size_t idx) const */ inline uint32_t bit_vector::unsafe_get_field(size_t idx, size_t len) const { + //TODO FIX TO REFLECT BIG ENDIAN ORDER. switch (len){ case 8: return ((uint8_t*)_vector)[idx];