Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a4e3a52

Browse files
Move ExtractStr and ExtracBin methods to separate classes
1 parent f666995 commit a4e3a52

14 files changed

+159
-120
lines changed

blobstamper/blob.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,6 @@ Blob::Size()
9393
return end - begin + 1;
9494
}
9595

96-
std::vector<char>
97-
Blob::ShiftSingleStampBin(StampBase& stmp)
98-
{
99-
return stmp.ExtractBin(*this);
100-
}
101-
102-
std::string
103-
Blob::ShiftSingleStampStr(StampBase& stmp)
104-
{
105-
return stmp.ExtractStr(*this);
106-
}
107-
10896
void
10997
Blob::DataDup(char *& data_out, size_t& size_out)
11098
{

blobstamper/blob.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ class Blob
4242
std::vector<char> ChopBlank(StampBase &stmp);
4343
void DataDup(char *& data_out, size_t& size_out);
4444
std::vector<char> asVector();
45-
46-
47-
std::vector<char> ShiftSingleStampBin(StampBase &stmp);
48-
std::string ShiftSingleStampStr(StampBase &stmp);
4945
};
5046

5147
class OutOfData /*An exeption. Experemental for now*/

blobstamper/galley.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
int
30-
GalleyVector::minSize()
30+
GalleyVectorBase::minSize()
3131
{
3232
if (stamp.isFixedSize())
3333
{
@@ -49,33 +49,33 @@ GalleyVector::minSize()
4949

5050

5151
std::vector<std::string>
52-
GalleyVector::ExtractStr(Blob &blob)
52+
GalleyVectorStr::ExtractStr(Blob &blob)
5353
{
5454
std::vector<Blob> blobs = extract_internal(blob);
5555
std::vector<std::string> res(blobs.size());
5656

5757
for(int i = 0; i<blobs.size(); i++)
5858
{
59-
res[i] = blobs[i].ShiftSingleStampStr(stamp);
59+
res[i] = s_stamp.ExtractStr(blobs[i]);
6060
}
6161
return res;
6262
}
6363

6464
std::vector<std::vector<char>>
65-
GalleyVector::ExtractBin(Blob &blob)
65+
GalleyVectorBin::ExtractBin(Blob &blob)
6666
{
6767
std::vector<Blob> blobs = extract_internal(blob);
6868
std::vector<std::vector<char>> res(blobs.size());
6969

7070
for(int i = 0; i<blobs.size(); i++)
7171
{
72-
res[i] = blobs[i].ShiftSingleStampBin(stamp);
72+
res[i] = b_stamp.ExtractBin(blobs[i]);
7373
}
7474
return res;
7575
}
7676

7777
std::vector<Blob>
78-
GalleyVector::extract_internal(Blob &blob)
78+
GalleyVectorBase::extract_internal(Blob &blob)
7979
{
8080
if (blob.Size()<stamp.minSize())
8181
{
@@ -182,7 +182,7 @@ GalleyVector::extract_internal(Blob &blob)
182182
/**********************************************/
183183

184184
std::vector<Blob>
185-
GalleySet::extract_internal(Blob &blob)
185+
GalleySetBase::extract_internal(Blob &blob)
186186
{
187187
std::vector<Blob> res;
188188
int fixed_total_size = 0; // Summ of sizes of fixed parts of all stamps
@@ -348,37 +348,37 @@ GalleySet::extract_internal(Blob &blob)
348348
}
349349

350350
std::vector<std::string>
351-
GalleySet::ExtractStr(Blob &blob)
351+
GalleySetStr::ExtractStr(Blob &blob)
352352
{
353353
std::vector<std::string> res;
354354
std::vector<Blob> blobs = extract_internal(blob);
355355
for(int i=0; i<blobs.size(); i++)
356356
{
357357
Blob blob = blobs[i];
358-
StampBase & stamp = stamps[i];
359-
std::string str= blob.ShiftSingleStampStr(stamp);
358+
StampBaseStr & stamp = s_stamps[i];
359+
std::string str= stamp.ExtractStr(blob);
360360
res.push_back(str);
361361
}
362362
return res;
363363
}
364364

365365
std::vector<std::vector<char>>
366-
GalleySet::ExtractBin(Blob &blob)
366+
GalleySetBin::ExtractBin(Blob &blob)
367367
{
368368
std::vector<std::vector<char>> res;
369369
std::vector<Blob> blobs = extract_internal(blob);
370370
for(int i=0; i<blobs.size(); i++)
371371
{
372372
Blob blob = blobs[i];
373-
StampBase & stamp = stamps[i];
373+
StampBaseBin & stamp = b_stamps[i];
374374
std::vector<char> v = stamp.ExtractBin(blob);
375375
res.push_back(v);
376376
}
377377
return res;
378378
}
379379

380380
int
381-
GalleySet::minSize()
381+
GalleySetBase::minSize()
382382
{
383383
bool has_variated_stamps = false;
384384
bool has_unbounded_stamps = false;
@@ -415,7 +415,7 @@ GalleySet::minSize()
415415
}
416416

417417
int
418-
GalleySet::maxSize()
418+
GalleySetBase::maxSize()
419419
{
420420
int res = 0;
421421

blobstamper/galley.h

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,40 @@ class GalleyBase
3939

4040
};
4141

42-
class GalleyVector : public GalleyBase
42+
class GalleyVectorBase : public GalleyBase
4343
{
4444
protected:
4545
StampBase &stamp;
4646
public:
47-
GalleyVector(StampBase & stamp_arg) : stamp(stamp_arg) {};
47+
GalleyVectorBase(StampBase & stamp_arg) : stamp(stamp_arg) {};
4848
std::vector<Blob> extract_internal(Blob &blob);
49-
std::vector<std::string> ExtractStr(Blob &blob);
50-
std::vector<std::vector<char>> ExtractBin(Blob &blob);
51-
5249
int minSize() override;
5350
int maxSize() override {return -1;}; /* Sereies always takes as much data as it can take */
5451
};
5552

56-
template<class T> class GalleyVectorV: public GalleyVector
53+
54+
class GalleyVectorStr: public GalleyVectorBase
55+
{
56+
StampBaseStr & s_stamp;
57+
public:
58+
GalleyVectorStr(StampBaseStr & stamp_arg): GalleyVectorBase(stamp_arg), s_stamp(stamp_arg) {};
59+
std::vector<std::string> ExtractStr(Blob &blob);
60+
};
61+
62+
class GalleyVectorBin: public GalleyVectorBase
63+
{
64+
StampBaseBin & b_stamp;
65+
public:
66+
GalleyVectorBin(StampBaseBin & stamp_arg): GalleyVectorBase(stamp_arg), b_stamp(stamp_arg) {};
67+
std::vector<std::vector<char>> ExtractBin(Blob &blob);
68+
};
69+
70+
71+
template<class T> class GalleyVectorV: public GalleyVectorBase
5772
{
5873
StampBaseV<T>& v_stamp;
5974
public:
60-
GalleyVectorV(StampBaseV<T> & stamp_arg): GalleyVector(stamp_arg), v_stamp(stamp_arg) {};
75+
GalleyVectorV(StampBaseV<T> & stamp_arg): GalleyVectorBase(stamp_arg), v_stamp(stamp_arg) {};
6176
std::vector<T> ExtractValues(Blob &blob);
6277
};
6378

@@ -76,19 +91,54 @@ GalleyVectorV<T>::ExtractValues(Blob &blob)
7691
}
7792

7893

79-
class GalleySet : public GalleyBase
94+
class GalleySetBase : public GalleyBase
8095
{
8196
protected:
8297
std::vector<std::reference_wrapper<StampBase>> stamps;
8398
public:
84-
GalleySet(std::vector<std::reference_wrapper<StampBase>> arg) : stamps(arg) {};
99+
GalleySetBase(std::vector<std::reference_wrapper<StampBase>> arg) : stamps(arg) {};
85100
std::vector<Blob> extract_internal(Blob &blob);
86-
std::vector<std::string> ExtractStr(Blob &blob);
87-
std::vector<std::vector<char>> ExtractBin(Blob &blob);
88101

89102
int minSize() override;
90103
int maxSize() override;
91104
};
92105

106+
class GalleySetBin : public GalleySetBase
107+
{
108+
std::vector<std::reference_wrapper<StampBaseBin>> b_stamps;
109+
public:
110+
GalleySetBin(std::vector<std::reference_wrapper<StampBaseBin>> arg) : GalleySetBase(cast_arg(arg)), b_stamps(arg) {};
111+
std::vector<std::vector<char>> ExtractBin(Blob &blob);
112+
113+
std::vector<std::reference_wrapper<StampBase>> cast_arg(std::vector<std::reference_wrapper<StampBaseBin>> in)
114+
{
115+
std::vector<std::reference_wrapper<StampBase>> res;
116+
for(StampBaseBin & s : in)
117+
{
118+
res.push_back(s);
119+
}
120+
return res;
121+
};
122+
};
123+
124+
125+
class GalleySetStr : public GalleySetBase
126+
{
127+
std::vector<std::reference_wrapper<StampBaseStr>> s_stamps;
128+
public:
129+
GalleySetStr(std::vector<std::reference_wrapper<StampBaseStr>> arg) : GalleySetBase(cast_arg(arg)), s_stamps(arg) {};
130+
std::vector<std::string> ExtractStr(Blob &blob);
131+
132+
std::vector<std::reference_wrapper<StampBase>> cast_arg(std::vector<std::reference_wrapper<StampBaseStr>> in)
133+
{
134+
std::vector<std::reference_wrapper<StampBase>> res;
135+
for(StampBaseStr & s : in)
136+
{
137+
res.push_back(s);
138+
}
139+
return res;
140+
};
141+
};
142+
93143

94144
#endif /* GALLEY_H */

blobstamper/stamp.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,24 @@ class StampBase
3535
bool isFixedSize() {return minSize() == maxSize();}
3636
bool isVariated() {return ! isFixedSize() && ! isUnbounded();}
3737
bool isUnbounded() {return maxSize() == -1;}
38+
};
39+
3840

39-
virtual std::vector<char> ExtractBin(Blob &blob) {throw NotImplemented();};
40-
virtual std::string ExtractStr(Blob &blob) {throw NotImplemented();};
41+
class StampBaseStr: public virtual StampBase
42+
{
43+
public:
44+
virtual std::string ExtractStr(Blob &blob) = 0;
45+
};
46+
47+
48+
class StampBaseBin: public virtual StampBase
49+
{
50+
public:
51+
virtual std::vector<char> ExtractBin(Blob &blob) = 0;
4152
};
4253

4354

44-
template<class T> class StampBasePV: public virtual StampBase
55+
template<class T> class StampBasePV: public StampBaseBin
4556
{
4657
public:
4758
virtual sized_ptr<T> ExtractPValue(Blob &blob) = 0;/* Shoud be defined by derived classes*/

blobstamper/stamp_arithm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "helpers.h"
2424
#include "stamp.h"
2525

26-
template<class T> class StampArithm: public StampFixed, public StampBaseV<T>
26+
template<class T> class StampArithm: public StampFixed, public StampBaseStr, public StampBaseV<T>
2727
{
2828
public:
2929
StampArithm() { size = sizeof(T);};

blobstamper/stamp_dict.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@
2121
#include "stamp_arithm.h"
2222
#include "stamp_dict.h"
2323

24-
StampFixed&
25-
StampDict::GuessStamp(DictBase & dict)
24+
int
25+
StampDict::ChooseStampSize(DictBase & dict)
2626
{
2727
if (dict.size()<= UCHAR_MAX+1)
2828
{
2929
stamp_max_value = UCHAR_MAX;
30-
return stamp8;
30+
return 1;
3131
}
3232
if (dict.size()<= USHRT_MAX+1)
3333
{
3434
stamp_max_value = USHRT_MAX;
35-
return stamp16;
35+
return 2;
3636
}
3737
if (dict.size()<= UINT_MAX+1)
3838
{
3939
stamp_max_value = UINT_MAX;
40-
return stamp32;
40+
return 4;
4141
}
4242
stamp_max_value = ULONG_MAX;
43-
return stamp64;
43+
return 8;
4444
}
4545

4646
std::string
@@ -49,38 +49,30 @@ StampDict::ExtractStr(Blob &blob)
4949
unsigned long long index_oracle;
5050

5151
/* Shifting index oracle according to size of dictionary index variable*/
52-
switch (stamp.minSize())
52+
switch (stamp_size)
5353
{
5454
case 1:
5555
{
56-
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
57-
unsigned char * i = (unsigned char *) &v[0];
58-
index_oracle = * i;
56+
index_oracle = stamp8.ExtractValue(blob);
5957
break;
6058
}
6159
case 2:
6260
{
63-
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
64-
unsigned short int * i = (unsigned short int *) &v[0];
65-
index_oracle = * i;
61+
index_oracle = stamp16.ExtractValue(blob);
6662
break;
6763
}
6864
case 4:
6965
{
70-
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
71-
unsigned int * i = ( unsigned int *) &v[0];
72-
index_oracle = * i;
66+
index_oracle = stamp32.ExtractValue(blob);
7367
break;
7468
}
7569
case 8:
7670
{
77-
std::vector<char> v = blob.ShiftSingleStampBin(stamp);
78-
unsigned long long * i = ( unsigned long long *) &v[0];
79-
index_oracle = * i;
71+
index_oracle = stamp64.ExtractValue(blob);
8072
break;
8173
}
8274
default:
83-
printf("StampDict::ExtractStr: Something is really wrong\n");
75+
printf("StampDict::ExtractStr: Something is really wrong\n"); // FIXME better to throw something here :-)
8476
exit(1);
8577
}
8678
long long actual_index = ((double) index_oracle) / stamp_max_value * dict.size();

blobstamper/stamp_dict.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@
2626
#include "stamp_arithm.h"
2727
#include "dict.h"
2828

29-
class StampDict: public StampFixed
29+
class StampDict: public StampFixed, public StampBaseStr
3030
{
3131
protected:
32-
StampArithm<char> stamp8;
32+
StampArithm<unsigned char> stamp8;
3333
StampArithm<unsigned short int> stamp16;
3434
StampArithm<unsigned int> stamp32;
3535
StampArithm<unsigned long long> stamp64;
36-
StampFixed& stamp;
36+
int stamp_size;
3737
DictBase& dict;
3838
unsigned long long stamp_max_value;
3939

40-
StampFixed& GuessStamp(DictBase & dict);
40+
int ChooseStampSize(DictBase & dict);
4141

4242
public:
43-
StampDict(DictBase & dict_arg) : dict{dict_arg}, stamp{GuessStamp(dict_arg)} {};
43+
StampDict(DictBase & dict_arg) : dict{dict_arg}, stamp_size{ChooseStampSize(dict_arg)} {};
4444
std::string ExtractStr(Blob &blob) override;
45-
int minSize() override {return stamp.minSize();}
46-
int maxSize() override {return stamp.maxSize();}
45+
int minSize() override {return stamp_size;}
46+
int maxSize() override {return stamp_size;}
4747
};
4848

4949
#endif /* STAMP_DICT_H */

0 commit comments

Comments
 (0)