SheafSystem  0.0.0.0
interval_index_space_record.cc
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 // Implementation for class interval_index_space_record
19 
20 #include "SheafSystem/interval_index_space_record.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/error_message.h"
24 #include "SheafSystem/member_record_set.h"
25 #include "SheafSystem/interval_index_space_iterator.h"
26 
27 //#define DIAGNOSTIC_OUTPUT
28 // #undef DIAGNOSTIC_OUTPUT
29 
30 // ===========================================================
31 // INTERVAL_INDEX_SPACE_RECORD FACET
32 // ===========================================================
33 
37  : variable_length_record(xhost.scaffold())
38 {
39 
40  // Preconditions:
41 
42  // Body:
43 
44  // Neither buffer is initialized.
45 
46  put_is_internal(false);
47  put_is_external(false);
48 
49  // Postconditions:
50 
51  ensure(invariant());
52  ensure(buf() == 0);
53  ensure(buf_ub() == 0);
54  ensure(!is_internal());
55  ensure(!is_external());
56 
57  // Exit:
58 
59  return;
60 }
61 
65  : variable_length_record(xother)
66 {
67 
68  // Preconditions:
69 
70  // Body:
71 
72  not_implemented();
73 
74  // Postconditions:
75 
76  ensure(invariant());
77 }
78 
82 {
83 
84  // Preconditions:
85 
86  // Body:
87 
88  // Nothing specific to do.
90 
91  // Postconditions:
92 
93  // Exit:
94 
95  return;
96 }
97 
98 // ===========================================================
99 // WRITE FACET
100 // ===========================================================
101 
103 void
105 externalize(hvl_t* xhdf_buffer, const interval_index_space_handle& xid_space)
106 {
107 
108  // Preconditions:
109 
110  require(scaffold().structure().state_is_read_accessible());
111 
112  // Body:
113 
114 #ifdef DIAGNOSTIC_OUTPUT
115  cout << "interval_index_space_record::externalize:" << endl;
116 #endif
117 
118  // Initialize the internal buffer from the id space.
119 
120  transfer_id_space_to_internal_buffer();
121 
122  // Convert the internal buffer to external form
123  // and place the result in the external buffer.
124 
125  convert_internal_buffer_to_external_buffer(xid_space);
126 
127  // Transfer the external buffer to HDF
128 
129  transfer_external_buffer_to_HDF(xhdf_buffer);
130 
131  // Postconditions:
132 
133  // Exit
134 
135  return;
136 }
137 
139 void
140 sheaf::interval_index_space_record::
141 transfer_id_space_to_internal_buffer()
142 {
143  // Preconditions:
144 
145  require(scaffold().structure().state_is_read_accessible());
146 
147  // Body:
148 
149  // Nothing to do; internal buffer is the id space itself.
150 
151  // Now the internal buffer is initialized;
152 
153  put_is_internal(true);
154 
155  // Postconditions:
156 
157  ensure(is_internal());
158 
159  // Exit
160 
161  return;
162 }
163 
164 void
165 sheaf::interval_index_space_record::
166 convert_internal_buffer_to_external_buffer(const interval_index_space_handle& xid_space)
167 {
168  // Preconditions:
169 
170  // Body:
171 
172  // Initialize the external buffer from the internal buffer.
173 
174  initialize_external_buffer(xid_space);
175 
176  // Externalize all the data fields.
177 
178  size_t lnext_field_offset = 0;
179 
180  externalize_id_space_data(lnext_field_offset, xid_space);
181 
182  // Now the external buffer is valid.
183 
184  put_is_external(true);
185 
186  // Postconditions:
187 
188  ensure(is_external());
189 
190  // Exit
191 
192  return;
193 }
194 
196 void
197 sheaf::interval_index_space_record::
198 initialize_external_buffer(const interval_index_space_handle& xid_space)
199 {
200  // Preconditions:
201 
202 
203  // Body:
204 
205  // Determine the size of the external buffer.
206 
207  size_type lend_offset = 0;
208 
210  dynamic_cast<interval_index_space_iterator&>(xid_space.get_iterator());
211  while(!litr.is_done())
212  {
213  lend_offset += 4;
214  litr.next_interval();
215  }
216  xid_space.release_iterator(litr);
217 
218  // Allocate the external buffer.
219  // Buffer will be deleted in member_record_set::delete_external_buffers.
220 
221  void* lbuf = lend_offset > 0 ? new pod_index_type[lend_offset] : 0;
222 
223  put_buf(lbuf, lend_offset);
224 
225  // Buffer will not be deleted in destructor for this.
226 
227  put_delete_buffer(false);
228 
229  // The external buffer is allocated but not initialized.
230 
231  put_is_external(false);
232 
233  // Postconditions:
234 
235  ensure(!is_external());
236 
237  // Exit
238 
239  return;
240 }
241 
243 void
244 sheaf::interval_index_space_record::
245 externalize_id_space_data(size_t& xnext_field_offset, const interval_index_space_handle& xid_space)
246 {
247 
248  // Preconditions:
249 
250  require(is_internal());
251 
252  // Body:
253 
254  define_old_variable(size_t old_xnext_field_offset = xnext_field_offset);
255 
256 #ifdef DIAGNOSTIC_OUTPUT
257  cout << scaffold().structure().name()
258  << " file_id_space: " << endl;
259  xid_space.print_map_rep(cout);
260  cout << xid_space << endl;
261 #endif
262 
264  dynamic_cast<interval_index_space_iterator&>(xid_space.get_iterator());
265  while(!litr.is_done())
266  {
267  int_buf()[xnext_field_offset++] = litr.interval_begin();
268  int_buf()[xnext_field_offset++] = litr.interval_end();
269  int_buf()[xnext_field_offset++] = xid_space.hub_pod(litr.interval_begin());
270  int_buf()[xnext_field_offset++] = xid_space.hub_pod(litr.interval_end());
271  litr.next_interval();
272 
273 #ifdef DIAGNOSTIC_OUTPUT
274  pod_index_type* lbuf = int_buf() + old_xnext_field_offset;
275  cout << setw(12) << lbuf[0]
276  << setw(12) << lbuf[1]
277  << setw(12) << lbuf[2]
278  << setw(12) << lbuf[3]
279  << endl;
280 #endif
281 
282  }
283  xid_space.release_iterator(litr);
284 
285  // Postconditions:
286 
287  ensure(xnext_field_offset == buf_ub());
288 
289  // Exit
290 
291  return;
292 }
293 
295 void
296 sheaf::interval_index_space_record::
297 transfer_external_buffer_to_HDF(hvl_t* xbuf)
298 {
299  // Preconditions:
300 
301  require(xbuf != 0);
302  require(is_external());
303 
304  // Body:
305 
306  xbuf->p = buf();
307  xbuf->len = buf_ub();
308 
309  // Postconditions:
310 
311  ensure(xbuf->p == buf());
312  ensure(xbuf->len == buf_ub());
313 
314  // Exit
315 
316  return;
317 }
318 
319 // ===========================================================
320 // READ FACET
321 // ===========================================================
322 
324 void
326 internalize(hvl_t* xhdf_buffer, interval_index_space_handle& xid_space)
327 {
328 
329  // Preconditions:
330 
331  require(scaffold().structure().state_is_read_write_accessible());
332 
333  // Body:
334 
335 #ifdef DIAGNOSTIC_OUTPUT
336  cout << "interval_index_space_record::internalize:" << endl;
337 #endif
338 
339  // Initialize the external buffer from the HDF buffer.
340 
341  transfer_HDF_to_external_buffer(xhdf_buffer);
342 
343  // Convert the external buffer to internal format
344  // and place the result in the internal buffer
345 
346  convert_external_buffer_to_internal_buffer(xid_space);
347 
348  // Transfer the internal buffer to the member.
349 
350  transfer_internal_buffer_to_id_space();
351 
352  // Postconditions:
353 
354  // Exit
355 
356  return;
357 }
358 
360 void
361 sheaf::interval_index_space_record::
362 transfer_HDF_to_external_buffer(hvl_t* xbuf)
363 {
364  // Preconditions:
365 
366  require(xbuf != 0);
367 
368  // Body:
369 
370  put_buf(xbuf->p, xbuf->len);
371 
372  put_is_external(true);
373  put_is_internal(false);
374 
375  // Postconditions:
376 
377  ensure(buf() == xbuf->p);
378  ensure(buf_ub() == xbuf->len);
379  ensure(is_external());
380  ensure(!is_internal());
381 
382  // Exit
383 
384  return;
385 }
386 
388 void
389 sheaf::interval_index_space_record::
390 convert_external_buffer_to_internal_buffer(interval_index_space_handle& xid_space)
391 {
392  // Preconditions:
393 
394  // Body:
395 
396  size_t lnext_field_offset = 0;
397 
398  // Internalize all the data fields.
399 
400  internalize_id_space_data(lnext_field_offset, xid_space);
401 
402  // Now the internal buffer is valid.
403 
404  put_is_internal(true);
405 
406  // Postconditions:
407 
408  ensure(is_internal());
409 
410  // Exit
411 
412  return;
413 }
414 
416 void
417 sheaf::interval_index_space_record::
418 internalize_id_space_data(size_t& xnext_field_offset, interval_index_space_handle& xid_space)
419 {
420 
421  // Preconditions:
422 
423  require(is_external());
424 
425  // Body:
426 
427  define_old_variable(size_t old_xnext_field_offset = xnext_field_offset);
428 
429  pod_index_type* lbuf = int_buf();
430  while(xnext_field_offset < buf_ub())
431  {
432 
433 #ifdef DIAGNOSTIC_OUTPUT
434  cout << setw(12) << lbuf[0]
435  << setw(12) << lbuf[1]
436  << setw(12) << lbuf[2]
437  << setw(12) << lbuf[3]
438  << endl;
439 #endif
440 
441  xid_space.insert_interval(lbuf[0], lbuf[1], lbuf[2], lbuf[3]);
442  xnext_field_offset += 4;
443  lbuf += 4;
444  }
445 
446 
447 #ifdef DIAGNOSTIC_OUTPUT
448  cout << scaffold().structure().name()
449  << " file_id_space: " << endl;
450  xid_space.print_map_rep(cout);
451  cout << xid_space << endl;
452 #endif
453 
454  // Postconditions:
455 
456  ensure(xnext_field_offset == buf_ub());
457 
458  // Exit
459 
460  return;
461 }
462 
464 void
465 sheaf::interval_index_space_record::
466 transfer_internal_buffer_to_id_space()
467 {
468  // Preconditions:
469 
470  require(scaffold().structure().state_is_read_write_accessible());
471 
472  // Body:
473 
474  // Nothing to do; the internal buffer is the id space itself;
475 
476  // Postconditions:
477 
478  // Exit
479 
480  return;
481 }
482 
483 // ===========================================================
484 // ANY FACET
485 // ===========================================================
486 
490 clone() const
491 {
493 
494  // Preconditions:
495 
496  // Body:
497 
498  result = new interval_index_space_record(*this);
499 
500  // Postconditions:
501 
502  ensure(result != 0);
503  ensure(is_same_type(result));
504 
505  // Exit:
506 
507  return result;
508 }
509 
511 bool
513 invariant() const
514 {
515  bool result = true;
516 
517  // Preconditions:
518 
519  // Body:
520 
521  // Must satisfy base class invariant
522 
523  result = result && variable_length_record::invariant();
524 
525  if(invariant_check())
526  {
527  // Prevent recursive calls to invariant
528 
530 
531  // Finished, turn invariant checking back on.
532 
534  }
535 
536  // Postconditions:
537 
538  // Exit
539 
540  return result;
541 }
542 
544 bool
546 is_ancestor_of(const any* other) const
547 {
548 
549  // Preconditions:
550 
551  require(other != 0);
552 
553  // Body:
554 
555  // True if other conforms to this
556 
557  bool result = dynamic_cast<const interval_index_space_record*>(other) != 0;
558 
559  // Postconditions:
560 
561  return result;
562 
563 }
564 
565 
An implementation of class scattered_insertion_index_space_handle that has a interval id space state...
virtual bool invariant() const
Class invariant.
poset_scaffold & scaffold()
The scaffold for the poset associated with this record (mutable version).
Definition: record.h:112
void * buf() const
The buffer.
void next_interval()
Advances the iteration to the beginning of the next interval.
void internalize(hvl_t *xhdf_buffer, interval_index_space_handle &xid_space)
Converts the record from external to internal form.
The general variable length record wrapper/adapter for transferring data between the kernel and the i...
void put_delete_buffer(bool xval)
Sets delete_buffer to value xval.
void put_buf(const void *xbuf, size_t xub)
Sets the buffer to xbuf.
virtual index_space_iterator & get_iterator() const
Allocates an id space iterator from the iterator pool.
virtual bool invariant() const
Class invariant.
Abstract base class with useful features for all objects.
Definition: any.h:39
A record_set containing records of type member_record.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
pod_type interval_begin() const
The begin of the current interval.
bool is_done() const
True if iteration is finished.
void insert_interval(pod_type xbegin, pod_type xend, const scoped_index &xhub_begin, const scoped_index &xhub_end)
Make the closed interval [xbegin, xend] equivalent to [xbegin.hub_pod(), xhub_end.hub_pod()]. synonym for insert_interval(xbegin, xend, xhub_begin.hub_pod(), xhub_end.hub_pod()).
A record buffer for transferring file id id space data for the member record data set between the ker...
size_t buf_ub() const
The size of the buffer, in bytes.
pod_type interval_end() const
The end of the current interval.
bool is_internal() const
True if the internal buffer has been initialized.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
An iterator over an id space in which the equivalence between the ids in the space and the hub id spa...
void put_is_internal(bool xis_internal)
Sets is_internal to xis_internal.
virtual std::string name() const
The name of this poset.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
poset_state_handle & structure()
The handle for the poset being transferred. (Name chosen to void name conflict with class poset...
void externalize(hvl_t *xhdf_buffer, const interval_index_space_handle &xid_space)
Converts the record from internal to external form.
pod_type hub_pod(pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in this id space; synonym for unglued_hub...
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
interval_index_space_record(member_record_set &xhost)
Creates an instance in record set xhost for reading or writing member data.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
bool is_external() const
True if the external buffer has been initialized.
virtual void release_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
virtual interval_index_space_record * clone() const
Virtual constructor; makes a new instance of the same type as this.
void put_is_external(bool xis_external)
Sets is_external to xis_external.
void print_map_rep(std::ostream &xos) const
Inserts the map representation into ostream xos.