Skip to content

StampBase to shared_ptr #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions blobstamper/galley.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
int
GalleyVectorBase::minSize()
{
if (stamp.isFixedSize())
if (stamp->isFixedSize())
{
return stamp.minSize(); // When size is fixed, series can have only one member with no extra data used
return stamp->minSize(); // When size is fixed, series can have only one member with no extra data used
}
else
{
if (stamp.isUnbounded())
if (stamp->isUnbounded())
{
return stamp.minSize() + ORACLE_SIZE * 2; // One -- count oracle, one -- size oracle
return stamp->minSize() + ORACLE_SIZE * 2; // One -- count oracle, one -- size oracle
}
else
{
return stamp.minSize() + ORACLE_SIZE; // At leas one element with an oracle
return stamp->minSize() + ORACLE_SIZE; // At leas one element with an oracle
}
}
}
Expand All @@ -56,7 +56,8 @@ GalleyVectorStr::ExtractStrVector(std::shared_ptr<Blob> blob)

for(int i = 0; i<blobs.size(); i++)
{
res[i] = (dynamic_cast<StampBaseStr &>(stamp)).ExtractStr(blobs[i]); // We know for sure that stamp is StampBaseStr
std::shared_ptr<StampBaseStr> s = std::dynamic_pointer_cast<StampBaseStr>(stamp);
res[i] = s->ExtractStr(blobs[i]); // We know for sure that stamp is StampBaseStr
}
return res;
}
Expand All @@ -69,22 +70,22 @@ GalleyVectorBin::ExtractBinVector(std::shared_ptr<Blob> blob)

for(int i = 0; i<blobs.size(); i++)
{
res[i] = b_stamp.ExtractBin(blobs[i]);
res[i] = b_stamp->ExtractBin(blobs[i]);
}
return res;
}

