37 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_ARRAY_POLICIES_HPP
38 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_ARRAY_POLICIES_HPP
42 #include <boost/shared_ptr.hpp>
43 #include <boost/multi_array.hpp>
44 #include <boost/bind.hpp>
48 template<
typename ObjectType,
typename enabled =
void>
53 template<
typename ObjectType>
55 typename boost::enable_if<boost::is_fundamental<ObjectType> >::type >
58 static void Initialize(ObjectType* data,
unsigned int itemsToCreate)
62 static void Initialize(ObjectType* data,
unsigned int itemsToCreate,
const ObjectType& initValue)
64 std::fill_n(data, itemsToCreate, initValue);
67 static void Initialize(ObjectType* data,
unsigned int itemsToCreate,
const ObjectType* initValue)
69 std::copy(initValue, initValue + itemsToCreate, data);
83 template<
typename ObjectType>
85 typename boost::disable_if<boost::is_fundamental<ObjectType> >::type >
91 static void Initialize(ObjectType* data,
unsigned int itemsToCreate)
93 DoInitialization(data, itemsToCreate,
101 static void Initialize(ObjectType* data,
unsigned int itemsToCreate,
const ObjectType& initValue)
103 DoInitialization(data, itemsToCreate,
107 static void Initialize(ObjectType* data,
unsigned int itemsToCreate,
const ObjectType* initValue)
109 DoInitialization(data, itemsToCreate,
114 template<
typename CreateType>
115 static void DoInitialization(ObjectType* data,
unsigned int itemsToCreate,
const CreateType& f)
117 unsigned int nextObjectToCreate = 0;
120 for(
unsigned int i = 0; i < itemsToCreate; ++i)
122 ObjectType* memLocation = &data[i];
124 ++nextObjectToCreate;
129 for(
unsigned int i = 0; i < nextObjectToCreate; ++i)
131 ObjectType* memLocation = &data[nextObjectToCreate - i - 1];
132 memLocation->~ObjectType();
140 new (element) ObjectType;
145 new (element) ObjectType(initValue);
150 new (element) ObjectType(*rhs);
156 template<
typename ObjectType,
typename enabled =
void>
159 template<
typename ObjectType>
161 typename boost::enable_if<boost::is_fundamental<ObjectType> >::type >
164 static void Destroy(ObjectType* data,
unsigned int itemsToDestroy)
169 template<
typename ObjectType>
171 typename boost::disable_if<boost::is_fundamental<ObjectType> >::type >
174 static void Destroy(ObjectType* data,
unsigned int itemsToDestroy)
176 for(
unsigned int i = 0; i < itemsToDestroy; ++i)
178 ObjectType* memLocation = &data[itemsToDestroy - i - 1];
179 memLocation->~ObjectType();
184 template<
typename DataType>
191 template<
typename Dim,
typename DataType,
typename ExtentListType>
192 boost::shared_ptr<boost::multi_array_ref<DataType, Dim::Value> >
195 typedef boost::multi_array_ref<DataType, Dim::Value> ArrayType;
196 unsigned int size = std::accumulate(extent.begin(), extent.end(), 1,
197 std::multiplies<unsigned int>());
200 boost::bind(DeleteStorage<DataType>, storage, size),
204 template<
typename DataType>
205 boost::shared_ptr<boost::multi_array_ref<DataType, 1> >
208 std::vector<unsigned int> extents(1, d1);
209 return CreateStorage<OneD, DataType>(extents);
212 template<
typename DataType>
213 boost::shared_ptr<boost::multi_array_ref<DataType, 2> >
216 unsigned int vals[] = {d1, d2};
217 std::vector<unsigned int> extents(vals, vals+2);
218 return CreateStorage<TwoD, DataType>(extents);
221 template<
typename DataType>
222 boost::shared_ptr<boost::multi_array_ref<DataType, 3> >
225 unsigned int vals[] = {d1, d2, d3};
226 std::vector<unsigned int> extents(vals, vals+3);
227 return CreateStorage<ThreeD, DataType>(extents);
231 #endif //NEKTAR_LIB_UTILITIES_BASIC_UTILS_ARRAY_POLICIES_HPP
static void Destroy(ObjectType *data, unsigned int itemsToDestroy)
static void Initialize(ObjectType *data, unsigned int itemsToCreate, const ObjectType *initValue)
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
static void Initialize(ObjectType *data, unsigned int itemsToCreate)
Initalize each element in the array with ObjectType's default constructor.
boost::shared_ptr< boost::multi_array_ref< DataType, Dim::Value > > CreateStorage(const ExtentListType &extent)
static void Initialize(ObjectType *data, unsigned int itemsToCreate)
static void Initialize(ObjectType *data, unsigned int itemsToCreate, const ObjectType &initValue)
Initalize each element in the array with ObjectType's copy constructor.
static void RawDeallocate(DataType *array, unsigned int NumberOfElements)
Deallocates memory allocated from RawAllocate.
static void CopyConstructionFromArray(ObjectType *element, const ObjectType *&rhs)
static void Destroy(ObjectType *data, unsigned int itemsToDestroy)
void DeleteStorage(DataType *data, unsigned int num)
static void CopyConstructionWithPlacementNew(ObjectType *element, const ObjectType &initValue)
static void Initialize(ObjectType *data, unsigned int itemsToCreate, const ObjectType &initValue)
static void DoInitialization(ObjectType *data, unsigned int itemsToCreate, const CreateType &f)
static DataType * RawAllocate(unsigned int NumberOfElements)
Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects...
static void DefaultConstructionWithPlacementNew(ObjectType *element)
static void Initialize(ObjectType *data, unsigned int itemsToCreate, const ObjectType *initValue)