Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::CA::Dense_Cell_Storage< T, N > Class Template Reference

Row-major dense storage for N-dimensional cellular automata. More...

#include <tpl_ca_storage.H>

Inheritance diagram for Aleph::CA::Dense_Cell_Storage< T, N >:
[legend]
Collaboration diagram for Aleph::CA::Dense_Cell_Storage< T, N >:
[legend]

Public Types

using state_type = T
 
using coord_type = Coord_Vec< N >
 
using extents_type = std::array< ca_size_t, N >
 
using view_type = Aleph::mdspan< T, Aleph::dextents< ca_size_t, N > >
 Non-owning, zero-copy N-dimensional view of this storage's buffer.
 
using const_view_type = Aleph::mdspan< const T, Aleph::dextents< ca_size_t, N > >
 Non-owning, zero-copy read-only N-dimensional view of this storage's buffer.
 

Public Member Functions

 Dense_Cell_Storage ()=default
 Construct an empty storage (zero extents, no cells allocated).
 
 Dense_Cell_Storage (const extents_type &extents, const T &init=T{})
 Construct a storage with the given extents and initial value.
 
 Dense_Cell_Storage (const Dense_Cell_Storage &other)=default
 Copy another storage: its extents and its cells, which the copy keeps apart from those of other.
 
 Dense_Cell_Storage (Dense_Cell_Storage &&other) noexcept
 Take the cells of another storage, which is left empty.
 
Dense_Cell_Storage & operator= (const Dense_Cell_Storage &other)=default
 Copy another storage into this one: its extents and its cells, which this one keeps apart from those of other.
 
Dense_Cell_Storage & operator= (Dense_Cell_Storage &&other) noexcept
 Exchange this storage with another, which is left with the previous cells of this one.
 
ca_size_t size () const noexcept
 
ca_size_t size (std::size_t d) const
 Extent of axis d.
 
const extents_type & extents () const noexcept
 
ca_size_t linear_index (const coord_type &c) const
 Map a multi-index to a linear offset.
 
T at (const coord_type &c) const
 Read access through a multi-index.
 
void set (const coord_type &c, const T &v)
 Write access through a multi-index.
 
T at (ca_index_t i) const
 1D shorthand for at({i}).
 
T at (ca_index_t i, ca_index_t j) const
 2D shorthand for at({i, j}).
 
T at (ca_index_t i, ca_index_t j, ca_index_t k) const
 3D shorthand for at({i, j, k}).
 
void set (ca_index_t i, const T &v)
 1D shorthand for set({i}, v).
 
void set (ca_index_t i, ca_index_t j, const T &v)
 2D shorthand for set({i, j}, v).
 
void set (ca_index_t i, ca_index_t j, ca_index_t k, const T &v)
 3D shorthand for set({i, j, k}, v).
 
T * data () noexcept
 
const T * data () const noexcept
 
view_type view () noexcept
 Return a mutable Aleph::mdspan view of this storage.
 
const_view_type view () const noexcept
 
void swap (Dense_Cell_Storage &other) noexcept
 O(1) swap with another storage of the same type.
 
void fill (const T &value)
 Set every cell to value.
 

Static Public Member Functions

static constexpr std::size_t dimension () noexcept
 

Static Public Attributes

static constexpr std::size_t rank = N
 Number of axes known at compile time.
 

Static Private Member Functions

static ca_size_t product (const extents_type &e)
 

Private Attributes

extents_type ext_ {}
 per-axis extents
 
ca_size_t total_cells_ = 0
 product of ext
 
Array< T > buffer_
 contiguous row-major storage
 

Detailed Description

template<typename T, std::size_t N>
class Aleph::CA::Dense_Cell_Storage< T, N >

Row-major dense storage for N-dimensional cellular automata.

Stores extents()[0] * extents()[1] * ... * extents()[N-1] cells of type T in a single contiguous buffer. Indexing is row-major (axis N-1 varies fastest), matching the convention of C arrays and of al-matrix.H.

The class satisfies the CA::LatticeLike concept directly: it exposes dimension(), size(d), at(coord) and set(coord, v). Higher-level boundary handling is layered on top by Lattice<Storage, Boundary> in tpl_ca_lattice.H.

