Skip to content

Commit c7f6510

Browse files
Move StampLotery to poper file in the lib
1 parent b139576 commit c7f6510

File tree

5 files changed

+156
-156
lines changed

5 files changed

+156
-156
lines changed

blobstamper/blobstamper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
#include "dict.h"
2424
#include "galley.h"
2525
#include "stamp_enumerator.h"
26-
26+
#include "stamp_lottery.h"

blobstamper/helpers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ max precision
9595
}
9696

9797

98+
namespace std
99+
{
100+
template<class T> using ref_vector = vector<reference_wrapper<T>>;
101+
}
102+
103+
98104
template<class T> class sized_ptr
99105
{
100106
private:

blobstamper/stamp_lottery.cpp

Whitespace-only changes.

blobstamper/stamp_lottery.h

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
template<class StampT> class StampLottery: public StampT
2+
{
3+
protected:
4+
std::ref_vector<StampT> stamps;
5+
int oracle_size;
6+
int init_oracle_size(std::ref_vector<StampT> stamps_arg);
7+
8+
int stored_min;
9+
int init_stored_min(std::ref_vector<StampT> stamps_arg);
10+
11+
public:
12+
StampLottery(std::ref_vector<StampT> stamps_arg): stamps(stamps_arg), oracle_size(init_oracle_size(stamps_arg)), stored_min(init_stored_min(stamps_arg)) {};
13+
StampLottery(): stored_min(-1) {};
14+
15+
virtual int minSize() override;
16+
virtual int maxSize() override;
17+
virtual std::string ExtractStr(Blob &blob) override;
18+
void Append(StampT & stamp);
19+
};
20+
21+
22+
template<class StampT> int
23+
StampLottery<StampT>::
24+
init_stored_min(std::ref_vector<StampT> stamps_arg)
25+
{
26+
int min = std::numeric_limits<int>::max();
27+
28+
for(StampT & stamp : stamps)
29+
{
30+
31+
if (min > stamp.minSize())
32+
min = stamp.minSize();
33+
}
34+
return min;
35+
}
36+
37+
template<class StampT> int
38+
StampLottery<StampT>::init_oracle_size(std::ref_vector<StampT> stamps_arg)
39+
{
40+
unsigned long size = stamps_arg.size();
41+
if (size < std::numeric_limits<unsigned char>::max())
42+
return 1;
43+
if (size < std::numeric_limits<unsigned short int>::max())
44+
return 2;
45+
if (size < std::numeric_limits<unsigned int>::max())
46+
return 4;
47+
return 8;
48+
}
49+
50+
51+
template<class StampT> int
52+
StampLottery<StampT>::minSize()
53+
{
54+
return stored_min + oracle_size;
55+
}
56+
57+
template<class StampT> int
58+
StampLottery<StampT>::maxSize()
59+
{
60+
return -1; // FIXME this is true only for recurion case. Should fix it somehow if Lottery is used in other cases
61+
}
62+
63+
64+
template<class StampT> std::string
65+
StampLottery<StampT>::ExtractStr(Blob &blob)
66+
{
67+
unsigned long oracle;
68+
unsigned long oracle_max;
69+
70+
switch (oracle_size)
71+
{
72+
case 1:
73+
{
74+
StampArithm<unsigned char> stamp;
75+
oracle = stamp.ExtractValue(blob);
76+
oracle_max = std::numeric_limits<unsigned char>::max();
77+
break;
78+
}
79+
case 2:
80+
{
81+
StampArithm<unsigned short> stamp;
82+
oracle = stamp.ExtractValue(blob);
83+
oracle_max = std::numeric_limits<unsigned short>::max();
84+
break;
85+
}
86+
case 4:
87+
{
88+
StampArithm<unsigned int> stamp;
89+
oracle = stamp.ExtractValue(blob);
90+
oracle_max = std::numeric_limits<unsigned int>::max();
91+
break;
92+
}
93+
case 8:
94+
{
95+
StampArithm<unsigned long> stamp;
96+
oracle = stamp.ExtractValue(blob);
97+
oracle_max = std::numeric_limits<unsigned long>::max();
98+
break;
99+
}
100+
default:
101+
abort(); // Should never get here
102+
}
103+
104+
/* Actually we use only stamps that short enogh to consume blob's available data*/
105+
std::ref_vector<StampT> actual_stamps;
106+
for(StampT & stamp : stamps)
107+
{
108+
if(blob.Size() < stamp.minSize()) // Skip all stamps that dose not fit
109+
continue;
110+
if ( stamp.isUnbounded() || // Unbounded is always ok
111+
stamp.maxSize() > blob.Size() || // Variated that can consume all data is ok
112+
stamp.minSize() * 2 > blob.Size() // Fixed or variated stamp that lefts less data then it's min size will also do
113+
)
114+
{
115+
actual_stamps.push_back(stamp);
116+
}
117+
}
118+
if (actual_stamps.empty())
119+
{
120+
// Add just everything that fits
121+
for(StampT & stamp : stamps)
122+
{
123+
if(blob.Size() < stamp.minSize()) // Skip all stamps that dose not fit
124+
continue;
125+
actual_stamps.push_back(stamp);
126+
}
127+
}
128+
129+
if (actual_stamps.empty())
130+
throw OutOfData(); // This should never happen
131+
132+
long long index = ((double) oracle) / oracle_max * actual_stamps.size();
133+
if ( index == actual_stamps.size()) index--; /* If we hit the boundary step inside a bit*/
134+
135+
StampT& stamp = actual_stamps[index];
136+
return stamp.ExtractStr(blob);
137+
}
138+
139+
140+
template<class StampT> void
141+
StampLottery<StampT>::Append(StampT & stamp)
142+
{
143+
if (stamp.minSize()<stored_min)
144+
{
145+
stored_min = stamp.minSize();
146+
}
147+
stamps.push_back(stamp);
148+
oracle_size = init_oracle_size(stamps);
149+
}

examples/exampleZZ.cpp

Lines changed: 0 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -7,161 +7,6 @@
77

88
#include<blobstamper/blobstamper.h>
99

10-
namespace std
11-
{
12-
template<class T> using ref_vector = vector<reference_wrapper<T>>;
13-
}
14-
15-
template<class StampT> class StampLottery: public StampT
16-
{
17-
protected:
18-
std::ref_vector<StampT> stamps;
19-
int oracle_size;
20-
int init_oracle_size(std::ref_vector<StampT> stamps_arg);
21-
22-
int stored_min;
23-
int init_stored_min(std::ref_vector<StampT> stamps_arg);
24-
25-
public:
26-
StampLottery(std::ref_vector<StampT> stamps_arg): stamps(stamps_arg), oracle_size(init_oracle_size(stamps_arg)), stored_min(init_stored_min(stamps_arg)) {};
27-
StampLottery(): stored_min(-1) {};
28-
29-
virtual int minSize() override;
30-
virtual int maxSize() override;
31-
virtual std::string ExtractStr(Blob &blob) override;
32-
void Append(StampT & stamp);
33-
};
34-
35-
36-
template<class StampT> int
37-
StampLottery<StampT>::
38-
init_stored_min(std::ref_vector<StampT> stamps_arg)
39-
{
40-
int min = std::numeric_limits<int>::max();
41-
42-
for(StampT & stamp : stamps)
43-
{
44-
45-
if (min > stamp.minSize())
46-
min = stamp.minSize();
47-
}
48-
return min;
49-
}
50-
51-
template<class StampT> int
52-
StampLottery<StampT>::init_oracle_size(std::ref_vector<StampT> stamps_arg)
53-
{
54-
unsigned long size = stamps_arg.size();
55-
if (size < std::numeric_limits<unsigned char>::max())
56-
return 1;
57-
if (size < std::numeric_limits<unsigned short int>::max())
58-
return 2;
59-
if (size < std::numeric_limits<unsigned int>::max())
60-
return 4;
61-
return 8;
62-
}
63-
64-
65-
template<class StampT> int
66-
StampLottery<StampT>::minSize()
67-
{
68-
return stored_min + oracle_size;
69-
}
70-
71-
template<class StampT> int
72-
StampLottery<StampT>::maxSize()
73-
{
74-
return -1; // FIXME this is true only for recurion case. Should fix it somehow if Lottery is used in other cases
75-
}
76-
77-
78-
template<class StampT> std::string
79-
StampLottery<StampT>::ExtractStr(Blob &blob)
80-
{
81-
unsigned long oracle;
82-
unsigned long oracle_max;
83-
84-
switch (oracle_size)
85-
{
86-
case 1:
87-
{
88-
StampArithm<unsigned char> stamp;
89-
oracle = stamp.ExtractValue(blob);
90-
oracle_max = std::numeric_limits<unsigned char>::max();
91-
break;
92-
}
93-
case 2:
94-
{
95-
StampArithm<unsigned short> stamp;
96-
oracle = stamp.ExtractValue(blob);
97-
oracle_max = std::numeric_limits<unsigned short>::max();
98-
break;
99-
}
100-
case 4:
101-
{
102-
StampArithm<unsigned int> stamp;
103-
oracle = stamp.ExtractValue(blob);
104-
oracle_max = std::numeric_limits<unsigned int>::max();
105-
break;
106-
}
107-
case 8:
108-
{
109-
StampArithm<unsigned long> stamp;
110-
oracle = stamp.ExtractValue(blob);
111-
oracle_max = std::numeric_limits<unsigned long>::max();
112-
break;
113-
}
114-
default:
115-
abort(); // Should never get here
116-
}
117-
118-
/* Actually we use only stamps that short enogh to consume blob's available data*/
119-
std::ref_vector<StampT> actual_stamps;
120-
for(StampT & stamp : stamps)
121-
{
122-
if(blob.Size() < stamp.minSize()) // Skip all stamps that dose not fit
123-
continue;
124-
if ( stamp.isUnbounded() || // Unbounded is always ok
125-
stamp.maxSize() > blob.Size() || // Variated that can consume all data is ok
126-
stamp.minSize() * 2 > blob.Size() // Fixed or variated stamp that lefts less data then it's min size will also do
127-
)
128-
{
129-
actual_stamps.push_back(stamp);
130-
}
131-
}
132-
if (actual_stamps.empty())
133-
{
134-
// Add just everything that fits
135-
for(StampT & stamp : stamps)
136-
{
137-
if(blob.Size() < stamp.minSize()) // Skip all stamps that dose not fit
138-
continue;
139-
actual_stamps.push_back(stamp);
140-
}
141-
}
142-
143-
if (actual_stamps.empty())
144-
throw OutOfData(); // This should not happen
145-
146-
long long index = ((double) oracle) / oracle_max * actual_stamps.size();
147-
if ( index == actual_stamps.size()) index--; /* If we hit the boundary step inside a bit*/
148-
149-
StampT& stamp = actual_stamps[index];
150-
return stamp.ExtractStr(blob);
151-
}
152-
153-
154-
template<class StampT> void
155-
StampLottery<StampT>::Append(StampT & stamp)
156-
{
157-
if (stamp.minSize()<stored_min)
158-
{
159-
stored_min = stamp.minSize();
160-
}
161-
stamps.push_back(stamp);
162-
oracle_size = init_oracle_size(stamps);
163-
}
164-
16510

16611
class BinaryOp: public StampBaseStr, public GalleySetBase
16712
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy