void BitVector::operator&=(const BitVector& other) { int length = MIN(WordLength(), other.WordLength()); for (int w = 0; w < length; ++w) array_[w] &= other.array_[w]; for (int w = WordLength() - 1; w >= length; --w) array_[w] = 0; }
// Allocates memory for a vector of the given length. // Reallocates if the array is a different size, larger or smaller. void BitVector::Alloc(int length) { int initial_wordlength = WordLength(); bit_size_ = length; int new_wordlength = WordLength(); if (new_wordlength != initial_wordlength) { delete [] array_; array_ = new uinT32[new_wordlength]; } }
// Writes to the given file. Returns false in case of error. bool BitVector::Serialize(FILE* fp) const { if (fwrite(&bit_size_, sizeof(bit_size_), 1, fp) != 1) return false; int wordlen = WordLength(); if (static_cast<int>(fwrite(array_, sizeof(*array_), wordlen, fp)) != wordlen) return false; return true; }
// Set subtraction *this = v1 - v2. void BitVector::SetSubtract(const BitVector& v1, const BitVector& v2) { Alloc(v1.size()); int length = MIN(v1.WordLength(), v2.WordLength()); for (int w = 0; w < length; ++w) array_[w] = v1.array_[w] ^ (v1.array_[w] & v2.array_[w]); for (int w = WordLength() - 1; w >= length; --w) array_[w] = v1.array_[w]; }
// Returns the number of set bits in the vector. int BitVector::NumSetBits() const { int wordlen = WordLength(); int total_bits = 0; for (int w = 0; w < wordlen; ++w) { uinT32 word = array_[w]; for (int i = 0; i < 4; ++i) { total_bits += hamming_table_[word & 0xff]; word >>= 8; } } return total_bits; }
// Reads from the given file. Returns false in case of error. // If swap is true, assumes a big/little-endian swap is needed. bool BitVector::DeSerialize(bool swap, FILE* fp) { uinT32 new_bit_size; if (fread(&new_bit_size, sizeof(new_bit_size), 1, fp) != 1) return false; if (swap) { ReverseN(&new_bit_size, sizeof(new_bit_size)); } Alloc(new_bit_size); int wordlen = WordLength(); if (fread(array_, sizeof(*array_), wordlen, fp) != wordlen) return false; if (swap) { for (int i = 0; i < wordlen; ++i) ReverseN(&array_[i], sizeof(array_[i])); } return true; }
word WordGen( gen g ) { word w; int l; if ( g == 0 ) { w = (word)Allocate( sizeof( gpower ) ); w[0].g = EOW; } else { if ( (*PcGenerator)(g) == (word)0 ) return (word)0; l = WordLength( (*PcGenerator)(g) ); w = (word)Allocate( (l+1)*sizeof( gpower ) ); WordCopy( (*PcGenerator)(g), w ); } return w; }
// Returns the index of the next set bit after the given index. // Useful for quickly iterating through the set bits in a sparse vector. int BitVector::NextSetBit(int prev_bit) const { // Move on to the next bit. int next_bit = prev_bit + 1; if (next_bit >= bit_size_) return -1; // Check the remains of the word containing the next_bit first. int next_word = WordIndex(next_bit); int bit_index = next_word * kBitFactor; int word_end = bit_index + kBitFactor; uinT32 word = array_[next_word]; uinT8 byte = word & 0xff; while (bit_index < word_end) { if (bit_index + 8 > next_bit && byte != 0) { while (bit_index + lsb_index_[byte] < next_bit && byte != 0) byte = lsb_eroded_[byte]; if (byte != 0) return bit_index + lsb_index_[byte]; } word >>= 8; bit_index += 8; byte = word & 0xff; } // next_word didn't contain a 1, so find the next word with set bit. ++next_word; int wordlen = WordLength(); while (next_word < wordlen && (word = array_[next_word]) == 0) { ++next_word; bit_index += kBitFactor; } if (bit_index >= bit_size_) return -1; // Find the first non-zero byte within the word. while ((word & 0xff) == 0) { word >>= 8; bit_index += 8; } return bit_index + lsb_index_[word & 0xff]; }
void BitVector::operator^=(const BitVector& other) { int length = MIN(WordLength(), other.WordLength()); for (int w = 0; w < length; ++w) array_[w] ^= other.array_[w]; }
BitVector::BitVector(const BitVector& src) : bit_size_(src.bit_size_) { array_ = new uinT32[WordLength()]; memcpy(array_, src.array_, ByteLength()); }
BitVector::BitVector(int length) : bit_size_(length) { array_ = new uinT32[WordLength()]; SetAllFalse(); }
void sizePcPres(void) { int size = 0, nrPt = 0; gen g, h; nrPt += 1; size += sizeof(gpower); /* Identty */ /* First calculate the size of all those arrays. */ nrPt += 1; size += Class * sizeof(int); /* Dimension[]. */ nrPt += 1 + 2 * NrPcGens; size += (2 * NrPcGens + 1) * sizeof(word); /* Generators[]. */ size += 2 * NrPcGens * 2 * sizeof(gpower); /* Generators. */ nrPt += 1; size += NrPcGens * sizeof(int); /* Weight[]. */ nrPt += 1; size += NrPcGens * sizeof(gen); /* Commute[]. */ nrPt += 1; size += NrPcGens * sizeof(expo); /* Exponent[]. */ nrPt += 1; size += NrPcGens * sizeof(Definition); /* Definition[]. */ nrPt += 1; size += NrPcGens * sizeof(word); /* Power[]. */ for (g = 1; g <= NrPcGens; g++) if (Exponent[g] != (expo)0) { nrPt += 1; size += sizeof(gpower) * (WordLength(Power[g]) + 1); } nrPt += 1; size += (2 * NrPcGens + 1) * sizeof(word*); /* Conjugate[]. */ for (h = 1; h <= NrPcGens; h++) { nrPt += 1; size += sizeof(word); if (Exponent[h] == (expo)0) { nrPt += 1; size += sizeof(word); } for (g = 1; g < h; g++) if (Wt(h) + Wt(g) <= Class + (NrCenGens == 0 ? 0 : 1)) { nrPt += 1; size += sizeof(word); size += sizeof(gpower) * (WordLength(Conjugate[h][g]) + 1); if (Exponent[h] == (expo)0) { nrPt += 1; size += sizeof(word); size += sizeof(gpower) * (WordLength(Conjugate[-h][ g]) + 1); } if (Exponent[g] == (expo)0) { nrPt += 1; size += sizeof(word); size += sizeof(gpower) * (WordLength(Conjugate[ h][-g]) + 1); } if (Exponent[h] + Exponent[g] == (expo)0) { nrPt += 1; size += sizeof(word); size += sizeof(gpower) * (WordLength(Conjugate[-h][-g]) + 1); } } } printf("size of the presentation : %d bytes\n", size); printf("pointers in the presentation : %d\n", nrPt); }