@@ -82,6 +82,22 @@ public int getMaxIndex() {
8282 throw new NoSuchElementException ();
8383 }
8484
85+ @ Override
86+ public double getTotalCount () {
87+ if (isEmpty ()) {
88+ return 0D ;
89+ }
90+ double total = 0D ;
91+ for (double [] page : pages ) {
92+ if (null != page ) {
93+ for (double count : page ) {
94+ total += count ;
95+ }
96+ }
97+ }
98+ return total ;
99+ }
100+
85101 @ Override
86102 public void add (int index , double count ) {
87103 if (count > 0 ) {
@@ -147,6 +163,84 @@ private void extendTo(int pageIndex) {
147163 this .pages = Arrays .copyOf (pages , pages .length + aligned (pageIndex - minPageIndex + 1 ));
148164 }
149165
166+ @ Override
167+ public void mergeWith (Store store ) {
168+ if (store .isEmpty ()) {
169+ return ;
170+ }
171+ if (store instanceof PaginatedStore ) {
172+ mergeWith ((PaginatedStore ) store );
173+ } else {
174+ store .getStream ().forEach (this ::add );
175+ }
176+ }
177+
178+ private void mergeWith (PaginatedStore store ) {
179+ if (isEmpty ()) {
180+ this .pages = deepCopy (store .pages );
181+ this .minPageIndex = store .minPageIndex ;
182+ } else {
183+ int min = minPageIndex ;
184+ int max = minPageIndex + pages .length ;
185+ int storeMin = store .minPageIndex ;
186+ int storeMax = store .minPageIndex + store .pages .length ;
187+ if (max < storeMin ) {
188+ extendTo (storeMax );
189+ for (int i = 0 ; i < store .pages .length ; ++i ) {
190+ double [] page = store .pages [i ];
191+ if (null != page ) {
192+ pages [i + storeMin - min ] = Arrays .copyOf (page , page .length );
193+ }
194+ }
195+ } else if (min > storeMax ) {
196+ shiftPagesRight (storeMin );
197+ for (int i = 0 ; i < store .pages .length ; ++i ) {
198+ double [] page = store .pages [i ];
199+ if (null != page ) {
200+ pages [i ] = Arrays .copyOf (page , page .length );
201+ }
202+ }
203+ } else if (min < storeMin ) {
204+ if (storeMax > max ) {
205+ extendTo (storeMax );
206+ }
207+ for (int i = 0 ; i < store .pages .length ; ++i ) {
208+ double [] page = store .pages [i ];
209+ if (null != page ) {
210+ double [] target = pages [i + storeMin - min ];
211+ if (null == target ) {
212+ pages [i + storeMin - min ] = Arrays .copyOf (page , page .length );
213+ } else {
214+ for (int j = 0 ; j < page .length ; ++j ) {
215+ target [j ] += page [j ];
216+ }
217+ }
218+ }
219+ }
220+ } else {
221+ if (min > storeMin ) {
222+ shiftPagesRight (storeMin );
223+ }
224+ if (storeMax > max ) {
225+ extendTo (storeMax );
226+ }
227+ for (int i = 0 ; i < store .pages .length ; ++i ) {
228+ double [] page = store .pages [i ];
229+ if (null != page ) {
230+ double [] target = pages [i ];
231+ if (null == target ) {
232+ pages [i ] = Arrays .copyOf (page , page .length );
233+ } else {
234+ for (int j = 0 ; j < page .length ; ++j ) {
235+ target [j ] += page [j ];
236+ }
237+ }
238+ }
239+ }
240+ }
241+ }
242+ }
243+
150244 @ Override
151245 public Store copy () {
152246 return new PaginatedStore (this );
0 commit comments