MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
maia::container::Container< Derived, Invalid > Class Template Reference

#include <container.h>

Inheritance diagram for maia::container::Container< Derived, Invalid >:
[legend]

Classes

struct  Copy
 

Public Member Functions

constexpr Container ()=default
 Default c'tor does nothing. More...
 
constexpr MInt capacity () const
 Return capacity (i.e., maximum number of nodes) More...
 
void reset (const MInt capacity)
 Reset tree, re-create data structures with given capacity, and set size to zero. More...
 
void resize (const MInt capacity)
 Resize the container capacity. More...
 
constexpr MInt size () const
 Return size (i.e., currently used number of nodes) More...
 
void size (const MInt size_)
 Resize tree WITHOUT CONSIDERING ANY NODE CONSISTENCY! Use at own risk and remove ASAP... More...
 
void append (const MInt count)
 Append nodes to end of tree. More...
 
void append ()
 
void shrink (const MInt count)
 Remove nodes from end of tree. More...
 
void shrink ()
 
template<class T >
void copy (const T &source, const MInt begin, const MInt end, const MInt to)
 Copy nodes to another location without changing any parent/child/neighbor information. More...
 
template<class T >
void copy (const T &source, const MInt from, const MInt to)
 
void copy (const MInt begin, const MInt end, const MInt to)
 
void copy (const MInt from, const MInt to)
 
void move (const MInt begin, const MInt end, const MInt to)
 Move nodes to another location and update parent/child/neighbor information accordingly. More...
 
void move (const MInt from, const MInt to)
 
void swap (const MInt a, const MInt b)
 Swap two nodes and update parent/child/neighbor information accordingly. More...
 
void insert (const MInt begin, const MInt count)
 
void insert (const MInt id)
 
void erase (const MInt begin, const MInt end)
 Erase nodes in range [begin, end) and update parent/child/neighbor information. More...
 
void erase (const MInt id)
 
void removeAndShift (const MInt begin, const MInt end)
 
void removeAndShift (const MInt id)
 
void removeAndFill (const MInt begin, const MInt end)
 
void removeAndFill (const MInt id)
 
void clear ()
 Clear tree by invalidating all nodes and setting size to zero. More...
 
Derived & derived ()
 
const Derived & derived () const
 
template<class T >
void rawCopy (const T &source, const MInt begin, const MInt end, const MInt to)
 Copy range of nodes [begin, end) to range starting at 'to'. More...
 
template<class T >
void rawCopy (const T &source, const MInt from, const MInt to)
 
void deleteConnectivity (const MInt NotUsed(begin), const MInt NotUsed(end))
 
void moveConnectivity (const MInt NotUsed(begin), const MInt NotUsed(end), const MInt NotUsed(to))
 
void moveConnectivity (const MInt from, const MInt to)
 
constexpr MInt dummy () const
 

Protected Types

template<class T >
using Storage = std::vector< T >
 

Protected Member Functions

template<class T >
void resetStorage (const MInt n, Storage< T > &c)
 Create new container with given size and replace original one. More...
 
template<class T >
void resizeStorage (const MInt n, Storage< T > &c)
 Resize container with given size. More...
 
virtual void resize ()
 
template<typename Container_ , typename T = typename Container_::value_type>
void fill_invalid (Container_ &c, const MInt begin, const MInt end, const MInt solverSize=1, const T value=Invalid< T >::value())
 
template<typename Container_ , typename Functor >
void copyData (const Container_ &source, Container_ &target, Functor &&f, const MInt begin, const MInt end, const MInt dest, const MInt solverSize=1)
 Copy [begin, end) range with given solver size from source to dest position of target. More...
 
MBool isValidId (const MInt id) const
 Return whether given id refers to a valid node (auxiliary method). More...
 

Private Attributes

MInt m_capacity = 0
 
MInt m_size = 0
 

Detailed Description

template<class Derived, template< class > class Invalid>
class maia::container::Container< Derived, Invalid >

Base class for collector-like containers.

To use this as a base class for a specific container type, a derived class must implement the following:

  • template <class Functor, class T>
  • void invalidate(MInt begin, MInt end)
  • void reset()
  • void resize() Optionally, the following methods may be implemented to maintain internal connectivity on delete and move operations:
  • void moveConnectivity(MInt begin, MInt end, MInt to)
  • void deleteConnectivity(MInt begin, MInt end)

