35#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_ARRAY_POLICIES_HPP
36#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_ARRAY_POLICIES_HPP
43#include <boost/multi_array.hpp>
47template <
typename ObjectType,
typename enabled =
void>
52template <
typename ObjectType>
55 typename
std::enable_if<std::is_fundamental<ObjectType>::value>::type>
58 static void Initialize([[maybe_unused]] ObjectType *data,
59 [[maybe_unused]]
size_t itemsToCreate)
63 static void Initialize(ObjectType *data,
size_t itemsToCreate,
64 const ObjectType &initValue)
66 std::fill_n(data, itemsToCreate, initValue);
69 static void Initialize(ObjectType *data,
size_t itemsToCreate,
70 const ObjectType *initValue)
72 std::copy(initValue, initValue + itemsToCreate, data);
86template <
typename ObjectType>
89 typename
std::enable_if<!std::is_fundamental<ObjectType>::value>::type>
95 static void Initialize(ObjectType *data,
size_t itemsToCreate)
97 DoInitialization(data, itemsToCreate,
98 [](ObjectType *element) {
new (element) ObjectType; });
105 static void Initialize(ObjectType *data,
size_t itemsToCreate,
106 const ObjectType &initValue)
108 DoInitialization(data, itemsToCreate, [&](ObjectType *element) {
109 new (element) ObjectType(initValue);
113 static void Initialize(ObjectType *data,
size_t itemsToCreate,
114 const ObjectType *initValue)
116 DoInitialization(data, itemsToCreate, [&](ObjectType *element) {
117 new (element) ObjectType(*initValue);
123 template <
typename CreateType>
127 size_t nextObjectToCreate = 0;
130 for (
size_t i = 0; i < itemsToCreate; ++i)
132 ObjectType *memLocation = &data[i];
134 ++nextObjectToCreate;
139 for (
size_t i = 0; i < nextObjectToCreate; ++i)
141 ObjectType *memLocation = &data[nextObjectToCreate - i - 1];
142 memLocation->~ObjectType();
149template <
typename ObjectType,
typename enabled =
void>
152template <
typename ObjectType>
155 typename
std::enable_if<std::is_fundamental<ObjectType>::value>::type>
158 static void Destroy([[maybe_unused]] ObjectType *data,
159 [[maybe_unused]]
size_t itemsToDestroy)
164template <
typename ObjectType>
167 typename
std::enable_if<!std::is_fundamental<ObjectType>::value>::type>
170 static void Destroy(ObjectType *data,
size_t itemsToDestroy)
172 for (
size_t i = 0; i < itemsToDestroy; ++i)
174 ObjectType *memLocation = &data[itemsToDestroy - i - 1];
175 memLocation->~ObjectType();
180template <
typename Dim,
typename DataType,
typename ExtentListType>
182 const ExtentListType &extent)
184 typedef boost::multi_array_ref<DataType, Dim::Value> ArrayType;
185 size_t size = std::accumulate(extent.begin(), extent.end(), 1,
186 std::multiplies<size_t>());
189 [=]([[maybe_unused]] boost::multi_array_ref<DataType, Dim::Value>
197template <
typename DataType>
198std::shared_ptr<boost::multi_array_ref<DataType, 1>>
CreateStorage(
size_t d1)
200 std::vector<size_t> extents = {d1};
201 return CreateStorage<OneD, DataType>(extents);
204template <
typename DataType>
205std::shared_ptr<boost::multi_array_ref<DataType, 2>>
CreateStorage(
size_t d1,
208 std::vector<size_t> extents = {d1, d2};
209 return CreateStorage<TwoD, DataType>(extents);
212template <
typename DataType>
213std::shared_ptr<boost::multi_array_ref<DataType, 3>>
CreateStorage(
size_t d1,
217 std::vector<size_t> extents = {d1, d2, d3};
218 return CreateStorage<ThreeD, DataType>(extents);
static void Destroy(ObjectType *data, size_t itemsToDestroy)
static void Destroy(ObjectType *data, size_t itemsToDestroy)
static void DoInitialization(ObjectType *data, size_t itemsToCreate, const CreateType &f)
static void Initialize(ObjectType *data, size_t itemsToCreate, const ObjectType &initValue)
Initalize each element in the array with ObjectType's copy constructor.
static void Initialize(ObjectType *data, size_t itemsToCreate)
Initalize each element in the array with ObjectType's default constructor.
static void Initialize(ObjectType *data, size_t itemsToCreate, const ObjectType *initValue)
static void Initialize(ObjectType *data, size_t itemsToCreate)
static void Initialize(ObjectType *data, size_t itemsToCreate, const ObjectType &initValue)
static void Initialize(ObjectType *data, size_t itemsToCreate, const ObjectType *initValue)
static DataType * RawAllocate(size_t NumberOfElements)
Allocates a chunk of raw, uninitialized memory, capable of holding NumberOfElements objects.
static void RawDeallocate(DataType *array, size_t NumberOfElements)
Deallocates memory allocated from RawAllocate.
static std::shared_ptr< DataType > AllocateSharedPtrD(const DeallocatorType &d, const Args &...args)
std::shared_ptr< boost::multi_array_ref< DataType, Dim::Value > > CreateStorage(const ExtentListType &extent)