SheafSystem  0.0.0.0
binary_index_block.impl.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Interface for class binary_index_block
19 
20 #ifndef BINARY_INDEX_BLOCK_IMPL_H
21 #define BINARY_INDEX_BLOCK_IMPL_H
22 
23 #ifndef SHEAF_DLL_SPEC_H
24 #include "SheafSystem/sheaf_dll_spec.h"
25 #endif
26 
27 #ifndef BINARY_INDEX_BLOCK_H
28 #include "SheafSystem/binary_index_block.h"
29 #endif
30 
31 #ifndef BLOCK_IMPL_H
32 #include "SheafSystem/block.impl.h"
33 #endif
34 
35 namespace sheaf
36 {
37 
38 // ===========================================================
39 // ANY FACET
40 // ===========================================================
41 
42 template <typename T>
43 binary_index_block<T>&
46 {
47  // Preconditions:
48 
49 
50  // Body:
51 
52  block<T>::operator=(xother);
53 
54  // Postconditions:
55 
56 
57  // Exit:
58 
59  return *this;
60 };
61 
62 template <typename T>
63 bool
65 invariant() const
66 {
67  // Preconditions:
68 
69  // Body:
70 
71  // Postconditions:
72 
73  bool result = block<T>::invariant();
74 
75  result = result && ( _i_ub >= 0 );
76  result = result && ( _i_ct >= 0 );
77  result = result && ( _i_ct <= _i_ub );
78  result = result && ((_i_ub > 0) == (this->_base != 0));
79 
80  result = result && ( _j_ub >= 0 );
81  result = result && ( _j_ct >= 0 );
82  result = result && ( _j_ct <= _j_ub );
83  result = result && ((_j_ub > 0) == (this->_base != 0));
84 
85  // Return:
86 
87  return result;
88 }
89 
90 // ===========================================================
91 // BLOCK FACET
92 // ===========================================================
93 
94 template <typename T>
95 void
98 {
99  // Preconditions:
100 
101  // Body:
102 
103  define_old_variable(int old_ct = this->ct());
104 
105 
106  for(int i=0; i<xother.ct(); ++i)
107  {
108  push_back(xother.item(i));
109  }
110 
111  // Postconditions:
112 
113  ensure(this->ct() == old_ct + xother.ct());
114  ensure_for_all(i, 0, xother.ct(), ((*this)[i + old_ct] == xother[i]) );
115 
116  // Exit:
117 
118  return;
119 }
120 
121 // ===========================================================
122 // BINARY_INDEX_BLOCK FACET
123 // ===========================================================
124 
125 template <typename T>
127 binary_index_block(int xi_ub, int xj_ub)
128 {
129  // Preconditions:
130 
131  require(xi_ub >= 0);
132  require(xj_ub >= 0);
133 
134  // Body:
135 
136  this->_base = 0;
137 
138  _i_ub = xi_ub;
139  _j_ub = xj_ub;
140  this->_ub = xi_ub*xj_ub;
141 
142  _i_ct = 0;
143  _j_ct = 0;
144  this->_ct = 0;
145 
146  reserve(xi_ub, xj_ub);
147 
148  // Postconditions:
149 
150  ensure(invariant());
151 
152  ensure(i_ub( )>= xi_ub);
153  ensure(j_ub( )>= xj_ub);
154  ensure(this->ub() >= xi_ub*xj_ub);
155  ensure((xi_ub*xj_ub > 0) == (this->base() != 0));
156 
157  ensure(i_ct() == 0);
158  ensure(j_ct() == 0);
159  ensure(this->ct() == 0);
160 
161  // Exit:
162 
163  return;
164 };
165 
166 template <typename T>
169  : block<T>(xother)
170 {
171  // Preconditions:
172 
173  require(precondition_of(block_xother));
174 
175  // Body:
176 
177 
178  // Postconditions:
179 
180  ensure(postcondition_of(block(xother)));
181 
182  // Exit:
183 
184  return;
185 };
186 
187 
188 template <typename T>
191 {}
192 
194 template <typename T>
196 binary_index_block(int xi_ub, int xi_ct, int xj_ub, int xj_ct, const T *xitems)
197 {
198 
199  // Preconditions:
200 
201  require(xi_ub >= 0);
202  require((0 <= xi_ct) && (xi_ct <= xi_ub));
203  require((xi_ct > 0) == (xitems != 0));
204  require(xj_ub >= 0);
205  require((0 <= xj_ct) && (xj_ct <= xj_ub));
206  require((xj_ct > 0) == (xitems != 0));
207  require(unexecutable(xitems != 0 implies xitems points to array of length >= xi_ct*xj_ct));
208 
209  // Body:
210 
211  this->_base = 0;
212  this->_ct = 0;
213  this->_ub = 0;
214  _i_ub = 0;
215  _j_ub = 0;
216  reserve(xi_ub, xj_ub);
217 
218  for(int i = 0; i < xi_ct*xj_ct; ++i)
219  {
220  this->_base[i] = xitems[i];
221  }
222  _i_ct = xi_ct;
223  _j_ct = xj_ct;
224  this->_ct = _i_ct*_j_ct;
225 
226  // Postconditions:
227 
228  ensure(invariant());
229  ensure(i_ub() >= xi_ub);
230  ensure(j_ub() >= xj_ub);
231  ensure((xi_ub*xj_ub > 0) == (this->base() != 0));
232  ensure(i_ct() == xi_ct);
233  ensure(j_ct() == xj_ct);
234  ensure(this->ct() == i_ct()*j_ct());
235 }
236 
238 template <typename T>
239 int
241 i_ub() const
242 {
243  // Preconditions:
244 
245  // Body:
246 
247  int result = _i_ub;
248 
249  // Postconditions:
250 
251  ensure(invariant());
252  ensure(result >= 0);
253 
254  // Return:
255 
256  return result;
257 }
258 
259 
261 template <typename T>
262 int
264 j_ub() const
265 {
266  // Preconditions:
267 
268  // Body:
269 
270  int result = _j_ub;
271 
272  // Postconditions:
273 
274  ensure(invariant());
275  ensure(result >= 0);
276 
277  // Return:
278 
279  return result;
280 }
281 
283 template <typename T>
284 int
286 i_ct() const
287 {
288  // Preconditions:
289 
290  // Body:
291 
292  int result = _i_ct;
293 
294  // Postconditions:
295 
296  ensure(invariant());
297  ensure(result >= 0);
298 
299  // Return:
300 
301  return result;
302 }
303 
305 template <typename T>
306 int
308 j_ct() const
309 {
310  // Preconditions:
311 
312  // Body:
313 
314  int result = _j_ct;
315 
316  // Postconditions:
317 
318  ensure(invariant());
319  ensure(result >= 0);
320 
321  // Return:
322 
323  return result;
324 }
325 
327 template <typename T>
328 T &
330 item(int xi, int xj)
331 {
332 
333  // Preconditions:
334 
335  require(xi >= 0 && xi < i_ub());
336  require(xj >= 0 && xj < j_ub());
337 
338  // Body:
339 
340  int index = xj + xi*j_ub();
341  T & result = *(this->_base+index);
342 
343  // Postconditions:
344 
345  ensure(invariant());
346 
347  // Return:
348 
349  return result;
350 }
351 
353 template <typename T>
354 const T &
356 item(int xi, int xj) const
357 {
358 
359  // Preconditions:
360 
361  require(xi >= 0 && xi < i_ub());
362  require(xj >= 0 && xj < j_ub());
363 
364  // Body:
365 
366  int index = xj + xi*j_ub();
367  T & result = *(this->_base+index);
368 
369  // Postconditions:
370 
371  ensure(invariant());
372 
373  // Return:
374 
375  return result;
376 }
377 
379 template <typename T>
380 T *
382 operator [](int xi) const
383 {
384 
385  // Preconditions:
386 
387  require(xi >= 0 && xi < i_ub());
388 
389  // Body:
390 
391  int index = xi*_j_ub;
392  T* result = this->_base+index;
393 
394  // Postconditions:
395 
396  ensure(invariant());
397 
398  // Return:
399 
400  return result;
401 }
402 
404 template <typename T>
405 void
407 force_item(int xi, int xj, const T& xitem)
408 {
409 
410  // Preconditions:
411 
412  require(0 <= xi);
413  require(0 <= xj);
414 
415  // Body:
416 
417  if(xi >= _i_ub || xj >= _j_ub)
418  {
420 
421  int new_i_ub = _i_ub*2 > xi ? _i_ub*2 : xi + 1;
422  int new_j_ub = _j_ub*2 > xj ? _j_ub*2 : xj + 1;
423 
427 
428  reserve(new_i_ub, new_j_ub);
429  }
430 
431  put_item(xi, xj, xitem);
432 
433  // Postconditions:
434 
435  ensure(invariant());
436  ensure(item(xi, xj)==xitem);
437 }
438 
440 template <typename T>
441 void
443 reserve(int xi_ub, int xj_ub)
444 {
445 
446  // Preconditions:
447 
448  require(xi_ub >= 0);
449  require(xj_ub >= 0);
450 
451  // Body:
452 
453  define_old_variable(int old_i_ct = _i_ct);
454  define_old_variable(int old_i_ub = _i_ub);
455  define_old_variable(int old_j_ct = _j_ct);
456  define_old_variable(int old_j_ub = _j_ub);
457 
458  reserve(xi_ub*xj_ub);
459 
460  if(xi_ub > _i_ub)
461  {
462  _i_ub = xi_ub;
463  }
464 
465  if(xj_ub > _j_ub)
466  {
467  _j_ub = xj_ub;
468  }
469 
470  // Postconditions:
471 
472  ensure(invariant());
473  ensure(i_ct() == old_i_ct);
474  ensure(i_ub() >= old_i_ub);
475  ensure(i_ub() >= xi_ub);
476  ensure((i_ub() > 0) == (this->base() != 0));
477  ensure(j_ct() == old_j_ct);
478  ensure(j_ub() >= old_j_ub);
479  ensure(j_ub() >= xj_ub);
480  ensure((j_ub() > 0) == (this->base() != 0));
481  ensure(this->ub() >= xi_ub*xj_ub);
482  ensure(this->ub() == i_ub()*j_ub());
483 }
484 
486 template <typename T>
487 void
489 set_ct(int xi_ct, int xj_ct)
490 {
491 
492  // Preconditions:
493 
494  require(0 <= xi_ct && xi_ct <= i_ub());
495  require(0 <= xj_ct && xj_ct <= j_ub());
496 
497  // Body:
498 
499  _i_ct = xi_ct;
500  _j_ct = xj_ct;
501  set_ct(xi_ct*xj_ct);
502 
503  // Postconditions:
504 
505  ensure(i_ct() == xi_ct);
506  ensure(j_ct() == xj_ct);
507 }
508 
510 template <typename T>
511 void
513 put_item(int xi, int xj, const T& xitem)
514 {
515 
516  // Preconditions:
517 
518  require(0 <= xi && xi < i_ub());
519  require(0 <= xj && xj < j_ub());
520 
521  // Body:
522 
523  int index = xj + xi*j_ub();
524  *(this->_base+index) = xitem;
525 
526  // Postconditions:
527 
528  ensure(invariant());
529  ensure(item(xi, xj)==xitem);
530 }
531 
532 
534 template <typename T>
535 void
537 assign(const T& xitem)
538 {
539  // Preconditions:
540 
541  // Body:
542 
543  define_old_variable(int old_ct = i_ct()*j_ct());
544 
545  for(int i=0; i<i_ct()*j_ct(); i++)
546  {
547  this->_base[i] = xitem;
548  }
549 
550  // Postconditions:
551 
552  ensure(invariant());
553  ensure(i_ct()*j_ct() == old_ct);
554  ensure_for_all(i, 0, i_ct()*j_ct(), item(i) == xitem);
555 
556 }
557 
558 // ===========================================================
559 // NON-MEMBER FUNCTIONS
560 // ===========================================================
561 
563 template <typename T>
564 std::ostream&
565 operator << (std::ostream& xos, const binary_index_block<T>& xblk)
566 {
567  for(int i=0; i<xblk.i_ct(); ++i)
568  {
569  for(int j=0; j<xblk.j_ct(); ++j)
570  {
571  xos << " " << xblk.item(i,j);
572  }
573  xos << '\n';
574  }
575 
576  return xos;
577 }
578 
579 } // namespace sheaf
580 
581 #endif // ifndef BINARY_INDEX_BLOCK_IMPL_H
index_type ub() const
The upper bound on the storage array. The number of items current allocated in the storage array...
size_type ct() const
The number of items currently in use.
void put_item(int xi, int xj, const T &xitem)
Sets the item xitem at index xi, xj to xitem, but will not resize.
block & operator=(const block &xother)
Assignment operator.
Definition: block.impl.h:99
int i_ct() const
The number of i index values currently in use.
block(index_type xub=0)
Creates an instance with ub() == xub; storage is uninitialized.
Definition: block.impl.h:42
SHEAF_DLL_SPEC vd_value_type length(const ed &x0, bool xauto_access)
The Euclidean length (magnitude) of x0 (version for persistent types).
Definition: ed.cc:906
void push_back(const binary_index_block &xother)
Appends xother to the back of this.
T * operator[](int xi) const
The address of the first item in row xi. /.
pointer_type base() const
The underlying storage array.
void reserve(int xi_ub, int xj_ub)
Makes i_ub() at least xi_ub and j_ub() at least xj_ub.
int j_ub() const
The upper bound for the second index.
void assign(const T &xitem)
Sets the values of all items to xitem.
int j_ct() const
The number of j index values currently in use.
int _j_ub
The upper bound for the second index.
size_type _ct
The number of items currently stored.
Definition: auto_block.h:316
int _i_ub
The upper bound for the first index.
void set_ct(int xi_ct, int xj_ct)
Sets i_ct() == xi_ct and j_ct() == xj_ct.
A block which can be accessed using two indices, similarly to a two index array.
void force_item(int xi, int xj, const T &xitem)
Puts the item xitem at index xi,xj, resizing if necessary.
int _i_ct
The number of i index values currently in use.
int i_ub() const
The upper bound for the first index.
Namespace for the sheaves component of the sheaf system.
pointer_type _base
Start of storage for this.
Definition: auto_block.h:306
binary_index_block & operator=(const binary_index_block &xother)
Sets the contents of this to the contents of xother.
T & item(int xi, int xj)
The item at index xi, xj (mutable version).
An auto_block with a no-initialization initialization policy.
int _j_ct
The number of j index values currently in use.
binary_index_block(int xi_ub=0, int xj_ub=0)
Default constructor.