Skip to content

Commit

Permalink
Add interface between stuffedBits abd readBuffer/writeBuffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianwalenz committed Jul 5, 2019
1 parent 2b836b4 commit 2b06be7
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
125 changes: 125 additions & 0 deletions src/utility/bits.C
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ stuffedBits::stuffedBits(FILE *inFile) {
};


stuffedBits::stuffedBits(readBuffer *B) {

_dataBlockLenMax = 0;

_dataBlocksLen = 0;
_dataBlocksMax = 0;

_dataBlockBgn = NULL;
_dataBlockLen = NULL;
_dataBlocks = NULL;

_dataPos = 0;
_data = NULL;

loadFromBuffer(B);

_dataBlk = 0;
_dataWrd = 0;
_dataBit = 64;
};


#if 0
// This is untested.
stuffedBits::stuffedBits(stuffedBits &that) {
Expand Down Expand Up @@ -168,6 +190,109 @@ stuffedBits::~stuffedBits() {



void
stuffedBits::dumpToBuffer(writeBuffer *B) {

B->write(&_dataBlockLenMax, sizeof(uint64));
B->write(&_dataBlocksLen, sizeof(uint32));
B->write(&_dataBlocksMax, sizeof(uint32));
B->write( _dataBlockBgn, sizeof(uint64) * _dataBlocksLen);
B->write( _dataBlockLen, sizeof(uint64) * _dataBlocksLen);

for (uint32 ii=0; ii<_dataBlocksLen; ii++) {
uint64 nWordsToWrite = _dataBlockLen[ii] / 64 + (((_dataBlockLen[ii] % 64) == 0) ? 0 : 1);
uint64 nWordsAllocd = _dataBlockLenMax / 64;

assert(nWordsToWrite <= nWordsAllocd);

B->write(_dataBlocks[ii], sizeof(uint64) * nWordsToWrite);
}
}



bool
stuffedBits::loadFromBuffer(readBuffer *B) {
uint32 nLoad = 0;
uint64 inLenMax = 0;
uint32 inLen = 0;
uint32 inMax = 0;

if (B == NULL) // No buffer,
return(false); // no load.

// Try to load the new parameters into temporary storage, so we can
// compare against what have already allocated.

nLoad += B->read(&inLenMax, sizeof(uint64)); // Max length of each block.
nLoad += B->read(&inLen, sizeof(uint32)); // Number of blocks stored.
nLoad += B->read(&inMax, sizeof(uint32)); // Number of blocks allocated.

if (nLoad != 3)
return(false);

// If the input blocks are not the same size as the blocks we have, remove them.

if (_dataBlockLenMax != inLenMax) {
for (uint32 ii=0; ii<_dataBlocksLen; ii++)
delete [] _dataBlocks[ii];

for (uint32 ii=0; ii<_dataBlocksMax; ii++)
_dataBlocks[ii] = NULL;

_dataBlockLenMax = inLenMax;
}

// If there are more blocks than we have space for, grab more space. Bgn and Len can just be
// reallocated. The pointers need to be extended (to preserve what's already in there).

if (_dataBlocksMax < inLen) {
delete [] _dataBlockBgn;
delete [] _dataBlockLen;

_dataBlockBgn = new uint64 [inLen];
_dataBlockLen = new uint64 [inLen];

resizeArray(_dataBlocks, _dataBlocksLen, _dataBlocksMax, inLen, resizeArray_copyData | resizeArray_clearNew);
}

// Update the parameters.

_dataBlocksLen = inLen;

// Load the data.

B->read(_dataBlockBgn, sizeof(uint64) * _dataBlocksLen);
B->read(_dataBlockLen, sizeof(uint64) * _dataBlocksLen);

for (uint32 ii=0; ii<_dataBlocksLen; ii++) {
uint64 nWordsToRead = _dataBlockLen[ii] / 64 + (((_dataBlockLen[ii] % 64) == 0) ? 0 : 1);
uint64 nWordsAllocd = _dataBlockLenMax / 64;

assert(nWordsToRead <= nWordsAllocd);

if (_dataBlocks[ii] == NULL)
_dataBlocks[ii] = new uint64 [nWordsAllocd];

B->read(_dataBlocks[ii], sizeof(uint64) * nWordsToRead);

memset(_dataBlocks[ii] + nWordsToRead, 0, sizeof(uint64) * (nWordsAllocd - nWordsToRead));
}

// Set up the read/write head.

_dataPos = 0;
_data = _dataBlocks[0];

_dataBlk = 0;
_dataWrd = 0;
_dataBit = 64;

return(true);
}



void
stuffedBits::dumpToFile(FILE *F) {

Expand Down
6 changes: 6 additions & 0 deletions src/utility/bits.H
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include "AS_global.H"

#include "files.H"

#include <algorithm>

// Define this to enable testing that the width of the data element is greater than zero. The
Expand Down Expand Up @@ -529,6 +531,7 @@ public:
stuffedBits(uint64 nBits=16 * 1024 * 1024 * 8);
stuffedBits(const char *inputName);
stuffedBits(FILE *inFile);
stuffedBits(readBuffer *B);
//stuffedBits(stuffedBits &that); // Untested.
~stuffedBits();

Expand All @@ -540,6 +543,9 @@ public:

// Files.

void dumpToBuffer(writeBuffer *B);
bool loadFromBuffer(readBuffer *B);

void dumpToFile(FILE *F);
bool loadFromFile(FILE *F);

Expand Down

0 comments on commit 2b06be7

Please sign in to comment.