std::vector<std::shared_ptr<Blob>>
GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
{
if (blob->Size()<stamp.minSize())
if (blob->Size()<stamp->minSize())
{
throw OutOfData(); /* FIXME: May be later add option that allows empty lists if needed*/
}
std::vector<std::shared_ptr<Blob>> res;
if (stamp.isFixedSize())
if (stamp->isFixedSize())
{
int size = stamp.minSize();
int size = stamp->minSize();
while (blob->Size() >= size)
{
std::shared_ptr<Blob> el = blob->Chop(size);
Expand All @@ -93,7 +94,7 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
}
else
{
if (stamp.isUnbounded())
if (stamp->isUnbounded())
{
/*
The idea of this part is following:
Expand All @@ -112,7 +113,7 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
*/

/* Getting count oracle and normalze it to fit available size */
size_t count_max = (blob->Size() - ORACLE_SIZE) / (stamp.minSize() + ORACLE_SIZE); //First oracle - for number of items, and second one is oracle for each item size
size_t count_max = (blob->Size() - ORACLE_SIZE) / (stamp->minSize() + ORACLE_SIZE); //First oracle - for number of items, and second one is oracle for each item size

ORACLE_STAMP stamp_oracle;
ORACLE_TYPE count_oracle = stamp_oracle.ExtractValue(blob);
Expand Down Expand Up @@ -140,14 +141,14 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)

/* Calculating available vairable size, that will be destributed between parts according to size oracles */
int data_size = blob->Size();
int fixed_data_size = stamp.minSize() * count_target;
int fixed_data_size = stamp->minSize() * count_target;
int var_data_size = data_size - fixed_data_size;

/* normalizing oracles so they fit total variable size, chop to parts and stamp parts */
float remainder = 0; /* we do not want waste bytes because of rounding, so we keep the remainder, and reuse it. Thus we will use all bytes (alomost, may loose last one due to remainder=0.99999)*/
for(ORACLE_TYPE o : size_oracles)
{
float el_size_f = stamp.minSize() + (float) o / size_oracle_total * var_data_size + remainder;
float el_size_f = stamp->minSize() + (float) o / size_oracle_total * var_data_size + remainder;
int el_size = el_size_f;
remainder = el_size_f - el_size;

Expand All @@ -158,12 +159,12 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
else
{
/* Stamp is variated size */
int fixed_size = stamp.minSize();
int var_size = stamp.maxSize() - fixed_size;
int fixed_size = stamp->minSize();
int var_size = stamp->maxSize() - fixed_size;
ORACLE_STAMP stamp_oracle;
while(1)
{
if(stamp.minSize() + stamp_oracle.minSize() > blob->Size())
if(stamp->minSize() + stamp_oracle.minSize() > blob->Size())
break;

ORACLE_TYPE oracle = stamp_oracle.ExtractValue(blob);
Expand Down
22 changes: 10 additions & 12 deletions blobstamper/galley.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class GalleyBase: public virtual StampBase
class GalleyVectorBase : public GalleyBase
{
protected:
StampBase &stamp;
std::shared_ptr<StampBase> stamp;
public:
GalleyVectorBase(StampBase & stamp_arg) : stamp(stamp_arg) {};
GalleyVectorBase(std::shared_ptr<StampBase> stamp_arg) : stamp(stamp_arg) {};
std::vector<std::shared_ptr<Blob>> extract_internal(std::shared_ptr<Blob> blob);
int minSize() override;
int maxSize() override {return -1;}; /* Sereies always takes as much data as it can take */
Expand All @@ -55,34 +55,32 @@ class GalleyVectorBase : public GalleyBase
class GalleyVectorStr: public GalleyVectorBase
{
public:
GalleyVectorStr(StampBaseStr & stamp_arg): GalleyVectorBase(stamp_arg) {};
GalleyVectorStr(std::shared_ptr<StampBaseStr> stamp_arg): GalleyVectorBase(stamp_arg) {};
std::vector<std::string> ExtractStrVector(std::shared_ptr<Blob> blob);
};

template<class T> class GalleyVectorStrStampBase: public GalleyVectorStr, public StampBaseStr
{
protected:
T * item_stamp_p;
public:
GalleyVectorStrStampBase(): GalleyVectorStr(*(item_stamp_p = new T())) {};
~GalleyVectorStrStampBase() {delete item_stamp_p;};
GalleyVectorStrStampBase(): GalleyVectorStr(std::make_shared<T>()) {};

};


class GalleyVectorBin: public GalleyVectorBase
{
StampBaseBin & b_stamp;
std::shared_ptr<StampBaseBin> b_stamp;
public:
GalleyVectorBin(StampBaseBin & stamp_arg): GalleyVectorBase(stamp_arg), b_stamp(stamp_arg) {};
GalleyVectorBin(std::shared_ptr<StampBaseBin> stamp_arg): GalleyVectorBase(stamp_arg), b_stamp(stamp_arg) {};
std::vector<std::vector<char>> ExtractBinVector(std::shared_ptr<Blob> blob);
};


template<class T> class GalleyVectorV: public GalleyVectorBase
{
StampBaseV<T>& v_stamp;
std::shared_ptr<StampBaseV<T>> v_stamp;
public:
GalleyVectorV(StampBaseV<T> & stamp_arg): GalleyVectorBase(stamp_arg), v_stamp(stamp_arg) {};
GalleyVectorV(std::shared_ptr<StampBaseV<T>> stamp_arg): GalleyVectorBase(stamp_arg), v_stamp(stamp_arg) {};
std::vector<T> ExtractValuesVector(std::shared_ptr<Blob> blob);
};

Expand All @@ -95,7 +93,7 @@ GalleyVectorV<T>::ExtractValuesVector(std::shared_ptr<Blob> blob)

for(int i=0; i<blobs.size(); i++)
{
res[i] = v_stamp.ExtractValue(blobs[i]);
res[i] = v_stamp->ExtractValue(blobs[i]);
}
return res;
}
Expand Down
4 changes: 2 additions & 2 deletions blobstamper/stamp_enumerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
class StampStrEnumerator: public GalleyVectorStr, public StampBaseStr
{
protected:
StampBaseStr & stamp_str;
std::shared_ptr<StampBaseStr> stamp_str;
const std::string separator;
const std::string left_bracket;
const std::string right_bracket;
public:
StampStrEnumerator(StampBaseStr &arg_stamp,
StampStrEnumerator(std::shared_ptr<StampBaseStr> arg_stamp,
const std::string arg_sep,
const std::string arg_l,
const std::string arg_r
Expand Down
4 changes: 2 additions & 2 deletions t/130-stamp_enumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ main()
{
TEST_START(1);
{ /* 1..1 */
std::shared_ptr<Blob> blob = std::make_shared<Blob>((char *) sample, sizeof(sample));
StampArithm<unsigned char> base_stamp;
std::shared_ptr<Blob> blob = std::make_shared<Blob>((char *) sample, sizeof(sample));
std::shared_ptr<StampArithm<unsigned char>> base_stamp = std::make_shared<StampArithm<unsigned char>>();
StampStrEnumerator stamp(base_stamp, "; ", "<", ">");

std::string s = stamp.ExtractStr(blob);
Expand Down
18 changes: 9 additions & 9 deletions t/300-galley.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ main()
std::string expected2 = "34";
std::string expected3 = "56";

StampTwoChars stamp;
std::shared_ptr<StampTwoChars> stamp = std::make_shared<StampTwoChars>();
GalleyVectorStr galley(stamp);
std::shared_ptr<Blob> blob = std::make_shared<Blob>(short_sample, strlen(short_sample));
std::shared_ptr<Blob> blob = std::make_shared<Blob>(short_sample, strlen(short_sample));
std::vector<std::string> res = galley.ExtractStrVector(blob);

is(res[0], expected1, "GalleyVector, fixed size string stamp: First element of shifted list is ok");
Expand All @@ -64,8 +64,8 @@ main()
std::string expected3 = "(zA, B%, CD, EF, GH, IJ, KL)";
std::string expected4 = "(MN, OP, QR, ST, UV, WX, YZ)";

std::shared_ptr<Blob> blob= std::make_shared<Blob>(longer_sample, strlen(longer_sample));
StampTwoCharsList stamp_charlist;
std::shared_ptr<Blob> blob= std::make_shared<Blob>(longer_sample, strlen(longer_sample));
std::shared_ptr<StampTwoCharsList> stamp_charlist = std::make_shared<StampTwoCharsList>();
GalleyVectorStr galley(stamp_charlist);

std::vector<std::string> res = galley.ExtractStrVector(blob);
Expand All @@ -84,9 +84,9 @@ main()
unsigned short int expected2 = (unsigned char) '4' * 256 +(unsigned char) '3';
unsigned short int expected3 = (unsigned char) '6' * 256 +(unsigned char) '5';

StampArithm<unsigned short int> stamp;
std::shared_ptr<StampArithm<unsigned short int>> stamp = std::make_shared<StampArithm<unsigned short int>>();
GalleyVectorBin galley(stamp);
std::shared_ptr<Blob> blob = std::make_shared<Blob>(short_sample, strlen(short_sample));
std::shared_ptr<Blob> blob = std::make_shared<Blob>(short_sample, strlen(short_sample));
std::vector<std::vector<char>> res = galley.ExtractBinVector(blob);

std::vector<char> v;
Expand All @@ -110,7 +110,7 @@ main()
{ /* 14 */

signed int sample[] = {1, -2, -30, 40, -55, 6};
StampArithm<signed int> stamp;
std::shared_ptr<StampArithm<signed int>> stamp = std::make_shared<StampArithm<signed int>>();
GalleyVectorV<signed int> galley(stamp);
std::shared_ptr<Blob> blob = std::make_shared<Blob>((char*)sample, sizeof(sample));
std::vector<signed int> res = galley.ExtractValuesVector(blob);
Expand All @@ -129,8 +129,8 @@ main()
std::string expected3 = "bcde";
std::string expected4 = "ghij";

std::shared_ptr<Blob> blob = std::make_shared<Blob>(sample, strlen(sample));
StampSeveralChars stamp;
std::shared_ptr<Blob> blob = std::make_shared<Blob>(sample, strlen(sample));
std::shared_ptr<StampSeveralChars> stamp = std::make_shared<StampSeveralChars>();
GalleyVectorStr galley(stamp);

std::vector<std::string> res = galley.ExtractStrVector(blob);
Expand Down
2 changes: 1 addition & 1 deletion t/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ build-libtappp:
$(MAKE) -C ../libtappp

%.t: %.cpp $(BLOBSTAMPER_OBJ)
$(CXX) $(CXXFLAGS) -I../libtappp/include -I.. -o $@ $< $(BLOBSTAMPER_OBJ) -L../libtappp -ltap++
$(CXX) $(CXXFLAGS) -I../libtappp/include -g -I.. -o $@ $< $(BLOBSTAMPER_OBJ) -L../libtappp -ltap++

test: all
@echo run_tests.pl $(TEST_BIN)
Expand Down
6 changes: 3 additions & 3 deletions t/test-chars-stamps.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ StampSeveralChars::ExtractStr(std::shared_ptr<Blob> blob)
class StampTwoCharsList: public StampBaseStr
{
protected:
StampTwoChars el_stamp;
std::shared_ptr<StampTwoChars> el_stamp;
GalleyVectorStr galley;
public:
std::string ExtractStr(std::shared_ptr<Blob> blob) override;
StampTwoCharsList(): el_stamp {}, galley {el_stamp} {};
StampTwoCharsList(): el_stamp {std::make_shared<StampTwoChars>()}, galley {el_stamp} {};

virtual int minSize() override {return el_stamp.minSize();};
virtual int minSize() override {return el_stamp->minSize();};
virtual int maxSize() override {return -1;};
};

Expand Down