@@ -7,10 +7,15 @@ template<class StampT> class StampLottery: public StampT
7
7
8
8
int stored_min;
9
9
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);
10
12
11
13
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 ) {};
14
19
15
20
virtual int minSize () override ;
16
21
virtual int maxSize () override ;
@@ -34,6 +39,24 @@ init_stored_min(std::ref_vector<StampT> stamps_arg)
34
39
return min;
35
40
}
36
41
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
+
37
60
template <class StampT > int
38
61
StampLottery<StampT>::init_oracle_size(std::ref_vector<StampT> stamps_arg)
39
62
{
@@ -48,6 +71,11 @@ StampLottery<StampT>::init_oracle_size(std::ref_vector<StampT> stamps_arg)
48
71
}
49
72
50
73
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
+
51
79
template <class StampT > int
52
80
StampLottery<StampT>::minSize()
53
81
{
@@ -57,7 +85,9 @@ StampLottery<StampT>::minSize()
57
85
template <class StampT > int
58
86
StampLottery<StampT>::maxSize()
59
87
{
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;
61
91
}
62
92
63
93
@@ -140,10 +170,21 @@ StampLottery<StampT>::ExtractStr(Blob &blob)
140
170
template <class StampT > void
141
171
StampLottery<StampT>::Append(StampT & stamp)
142
172
{
143
- if (stamp.minSize ()<stored_min )
173
+ if (stamp.maxSize () == - 1 )
144
174
{
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 ();
146
180
}
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
+
147
188
stamps.push_back (stamp);
148
189
oracle_size = init_oracle_size (stamps);
149
190
}
0 commit comments