SheafSystem  0.0.0.0
block.impl.h
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 #ifndef BLOCK_IMPL_H
22 #define BLOCK_IMPL_H
23 
24 #ifndef SHEAF_DLL_SPEC_H
25 #include "SheafSystem/sheaf_dll_spec.h"
26 #endif
27 
28 #ifndef BLOCK_H
29 #include "SheafSystem/block.h"
30 #endif
31 
32 #ifndef AUTO_BLOCK_IMPL_H
33 #include "SheafSystem/auto_block.impl.h"
34 #endif
35 
36 namespace sheaf
37 {
38 
40 template <typename T>
43  : base_type::auto_block(xub)
44 {
45 
46  // Preconditions:
47 
48  require(precondition_of(auto_block(xub)));
49 
50  // Body:
51 
52  // Nothing to do.
53 
54  // Postconditions:
55 
56  ensure(postcondition_of(auto_block(xub)));
57 }
58 
60 template <typename T>
62 block(index_type xub, size_type xct, const T *xitems)
63  : base_type::auto_block(xub, xct, xitems)
64 {
65 
66  // Preconditions:
67 
68  require(precondition_of(auto_block(xub, xct, xitems)));
69 
70  // Body:
71 
72  // Postconditions:
73 
74  ensure(postcondition_of(auto_block(xub, xct, xitems)));
75 }
76 
78 template <typename T>
80 block(const block& xother)
81  : base_type::auto_block(xother)
82 {
83 
84  // Preconditions:
85 
86  require(precondition_of(auto_block(xother)));
87 
88  // Body:
89 
90  // Postconditions:
91 
92  ensure(postcondition_of(auto_block(xother)));
93 }
94 
96 template <typename T>
97 block<T>&
99 operator=(const block& xother)
100 {
101 
102  // Preconditions:
103 
104  require(precondition_of(auto_block::operator=(xother)));
105 
106  // Body:
107 
108  base_type::operator=(xother);
109 
110  // Postconditions:
111 
112  ensure(postcondition_of(auto_block::operator=(xother)));
113 
114  // Return:
115 
116  return *this;
117 }
118 
120 template <typename T>
123 {
124  // Nothing to do.
125 }
126 
127 
128 // ===========================================================
129 // NON-MEMBER FUNCTIONS
130 // ===========================================================
131 
132 #ifndef DOXYGEN_1_5_4_SKIP_UNKNOWN
133 // $$SCRIBBLE doxygen v1.5.4 - Warning: no matching file member found for
134 // Appears to be trying to match
135 // bool sheaf::operator==(const block< T > &xblk1, const block< T > &xblk2)
136 // but is finding
137 // bool operator==(const block< T > &xblk1, const block< T > &xblk2)
138 // NOTE: it has no issue with the last deep_size function
139 
140 template <typename T>
141 bool
142 operator==(const block<T>& xblk1, const block<T>& xblk2)
143 {
144  bool result;
145 
146  // Preconditions:
147 
148  // Body:
149 
150  result = (xblk1.ub() >= xblk2.ub());
151  result = result && (xblk1.ct() == xblk2.ct());
152 
153  for(sheaf::pod_index_type i=0; result && (i<xblk1.ct()); ++i)
154  {
155  result = (xblk1[i] == xblk2[i]);
156  }
157 
158  // Postconditions:
159 
160  // Return:
161 
162  return result;
163 }
164 
166 template <typename T>
167 std::ostream&
168 operator << (std::ostream& xos, const block<T>& xblk)
169 {
170  for(long int i=0; i<xblk.ct(); ++i)
171  {
172  xos << " " << xblk[i];
173  }
174 
175  return xos;
176 }
177 
179 template <typename T>
180 size_t
181 deep_size(const block<T>& xblk, bool xinclude_shallow)
182 {
183  size_t result;
184 
185  // Preconditions:
186 
187  // Body:
188 
189  result = xinclude_shallow ? sizeof(xblk) : 0;
190 
191  for(int i=0; i<xblk.ub(); ++i)
192  {
193  result += deep_size(xblk[i], true);
194  }
195 
196  // Postconditions:
197 
198  ensure(result >= 0);
199 
200  return result;
201 }
202 
203 #endif // DOXYGEN_1_5_4_SKIP_UNKNOWN
204 
206 template <typename T>
207 size_t
208 deep_size(const block<T*>& xblk, bool xinclude_shallow)
209 {
210  size_t result;
211 
212  // Preconditions:
213 
214  // Body:
215 
216  result = xinclude_shallow ? sizeof(xblk) : 0;
217 
218  result += xblk.ub()*sizeof(T*);
219 
220  for(int i=0; i<xblk.ub(); ++i)
221  {
222  if(xblk[i] != 0)
223  {
224  result += deep_size(*xblk[i], true);
225  }
226  }
227 
228  // Postconditions:
229 
230  ensure(result >= 0);
231 
232  return result;
233 }
234 
235 } // namespace sheaf
236 
237 #endif // BLOCK_IMPL_H
238 
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.
unsigned long int size_type
An unsigned integral type used to represent sizes and capacities.
Definition: auto_block.h:171
A contiguously allocated array of items of type T with size information and automatic resizing...
Definition: auto_block.h:122
block(index_type xub=0)
Creates an instance with ub() == xub; storage is uninitialized.
Definition: block.impl.h:42
bool operator==(const singly_linked_list< T, Alloc > &lhs, const singly_linked_list< T, Alloc > &rhs)
Checks if the contents of lhs and rhs are equal, that is, whether lhs.size() == rhs.size() and each element in lhs compares equal with the element in rhs at the same position.
sheaf::pod_index_type index_type
The containers index type.
Definition: auto_block.h:160
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
Namespace for the sheaves component of the sheaf system.
An auto_block with a no-initialization initialization policy.