Template Parameters
Tcell state type. Must satisfy CA::CellState.
Nnumber of axes (1, 2 or 3 are the typical values).
Complexity
  • at, set, linear_index, data, size, extents, view: O(1).
  • swap: O(1) (swaps the internal Array<T> pointers).
  • fill: O(size()).
Thread-safety
Concurrent reads on different cells are safe. Concurrent writes to overlapping cells are not synchronised — engines are expected to use a double-buffer pattern.

Definition at line 97 of file tpl_ca_storage.H.

Member Typedef Documentation

◆ const_view_type

template<typename T , std::size_t N>
using Aleph::CA::Dense_Cell_Storage< T, N >::const_view_type = Aleph::mdspan<const T, Aleph::dextents<ca_size_t, N> >

Non-owning, zero-copy read-only N-dimensional view of this storage's buffer.

Definition at line 312 of file tpl_ca_storage.H.

◆ coord_type

template<typename T , std::size_t N>
using Aleph::CA::Dense_Cell_Storage< T, N >::coord_type = Coord_Vec<N>

Definition at line 104 of file tpl_ca_storage.H.

◆ extents_type

template<typename T , std::size_t N>
using Aleph::CA::Dense_Cell_Storage< T, N >::extents_type = std::array<ca_size_t, N>

Definition at line 105 of file tpl_ca_storage.H.

◆ state_type

template<typename T , std::size_t N>
using Aleph::CA::Dense_Cell_Storage< T, N >::state_type = T

Definition at line 103 of file tpl_ca_storage.H.

◆ view_type

template<typename T , std::size_t N>
using Aleph::CA::Dense_Cell_Storage< T, N >::view_type = Aleph::mdspan<T, Aleph::dextents<ca_size_t, N> >

Non-owning, zero-copy N-dimensional view of this storage's buffer.

Definition at line 309 of file tpl_ca_storage.H.

Constructor & Destructor Documentation

◆ Dense_Cell_Storage() [1/4]

template<typename T , std::size_t N>
Aleph::CA::Dense_Cell_Storage< T, N >::Dense_Cell_Storage ( )
default

Construct an empty storage (zero extents, no cells allocated).

◆ Dense_Cell_Storage() [2/4]

template<typename T , std::size_t N>
Aleph::CA::Dense_Cell_Storage< T, N >::Dense_Cell_Storage ( const extents_type &  extents,
const T &  init = T{} 
)
inlineexplicit

Construct a storage with the given extents and initial value.

Allocates and value-initialises prod(extents) cells of type T, each set to init.

Parameters
[in]extentsper-axis sizes.
[in]initinitial value for every cell (default: T{}).
Exceptions
std::bad_allocon allocation failure.

Definition at line 143 of file tpl_ca_storage.H.

◆ Dense_Cell_Storage() [3/4]

template<typename T , std::size_t N>
Aleph::CA::Dense_Cell_Storage< T, N >::Dense_Cell_Storage ( const Dense_Cell_Storage< T, N > &  other)
default

Copy another storage: its extents and its cells, which the copy keeps apart from those of other.

Parameters
[in]otherstorage to copy.

◆ Dense_Cell_Storage() [4/4]

template<typename T , std::size_t N>
Aleph::CA::Dense_Cell_Storage< T, N >::Dense_Cell_Storage ( Dense_Cell_Storage< T, N > &&  other)
inlinenoexcept

Take the cells of another storage, which is left empty.

The implicit move took the buffer but copied the extents: the moved-from storage then indexed a buffer that it no longer had.

Parameters
[in,out]otherstorage to move from.

Definition at line 160 of file tpl_ca_storage.H.

Member Function Documentation

◆ at() [1/4]

template<typename T , std::size_t N>
T Aleph::CA::Dense_Cell_Storage< T, N >::at ( ca_index_t  i) const
inline

1D shorthand for at({i}).

Definition at line 255 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::at().

◆ at() [2/4]

template<typename T , std::size_t N>
T Aleph::CA::Dense_Cell_Storage< T, N >::at ( ca_index_t  i,
ca_index_t  j 
) const
inline