Definition at line 77 of file container.h.

Member Typedef Documentation

◆ Storage

template<class Derived , template< class > class Invalid>
template<class T >
using maia::container::Container< Derived, Invalid >::Storage = std::vector<T>
protected

Definition at line 121 of file container.h.

Constructor & Destructor Documentation

◆ Container()

template<class Derived , template< class > class Invalid>
constexpr maia::container::Container< Derived, Invalid >::Container ( )
constexprdefault

Member Function Documentation

◆ append() [1/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::append ( )
inline

Definition at line 94 of file container.h.

94{ append(1); }

◆ append() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::append ( const MInt  count)

Definition at line 223 of file container.h.

223 {
224 ENSURE_CONDITION(count >= 0, "Count must be non-negative");
225
226 if(size() + count > capacity()) {
227 std::cerr << "Container capacity exceeded: is:" + std::to_string(capacity())
228 + ", desired: " + std::to_string(size() + count)
229 << std::endl;
230 // Allow containers to grow if required -> for now resize with +10% capacity
231 const MInt newCapacity = 1.1 * (size() + count);
232 std::cerr << "New container capacity is: " << newCapacity << std::endl;
233 resize(size() + count);
234 }
235 derived().invalidate(size(), size() + count);
236 m_size += count;
237}
constexpr MInt size() const
Return size (i.e., currently used number of nodes)
Definition: container.h:89
virtual void resize()
Definition: container.h:127
constexpr MInt capacity() const
Return capacity (i.e., maximum number of nodes)
Definition: container.h:85
int32_t MInt
Definition: maiatypes.h:62

◆ capacity()

template<class Derived , template< class > class Invalid>
constexpr MInt maia::container::Container< Derived, Invalid >::capacity ( ) const
inlineconstexpr

Definition at line 85 of file container.h.

85{ return m_capacity; }

◆ clear()

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::clear

Definition at line 395 of file container.h.

395 {
396 derived().invalidate(0, size());
397 m_size = 0;
398}

◆ copy() [1/4]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::copy ( const MInt  begin,
const MInt  end,
const MInt  to 
)
inline

Definition at line 103 of file container.h.

103{ copy(derived(), begin, end, to); }
void copy(const T &source, const MInt begin, const MInt end, const MInt to)
Copy nodes to another location without changing any parent/child/neighbor information.
Definition: container.h:253

◆ copy() [2/4]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::copy ( const MInt  from,
const MInt  to 
)
inline

Definition at line 104 of file container.h.

104{ copy(from, from + 1, to); }

◆ copy() [3/4]

template<class Derived , template< class > class Invalid>
template<class T >
void maia::container::Container< Derived, Invalid >::copy ( const T &  source,
const MInt  begin,
const MInt  end,
const MInt  to 
)

Definition at line 253 of file container.h.

253 {
254 ENSURE_CONDITION(begin >= 0 && begin < source.size(), "Begin position outside valid range");
255 ENSURE_CONDITION(end - 1 < source.size(), "End position outside valid range");
256 ENSURE_VALID_ID(to);
257 ENSURE_CONDITION(to + (end - begin) <= size(), "Target range outside valid size");
258
259 // Exit early if there is nothing to do
260 if(end <= begin || (&source == this && begin == to)) {
261 return;
262 }
263
264 rawCopy(source, begin, end, to);
265}
void rawCopy(const T &source, const MInt begin, const MInt end, const MInt to)
Copy range of nodes [begin, end) to range starting at 'to'.
Definition: container.h:404

◆ copy() [4/4]

template<class Derived , template< class > class Invalid>
template<class T >
void maia::container::Container< Derived, Invalid >::copy ( const T &  source,
const MInt  from,
const MInt  to 
)
inline

Definition at line 100 of file container.h.

100 {
101 copy(source, from, from + 1, to);
102 }

◆ copyData()

template<class Derived , template< class > class Invalid>
template<typename Container_ , typename Functor >
void maia::container::Container< Derived, Invalid >::copyData ( const Container_ &  source,
Container_ &  target,
Functor &&  f,
const MInt  begin,
const MInt  end,
const MInt  dest,
const MInt  solverSize = 1 
)
inlineprotected

Definition at line 138 of file container.h.

139 {
140 f(source.data() + begin * solverSize, source.data() + end * solverSize, target.data() + dest * solverSize);
141 }

◆ deleteConnectivity()

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::deleteConnectivity ( const MInt   NotUsedbegin,
const MInt   NotUsedend 
)
inline

Definition at line 169 of file container.h.

169{}

◆ derived() [1/2]

template<class Derived , template< class > class Invalid>
Derived & maia::container::Container< Derived, Invalid >::derived ( )
inline

Definition at line 146 of file container.h.

146{ return static_cast<Derived&>(*this); }

◆ derived() [2/2]

template<class Derived , template< class > class Invalid>
const Derived & maia::container::Container< Derived, Invalid >::derived ( ) const
inline

Definition at line 147 of file container.h.

147{ return static_cast<const Derived&>(*this); }

◆ dummy()

template<class Derived , template< class > class Invalid>
constexpr MInt maia::container::Container< Derived, Invalid >::dummy ( ) const
inlineconstexpr

Definition at line 172 of file container.h.

172{ return m_capacity; }

◆ erase() [1/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::erase ( const MInt  begin,
const MInt  end 
)

Definition at line 335 of file container.h.

335 {
336 ENSURE_VALID_ID(begin);
337 ENSURE_VALID_ID(end - 1);
338
339 // Exit early if there is nothing to do
340 if(end <= begin) {
341 return;
342 }
343
344 derived().deleteConnectivity(begin, end);
345 derived().invalidate(begin, end);
346}

◆ erase() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::erase ( const MInt  id)
inline

Definition at line 111 of file container.h.

111{ erase(id, id + 1); }
void erase(const MInt begin, const MInt end)
Erase nodes in range [begin, end) and update parent/child/neighbor information.
Definition: container.h:335

◆ fill_invalid()

template<class Derived , template< class > class Invalid>
template<typename Container_ , typename T = typename Container_::value_type>
void maia::container::Container< Derived, Invalid >::fill_invalid ( Container_ &  c,
const MInt  begin,
const MInt  end,
const MInt  solverSize = 1,
const T  value = Invalid<T>::value() 
)
inlineprotected

Definition at line 131 of file container.h.

132 {
133 std::fill(c.data() + begin * solverSize, c.data() + end * solverSize, value);
134 }

◆ insert() [1/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::insert ( const MInt  id,
const MInt  count 
)

Insert 'count' nodes and push back existing nodes while updating parent/child/neighbor information.

Definition at line 318 of file container.h.

318 {
319 ENSURE_CONDITION(isValidId(id) || id == size(), "Id must refer to a valid node or be equal to size()");
320 ENSURE_CONDITION(count >= 0, "Count must be non-negative");
321 ENSURE_CONDITION(count + size() <= capacity(), "New size exceeds capacity");
322
323 // Exit early if there is nothing to do
324 if(count == 0) {
325 return;
326 }
327
328 m_size += count;
329 move(id, size() - count, id + count);
330}
void move(const MInt begin, const MInt end, const MInt to)
Move nodes to another location and update parent/child/neighbor information accordingly.
Definition: container.h:270
MBool isValidId(const MInt id) const
Return whether given id refers to a valid node (auxiliary method).
Definition: container.h:443

◆ insert() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::insert ( const MInt  id)
inline

Definition at line 109 of file container.h.

109{ insert(id, 1); }
void insert(const MInt begin, const MInt count)
Definition: container.h:318

◆ isValidId()

template<class Derived , template< class > class Invalid>
MBool maia::container::Container< Derived, Invalid >::isValidId ( const MInt  id) const
protected

Definition at line 443 of file container.h.

443 {
444 // @memSplitGrid Note: allow second last position to be used as a dummy id (needed for FV solver to
445 // work atm, changing to the last position requires changing the allocation of solver variables to
446 // be of size maxNoCells+1)
447 if(!((id >= 0 && id < size()) || id == capacity() - 1)) {
448 std::cerr << "id = " << id << std::endl
449 << "size() = " << size() << std::endl
450 << "capacity() = " << capacity() << std::endl;
451 }
452 return ((id >= 0 && id < size()) || id == capacity() - 1);
453}

◆ move() [1/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::move ( const MInt  begin,
const MInt  end,
const MInt  to 
)

Definition at line 270 of file container.h.

270 {
271 ENSURE_VALID_ID(begin);
272 ENSURE_VALID_ID(end - 1);
273 ENSURE_VALID_ID(to);
274 ENSURE_CONDITION(to + (end - begin) - 1 < size(), "Target range outside valid size");
275
276 // Exit early if there is nothing to do
277 if(end <= begin || begin == to) {
278 return;
279 }
280
281 rawCopy(derived(), begin, end, to);
282 derived().moveConnectivity(begin, end, to);
283 derived().invalidate(begin, end);
284}

◆ move() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::move ( const MInt  from,
const MInt  to 
)
inline

Definition at line 106 of file container.h.

106{ move(from, from + 1, to); }

◆ moveConnectivity() [1/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::moveConnectivity ( const MInt  from,
const MInt  to 
)
inline

Definition at line 171 of file container.h.

171{ moveConnectivity(from, from + 1, to); }
void moveConnectivity(const MInt NotUsed(begin), const MInt NotUsed(end), const MInt NotUsed(to))
Definition: container.h:170

◆ moveConnectivity() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::moveConnectivity ( const MInt   NotUsedbegin,
const MInt   NotUsedend,
const MInt   NotUsedto 
)
inline

Definition at line 170 of file container.h.

170{}

◆ rawCopy() [1/2]

template<class Derived , template< class > class Invalid>
template<class T >
void maia::container::Container< Derived, Invalid >::rawCopy ( const T &  source,
const MInt  begin,
const MInt  end,
const MInt  to 
)

Definition at line 404 of file container.h.

404 {
405 // Use different methods for overlapping and non-overlapping ranges, depending on overlap type
406 if(to < begin || to >= end) {
407 // Non-overlapping ranges or overlap while copying left can be handled by std::copy
408 derived().rawCopyGeneric(Copy<true>{}, source, begin, end, to);
409 } else {
410 // Overlap while copying right can be handled by std::copy_backward
411 const MInt count = end - begin;
412 derived().rawCopyGeneric(Copy<false>{}, source, begin, end, to + count);
413 }
414}

◆ rawCopy() [2/2]

template<class Derived , template< class > class Invalid>
template<class T >
void maia::container::Container< Derived, Invalid >::rawCopy ( const T &  source,
const MInt  from,
const MInt  to 
)
inline

Definition at line 166 of file container.h.

166 {
167 rawCopy(source, from, from + 1, to);
168 }

◆ removeAndFill() [1/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::removeAndFill ( const MInt  begin,
const MInt  end 
)

Remove nodes in range [begin, end) and fill gap with nodes from end while updating parent/child/neighbor information.

Definition at line 374 of file container.h.

374 {
375 ENSURE_VALID_ID(begin);
376 ENSURE_VALID_ID(end - 1);
377
378 // Exit early if there is nothing to do
379 if(end <= begin) {
380 return;
381 }
382
383 derived().deleteConnectivity(begin, end);
384 derived().invalidate(begin, end);
385 const MInt count = end - begin;
386 if(end < size()) {
387 move(std::max(end, size() - count), size(), begin);
388 }
389 m_size -= count;
390}

◆ removeAndFill() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::removeAndFill ( const MInt  id)
inline

Definition at line 115 of file container.h.

115{ removeAndFill(id, id + 1); }
void removeAndFill(const MInt begin, const MInt end)
Definition: container.h:374

◆ removeAndShift() [1/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::removeAndShift ( const MInt  begin,
const MInt  end 
)

Remove nodes in range [begin, end) and pull forward existing nodes while updating parent/child/neighbor information.

Definition at line 352 of file container.h.

352 {
353 ENSURE_VALID_ID(begin);
354 ENSURE_VALID_ID(end - 1);
355
356 // Exit early if there is nothing to do
357 if(end <= begin) {
358 return;
359 }
360
361 derived().deleteConnectivity(begin, end);
362 derived().invalidate(begin, end);
363 if(end < size()) {
364 move(end, size(), begin);
365 }
366 const MInt count = end - begin;
367 m_size -= count;
368}

◆ removeAndShift() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::removeAndShift ( const MInt  id)
inline

Definition at line 113 of file container.h.

113{ removeAndShift(id, id + 1); }
void removeAndShift(const MInt begin, const MInt end)
Definition: container.h:352

◆ reset()

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::reset ( const MInt  capacity)

Definition at line 187 of file container.h.

187 {
188 ENSURE_CONDITION(capacity_ >= 0, "Capacity must be non-negative");
189
190 m_capacity = capacity_;
191 m_size = 0;
192
193 derived().reset();
194}

◆ resetStorage()

template<class Derived , template< class > class Invalid>
template<class T >
void maia::container::Container< Derived, Invalid >::resetStorage ( const MInt  n,
Storage< T > &  c 
)
protected

Definition at line 420 of file container.h.

420 {
421 // Note: Containers are always initialized with an invalid value as for, e.g., std::vector, all
422 // entries are default initialized anyways and not providing an invalid value would zero all int's
423 // and probably all float's as well.
424 Storage<T>(std::max((m_capacity + 1) * n, 1), Invalid<T>::value()).swap(c);
425 // TODO: This step is critical for OpenMP parallelization. In most numa
426 // architecture data is placed by a 'first touch'-policy.
427 // Storage(==std::vector) does a sequential initialization, i.e., it touches
428 // all memory from the core associated with the main thread. That might
429 // introduce memory access penalty in OpenMP usage.
430}

◆ resize() [1/2]

template<class Derived , template< class > class Invalid>
virtual void maia::container::Container< Derived, Invalid >::resize ( )
inlineprotectedvirtual

◆ resize() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::resize ( const MInt  capacity)

Definition at line 199 of file container.h.

199 {
200 ENSURE_CONDITION(capacity_ >= 0, "Capacity must be non-negative");
201 ENSURE_CONDITION(capacity_ >= m_capacity, "So far only resize to a larger size is tested.");
202
203 if(capacity_ == m_capacity) return;
204
205 m_capacity = capacity_;
206
207 derived().resize();
208}

◆ resizeStorage()

template<class Derived , template< class > class Invalid>
template<class T >
void maia::container::Container< Derived, Invalid >::resizeStorage ( const MInt  n,
Storage< T > &  c 
)
protected

Definition at line 436 of file container.h.

436 {
437 c.resize(std::max((m_capacity + 1) * n, 1), Invalid<T>::value());
438}

◆ shrink() [1/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::shrink ( )
inline

Definition at line 96 of file container.h.

96{ shrink(1); }

◆ shrink() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::shrink ( const MInt  count)

Definition at line 242 of file container.h.

242 {
243 ENSURE_CONDITION(count >= 0, "Count must be non-negative");
244 ENSURE_CONDITION(size() - count >= 0, "New size below zero");
245
246 removeAndShift(size() - count, size());
247}

◆ size() [1/2]

template<class Derived , template< class > class Invalid>
constexpr MInt maia::container::Container< Derived, Invalid >::size ( ) const
inlineconstexpr

Definition at line 89 of file container.h.

89{ return m_size; }

◆ size() [2/2]

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::size ( const MInt  size_)

Definition at line 213 of file container.h.

213 {
214 ENSURE_CONDITION(size_ >= 0, "Size must be non-negative");
215 ENSURE_CONDITION(size_ <= capacity(), "Size must not exceed capacity");
216
217 m_size = size_;
218}

◆ swap()

template<class Derived , template< class > class Invalid>
void maia::container::Container< Derived, Invalid >::swap ( const MInt  a,
const MInt  b 
)

Definition at line 289 of file container.h.

289 {
290 ENSURE_VALID_ID(a);
291 ENSURE_VALID_ID(b);
292
293 // Exit early if a and b are the same node
294 if(a == b) {
295 return;
296 }
297
298 // Move a to dummy position
299 rawCopy(derived(), a, dummy());
300 derived().moveConnectivity(a, dummy());
301
302 // Move b to a
303 rawCopy(derived(), b, a);
304 derived().moveConnectivity(b, a);
305
306 // Move from dummy position to b
307 rawCopy(derived(), dummy(), b);
308 derived().moveConnectivity(dummy(), b);
309
310 // Invalidate dummy to be sure
311 derived().invalidate(dummy(), dummy() + 1);
312}
constexpr MInt dummy() const
Definition: container.h:172
Definition: contexttypes.h:19

Member Data Documentation

◆ m_capacity

template<class Derived , template< class > class Invalid>
MInt maia::container::Container< Derived, Invalid >::m_capacity = 0
private

Definition at line 180 of file container.h.

◆ m_size

template<class Derived , template< class > class Invalid>
MInt maia::container::Container< Derived, Invalid >::m_size = 0
private

Definition at line 181 of file container.h.


The documentation for this class was generated from the following file: