SheafSystem  0.0.0.0
deep_size.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 // Implementation for deep_size functions
19 
20 #ifndef DEEP_SIZE_IMPL_H
21 #define DEEP_SIZE_IMPL_H
22 
23 #ifndef SHEAF_DLL_SPEC_H
24 #include "SheafSystem/sheaf_dll_spec.h"
25 #endif
26 
27 #ifndef DEEP_SIZE_H
28 #include "SheafSystem/deep_size.h"
29 #endif
30 
31 #ifndef ASSERT_CONTRACT_H
32 #include "SheafSystem/assert_contract.h"
33 #endif
34 
35 #ifndef PRIMITIVE_TRAITS_H
36 #include "SheafSystem/primitive_traits.h"
37 #endif
38 
39 #ifndef STD_IOSTREAM_H
40 #include "SheafSystem/std_iostream.h"
41 #endif
42 
43 
44 //#define DIAGNOSTIC_OUTPUT
45 //#undef DIAGNOSTIC_OUTPUT
46 
47 namespace sheaf
48 {
49 
50 //=============================================================================
51 // deep size map properties
52 //=============================================================================
53 
54 template <typename M>
55 size_t
57 items_deep_size(const M& xp)
58 {
59  size_t result = 0;
60 
61  // Preconditions:
62 
63  // Body:
64 
65  // Do nothing.
66 
67  // Postconditions:
68 
69  ensure(result == 0);
70 
71  // Exit:
72 
73  return result;
74 }
75 
76 template <typename M>
77 size_t
79 items_deep_size(const M& xp)
80 {
81  size_t result = 0;
82 
83  // Preconditions:
84 
85  // Body:
86 
87  typename M::const_iterator litr;
88  for(litr = xp.begin(); litr != xp.end(); ++litr)
89  {
90  if(litr->second != 0)
91  result += deep_size(*(litr->second), false);
92  }
93 
94  // Postconditions:
95 
96  ensure(result >= 0);
97 
98  // Exit:
99 
100  return result;
101 }
102 
103 template <typename M>
104 size_t
106 items_deep_size(const M& xp)
107 {
108  size_t result = 0;
109 
110  // Preconditions:
111 
112  // Body:
113 
114  typename M::const_iterator litr;
115  for(litr = xp.begin(); litr != xp.end(); ++litr)
116  {
117  result += deep_size(litr->first, false);
118  }
119 
120  // Postconditions:
121 
122  ensure(result >= 0);
123 
124  // Exit:
125 
126  return result;
127 }
128 
129 template <typename M>
130 size_t
132 items_deep_size(const M& xp)
133 {
134  size_t result = 0;
135 
136  // Preconditions:
137 
138  // Body:
139 
140  typename M::const_iterator litr;
141  for(litr = xp.begin(); litr != xp.end(); ++litr)
142  {
143  result += deep_size(litr->second, false);
144  }
145 
146  // Postconditions:
147 
148  ensure(result >= 0);
149 
150  // Exit:
151 
152  return result;
153 }
154 
155 template <typename M>
156 size_t
158 items_deep_size(const M& xp)
159 {
160  size_t result = 0;
161 
162  // Preconditions:
163 
164  // Body:
165 
166  typename M::const_iterator litr;
167  for(litr = xp.begin(); litr != xp.end(); ++litr)
168  {
169  result += deep_size(litr->first, false);
170  result += deep_size(litr->second, false);
171  }
172 
173  // Postconditions:
174 
175  ensure(result >= 0);
176 
177  // Exit:
178 
179  return result;
180 }
181 
182 template <typename K, typename V, typename S, typename H, typename E, typename A>
183 size_t
184 deep_size(const unordered::unordered_multimap<K, V, H, E, A>& xp, bool xinclude_shallow)
185 {
186  size_t result;
187 
188  // Preconditions:
189 
190  // Body:
191 
192  using namespace std;
193 
194  result = xinclude_shallow ? sizeof(xp) : 0;
195 
196  // @hack Using the same calculation ashash map. Should review.
197 
198  // Since we have no easy access to the memory allocation in STL class unordered_map
199  // (or map) we use the following as a best guess:
200  // deep_size = load_factor * (bucket_count) * sizeof(value_type);
201 
202  // We believe, based on some experiments, that the load factor is 2.
203 
204  static const int load_factor = 2;
205 
206  // The value_type for our data members is pair<int, int>, so the our best guess
207  // for the size is:
208 
209  size_t lbucket_count = xp.bucket_count();
210  size_t ldeep_size = load_factor * lbucket_count * sizeof(pair<K, V>);
211 
212  result += ldeep_size;
213 
214  // Use the deep_size policy to calculate the size of the key and value objects.
215 
216  result += S::items_deep_size(xp);
217 
218  // Postconditions:
219 
220  ensure(result >= 0);
221 
222  // Exit
223 
224  return result;
225 }
226 
227 template <typename K, typename V, typename S, typename H, typename E, typename A>
228 size_t
229 deep_size(const unordered::unordered_map<K, V, H, E, A>& xp, bool xinclude_shallow)
230 {
231  size_t result;
232 
233  // Preconditions:
234 
235  using namespace std;
236 
237  // Body:
238 
239  result = xinclude_shallow ? sizeof(xp) : 0;
240 
241  // Since we have no easy access to the memory allocation in STL class unordered_map
242  // (or map) we use the following as a best guess:
243  // deep_size = load_factor * (bucket_count) * sizeof(value_type);
244 
245  // We believe, based on some experiments, that the load factor is 2.
246 
247  static const int load_factor = 2;
248 
249  // The value_type for our data members is pair<int, int>, so the our best guess
250  // for the size is:
251 
252  size_t lbucket_count = xp.bucket_count();
253  size_t ldeep_size = load_factor * lbucket_count * sizeof(pair<K, V>);
254 
255  result += ldeep_size;
256 
257  // Use the deep_size policy to calculate the size of the key and value objects.
258 
259  result += S::items_deep_size(xp);
260 
261  // Postconditions:
262 
263  ensure(result >= 0);
264 
265  // Exit
266 
267  return result;
268 }
269 
270 template <typename K, typename V, typename S>
271 size_t
272 deep_size(const std::map<K, V>& xp, bool xinclude_shallow)
273 {
274  size_t result;
275 
276  // Preconditions:
277 
278  using namespace std;
279 
280  // Body:
281 
282  result = xinclude_shallow ? sizeof(xp) : 0;
283 
284  //
285  // @hack This is guess at the size of the map structure. This calculation
286  // should be revisited.
287  //
288 
289  size_t lsize = xp.size();
290  size_t ldeep_size = lsize * sizeof(pair<K, V>);
291 
292  result += ldeep_size;
293 
294  // Use the deep_size policy to calculate the size of the key and value objects.
295 
296  result += S::items_deep_size(xp);
297 
298  // Postconditions:
299 
300  ensure(result >= 0);
301 
302  // Exit
303 
304  return result;
305 }
306 
307 template <typename T>
308 size_t
309 deep_size(const std::list<T>& xp, bool xinclude_shallow)
310 {
311  size_t result;
312 
313  // Preconditions:
314 
315  // Body:
316 
317  result = xinclude_shallow ? sizeof(xp) : 0;
318 
319  //
320  // @hack This is guess at the size of the list structure. This calculation
321  // should be revisited.
322  //
323 
324  // Add the constributions from the pointers in the list.
325 
326  result += xp.size() * 2*sizeof(void*);
327 
328  // Add the deep size of the members.
329 
330  typename std::list<T>::const_iterator litr = xp.begin();
331  for(; litr != xp.end(); ++litr)
332  {
333  deep_size(*litr, true);
334  }
335 
336  // Postconitions:
337 
338  ensure(result >= 0);
339 
340  // Exit:
341 
342  return result;
343 }
344 
345 //=============================================================================
346 // Function for primitive
347 //=============================================================================
348 
349 template <typename T>
350 size_t
351 deep_size(const T& xvalue, bool xinclude_shallow)
352 {
353  size_t result;
354 
355  // Preconditions:
356 
357  // Body:
358 
359  result = xinclude_shallow ? primitive_traits<T>::size() : 0;
360 
361  // Postconditions:
362 
363  ensure(result >= 0);
364 
365  // Exit
366 
367  return result;
368 }
369 
370 } // namespace sheaf
371 
372 #endif // ifndef DEEP_SIZE_IMPL_H
static size_type size()
The size of T in bytes.
static size_t items_deep_size(const M &xp)
The deep_size of the items in xp.
static size_t items_deep_size(const M &xp)
The deep_size of the items in xp.
STL namespace.
static size_t items_deep_size(const M &xp)
The deep_size of the items in xp.
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.
static size_t items_deep_size(const M &xp)
The deep_size of the items in xp.
static size_t items_deep_size(const M &xp)
The deep_size of the items in xp.
Namespace for the sheaves component of the sheaf system.