2D shorthand for at({i, j}).

Definition at line 262 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::at().

◆ at() [3/4]

template<typename T , std::size_t N>
T Aleph::CA::Dense_Cell_Storage< T, N >::at ( ca_index_t  i,
ca_index_t  j,
ca_index_t  k 
) const
inline

3D shorthand for at({i, j, k}).

Definition at line 269 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::at(), and k.

◆ at() [4/4]

template<typename T , std::size_t N>
T Aleph::CA::Dense_Cell_Storage< T, N >::at ( const coord_type &  c) const
inline

Read access through a multi-index.

Parameters
[in]cmulti-index, in range.
Returns
value stored at c.
Exceptions
std::out_of_rangeif c is out of range.

Definition at line 239 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::buffer_, and Aleph::CA::Dense_Cell_Storage< T, N >::linear_index().

Referenced by Aleph::CA::Dense_Cell_Storage< T, N >::at(), Aleph::CA::Dense_Cell_Storage< T, N >::at(), Aleph::CA::Dense_Cell_Storage< T, N >::at(), and TEST().

◆ data() [1/2]

template<typename T , std::size_t N>
const T * Aleph::CA::Dense_Cell_Storage< T, N >::data ( ) const
inlinenoexcept
Returns
read-only pointer to the first cell (or nullptr if empty).

Definition at line 303 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::buffer_, and Aleph::CA::Dense_Cell_Storage< T, N >::total_cells_.

◆ data() [2/2]

template<typename T , std::size_t N>
T * Aleph::CA::Dense_Cell_Storage< T, N >::data ( )
inlinenoexcept

◆ dimension()

template<typename T , std::size_t N>
static constexpr std::size_t Aleph::CA::Dense_Cell_Storage< T, N >::dimension ( )
inlinestaticconstexprnoexcept
Returns
the number of axes (compile-time constant).

Definition at line 184 of file tpl_ca_storage.H.

References N.

Referenced by TEST().

◆ extents()

template<typename T , std::size_t N>
const extents_type & Aleph::CA::Dense_Cell_Storage< T, N >::extents ( ) const
inlinenoexcept

◆ fill()

template<typename T , std::size_t N>
void Aleph::CA::Dense_Cell_Storage< T, N >::fill ( const T &  value)
inline

◆ linear_index()

template<typename T , std::size_t N>
ca_size_t Aleph::CA::Dense_Cell_Storage< T, N >::linear_index ( const coord_type &  c) const
inline

Map a multi-index to a linear offset.

Layout is row-major: the last axis varies fastest. All components of c must be non-negative and within range; this method does the bounds check and throws on violation.

Parameters
[in]cmulti-index.
Returns
linear offset in [0, size()).
Exceptions
std::out_of_rangeif any component is out of range.

Definition at line 221 of file tpl_ca_storage.H.

References ah_out_of_range_error_if, Aleph::CA::Dense_Cell_Storage< T, N >::ext_, and N.

Referenced by Aleph::CA::Dense_Cell_Storage< T, N >::at(), and Aleph::CA::Dense_Cell_Storage< T, N >::set().

◆ operator=() [1/2]

template<typename T , std::size_t N>
Dense_Cell_Storage & Aleph::CA::Dense_Cell_Storage< T, N >::operator= ( const Dense_Cell_Storage< T, N > &  other)
default

Copy another storage into this one: its extents and its cells, which this one keeps apart from those of other.

Parameters
[in]otherstorage to copy.
Returns
this storage.

◆ operator=() [2/2]

template<typename T , std::size_t N>
Dense_Cell_Storage & Aleph::CA::Dense_Cell_Storage< T, N >::operator= ( Dense_Cell_Storage< T, N > &&  other)
inlinenoexcept

Exchange this storage with another, which is left with the previous cells of this one.

Parameters
[in,out]otherstorage to move from.
Returns
this storage.

Definition at line 177 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::swap().

◆ product()

template<typename T , std::size_t N>
static ca_size_t Aleph::CA::Dense_Cell_Storage< T, N >::product ( const extents_type &  e)
inlinestaticprivate

Definition at line 115 of file tpl_ca_storage.H.

