Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
mdspan_ca_lattice_view_example.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
54#include <cassert>
55#include <iostream>
56
57#include <print_rule.H>
58#include <tpl_ca_storage.H>
59
60using namespace Aleph;
61using namespace Aleph::CA;
62
63namespace
64{
66{
67 std::cout << "[1] .view() aliases the storage's own buffer\n";
68 print_rule();
69
71 auto v = grid.view();
72
73 v(1, 1) = 5; // write through the view
74 std::cout << "grid.at(1, 1) after v(1,1)=5: " << grid.at(1, 1) << " (expect 5)\n";
75
76 grid.set(0, 0, 9); // write through the storage API
77 std::cout << "v(0, 0) after grid.set(0,0,9): " << v(0, 0) << " (expect 9)\n\n";
78}
79
80// A minimal 4-neighbour (von Neumann) sum, reading interior cells only —
81// the kind of loop that benefits from plain m(i, j) indexing. Must not be used on edge cells.
82int neighbour_sum(const Dense_Cell_Storage<int, 2>::const_view_type &v, size_t i, size_t j)
83{
84 assert(i > 0 and j > 0);
85 return v(i - 1, j) + v(i + 1, j) + v(i, j - 1) + v(i, j + 1);
86}
87
89{
90 std::cout << "[2] A tiny stencil kernel driven directly through the view\n";
91 print_rule();
92
93 Dense_Cell_Storage<int, 2> grid({4, 4}, 1); // every cell starts at 1
94 auto v = grid.view();
95
96 std::cout << "Sum of the 4 orthogonal neighbours of every interior cell\n"
97 "(each has value 1, so every sum should be 4):\n";
98 for (size_t i = 1; i + 1 < v.extent(0); ++i)
99 {
100 for (size_t j = 1; j + 1 < v.extent(1); ++j)
101 std::cout << neighbour_sum(v, i, j) << " ";
102 std::cout << "\n";
103 }
104 std::cout << "\n";
105}
106
108{
109 for (size_t i = 0; i < v.extent(0); ++i)
110 {
111 for (size_t j = 0; j < v.extent(1); ++j)
112 std::cout << v(i, j) << " ";
113 std::cout << "\n";
114 }
115}
116
118{
119 std::cout << "[3] Read-only view for a function that only reads the lattice\n";
120 print_rule();
121
123 int next = 0;
124 for (size_t i = 0; i < 2; ++i)
125 for (size_t j = 0; j < 3; ++j)
126 grid.set(i, j, next++);
127
128 print_view(grid.view()); // grid is non-const, so view() returns view_type which converts to const_view_type in print_view
129 std::cout << "\n";
130}
131} // namespace
132
133int main()
134{
135 std::cout << "\n=== Dense_Cell_Storage::view(): mdspan over a CA lattice ===\n\n";
136
140
141 std::cout << "Done.\n";
142 return 0;
143}
Row-major dense storage for N-dimensional cellular automata.
view_type view() noexcept
Return a mutable Aleph::mdspan view of this storage.
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.
Non-owning view of a contiguous buffer as a multidimensional grid.
Definition ah-mdspan.H:576
constexpr index_type extent(rank_type r) const noexcept
Run-time size of axis r.
Definition ah-mdspan.H:678
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:171
Dense, contiguous storage for cellular automata cells (1D/2D/3D).