Skip to content

Commit 97772cd

Browse files
Fix StampLottery to have proper maxSize value
1 parent aba9b53 commit 97772cd

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

blobstamper/stamp_lottery.h

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ template<class StampT> class StampLottery: public StampT
77

88
int stored_min;
99
int init_stored_min(std::ref_vector<StampT> stamps_arg);
10+
int stored_max;
11+
int init_stored_max(std::ref_vector<StampT> stamps_arg);
1012

1113
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+
StampLottery(std::ref_vector<StampT> stamps_arg): stamps(stamps_arg),
15+
oracle_size(init_oracle_size(stamps_arg)),
16+
stored_min(init_stored_min(stamps_arg)),
17+
stored_max(init_stored_max(stamps_arg)) {};
18+
StampLottery(): stored_min(-1), stored_max(-2) {};
1419

1520
virtual int minSize() override;
1621
virtual int maxSize() override;
@@ -34,6 +39,24 @@ init_stored_min(std::ref_vector<StampT> stamps_arg)
3439
return min;
3540
}
3641

42+
template<class StampT> int
43+
StampLottery<StampT>::
44+
init_stored_max(std::ref_vector<StampT> stamps_arg)
45+
{
46+
int max = 0;
47+
48+
for(StampT & stamp : stamps)
49+
{
50+
if (stamp.maxSize() == -1)
51+
return -1;
52+
53+
if (max < stamp.maxSize())
54+
max = stamp.maxSize();
55+
}
56+
return max;
57+
}
58+
59+
3760
template<class StampT> int
3861
StampLottery<StampT>::init_oracle_size(std::ref_vector<StampT> stamps_arg)
3962
{
@@ -48,6 +71,11 @@ StampLottery<StampT>::init_oracle_size(std::ref_vector<StampT> stamps_arg)
4871
}
4972

5073

74+
75+
/* StampLottery is used for recustion. Lottery contains trams that uses this very lottery
76+
Calculating sizes on fly leads to infinite recrsion. So we calculate sizes when lottery
77+
item is added, and use stored value, when it is needed */
78+
5179
template<class StampT> int
5280
StampLottery<StampT>::minSize()
5381
{
@@ -57,7 +85,9 @@ StampLottery<StampT>::minSize()
5785
template<class StampT> int
5886
StampLottery<StampT>::maxSize()
5987
{
60-
return -1; // FIXME this is true only for recurion case. Should fix it somehow if Lottery is used in other cases
88+
if (stored_max == -1)
89+
return -1;
90+
return stored_max + oracle_size;
6191
}
6292

6393

@@ -140,10 +170,21 @@ StampLottery<StampT>::ExtractStr(Blob &blob)
140170
template<class StampT> void
141171
StampLottery<StampT>::Append(StampT & stamp)
142172
{
143-
if (stamp.minSize()<stored_min)
173+
if (stamp.maxSize() == -1)
144174
{
145-
stored_min = stamp.minSize();
175+
stored_max = -1;
176+
} else
177+
{
178+
if (stamp.maxSize() > stored_max) /* this case includes case when stored_max have not beed initialized (==-2)*/
179+
stored_max = stamp.maxSize();
146180
}
181+
182+
if (stored_min == -1) /* stored_min have not been initializes*/
183+
stored_min = stamp.minSize();
184+
185+
if (stamp.minSize() < stored_min)
186+
stored_min = stamp.minSize();
187+
147188
stamps.push_back(stamp);
148189
oracle_size = init_oracle_size(stamps);
149190
}

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