GEOSX
ArrayOfArrays.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
13 #pragma once
14 
15 #include "ArrayOfArraysView.hpp"
16 
17 namespace LvArray
18 {
19 
20 // Forward declaration of the ArrayOfSets class so that we can define the assimilate method.
21 template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
23 
30 template< typename T,
31  typename INDEX_TYPE,
32  template< typename > class BUFFER_TYPE >
33 class ArrayOfArrays : protected ArrayOfArraysView< T, INDEX_TYPE, false, BUFFER_TYPE >
34 {
37 
38 public:
39  using typename ParentClass::ValueType;
40  using typename ParentClass::IndexType;
41  using typename ParentClass::value_type;
42  using typename ParentClass::size_type;
43 
47 
54  inline
55  ArrayOfArrays( INDEX_TYPE const numArrays=0, INDEX_TYPE const defaultArrayCapacity=0 ):
56  ParentClass( true )
57  {
58  resize( numArrays, defaultArrayCapacity );
59  setName( "" );
60  }
61 
66  inline
67  ArrayOfArrays( ArrayOfArrays const & src ):
68  ParentClass( true )
69  { *this = src; }
70 
74  inline
75  ArrayOfArrays( ArrayOfArrays && ) = default;
76 
81  { ParentClass::free(); }
82 
84 
88 
96  constexpr inline
98  toView() const &
99  { return ParentClass::toView(); }
100 
108  constexpr inline
110  toView() const && = delete;
111 
118  constexpr inline
121  { return ParentClass::toViewConstSizes(); }
122 
130  constexpr inline
132  toViewConstSizes() const && = delete;
133 
140  constexpr inline
142  toViewConst() const &
143  { return ParentClass::toViewConst(); }
144 
152  constexpr inline
154  toViewConst() const && = delete;
155 
157 
161 
168  inline
170  {
172  src.m_offsets[ src.m_numArrays ],
173  src.m_offsets,
174  src.m_sizes,
175  src.m_values );
176  return *this;
177  }
178 
184  inline
186  {
188  ParentClass::operator=( std::move( src ) );
189  return *this;
190  }
191 
196  inline
198  {
200  ParentClass::assimilate( reinterpret_cast< ParentClass && >( src ) );
201  }
202 
204 
206 
210 
218  inline
219  INDEX_TYPE size() const
220  { return ParentClass::size(); }
221 
223  using ParentClass::capacity;
226 
228 
232 
234  using ParentClass::operator[];
235  using ParentClass::operator();
236 
238 
242 
244  using ParentClass::reserve;
246 
253  void resize( INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0 )
254  { return ParentClass::resize( newSize, defaultArrayCapacity ); }
255 
256  using ParentClass::compress;
257 
259 
263 
269  void appendArray( INDEX_TYPE const n )
270  {
272 
273  INDEX_TYPE const maxOffset = this->m_offsets[ this->m_numArrays ];
274  bufferManipulation::emplaceBack( this->m_offsets, this->m_numArrays + 1, maxOffset );
276  ++this->m_numArrays;
277 
278  resizeArray( this->m_numArrays - 1, n );
279  }
280 
287  template< typename ITER >
288  void appendArray( ITER const first, ITER const last )
289  {
290  INDEX_TYPE const maxOffset = this->m_offsets[ this->m_numArrays ];
291  bufferManipulation::emplaceBack( this->m_offsets, this->m_numArrays + 1, maxOffset );
293  ++this->m_numArrays;
294 
295  appendToArray( this->m_numArrays - 1, first, last );
296  }
297 
305  template< typename ITER >
306  void insertArray( INDEX_TYPE const i, ITER const first, ITER const last )
307  {
309 
310  // Insert an array of capacity zero at the given location
311  INDEX_TYPE const offset = this->m_offsets[ i ];
312  bufferManipulation::emplace( this->m_offsets, this->m_numArrays + 1, i + 1, offset );
313  bufferManipulation::emplace( this->m_sizes, this->m_numArrays, i, 0 );
314  ++this->m_numArrays;
315 
316  // Append to the new array
317  appendToArray( i, first, last );
318  }
319 
324  void eraseArray( INDEX_TYPE const i )
325  {
327 
328  setCapacityOfArray( i, 0 );
329  bufferManipulation::erase( this->m_offsets, this->m_numArrays + 1, i + 1 );
330  bufferManipulation::erase( this->m_sizes, this->m_numArrays, i );
331  --this->m_numArrays;
332  }
333 
335 
339 
347  template< typename ... ARGS >
348  void emplaceBack( INDEX_TYPE const i, ARGS && ... args )
349  {
350  dynamicallyGrowArray( i, 1 );
351  ParentClass::emplaceBack( i, std::forward< ARGS >( args )... );
352  }
353 
361  template< typename ITER >
362  void appendToArray( INDEX_TYPE const i, ITER const first, ITER const last )
363  {
364  INDEX_TYPE const n = arrayManipulation::iterDistance( first, last );
365  dynamicallyGrowArray( i, n );
366  ParentClass::appendToArray( i, first, last );
367  }
368 
370 
378  template< typename ... ARGS >
379  void emplace( INDEX_TYPE const i, INDEX_TYPE const j, ARGS && ... args )
380  {
381  dynamicallyGrowArray( i, 1 );
382  ParentClass::emplace( i, j, std::forward< ARGS >( args )... );
383  }
384 
393  template< typename ITER >
394  void insertIntoArray( INDEX_TYPE const i, INDEX_TYPE const j, ITER const first, ITER const last )
395  {
396  INDEX_TYPE const n = arrayManipulation::iterDistance( first, last );
397  dynamicallyGrowArray( i, n );
398  ParentClass::insertIntoArray( i, j, first, last );
399  }
400 
402 
410  template< class ... ARGS >
411  void resizeArray( INDEX_TYPE const i, INDEX_TYPE const newSize, ARGS && ... args )
412  {
415 
416  if( newSize > capacityOfArray( i ) )
417  {
418  setCapacityOfArray( i, newSize );
419  }
420 
421  INDEX_TYPE const prevSize = sizeOfArray( i );
422  T * const values = (*this)[i];
423  arrayManipulation::resize( values, prevSize, newSize, std::forward< ARGS >( args )... );
424  this->m_sizes[ i ] = newSize;
425  }
426 
431  void clearArray( INDEX_TYPE const i )
432  { resizeArray( i, 0 ); }
433 
439  void setCapacityOfArray( INDEX_TYPE const i, INDEX_TYPE const newCapacity )
440  { ParentClass::setCapacityOfArray( i, newCapacity ); }
441 
443 
447 
455  void move( MemorySpace const space, bool touch=true ) const
456  { ParentClass::move( space, touch ); }
457 
459 
464  void setName( std::string const & name )
465  { ParentClass::template setName< decltype( *this ) >( name ); }
466 
467 private:
468 
474  void dynamicallyGrowArray( INDEX_TYPE const i, INDEX_TYPE const increase )
475  {
477 
478  INDEX_TYPE const newArraySize = sizeOfArray( i ) + increase;
479  if( newArraySize > capacityOfArray( i ))
480  {
481  setCapacityOfArray( i, 2 * newArraySize );
482  }
483  }
484 };
485 
486 } /* namespace LvArray */
void emplace(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const pos, ARGS &&... args)
Construct a new value at position pos.
void assimilate(ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > &&src)
Steal the resources from an ArrayOfSets and convert it to an ArrayOfArrays.
#define LVARRAY_ASSERT(EXP)
Assert EXP is true with no message.
Definition: Macros.hpp:142
constexpr std::iterator_traits< ITER >::difference_type iterDistance(ITER first, ITER const last, std::input_iterator_tag)
constexpr ArrayOfArraysView< T, INDEX_TYPE const, false, BUFFER_TYPE > toView() const &
void emplace(INDEX_TYPE const i, INDEX_TYPE const j, ARGS &&... args) const
Insert a value into an array.
void emplaceBack(BUFFER &buf, std::ptrdiff_t const size, ARGS &&... args)
Construct a new value at the end of the buffer.
#define ARRAYOFARRAYS_CHECK_BOUNDS(i)
Check that i is a valid array index.
ArrayOfArrays(INDEX_TYPE const numArrays=0, INDEX_TYPE const defaultArrayCapacity=0)
Constructor.
INDEX_TYPE size_type
The integer type used for indexing, here for stl compatability.
void emplaceBack(INDEX_TYPE const i, ARGS &&... args)
Append a value to an array constructing it in place with the given arguments.
void reserve(INDEX_TYPE const newCapacity)
Reserve space for the given number of arrays.
void emplaceBack(INDEX_TYPE const i, ARGS &&... args) const
Append a value to an array.
INDEX_TYPE IndexType
The integer type used for indexing.
constexpr ArrayOfArraysView< T, INDEX_TYPE const, true, BUFFER_TYPE > toViewConstSizes() const &
void resize(INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0)
Set the number of arrays.
BUFFER_TYPE< INDEX_TYPE > m_offsets
void move(MemorySpace const space, bool touch=true) const
Move this ArrayOfArrays to the given memory space.
void insertIntoArray(INDEX_TYPE const i, INDEX_TYPE const j, ITER const first, ITER const last)
Insert values into an array.
INDEX_TYPE size() const
constexpr INDEX_TYPE_NC capacityOfArray(INDEX_TYPE const i) const
BUFFER_TYPE< SIZE_TYPE > m_sizes
Holds the size of each array.
T value_type
An alias for the type contained in the inner arrays, here for stl compatability.
void appendArray(ITER const first, ITER const last)
Append an array.
void compress(BUFFERS &... buffers)
Compress the arrays so that the values of each array are contiguous with no extra capacity in between...
This class implements an array of arrays like object with contiguous storage.
constexpr std::enable_if< std::is_signed< INDEX_TYPE >::value, bool >::type isPositive(INDEX_TYPE const i)
ArrayOfArraysView & operator=(ArrayOfArraysView const &)=default
Default copy assignment operator.
void resize(INDEX_TYPE const newSize, INDEX_TYPE const defaultArrayCapacity=0)
Set the number of arrays.
constexpr ArrayOfArraysView< T, INDEX_TYPE const, CONST_SIZES, BUFFER_TYPE > toView() const
void setCapacityOfArray(INDEX_TYPE const i, INDEX_TYPE const newCapacity)
Set the capacity of an array.
void insertArray(INDEX_TYPE const i, ITER const first, ITER const last)
Insert an array.
ArrayOfArrays(ArrayOfArrays const &src)
Copy constructor, performs a deep copy.
This class provides a view into an array of arrays like object.
void emplaceBackAtomic(INDEX_TYPE const i, ARGS &&... args) const
Append a value to an array in a thread safe manner.
void insertIntoArray(INDEX_TYPE const i, INDEX_TYPE const j, ITER const first, ITER const last) const
Insert values into an array.
constexpr INDEX_TYPE_NC sizeOfArray(INDEX_TYPE const i) const
void appendToArray(INDEX_TYPE const i, ITER const first, ITER const last) const
Append values to an array.
#define ARRAYOFARRAYS_CHECK_INSERT_BOUNDS(i)
Check that i is a valid index to insert an array at.
void appendToArray(INDEX_TYPE const i, ITER const first, ITER const last)
Append values to an array.
constexpr ArrayOfArraysView< T, INDEX_TYPE const, true, BUFFER_TYPE > toViewConstSizes() const
MemorySpace
An enum containing the available memory spaces.
The top level namespace.
Definition: Array.hpp:24
void appendArray(INDEX_TYPE const n)
Append an array.
Contains the implementation of LvArray::ArrayOfArraysView.
ArrayOfArrays & operator=(ArrayOfArrays &&src)
Default move assignment operator, performs a shallow copy.
This class implements an array of sets like object with contiguous storage.
void eraseFromArray(INDEX_TYPE const i, INDEX_TYPE const j, INDEX_TYPE const n=1) const
Erase values from an array.
void resizeFromCapacities(INDEX_TYPE const numSubArrays, INDEX_TYPE const *const capacities, BUFFERS &... buffers)
Clears the array and creates a new array with the given number of sub-arrays.
~ArrayOfArrays()
Destructor, frees the values, sizes and offsets buffers.
void setEqualTo(INDEX_TYPE const srcNumArrays, INDEX_TYPE const srcMaxOffset, BUFFER_TYPE< INDEX_TYPE > const &srcOffsets, BUFFER_TYPE< INDEX_TYPE > const &srcSizes, BUFFER_TYPE< T > const &srcValues, PAIRS_OF_BUFFERS &&... pairs)
Set this ArrayOfArraysView equal to the provided arrays.
void resize(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, std::ptrdiff_t const newSize, ARGS &&... args)
Resize the give array.
void clearArray(INDEX_TYPE const i)
Clear the given array.
INDEX_TYPE_NC m_numArrays
The number of arrays contained.
void emplace(INDEX_TYPE const i, INDEX_TYPE const j, ARGS &&... args)
Insert a value into an array constructing it in place.
void free(BUFFERS &... buffers)
Destroy all the objects held by this array and free all associated memory.
void setCapacityOfArray(INDEX_TYPE const i, INDEX_TYPE const newCapacity, BUFFERS &... buffers)
Set the capacity of the given array.
void assimilate(ArrayOfArraysView< T, INDEX_TYPE, CONST_SIZES, BUFFER_TYPE > &&src)
Steal the resources of src, clearing it in the process.
void move(MemorySpace const space, bool touch=true) const
Move this ArrayOfArrays to the given memory space.
std::string string
String type.
Definition: DataTypes.hpp:131
void setName(std::string const &name)
Set the name to be displayed whenever the underlying Buffer&#39;s user call back is called.
void eraseArray(INDEX_TYPE const i)
Erase an array.
ArrayOfArrays & operator=(ArrayOfArrays const &src)
Copy assignment operator, performs a deep copy.
void reserveValues(INDEX_TYPE const newValueCapacity, BUFFERS &... buffers)
Reserve space for the given number of values.
void resizeArray(INDEX_TYPE const i, INDEX_TYPE const newSize, ARGS &&... args)
Set the number of values in an array.
void erase(BUFFER &buf, std::ptrdiff_t const size, std::ptrdiff_t const pos)
Erase a value from the buffer.
constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toViewConst() const
T ValueType
An alias for the type contained in the inner arrays.
constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toViewConst() const &