Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
includeLinks/PatchDataMem.hpp
Go to the documentation of this file.
1 /* *****************************************************************
2  MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4  Copyright 2004 Lawrence Livermore National Laboratory. Under
5  the terms of Contract B545069 with the University of Wisconsin --
6  Madison, Lawrence Livermore National Laboratory retains certain
7  rights in this software.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  (lgpl.txt) along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  kraftche@cae.wisc.edu
24 
25  ***************************************************************** */
26 
27 #ifndef MESQUITE_PATCH_DATA_MEM_HPP
28 #define MESQUITE_PATCH_DATA_MEM_HPP
29 
30 #include <stdlib.h>
31 #include <assert.h>
32 
33 namespace Mesquite {
34 
35 template <typename X> class PatchDataMem
36 {
37  enum {
39  };
40 
41  private:
42 
43  size_t arrayLength;
44  size_t activeSize;
46 
47  inline void resize_storage( size_t size );
48 
49  public:
50 
51  inline PatchDataMem();
52  inline PatchDataMem( size_t size );
53  inline PatchDataMem( const PatchDataMem<X>& );
54 
55  inline ~PatchDataMem();
56 
57  inline size_t size() const;
58  inline bool empty() const;
59 
60  inline const X& operator[]( size_t index ) const;
61  inline X& operator[]( size_t index ) ;
62 
63  inline const X* to_array() const;
64  inline X* to_array();
65 
66  inline void resize( size_t new_size );
67  inline void clear();
68 
69  inline PatchDataMem<X>& operator=( const PatchDataMem<X>& );
70 
71  typedef X* iterator;
72  typedef const X* const_iterator;
73  inline iterator begin();
74  inline const_iterator begin() const;
75  inline iterator end();
76  inline const_iterator end() const;
77 };
78 
79 template <typename X>
81  : arrayLength(0),
82  activeSize(0),
83  arrayData(0)
84  {}
85 
86 template <typename X>
88  : arrayLength(0),
89  activeSize(0),
90  arrayData(0)
91  { resize( size ); }
92 
93 template <typename X>
95  : arrayLength(0),
96  activeSize(0),
97  arrayData(0)
98  { operator=(other); }
99 
100 template <typename X>
102  { clear(); free( arrayData ); }
103 
104 template <typename X>
105 size_t PatchDataMem<X>::size() const
106  { return activeSize; }
107 
108 template <typename X>
110  { return arrayData; }
111 
112 template <typename X>
114  { return arrayData; }
115 
116 template <typename X>
117 const X& PatchDataMem<X>::operator[]( size_t index ) const
118  { return arrayData[index]; }
119 
120 template <typename X>
121 X& PatchDataMem<X>::operator[]( size_t index )
122  { return arrayData[index]; }
123 
124 template <typename X>
126  { return !activeSize; }
127 
128 template <typename X>
130  { return arrayData; }
131 
132 template <typename X>
134  { return arrayData; }
135 
136 template <typename X>
138  { return arrayData + activeSize; }
139 
140 template <typename X>
142  { return arrayData + activeSize; }
143 
144 template <typename X>
145 void PatchDataMem<X>::resize_storage( size_t new_size )
146 {
147  assert( new_size >= activeSize );
148  X* new_array = (X*)malloc( sizeof(X) * new_size );
149  for (size_t i = 0; i < activeSize; ++i)
150  {
151  //new_array[i].X( arrayData[i] );
152  new (new_array + i) X( arrayData[i] );
153  arrayData[i].~X();
154  }
155  free( arrayData );
156  arrayData = new_array;
157  arrayLength = new_size;
158 }
159 
160 template <typename X>
161 void PatchDataMem<X>::resize( size_t new_size )
162 {
163  if (new_size > activeSize)
164  {
165  if (new_size > arrayLength)
166  resize_storage( new_size );
167 
168  for (size_t i = activeSize; i < new_size; ++i)
169  {
170  //arrayData[i].X();
171  new (arrayData + i) X();
172  }
173  activeSize = new_size;
174  }
175  else if (new_size < activeSize)
176  {
177  for (size_t i = new_size; i < activeSize; ++i)
178  arrayData[i].~X();
179  activeSize = new_size;
180 
181  if (activeSize && ((SHRINK_PERCENT*activeSize) < arrayLength))
182  resize_storage( new_size );
183  }
184 }
185 
186 template <typename X>
188 {
189  for (size_t i = 0; i < activeSize; ++i)
190  arrayData[i].~X();
191  activeSize = 0;
192 }
193 
194 template <typename X>
196 {
197  clear();
198 
199  if (other.activeSize)
200  {
201  if (arrayLength < other.activeSize ||
202  (SHRINK_PERCENT * other.activeSize) < arrayLength)
203  resize_storage( other.activeSize );
204 
205  activeSize = other.activeSize;
206  for (size_t i = 0; i < activeSize; ++i)
207  {
208  //arrayData[i].X( other.arrayData[i] );
209  new (arrayData + i) X( other.arrayData[i] );
210  }
211  }
212 
213  return *this;
214 }
215 
216 } // namespace Mesquite
217 
218 #endif
219 
220 
MsqMeshEntity is the Mesquite object that stores information about the elements in the mesh...
Shink memory when used portion is less than this percent.
blockLoc i
Definition: read.cpp:79
const X & operator[](size_t index) const
PatchDataMem< X > & operator=(const PatchDataMem< X > &)