15 #ifndef STK_UTIL_UTIL_Array_h 16 #define STK_UTIL_UTIL_Array_h 22 #include <stk_util/util/TypeList.hpp> 73 template<
class ElementType,
75 class Tag1 = TypeListEnd,
76 class Tag2 = TypeListEnd,
77 class Tag3 = TypeListEnd,
78 class Tag4 = TypeListEnd,
79 class Tag5 = TypeListEnd,
80 class Tag6 = TypeListEnd,
81 class Tag7 = TypeListEnd>
91 template<
class ElementType,
100 class A = std::allocator<ElementType> >
107 template<
class Tag0,
115 class Array<void, Tag0, Tag1, Tag2, Tag3, Tag4, Tag5, Tag6, Tag7> {};
117 template <
class ElementType>
121 typedef ElementType element_type;
138 template<
class ElementType,
147 class Array :
public FArray<ElementType, TypeListLength<typename MakeTypeList<Tag0, Tag1, Tag2, Tag3, Tag4, Tag5, Tag6, Tag7>::type>::value>
150 typedef ElementType element_type;
162 enum { NumDim = TypeListLength<TagList>::value };
164 typedef Array< ElementType,
165 typename TypeListAt<
typename TypeListEraseAt<TagList, NumDim - 1>::list_type, 0>::type,
166 typename TypeListAt<
typename TypeListEraseAt<TagList, NumDim - 1>::list_type, 1>::type,
167 typename TypeListAt<
typename TypeListEraseAt<TagList, NumDim - 1>::list_type, 2>::type,
168 typename TypeListAt<
typename TypeListEraseAt<TagList, NumDim - 1>::list_type, 3>::type,
169 typename TypeListAt<
typename TypeListEraseAt<TagList, NumDim - 1>::list_type, 4>::type,
170 typename TypeListAt<
typename TypeListEraseAt<TagList, NumDim - 1>::list_type, 5>::type,
171 typename TypeListAt<
typename TypeListEraseAt<TagList, NumDim - 1>::list_type, 6>::type,
178 return BaseType::dimension();
181 const unsigned * stride()
const {
182 return BaseType::stride();
189 return BaseType::m_dim[I];
192 unsigned dimension(
const unsigned i )
const {
193 this->array_dimension_verify(0, i, NumDim );
194 return BaseType::m_dim[i];
197 unsigned stride(
const unsigned i )
const {
198 this->array_dimension_verify(0, i, NumDim );
199 return BaseType::m_stride[i];
203 unsigned stride()
const {
205 return BaseType::m_stride[I];
210 enum { I = TypeListIndex<TagList, Tag>::value };
212 return BaseType::m_dim[I];
215 template<
class Tag,
unsigned Ordinal>
217 enum { I = TypeListIndex<TagList, Tag, Ordinal>::value };
219 return BaseType::m_dim[I];
223 unsigned stride()
const {
224 enum { I = TypeListIndex<TagList, Tag>::value };
226 return BaseType::m_stride[I];
229 template<
class Tag,
unsigned Ordinal>
230 unsigned stride()
const {
231 enum { I = TypeListIndex<TagList, Tag, Ordinal>::value };
233 return BaseType::m_stride[I];
236 bool operator == (
const SelfType & a )
const {
237 return ArrayHelper<NumDim>::equal( BaseType::m_dim, a.m_dim ) &&
238 ArrayHelper<NumDim>::equal(BaseType::m_dim, BaseType::m_ptr, BaseType::m_stride, a.m_ptr, a.m_stride);
243 return ArrayHelper<NumDim>::equal( BaseType::m_dim, a.
dimension() ) &&
244 ArrayHelper<NumDim>::equal(BaseType::m_dim, BaseType::m_ptr, BaseType::m_stride, a.ptr(), a.stride());
247 bool operator != (
const SelfType & a )
const {
248 return ! operator == ( a );
253 return ! operator == ( a );
263 Array(
const SelfType & a )
272 Array( element_type *
const in_ptr,
273 const unsigned n0,
const unsigned n1,
274 const unsigned n2,
const unsigned n3,
275 const unsigned n4,
const unsigned n5,
276 const unsigned n6,
const unsigned n7 )
277 : BaseType( in_ptr, n0, n1, n2, n3, n4, n5, n6, n7)
280 Array( element_type *
const in_ptr,
281 const unsigned n0,
const unsigned n1,
282 const unsigned n2,
const unsigned n3,
283 const unsigned n4,
const unsigned n5,
285 : BaseType( in_ptr, n0, n1, n2, n3, n4, n5, n6)
288 Array( element_type *
const in_ptr,
289 const unsigned n0,
const unsigned n1,
290 const unsigned n2,
const unsigned n3,
291 const unsigned n4,
const unsigned n5 )
292 : BaseType( in_ptr, n0, n1, n2, n3, n4, n5)
295 Array( element_type *
const in_ptr,
296 const unsigned n0,
const unsigned n1,
297 const unsigned n2,
const unsigned n3,
299 : BaseType( in_ptr, n0, n1, n2, n3, n4)
302 Array( element_type *
const in_ptr,
303 const unsigned n0,
const unsigned n1,
304 const unsigned n2,
const unsigned n3 )
305 : BaseType( in_ptr, n0, n1, n2, n3)
308 Array( element_type *
const in_ptr,
309 const unsigned n0,
const unsigned n1,
311 : BaseType( in_ptr, n0, n1, n2)
314 Array( element_type *
const in_ptr,
315 const unsigned n0,
const unsigned n1 )
316 : BaseType( in_ptr, n0, n1)
319 Array( element_type *
const in_ptr,
321 : BaseType( in_ptr, n0)
324 Array( element_type *
const in_ptr,
325 const unsigned n[NumDim] )
326 : BaseType( in_ptr, n){}
328 void set(
const SelfType & a ) {
329 BaseType::m_ptr = a.m_ptr;
331 ArrayHelper<NumDim >::copy( a.m_dim, BaseType::m_dim );
332 ArrayHelper<NumDim+1>::copy( a.m_stride, BaseType::m_stride );
335 void set( element_type *
const in_ptr,
336 const unsigned n0,
const unsigned n1,
337 const unsigned n2,
const unsigned n3,
338 const unsigned n4,
const unsigned n5,
339 const unsigned n6,
const unsigned n7 ) {
340 BaseType::set(in_ptr, n0, n1, n2, n3, n4, n5, n6, n7);
343 void set( element_type *
const in_ptr,
344 const unsigned n0,
const unsigned n1,
345 const unsigned n2,
const unsigned n3,
346 const unsigned n4,
const unsigned n5,
347 const unsigned n6 ) {
348 BaseType::set(in_ptr, n0, n1, n2, n3, n4, n5, n6);
351 void set( element_type *
const in_ptr,
352 const unsigned n0,
const unsigned n1,
353 const unsigned n2,
const unsigned n3,
354 const unsigned n4,
const unsigned n5 ) {
355 BaseType::set(in_ptr, n0, n1, n2, n3, n4, n5);
358 void set( element_type *
const in_ptr,
359 const unsigned n0,
const unsigned n1,
360 const unsigned n2,
const unsigned n3,
361 const unsigned n4 ) {
362 BaseType::set(in_ptr, n0, n1, n2, n3, n4);
365 void set( element_type *
const in_ptr,
366 const unsigned n0,
const unsigned n1,
367 const unsigned n2,
const unsigned n3 ) {
368 BaseType::set(in_ptr, n0, n1, n2, n3);
371 void set( element_type *
const in_ptr,
372 const unsigned n0,
const unsigned n1,
373 const unsigned n2 ) {
374 BaseType::set(in_ptr, n0, n1, n2);
377 void set( element_type *
const in_ptr,
378 const unsigned n0,
const unsigned n1 ) {
379 BaseType::set(in_ptr, n0, n1);
382 void set( element_type *
const in_ptr,
383 const unsigned n0 ) {
384 BaseType::set(in_ptr, n0);
387 void set( element_type *
const in_ptr,
388 const unsigned n[NumDim] ) {
389 BaseType::set(in_ptr, n);
393 this->array_dimension_verify(0, i, BaseType::m_dim[NumDim - 1] );
395 element_type *calc_ptr = BaseType::m_ptr + i*BaseType::m_stride[NumDim - 1];
397 return Trunc(calc_ptr, BaseType::m_dim);
400 const Trunc dive(
int i)
const {
401 this->array_dimension_verify(0, i, BaseType::m_dim[NumDim - 1] );
403 element_type *calc_ptr = BaseType::m_ptr + i*BaseType::m_stride[NumDim - 1];
405 return Trunc(calc_ptr, BaseType::m_dim);
410 ArrayHelper<NumDim>::copy(BaseType::m_dim, BaseType::m_ptr, BaseType::m_stride, a.ptr(), a.stride() );
414 void fill(
const T & value ) {
415 ArrayHelper<NumDim>::fill(BaseType::m_dim, BaseType::m_ptr, BaseType::m_stride, value);
422 SelfType & operator = ( SelfType
const & a );
425 template<
class ElementType,
436 :
public Array<ElementType, Tag0, Tag1, Tag2, Tag3, Tag4, Tag5, Tag6, Tag7>
443 typedef typename BaseType::element_type element_type;
445 typedef typename BaseType::TagList
TagList;
447 enum { NumDim = BaseType::NumDim };
450 using BaseType::m_ptr;
451 using BaseType::m_dim;
452 using BaseType::m_stride;
454 void resize_memory(
const unsigned new_size ) {
455 if ( m_capacity < new_size ) {
457 m_allocator.deallocate(m_ptr, m_capacity);
458 m_capacity = new_size;
459 m_ptr = m_allocator.allocate(m_capacity);
466 m_allocator.deallocate(m_ptr, m_capacity);
483 resize_memory( BaseType::set_dim( a.m_dim ) );
489 : BaseType(), m_capacity(0)
491 resize_memory( BaseType::set_dim( a.
dimension() ) );
495 SelfType & operator = (
const SelfType & a ) {
496 resize_memory( BaseType::set_dim( a.
dimension() ) );
503 resize_memory( BaseType::set_dim( a.
dimension() ) );
511 const unsigned n2,
const unsigned n3,
512 const unsigned n4,
const unsigned n5,
513 const unsigned n6,
const unsigned n7 )
514 : BaseType(NULL, n0, n1, n2, n3, n4, n5, n6, n7 ),
517 resize_memory( m_stride[NumDim] );
521 const unsigned n2,
const unsigned n3,
522 const unsigned n4,
const unsigned n5,
524 : BaseType( NULL, n0, n1, n2, n3, n4, n5, n6 ),
527 resize_memory( m_stride[NumDim] );
531 const unsigned n2,
const unsigned n3,
532 const unsigned n4,
const unsigned n5 )
533 : BaseType( NULL, n0, n1, n2, n3, n4, n5 ),
536 resize_memory( m_stride[NumDim] );
540 const unsigned n2,
const unsigned n3,
542 : BaseType( NULL, n0, n1, n2, n3, n4 ),
545 resize_memory( m_stride[NumDim] );
549 const unsigned n2,
const unsigned n3 )
550 : BaseType( NULL, n0, n1, n2, n3 ),
553 resize_memory( m_stride[NumDim] );
558 : BaseType( NULL, n0, n1, n2 ),
561 resize_memory( m_stride[NumDim] );
565 : BaseType( NULL, n0, n1 ),
568 resize_memory( m_stride[NumDim] );
572 : BaseType( NULL, n0 ),
575 resize_memory( m_stride[NumDim] );
579 : BaseType( NULL, n ),
582 resize_memory( m_stride[NumDim] );
589 resize_memory( BaseType::set_dim( a.
dimension() ) );
593 SelfType & resize(
const unsigned n0,
const unsigned n1,
594 const unsigned n2,
const unsigned n3,
595 const unsigned n4,
const unsigned n5,
596 const unsigned n6,
const unsigned n7 )
598 resize_memory(BaseType::set_dim(n0, n1, n2, n3, n4, n5, n6, n7));
602 SelfType & resize(
const unsigned n0,
const unsigned n1,
603 const unsigned n2,
const unsigned n3,
604 const unsigned n4,
const unsigned n5,
607 resize_memory(BaseType::set_dim(n0, n1, n2, n3, n4, n5, n6));
611 SelfType & resize(
const unsigned n0,
const unsigned n1,
612 const unsigned n2,
const unsigned n3,
613 const unsigned n4,
const unsigned n5 )
615 resize_memory(BaseType::set_dim(n0, n1, n2, n3, n4, n5));
619 SelfType & resize(
const unsigned n0,
const unsigned n1,
620 const unsigned n2,
const unsigned n3,
623 resize_memory(BaseType::set_dim(n0, n1, n2, n3, n4));
627 SelfType & resize(
const unsigned n0,
const unsigned n1,
628 const unsigned n2,
const unsigned n3 )
630 resize_memory(BaseType::set_dim(n0, n1, n2, n3));
635 SelfType & resize(
const unsigned n0,
const unsigned n1,
638 resize_memory(BaseType::set_dim(n0, n1, n2));
642 SelfType & resize(
const unsigned n0,
const unsigned n1 )
644 resize_memory(BaseType::set_dim(n0, n1));
648 SelfType & resize(
const unsigned n0 )
650 resize_memory(BaseType::set_dim(n0));
654 SelfType & resize(
const unsigned n[] )
656 resize_memory( BaseType::set_dim(n) );
673 #endif // STK_UTIL_UTIL_Array_h
Compile-time assertionIf the compile-time expression is true then defines.
Extend Array with deep copy assignment and resize operations.
Multidimensional array of contiguous memory. The memory is not owned by the array, but container access semantics are enforced, i.e. const Array<> elements cannot be assigned to.
const unsigned * dimension() const
Multidimensional array view of contiguous memory.