References ah_overflow_error_if, Aleph::and, and N.

◆ set() [1/4]

template<typename T , std::size_t N>
void Aleph::CA::Dense_Cell_Storage< T, N >::set ( ca_index_t  i,
ca_index_t  j,
ca_index_t  k,
const T &  v 
)
inline

3D shorthand for set({i, j, k}, v).

Definition at line 290 of file tpl_ca_storage.H.

References k, and Aleph::CA::Dense_Cell_Storage< T, N >::set().

◆ set() [2/4]

template<typename T , std::size_t N>
void Aleph::CA::Dense_Cell_Storage< T, N >::set ( ca_index_t  i,
ca_index_t  j,
const T &  v 
)
inline

2D shorthand for set({i, j}, v).

Definition at line 283 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::set().

◆ set() [3/4]

template<typename T , std::size_t N>
void Aleph::CA::Dense_Cell_Storage< T, N >::set ( ca_index_t  i,
const T &  v 
)
inline

1D shorthand for set({i}, v).

Definition at line 276 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::set().

◆ set() [4/4]

template<typename T , std::size_t N>
void Aleph::CA::Dense_Cell_Storage< T, N >::set ( const coord_type &  c,
const T &  v 
)
inline

Write access through a multi-index.

Parameters
[in]cmulti-index, in range.
[in]vnew value.
Exceptions
std::out_of_rangeif c is out of range.

Definition at line 249 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::buffer_, and Aleph::CA::Dense_Cell_Storage< T, N >::linear_index().

Referenced by Aleph::CA::Dense_Cell_Storage< T, N >::set(), Aleph::CA::Dense_Cell_Storage< T, N >::set(), Aleph::CA::Dense_Cell_Storage< T, N >::set(), TEST(), TEST(), TEST(), and TEST().

◆ size() [1/2]

◆ size() [2/2]

template<typename T , std::size_t N>
ca_size_t Aleph::CA::Dense_Cell_Storage< T, N >::size ( std::size_t  d) const
inline

Extent of axis d.

Parameters
[in]daxis index in [0, N).
Exceptions
std::out_of_rangeif d >= N.

Definition at line 199 of file tpl_ca_storage.H.

References ah_out_of_range_error_if, and Aleph::CA::Dense_Cell_Storage< T, N >::ext_.

◆ swap()

◆ view() [1/2]

template<typename T , std::size_t N>
Aleph::CA::Dense_Cell_Storage< T, N >::view ( ) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Complexity
O(1).

Definition at line 345 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::data(), and Aleph::CA::Dense_Cell_Storage< T, N >::ext_.

◆ view() [2/2]

template<typename T , std::size_t N>
view_type Aleph::CA::Dense_Cell_Storage< T, N >::view ( )
inlinenoexcept

Return a mutable Aleph::mdspan view of this storage.

The view is a pointer plus the current extents(): it aliases this object's buffer directly (no allocation, no copy). It is invalidated by anything that would also invalidate data() (e.g. swap() with another storage, or this being destroyed) but not by an in-place set()/fill()/at() through the original storage or through the view itself — both read and write the same cells.

Complexity
O(1).
Example
auto v = grid.view();
v(1, 2) = 5.0; // same cell as grid.at({1, 2})
for (ca_size_t i = 0; i < v.extent(0); ++i)
for (ca_size_t j = 0; j < v.extent(1); ++j)
std::cout << v(i, j) << " ";
Row-major dense storage for N-dimensional cellular automata.
view_type view() noexcept
Return a mutable Aleph::mdspan view of this storage.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63

Definition at line 336 of file tpl_ca_storage.H.

References Aleph::CA::Dense_Cell_Storage< T, N >::data(), and Aleph::CA::Dense_Cell_Storage< T, N >::ext_.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Dense_Cell_Storage< int, 2 > >::use().

Member Data Documentation

◆ buffer_

◆ ext_

◆ rank

template<typename T , std::size_t N>
constexpr std::size_t Aleph::CA::Dense_Cell_Storage< T, N >::rank = N
staticconstexpr

Number of axes known at compile time.

Definition at line 108 of file tpl_ca_storage.H.

Referenced by TEST().

◆ total_cells_


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