@@ -7,10 +7,15 @@ template<class StampT> class StampLottery: public StampT
77
88int stored_min;
99int 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
1113public:
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
1520virtual int minSize ()override ;
1621virtual int maxSize ()override ;
@@ -34,6 +39,24 @@ init_stored_min(std::ref_vector<StampT> stamps_arg)
3439return 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+
3760template <class StampT >int
3861StampLottery<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+
5179template <class StampT >int
5280StampLottery<StampT>::minSize()
5381{
@@ -57,7 +85,9 @@ StampLottery<StampT>::minSize()
5785template <class StampT >int
5886StampLottery<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)
140170template <class StampT >void
141171StampLottery<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}