SheafSystem  0.0.0.0
subposet_state.cc
Go to the documentation of this file.
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 
20 
21 #include "SheafSystem/subposet_state.h"
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/std_iomanip.h"
24 #include "SheafSystem/zn_to_bool.h"
25 
26 using namespace std;
27 
28 // ===========================================================
29 // SUBPOSET_STATE FACET
30 // ===========================================================
31 
32 // PUBLIC DATA MEMBERS
33 
36 {
37  // Preconditions:
38 
39  // Body:
40 
41  _members = 0;
42  _index.invalidate();
43  _is_dof_subposet = false;
44  _dof_ct = 0;
45  _dof_descriptors = 0;
46  _id_space = 0;
47  _is_persistent = true;
48 
49  // Postconditions:
50 
51  ensure(!has_id_space());
52  ensure(is_persistent());
53  ensure(is_terminated());
54 }
55 
58 {
59  // Preconditions:
60 
61  // Body:
62 
63  terminate();
64 
65  // Postconditions:
66 
67  ensure(is_terminated());
68 
69  return;
70 }
71 
72 void
74 initialize(int xmember_ub, const scoped_index& xindex, bool xinitialize)
75 {
76 
77  // Preconditions:
78 
79  require(xmember_ub >= 0);
80  require(xindex.is_valid());
81  require(unexecutable("for all i in xmembers: 0 <= i < xmember_ub"));
82 
83  // Body:
84 
85  _members = new zn_to_bool(xmember_ub);
86 
87  if(xinitialize)
88  {
89  // initialize to empty
90 
91  _members->make_false_sa();
92  }
93  else
94  {
95  // Do not initialize; leave membership undefined.
96  // Presumably client will initialize before using.
97  // Intended mostly for use by class storage_agent.
98  }
99 
100  _index = xindex;
101 
102  // Postconditions:
103 
104  ensure(is_initialized());
105  ensure(_members->ub() >= xmember_ub);
106  ensure(_index == xindex);
107  ensure(is_persistent());
108 
109  // Exit
110 
111  return;
112 }
113 
114 void
116 initialize(int xmember_ub,
117  const scoped_index& xindex,
118  const block<pod_index_type>& xmembers)
119 {
120 
121  // Preconditions:
122 
123  require(xmember_ub >= 0);
124  require(xindex.is_valid());
125  require(unexecutable("for all i in xmembers: 0 <= i < xmember_ub"));
126 
127  // Body:
128 
129  _members = new zn_to_bool(xmember_ub);
130 
131  if(xmember_ub > 0)
132  {
133  // Initialize with client provided members.
134 
135  for(int i=0; i<xmembers.ct(); ++i)
136  {
137  _members->put(xmembers[i], true);
138  }
139  }
140 
141  _index = xindex;
142 
143  // Postconditions:
144 
145  ensure(is_initialized());
146  ensure(_members->ub() >= xmember_ub);
147  ensure(_index == xindex);
148  ensure(is_persistent());
149 
150  // Exit
151 
152  return;
153 }
154 
155 void
157 initialize(int xmember_ub,
158  const scoped_index& xindex,
159  const block<scoped_index>& xmembers)
160 {
161 
162  // Preconditions:
163 
164  require(xmember_ub >= 0);
165  require(xindex.is_valid());
166  require(unexecutable("for all i in xmembers: 0 <= i < xmember_ub"));
167 
168  // Body:
169 
170  _members = new zn_to_bool(xmember_ub);
171 
172  if(xmember_ub > 0)
173  {
174  // Initialize with client provided members.
175 
176  for(int i=0; i<xmembers.ct(); ++i)
177  {
178  _members->put(xmembers[i].hub_pod(), true);
179  }
180  }
181 
182  _index = xindex;
183 
184  // Postconditions:
185 
186  ensure(is_initialized());
187  ensure(_members->ub() >= xmember_ub);
188  ensure(_index == xindex);
189  ensure(is_persistent());
190 
191  // Exit
192 
193  return;
194 }
195 
196 bool
199 {
200  bool result = true;
201 
202  // Preconditions:
203 
204  // Body:
205 
206  result = result && _members != 0;
207  result = result && _index.is_valid();
208 
209  // Postconditions:
210 
211  ensure(_members != 0);
212  ensure(_index.is_valid());
213 
214  // Exit
215 
216  return result;
217 }
218 
219 void
222 {
223  // Preconditions:
224 
225  // Body:
226 
227  if(_members != 0)
228  {
229  delete _members;
230  _members = 0;
231  }
232 
233  _index.invalidate();
234  _is_dof_subposet = false;
235  _dof_ct = 0;
236 
237  if(_dof_descriptors != 0)
238  {
239  _dof_descriptors->remove_reference(); // may delete _dof_descriptors
240  _dof_descriptors = 0;
241  }
242 
243  if(_id_space != 0)
244  {
245  _id_space->release_id_space();
246  _id_space = 0;
247  }
248 
249  // Postconditions:
250 
251  ensure(is_terminated());
252 
253  // Exit
254 
255  return;
256 }
257 
258 bool
261 {
262  bool result = true;
263 
264  // Preconditions:
265 
266  // Body:
267 
268  result = result && (_members == 0);
269  result = result && (!_index.is_valid());
270  result = result && (_is_dof_subposet == false);
271  result = result && (_dof_ct == 0);
272  result = result && (_dof_descriptors == 0);
273  result = result && (_id_space == 0);
274 
275  // Postconditions:
276 
277  ensure(result ? members() == 0 : true);
278  ensure(result ? !index().is_valid() : true);
279  ensure(result ? !is_dof_subposet() : true);
280  ensure(result ? dof_ct() == 0 : true);
281  ensure(result ? dof_descriptors() == 0 : true);
282  ensure(result ? !has_id_space() : true);
283 
284  // Exit
285 
286  return result;
287 }
288 
289 const sheaf::scoped_index&
291 next() const
292 {
293  // pool uses _index member as link in free list
294 
295  return _index;
296 }
297 
298 void
300 put_next(const scoped_index& xnext)
301 {
302  // Preconditions:
303 
304  // Body:
305 
306  _index = xnext;
307 
308  // Postconditions:
309 
310  // Exit
311 
312  return;
313 }
314 
317 members() const
318 {
319  return _members;
320 }
321 
322 void
324 put_members(const zn_to_bool* xmembers)
325 {
326  // Postconditions:
327 
328  // Body:
329 
330  _members = const_cast<zn_to_bool*>(xmembers);
331 
332  // Postconditions:
333 
334  ensure(members() == xmembers);
335 
336  // Exit:
337 
338  return;
339 }
340 
341 const sheaf::scoped_index&
343 index() const
344 {
345  return _index;
346 }
347 
348 bool
351 {
352  return _is_dof_subposet;
353 }
354 
355 void
357 put_is_dof_subposet(bool xis_dof_subposet)
358 {
359  // Preconditions:
360 
361  // Body:
362 
363  _is_dof_subposet = xis_dof_subposet;
364 
365  // Postconditions:
366 
367  ensure(is_dof_subposet() == xis_dof_subposet);
368 
369  // Exit:
370 
371  return;
372 }
373 
376 dof_ct() const
377 {
378  return _dof_ct;
379 }
380 
381 void
384 {
385  // Preconditions:
386 
387  // Body:
388 
389  _dof_ct = xdof_ct;
390 
391  // Postconditions:
392 
393  ensure(dof_ct() == xdof_ct);
394 
395  // Exit:
396 
397  return;
398 }
399 
403 {
404  // Preconditions:
405 
406  require(dof_descriptors() != 0);
407 
408  // Body:
409 
410  return (*_dof_descriptors)[_dof_ct].offset;
411 }
412 
416 {
417  return _dof_descriptors;
418 }
419 
420 void
423 {
424  // Preconditions:
425 
426  // Body:
427 
428  _dof_descriptors = const_cast<dof_descriptor_array*>(xdof_descriptors);
429  _dof_descriptors->add_reference();
430 
431  // Postconditions:
432 
433  ensure(dof_descriptors() == xdof_descriptors);
434 
435  // Exit:
436 
437  return;
438 }
439 
440 bool
443 {
444  return (_id_space != 0);
445 }
446 
449 id_space() const
450 {
451  // Preconditions:
452 
453  require(has_id_space());
454 
455  // Body:
456 
457  return *_id_space;
458 }
459 
463 {
464  // Preconditions:
465 
466  require(has_id_space());
467 
468  // Body:
469 
470  return *_id_space;
471 }
472 
473 void
476 {
477  // Preconditions:
478 
479  require(xid_spaces.contains(xid));
481 
482  // Body:
483 
484  if(_id_space != 0)
485  {
486  // A handle to the subposet id space is already attached,
487  // make sure it is release before assigning a new handle.
488 
489  _id_space->release_id_space();
490  }
491 
492  _id_space = &xid_spaces.get_id_space<scattered_insertion_index_space_handle>(xid);
493 
494  // Postconditions:
495 
496  ensure(&id_space().id_spaces() == &xid_spaces);
497  ensure(id_space().index() == xid);
498 
499  // Exit:
500 
501  return;
502 }
503 
504 void
506 put_id_space(const index_space_family& xid_spaces, const std::string& xname)
507 {
508  // Preconditions:
509 
510  require(xid_spaces.contains(xname));
512 
513  // Body:
514 
515  if(_id_space != 0)
516  {
517  // A handle to the subposet id space is already attached,
518  // make sure it is release before assigning a new handle.
519 
520  _id_space->release_id_space();
521  }
522 
523  _id_space = &xid_spaces.get_id_space<scattered_insertion_index_space_handle>(xname);
524 
525  // Postconditions:
526 
527  ensure(&id_space().id_spaces() == &xid_spaces);
528  ensure(id_space().name() == xname);
529 
530  // Exit:
531 
532  return;
533 }
534 
535 bool
538 {
539  return _is_persistent;
540 }
541 
542 void
544 put_is_persistent(bool xvalue)
545 {
546  // Preconditions:
547 
548  // Body:
549 
550  _is_persistent = xvalue;
551 
552  if(_id_space != 0)
553  {
554  _id_space->put_is_persistent(xvalue);
555  }
556 
557  // Postconditions:
558 
559  ensure(is_persistent() == xvalue);
560  ensure(!has_id_space() || id_space().is_persistent() == xvalue);
561 
562  // Exit:
563 
564  return;
565 }
566 
567 // ===========================================================
568 // NON-MEMBER FUNCTIONS
569 // ===========================================================
570 
571 std::ostream &
572 sheaf::
573 operator << (std::ostream &os, subposet_state& s)
574 {
575  if(s.members() != 0)
576  {
577  os << "members: ";
578  for(int i=0; i<s.members()->ub(); i++)
579  {
580  (*(s.members()))[i] ? os << '1' : os << '0';
581  }
582  os << boolalpha
583  << " is_dof_sp: " << setw(6) << s.is_dof_subposet()
584  << noboolalpha
585  << " dof_ct: " << s.dof_ct();
586 
587  os << " offsets: ";
588 
589  if(s.dof_descriptors() != 0)
590  {
591  for(int i=0; i<s.dof_ct(); i++)
592  {
593  os << " " << (*(s.dof_descriptors()))[i] << ",";
594  }
595  os << " " << (*(s.dof_descriptors()))[s.dof_ct()];
596 
597  }
598  }
599 
600  return os;
601 }
602 
603 size_t
604 sheaf::
605 deep_size(const subposet_state& xp, bool xinclude_shallow)
606 {
607  size_t result;
608 
609  // Preconditions:
610 
611  // Body:
612 
613  result = xinclude_shallow ? sizeof(xp) : 0;
614 
615  subposet_state& lxp = const_cast<subposet_state&>(xp);
616 
617  // Calculate the total memory used by data member _members
618  // of type zn_to_bool*.
619 
620  if(lxp.members() != 0)
621  {
622  result += deep_size(*(lxp.members()), true);
623  }
624 
625  // Calculate the total memory used by data member _dof_descriptors
626  // of type dof_descriptor_array*.
627 
628  if(lxp.dof_descriptors() != 0)
629  {
630  result += deep_size(*(lxp.dof_descriptors()), true);
631  }
632 
633 #ifdef DIAGNOSTIC_OUTPUT
634  cout << "$$$ deep_size(const subposet_state& xp) result = " << result << endl;
635 #endif
636 
637  // Postconditions:
638 
639  ensure(result >= 0);
640 
641  // Exit
642 
643  return result;
644 }
645 
646 // PROTECTED DATA MEMBERS
647 
648 // PRIVATE DATA MEMBERS
size_type dof_ct() const
Get the number of dofs Only defined if this is a dofs subposet, in which case _dof_ct == _members...
bool is_dof_subposet() const
True, if this is a dofs subposet.
bool is_valid() const
True if this is a valid id.
Definition: scoped_index.h:832
zn_to_bool * members() const
Get the members of the subposet.
void put_dof_descriptors(const dof_descriptor_array *xdof_descriptors)
Put the offset of each dof when the dofs are contiguously allocated. Only defined if this is a dofs...
const scattered_insertion_index_space_handle & id_space() const
The id space for this subposet state (const version).
size_type ct() const
The number of items currently in use.
void put_next(const scoped_index &xnext)
Put the next member in free list.
size_type dof_tuple_ub()
Get the upper bound of the dof tuple Only defined if this is a dofs subposet.
The data structure representing the state of a subposet.
index_space_handle & get_id_space(const std::string &xname) const
Allocates an id space handle from the handle pool attached to state with name xname.
STL namespace.
int ub() const
The upper bound for the domain index.
Definition: zn_to_bool.cc:214
dof_descriptor_array * dof_descriptors() const
Get the offset of each dof when the dofs are contiguously allocated. Only defined if this is a dofs...
void put_is_dof_subposet(bool xis_dof_subposet)
True, if this is a dofs subposet.
subposet_state()
Default constructor.
void put_members(const zn_to_bool *xmembers)
Put members of the subposet.
bool handle_conforms_to_state(const std::string &xname) const
True if handle of type T conforms to the state with name xname.
A map from Zn (the integers mod n) to bools. A characteristic function used to represent subsets of Z...
Definition: zn_to_bool.h:52
bool is_initialized() const
True, if this is initialized.
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
SHEAF_DLL_SPEC size_t deep_size(const dof_descriptor_array &xp, bool xinclude_shallow=true)
The deep size of the referenced object of type dof_descriptor_array.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
void put_id_space(const index_space_family &xid_spaces, pod_index_type xid)
Put the id space for this subposet state.
void put_is_persistent(bool xvalue)
Sets is_persistent() to xvalue.
An array for storing structs which describe the size, alignment, and offset of dofs within a dof tupl...
void put_dof_ct(size_type xdof_ct)
Put the number of dofs Only defined if this is a dofs subposet, in which case _dof_ct == _members...
const scoped_index & next() const
Get the next member in free list.
bool has_id_space() const
True if this has an id space.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
void add_reference()
Add a reference to this.
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const dof_descriptor_array &p)
Insert dof_descriptor_array& p into ostream& os.
A handle for a scattered_insertion_index_space_state.
~subposet_state()
Destructor.
bool contains(pod_type xid) const
True, if this contains an id space with id xid.
Factory and container for a family of id spaces.
bool is_terminated() const
True, if this is teminated.
void initialize(int xmember_ub, const scoped_index &xindex, bool xinitialize)
Initialize data members.
void terminate()
Set all data members to NULL.
bool is_persistent() const
True if this id space should be written to disk.
SHEAF_DLL_SPEC bool is_valid(pod_index_type xpod_index)
True if an only if xpod_index is valid.
Definition: pod_types.cc:37
const scoped_index & index() const
Get index of the subposet.