void ScrDB::Expect(TARGETING::TargetHandle_t i_ptargetHandle, BitString & bs, uint64_t registerId) { PRDF_TRAC( "ScrDB::Expect() huid: %X, addr: %016X, data: %08X %08X", getHuid(i_ptargetHandle), registerId, bs.getFieldJustify(0,32), bs.getFieldJustify(32,32) ); SimScrDataSet eValues; DataList data; // parse all data given data.push_back(bs.getFieldJustify(0,32)); data.push_back(bs.getFieldJustify(32,32)); eValues.AddData(data); // PRDF_TRAC("get a copy"); PreScrMap scrMap = eChipset[i_ptargetHandle]; // create/get copy of map // PRDF_TRAC("update register value"); scrMap[registerId] = eValues; // Add entree // PRDF_TRAC("update the master"); eChipset[i_ptargetHandle] = scrMap; // copy it back //PRDF_EXIT( "ScrDB::Expect()" ); }
DecodingTree* Huffman::obtainSubtree(uint symbol, uint k) { BinaryNode *node = tree; // Traversing the Huffman tree for (uint i=1; i<=k; i++) { bool bit = ((symbol >> (k-i)) & 1); if (bit == 0) node = node->leftChild; else node = node->rightChild; } // Retrieving the subtree vector<uint> xTree; vector<uint> symbols; uint bits = 0; retrieveSubtree(node, &xTree, &bits, &symbols); BitString *tree = new BitString(2*xTree.size()); for (uint i=0; i<xTree.size(); i++) tree->setBit(xTree[i]); return new DecodingTree(symbol, tree, &symbols); }
BitString<Char> encode_data_and_push_into_bit_string()const { BitString<Char> encoded; for (auto ch : data) encoded.push_back_bits(code_dictionary.at(ch)); return encoded; }
pair<BitString *,uint> generateBitmap(size_t length,vector<pair<uint,uint> > &nodes ) { uint bitmap_pos = 0; BitString *bitmap = new BitString(length); size_t a,b; cst->Root(&a,&b); bitmap_pos++; // cout << "(" ; generateBitmapAux(make_pair(a,b),bitmap,bitmap_pos,nodes); bitmap->setBit(bitmap_pos); bitmap_pos++; // cout << ")"; // cout << "nodes.size() = " << nodes.size() << endl; BitString *new_bitmap = new BitString(bitmap->getData(),bitmap_pos); // for (int i = 0 ; i < nodes.size();i++) { // cout << nodes[i].first << endl; // } // cout << "=========== tree ==========" << endl; // for (int i = 0 ;i < bitmap_pos;i++) { // if (bitmap->getBit(i)) // cout << ")"; // else // cout << "("; // } // cout << endl; return make_pair(new_bitmap,bitmap_pos); }
BitSequenceRG::BitSequenceRG(const BitString & bs, uint _factor) { /*cout << "*****" << endl; cout << bitarray << endl; cout << _n << endl; cout << _factor << endl; */ const uint * bitarray = bs.getData(); size_t _n = bs.getLength(); if(_factor==0) exit(-1); data=new uint[_n/W+1]; for(size_t i=0;i<uint_len(_n,1);i++) data[i] = bitarray[i]; for(size_t i=uint_len(_n,1);i<_n/W+1;i++) data[i] = 0; //this->owner = true; this->n=_n; uint lgn=bits(n-1); this->factor=_factor; if (_factor==0) this->factor=lgn; else this->factor=_factor; b=32; s=b*this->factor; integers = n/W+1; BuildRank(); this->length = n; this->ones = rank1(n-1); }
void BitString::setPattern( uint32_t i_sPos, uint32_t i_sLen, CPU_WORD i_pattern, uint32_t i_pLen ) { PRDF_ASSERT(nullptr != getBufAddr()); // must to have a valid address PRDF_ASSERT(0 < i_sLen); // must have at least one bit PRDF_ASSERT(i_sPos + i_sLen <= getBitLen()); // field must be within range PRDF_ASSERT(0 < i_pLen); // must have at least one bit PRDF_ASSERT(i_pLen <= CPU_WORD_BIT_LEN); // i_pLen length must be valid // Get a bit string for the pattern subset (right justified). BitString bso ( i_pLen, &i_pattern, CPU_WORD_BIT_LEN - i_pLen ); // Iterate the range in chunks the size of i_pLen. uint32_t endPos = i_sPos + i_sLen; for ( uint32_t pos = i_sPos; pos < endPos; pos += i_pLen ) { // The true chunk size is either i_pLen or the leftovers at the end. uint32_t len = std::min( i_pLen, endPos - pos ); // Get this chunk's pattern value, truncate (left justified) if needed. CPU_WORD pattern = bso.getField( 0, len ); // Set the pattern in this string. setField( pos, len, pattern ); } }
void FeatureNumList::ToBitString (BitString& bitStr) const { bitStr.ReSet (); kkint32 x; for (x = 0; x < NumOfFeatures (); x++) bitStr.Set (featureNums[x]); } /* ToBitString */
bool QuadTree_S::Exists(const BitString &code, int x, int y) { DeltaCode delta; uint64_t cur = 0; long long nodes = delta.DecodeNextInt(code, &cur); long long size = code.get_length(); long long nextIndx = 0; long long begRow = 0, begCol = 0, endRow = nodes, endCol = nodes; long long depth = 0, nodesAtDepth = 1, nodesAtNextDepth = 0; for (long long i = 0; i < size;) { for(long long j = i; j < nodesAtDepth + i; j++) { int curQuad = code.GetBit((j*2+cur)) << 1 | code.GetBit((j*2)+cur+1); if (j == nextIndx) { if (curQuad == 3) { double midRow = ((double)begRow + endRow)/2; double midCol = ((double)begCol + endCol)/2; if (x < midRow) { //We are in NE or NW if (y < midCol) { //We are in NW endRow = midRow; endCol = midCol; nextIndx = i + nodesAtDepth + nodesAtNextDepth; } else { //We are in NE endRow = midRow; begCol = midCol; nextIndx = i + nodesAtDepth + nodesAtNextDepth + 1; } } else if (y < midCol) { //We are in SW begRow = midRow; endCol = midCol; nextIndx = i + nodesAtDepth + nodesAtNextDepth + 2; } else { //We are in SE begRow = midRow; begCol = midCol; nextIndx = i + nodesAtDepth + nodesAtNextDepth + 3; } nodesAtNextDepth += 4; int nextQuad = code.GetBit((nextIndx*2)+cur) << 1 | code.GetBit((nextIndx*2)+cur+1); if (nextQuad == 1) return true; if (nextQuad == 0) return false; else if (nextQuad == 2) if (begCol-begRow + x == y) return false; else return true; } else if (curQuad == 1) return true; else if (curQuad == 0) return false; else if (curQuad == 2) { if (begCol-begRow + x == y) return false; else return true; } } else if (curQuad == 3) { nodesAtNextDepth += 4; } } i += nodesAtDepth; nodesAtDepth = nodesAtNextDepth; nodesAtNextDepth = 0; } }
void BitString::setString( const BitString & i_sStr, uint32_t i_sPos, uint32_t i_sLen, uint32_t i_dPos ) { // Ensure the source parameters are valid. PRDF_ASSERT( nullptr != i_sStr.getBufAddr() ); PRDF_ASSERT( 0 < i_sLen ); // at least one bit to copy PRDF_ASSERT( i_sPos + i_sLen <= i_sStr.getBitLen() ); // Ensure the destination has at least one bit available to copy. PRDF_ASSERT( nullptr != getBufAddr() ); PRDF_ASSERT( i_dPos < getBitLen() ); // If the source length is greater than the destination length than the // extra source bits are ignored. uint32_t actLen = std::min( i_sLen, getBitLen() - i_dPos ); // The bit strings may be in overlapping memory spaces. So we need to copy // the data in the correct direction to prevent overlapping. uint32_t sRelOffset = 0, dRelOffset = 0; CPU_WORD * sRelAddr = i_sStr.getRelativePosition( sRelOffset, i_sPos ); CPU_WORD * dRelAddr = getRelativePosition( dRelOffset, i_dPos ); // Copy the data. if ( (dRelAddr == sRelAddr) && (dRelOffset == sRelOffset) ) { // Do nothing. The source and destination are the same. } else if ( (dRelAddr < sRelAddr) || ((dRelAddr == sRelAddr) && (dRelOffset < sRelOffset)) ) { // Copy the data forward. for ( uint32_t pos = 0; pos < actLen; pos += CPU_WORD_BIT_LEN ) { uint32_t len = std::min( actLen - pos, CPU_WORD_BIT_LEN ); CPU_WORD value = i_sStr.getField( i_sPos + pos, len ); setField( i_dPos + pos, len, value ); } } else // Copy the data backwards. { // Get the first position of the last chunk (CPU_WORD aligned). uint32_t lastPos = ((actLen-1) / CPU_WORD_BIT_LEN) * CPU_WORD_BIT_LEN; // Start with the last chunk and work backwards. for ( int32_t pos = lastPos; 0 <= pos; pos -= CPU_WORD_BIT_LEN ) { uint32_t len = std::min( actLen - pos, CPU_WORD_BIT_LEN ); CPU_WORD value = i_sStr.getField( i_sPos + pos, len ); setField( i_dPos + pos, len, value ); } } }
std::vector<int> QuadTree_S::GetNeighbors(const BitString &code, int x) { DeltaCode delta; uint64_t cur = 0; int nodes = delta.DecodeNextInt(code, &cur); long long size = code.get_length(); std::vector<int> neighbors; QueueObj pos; std::queue<QueueObj> posQueue; posQueue.push(QueueObj(0, 0, nodes, 0, nodes)); pos = posQueue.front(); long long depth = 0, nodesAtDepth = 1, nodesAtNextDepth = 0; for(int i = 0; !posQueue.empty();) { for(long long j = i; j < nodesAtDepth + i; j++) { int curQuad = code.GetBit((j*2+cur)) << 1 | code.GetBit((j*2)+cur+1); if (j == pos.nextPos) { posQueue.pop(); if (curQuad == 3) { double midRow = ((double)pos.begRow + pos.endRow)/2; double midCol = ((double)pos.begCol + pos.endCol)/2; if (x < midRow) { posQueue.push(QueueObj(i + nodesAtDepth + nodesAtNextDepth, pos.begRow, midRow, pos.begCol, midCol)); posQueue.push(QueueObj(i + nodesAtDepth + nodesAtNextDepth + 1, pos.begRow, midRow, midCol, pos.endCol)); } else { posQueue.push(QueueObj(i + nodesAtDepth + nodesAtNextDepth + 2, midRow, pos.endRow, pos.begCol, midCol)); posQueue.push(QueueObj(i + nodesAtDepth + nodesAtNextDepth + 3, midRow, pos.endRow, midCol, pos.endCol)); } nodesAtNextDepth += 4; } else if (curQuad == 1) { for (int n = pos.begCol; n < pos.endCol; ++n) { neighbors.push_back(n); } } else if (curQuad == 2) { for (int n = pos.begCol; n < pos.endCol; ++n) { if (pos.begCol - pos.begRow + x == n) continue; neighbors.push_back(n); } } if (posQueue.empty()) break; pos = posQueue.front(); } else if (curQuad == 3){ nodesAtNextDepth += 4; } } i += nodesAtDepth; nodesAtDepth = nodesAtNextDepth; nodesAtNextDepth = 0; } return neighbors; }
void ScrDB::Read(TARGETING::TargetHandle_t i_ptargetHandle, BitString & bs, uint64_t registerId) { //PRDF_DENTER( "ScrDB::Read() huid: 0x%X, addr: 0x%016X", // getHuid(i_ptargetHandle), registerId ); DataList data; unsigned int dataWordSize = bs.getBitLen()/32; dataWordSize += (bs.getBitLen() % 32) ? 1 : 0; // if the register has a predetermined value than get it if(pChipset.find(i_ptargetHandle) != pChipset.end()) { PreScrMap pscrmap = pChipset[i_ptargetHandle]; if(pscrmap.find(registerId) != pscrmap.end()) // we must have a predetermined value { SimScrDataSet pValues = pscrmap[registerId]; data = pValues.GetData(); // get next set of values // pValues has changed - copy it back pscrmap[registerId] = pValues; pChipset[i_ptargetHandle] = pscrmap; } } if(data.size() == 0) // use the last value written to this reg { // get a copy of the scrMap for this chip - if one does not exist it will be created ScrMap scrMap = chipset[i_ptargetHandle]; // get a copy of the data for this address from the scrMap for this chip // if data structure does not exist, it will be created, but will be empty data = scrMap[registerId]; if(data.size() == 0) // This is the first time this register has been accessed { while(data.size() < dataWordSize) data.push_back(0); // zero fill scrMap[registerId] = data; chipset[i_ptargetHandle] = scrMap; // update the persistent copy of the scrMap } } if(0 != data.size()) { for(unsigned int i = 0; i < data.size(); ++i) { bs.setFieldJustify((i*32), 32, data[i]); } PRDF_TRAC( "ScrDB::Read() huid: %X, addr: %016X, data: %08X %08X", getHuid(i_ptargetHandle), registerId, data[0], 2 == data.size() ? data[1] : 0 ); } //PRDF_DEXIT( "ScrDB::Read()" ); }
void test_BitString() { try { BitString x(30); BitString y(30); x.Set(3); x.Set(7); x.Set(14); y.Set(3); y.Set(5); y.Set(12); // Test out of bounds bit number handling // y.Set(32); std::cout << "X = " << x << std::endl; std::cout << "Y = " << y << std::endl; // Test bit-wise AND // BitString z = x & y; // std::cout << "X & Y = " << z << std::endl; // Test bit-wise OR // BitString z = x | y; // std::cout << "X | Y = " << z << std::endl; // Test bit-wise XOR BitString z = x^y; std::cout << "X ^ Y = " << z << std::endl; // Test bit-wise Count size_t c = z.Count(); std::cout << "Count X | Y = " << (int) c << std::endl; } // Catch any simple string error messages catch( char* s ) { std::cout << s << std::endl; } }
BitSequenceSDArray::BitSequenceSDArray(const BitString & bs) { uint * tmp_seq = new uint[uint_len(bs.getLength(),1)+1]; ones = 0; for(uint i=0;i<uint_len(bs.getLength(),1)+1;i++) tmp_seq[i] = 0; for(uint i=0;i<bs.getLength();i++) if(bs[i]) { __setbit(tmp_seq,i,1); ones++; } if(ones) selects3_construct(&sd,bs.getLength(),tmp_seq); this->length = bs.getLength(); delete [] tmp_seq; }
bool BitString::isEqual( const BitString & i_str ) const { if ( getBitLen() != i_str.getBitLen() ) return false; // size not equal for ( uint32_t pos = 0; pos < getBitLen(); pos += CPU_WORD_BIT_LEN ) { uint32_t len = std::min( getBitLen() - pos, CPU_WORD_BIT_LEN ); if ( getField(pos, len) != i_str.getField(pos, len) ) return false; // bit strings do not match } return true; // bit strings match }
void BitString::maskString( const BitString & i_mask ) { // Get the length of the smallest string. uint32_t actLen = std::min( getBitLen(), i_mask.getBitLen() ); for ( uint32_t pos = 0; pos < actLen; pos += CPU_WORD_BIT_LEN ) { uint32_t len = std::min( actLen - pos, CPU_WORD_BIT_LEN ); CPU_WORD dVal = getField( pos, len ); CPU_WORD sVal = i_mask.getField( pos, len ); setField( pos, len, dVal & ~sVal ); } }
bool testBitSequence(BitString & a, BitSequence * bs) { size_t rank0SoFar = 0; size_t rank1SoFar = 0; for(size_t i=0; i<a.getLength(); i++) { if(a[i]) { rank1SoFar++; if(bs->select1(rank1SoFar)!=i) { cerr << "SELECT1 ERROR " << i << endl; return false; } if(i>0 && bs->selectNext1(i)!=i) { cout << "i=" << i << "sn=" << bs->selectNext1(i) << endl; cerr << "SELECTNEXT1 ERROR" << endl; return false; } } else { rank0SoFar++; if(bs->select0(rank0SoFar)!=i) { cerr << "SELECT0 ERROR" << endl; return false; } if(i>0 && bs->selectNext0(i)!=i) { cerr << "SELECTNEXT0 ERROR" << endl; return false; } } if(bs->rank1(i)!=rank1SoFar) return false; if(bs->rank0(i)!=rank0SoFar) return false; if(bs->access(i)!=a[i]) return false; } return true; }
BitSequenceRGK::BitSequenceRGK(const BitString &bs, uint _factor, uint selectsampling) { const uint *bitarray = bs.getData(); size_t _n = bs.getLength(); // cout << _factor << endl; if (_factor == 0) exit(-1); // cout << "_n/W+1=" << _n/W+1 << endl; data = new uint[_n / W + 1]; // cout << "_n/W+1=" << _n/W+1 << endl; for (size_t i = 0; i < uint_len(_n, 1); i++) data[i] = bitarray[i]; for (size_t i = uint_len(_n, 1); i < _n / W + 1; i++) data[i] = 0; //this->owner = true; this->n = _n; uint lgn = bits(n - 1); this->factor = _factor; if (_factor == 0) this->factor = lgn; else this->factor = _factor; b = 32; s = b * this->factor; integers = n / W + 1; BuildRank(); this->length = n; this->ones = rank1(n - 1); fastselect0 = true; fastselect1 = true; select1sampling = selectsampling; select0sampling = selectsampling; if (fastselect1) { // cout << "entered here" << endl; this->Stones = new uint[ones / select1sampling + 1]; Stones[0] = 0; for (size_t i = 0; i <= ones; i += select1sampling) { if (i == 0) continue; Stones[i / select1sampling] = this->select1_s(i); } } if (fastselect0) { this->Stzero = new uint[(n - ones) / select0sampling + 1]; Stzero[0] = 0; for (size_t i = 0; i <= (n - ones); i += select0sampling) { if (i == 0) continue; Stzero[i / select0sampling] = this->select0_s((i)); } } }
inline int QuadTree_S::GetNextHuffQuad(const BitString &code, const long long freq[4], long long *cur) { int order = 0; while (code.GetBit(*cur) == 0) { ++order; ++*cur; } ++*cur; return freq[order]; }
BitStringBuffer BitString::operator|( const BitString & i_bs ) const { // Get the length of the smallest string. uint32_t actLen = std::min( getBitLen(), i_bs.getBitLen() ); BitStringBuffer bsb( actLen ); for ( uint32_t pos = 0; pos < actLen; pos += CPU_WORD_BIT_LEN ) { uint32_t len = std::min( actLen - pos, CPU_WORD_BIT_LEN ); CPU_WORD dVal = getField( pos, len ); CPU_WORD sVal = i_bs.getField( pos, len ); bsb.setField( pos, len, dVal | sVal ); } return bsb; }
inline void QuadTree_S::GetHuffStrings(BitString * result, long long freq[4], BitString huff[4]) { int order[4], done[4] = {0, 0, 0, 0}; long long max , currentIndex = 0; for (int i = 0; i < 4; ++i) { BitString currentString; for (int j = 0; j < i; ++j) currentString.AppendBit(0); currentString.AppendBit(1); max = 0; for (int j = 0; j < 4; ++j) { if (max <= freq[j] && !done[j]) { currentIndex = j; max = freq[j]; } } huff[currentIndex] = currentString; done[currentIndex] = 1; AppendQuad(result, currentIndex); } }
bool test_long_write_read(unsigned long long value, int bits, BitString& bitString) { bool result = true; bitString.write_bits_from_long(bits, value); bitString.rewind(bits); unsigned long read_value = bitString.read_bits_as_long(bits); unsigned long xored = read_value ^ value; if(value != read_value) { cout << "\nFailed write_bits_from_long with value " << value << " and read_value " << read_value << " bits " << bits << " diff " << xored << endl; bitString.write_bits_from_long(64, read_value); result = false; } return result; }
void CaptureData::AddDataElement( TargetHandle_t i_trgt, int i_scomId, const BitString * i_bs, Place i_place, RegType i_type ) { // Initial values of the bit string buffer if i_bs has a zero value. uint8_t * buf = nullptr; size_t sz_buf = 0; // Add buffer only if the value is non-zero. if ( !i_bs->isZero() ) { // Get the size of i_bs and ensure byte alignment. sz_buf = (i_bs->getBitLen() + 8-1) / 8; // Since we are using a BitString below, which does everything on a // CPU_WORD boundary, we must make sure the buffer is CPU_WORD aligned. const size_t sz_word = sizeof(CPU_WORD); sz_buf = ((sz_buf + sz_word-1) / sz_word) * sz_word; // Allocate memory for the buffer. buf = new uint8_t[sz_buf]; memset( buf, 0x00, sz_buf ); // Use a BitString to copy i_bs to the buffer. BitString bs ( i_bs->getBitLen(), (CPU_WORD *)buf ); bs.setString( *i_bs ); // Create the new data element. Data element( i_trgt, i_scomId, sz_buf, buf ); element.registerType = i_type; // Add the new element to the data. if ( FRONT == i_place ) data.insert( data.begin(), element ); else data.push_back( element ); } }
FeatureNumList::FeatureNumList (const BitString& bitString): featureNums (NULL), featureNumsAllocatedSize (0), maxFeatureNum (0), numOfFeatures (0) { auto bitStringLen = bitString.BitLen (); if (bitStringLen > maxIntType) { KKStr errMsg (256); errMsg << "FeatureNumList (const BitString& bitString) bitString.BitLen()[" << bitStringLen << "] > maxIntType[" << maxIntType << "]."; cerr << errMsg << endl; throw KKException (errMsg); } maxFeatureNum = (IntType)(bitStringLen - 1); VectorIntType listOfSelectedFeatures; bitString.ListOfSetBits16 (listOfSelectedFeatures); AllocateArraySize ((IntType)listOfSelectedFeatures.size ()); for (kkuint32 x = 0; x < listOfSelectedFeatures.size (); x++) AddFeature (listOfSelectedFeatures[x]); }
uint32_t BitKey::size(void) const { const BitString bs(iv_Capacity,(CPU_WORD *)cDataPtr()); return bs.GetSetCount(); }
KKStr FeatureNumList::ToHexString () const { BitString bs (maxFeatureNum + 1); ToBitString (bs); return bs.HexStr (); } /* ToHexString */
KKStr FeatureNumList::ToHexString (FileDescConstPtr fileDesc) const { BitString bs (fileDesc->NumOfFields ()); ToBitString (bs); return bs.HexStr (); } /* ToHexString */
BitSequenceRRR::BitSequenceRRR(const BitString & bs, uint sample_rate) { build(bs.getData(),bs.getLength(),sample_rate); }
void QuadTree_H::Compress(char * filename, const int &lineSkip, long long size, BitString * result) { std::vector<long long> quadStrings; std::ifstream fin(filename); std::string line; int x, y; for (int i = 0; i < lineSkip; ++i) std::getline(fin, line); while (std::getline(fin, line)) { std::stringstream ss(line); ss >> x >> y; quadStrings.push_back(GetQuadString(size, x, y)); } std::sort(quadStrings.begin(), quadStrings.end()); long long currentString = GetQuadString(size, 0, 0); long long depth = LogFour(currentString); BitString * depthStrings = new BitString[depth]; long long * depthSizes = new long long[depth]; for (int i = 0; i < depth; ++i) depthSizes[i] = 0; long long freq[4] = {0, 0, 0, 0}; long long dCommon, upQuad, upString, upCommon; dCommon = DeepestCommonQuad(depth, currentString, quadStrings[0]); upQuad = QuadAtDepth(quadStrings[0], depth - dCommon - 1); upString = StripNQuads(quadStrings[0], depth - dCommon - 1) - 1; upCommon = DeepestCommonQuad(depth, currentString, upString); if (currentString <= upString) { for (long long d = upCommon; d >= dCommon; --d) { long long curQuad = QuadAtDepth(currentString, depth - d - 1); long long upQuad = QuadAtDepth(upString, depth - d - 1); for (long long q = curQuad; q <= upQuad; ++q) { depthStrings[d].AppendBit(0); depthStrings[d].AppendBit(0); ++depthSizes[d]; ++freq[0]; } if (upQuad == 3) { for (long long y = d; y >= 1; --y) { if (!Compress(depthStrings, depthSizes, freq, y)) break; } } } } currentString = upString + 1; if (currentString < quadStrings[0]) { for (long long d = dCommon; d < depth; ++d) { long long curQuad = QuadAtDepth(currentString, depth - d - 1); long long edgeQuad = QuadAtDepth(quadStrings[0], depth - d - 1); for (long long q = curQuad; q < edgeQuad; ++q) { depthStrings[d].AppendBit(0); depthStrings[d].AppendBit(0); ++depthSizes[d]; ++freq[0]; } if (edgeQuad == 3) Compress(depthStrings, depthSizes, freq, d); } currentString = quadStrings[0]; } depthStrings[depth-1].AppendBit(0); depthStrings[depth-1].AppendBit(1); ++depthSizes[depth-1]; ++freq[1]; for (long long d = depth - 1; d >= 1; --d) { if (!Compress(depthStrings, depthSizes, freq, d)) break; } for (int i = 1; i < quadStrings.size(); ++i) { dCommon = DeepestCommonQuad(depth, currentString, quadStrings[i]); upQuad = QuadAtDepth(quadStrings[i], depth - dCommon - 1); upString = StripNQuads(quadStrings[i], depth - dCommon - 1) - 1; upCommon = DeepestCommonQuad(depth, currentString, upString); if (currentString < upString) { for (long long d = depth - 1; d >= dCommon; --d) { long long curQuad = QuadAtDepth(currentString, depth - d - 1); long long upQuad = QuadAtDepth(upString, depth - d - 1); for (int q = curQuad; q < upQuad; ++q) { depthStrings[d].AppendBit(0); depthStrings[d].AppendBit(0); ++depthSizes[d]; ++freq[0]; } if (upQuad == 3 && upQuad != curQuad) { for (long long y = d; y >= 1; --y) { if (!Compress(depthStrings, depthSizes, freq, y)) break; } } } } currentString = upString + 1; if (currentString < quadStrings[i]) { for (long long d = dCommon; d < depth; ++d) { long long curQuad = QuadAtDepth(currentString, depth - d - 1); long long edgeQuad = QuadAtDepth(quadStrings[i], depth - d - 1); for (long long q = curQuad; q < edgeQuad; ++q) { depthStrings[d].AppendBit(0); depthStrings[d].AppendBit(0); ++depthSizes[d]; ++freq[0]; } if (edgeQuad == 3 && edgeQuad != curQuad) { for (long long y = d; y >= 1; --y) { if (!Compress(depthStrings, depthSizes, freq, y)) break; } } } currentString = quadStrings[i]; } depthStrings[depth-1].AppendBit(0); depthStrings[depth-1].AppendBit(1); ++depthSizes[depth-1]; ++freq[1]; for (long long d = depth - 1; d >= 1; --d) { if (!Compress(depthStrings, depthSizes, freq, d)) break; } } currentString = quadStrings.back(); long long endString = GetQuadString(size, size - 1, size - 1); dCommon = DeepestCommonQuad(depth, currentString, endString); if (currentString < endString) { for (long long d = depth - 1; d >= 0; --d) { long long curQuad = QuadAtDepth(currentString, depth - d - 1); long long upQuad = QuadAtDepth(endString, depth - d - 1); for (long long q = curQuad; q < upQuad; ++q) { depthStrings[d].AppendBit(0); depthStrings[d].AppendBit(0); ++depthSizes[d]; ++freq[0]; } if (upQuad == 3 && curQuad != upQuad) { for (long long y = d; y >= 1; --y) { if (!Compress(depthStrings, depthSizes, freq, y)) break; } } } } result->Init(0); DeltaCode delta; BitString tmp; delta.EncodeInt(size, &tmp); result->AppendBitString(tmp); ++freq[3]; BitString huff[4]; GetHuffStrings(result, freq, huff); result->AppendBitString(huff[3]); for (int i = 0; i < depth; ++i) { BitString * ds = &depthStrings[i]; for ( long long j = 0; j < ds->get_length(); j+=2) { int quad = ds->GetBit(j) << 1 | ds->GetBit(j+1); result->AppendBitString(huff[quad]); } } result->PrintRUSAGE(); }
bool QuadTree_DP::Exists(const BitString &code, int x, int y) { DeltaCode delta; uint64_t cur = 0; long long nodes = delta.DecodeNextInt(code, &cur); long long depth = delta.DecodeNextInt(code, &cur); std::vector<long long> depthSizes(depth+1); depthSizes[0] = 1; for (int i = 1; i <= depth; ++i) { depthSizes[i] = delta.DecodeNextInt(code, &cur); } long long offset = cur; std::vector<long long> depthIndices(depth+2); depthIndices[0] = 0; for (int i = 1; i < depth + 2; ++i) { depthIndices[i] = depthSizes[i-1] + depthIndices[i-1]; } long long size = code.get_length(); long long nextIndx = 0; long long begRow = 0, begCol = 0, endRow = nodes, endCol = nodes; long long curDepth = 0, nodesAtDepth = 1, nodesAtNextDepth = 0; for (long long i = 0; i < depth; ++i) { for(long long j = depthIndices[i]; j < depthIndices[i+1]; ++j) { int curQuad = code.GetBit(offset+(j*2)) << 1 | code.GetBit(offset+(j*2)+1); if (j == nextIndx) { if (curQuad == 3) { double midRow = ((double)begRow + endRow)/2; double midCol = ((double)begCol + endCol)/2; if (x < midRow) { //We are in NE or NW if (y < midCol) { //We are in NW endRow = midRow; endCol = midCol; nextIndx = depthIndices[i+1] + nodesAtNextDepth; } else { //We are in NE endRow = midRow; begCol = midCol; nextIndx = depthIndices[i+1] + nodesAtNextDepth + 1; } } else if (y < midCol) { //We are in SW begRow = midRow; endCol = midCol; nextIndx = depthIndices[i+1] + nodesAtNextDepth + 2; } else { //We are in SE begRow = midRow; begCol = midCol; nextIndx = depthIndices[i+1] + nodesAtNextDepth + 3; } nodesAtNextDepth += 4; int nextQuad = code.GetBit(offset+(nextIndx*2)) << 1 | code.GetBit(offset+(nextIndx*2)+1); if (nextQuad == 1) return true; if (nextQuad == 0) return false; else if (nextQuad == 2) if (begCol-begRow + x == y) return false; else return true; } else if (curQuad == 1) return true; else if (curQuad == 0) return false; else if (curQuad == 2) { if (begCol-begRow + x == y) return false; else return true; } break; } else if (curQuad == 3) { nodesAtNextDepth += 4; } } nodesAtDepth = nodesAtNextDepth; nodesAtNextDepth = 0; } }
std::vector<int> QuadTree_DP::GetNeighbors(const BitString &code, int x) { DeltaCode delta; uint64_t cur = 0; int nodes = delta.DecodeNextInt(code, &cur); long long depth = delta.DecodeNextInt(code, &cur); std::vector<long long> depthSizes(depth+1); depthSizes[0] = 1; for (int i = 1; i <= depth; ++i) { depthSizes[i] = delta.DecodeNextInt(code, &cur); } long long offset = cur; std::vector<long long> depthIndices(depth+2); depthIndices[0] = 0; for (int i = 1; i < depth + 2; ++i) { depthIndices[i] = depthSizes[i-1] + depthIndices[i-1]; } std::vector<int> neighbors; QueueObj pos; std::queue<QueueObj> posQueue; posQueue.push(QueueObj(0, 0, nodes, 0, nodes)); pos = posQueue.front(); long long nodesAtDepth = 1, nodesAtNextDepth = 0; for(int i = 0; i <= depth && !posQueue.empty(); ++i) { for(long long j = depthIndices[i]; j < depthIndices[i+1]; j++) { int curQuad = code.GetBit(offset+(j*2)) << 1 | code.GetBit(offset+(j*2)+1); if (j == pos.nextPos) { posQueue.pop(); if (curQuad == 3) { double midRow = ((double)pos.begRow + pos.endRow)/2; double midCol = ((double)pos.begCol + pos.endCol)/2; if (x < midRow) { posQueue.push(QueueObj(depthIndices[i+1] + nodesAtNextDepth, pos.begRow, midRow, pos.begCol, midCol)); posQueue.push(QueueObj(depthIndices[i+1] + nodesAtNextDepth + 1, pos.begRow, midRow, midCol, pos.endCol)); } else { posQueue.push(QueueObj(depthIndices[i+1] + nodesAtNextDepth + 2, midRow, pos.endRow, pos.begCol, midCol)); posQueue.push(QueueObj(depthIndices[i+1] + nodesAtNextDepth + 3, midRow, pos.endRow, midCol, pos.endCol)); } nodesAtNextDepth += 4; } else if (curQuad == 1) { for (int n = pos.begCol; n < pos.endCol; ++n) { neighbors.push_back(n); } } else if (curQuad == 2) { for (int n = pos.begCol; n < pos.endCol; ++n) { if (pos.begCol - pos.begRow + x == n) continue; neighbors.push_back(n); } } if (posQueue.empty()) break; pos = posQueue.front(); if (pos.nextPos > depthIndices[i+1]) break; } else if (curQuad == 3){ nodesAtNextDepth += 4; } } nodesAtDepth = nodesAtNextDepth; nodesAtNextDepth = 0; } return neighbors; }