Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
mdspan_matrix_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
52#include <iostream>
53#include <vector>
54
55#include <ah-mdspan.H>
56#include <print_rule.H>
57
58using namespace Aleph;
59
60namespace
61{
62void demo_row_major()
63{
64 std::cout << "[1] Row-major view (Aleph::layout_right, the default)\n";
65 print_rule();
66
67 // A 3x4 matrix stored flat, as numerical code typically hands it over.
68 std::vector<double> buf(3 * 4, 0.0);
69 mdspan<double, dextents<size_t, 2>> m(buf.data(), 3, 4);
70
71 for (size_t i = 0; i < m.extent(0); ++i)
72 for (size_t j = 0; j < m.extent(1); ++j)
73 m(i, j) = static_cast<double>(i * 10 + j);
74
75 std::cout << "m(" << m.extent(0) << "x" << m.extent(1) << "):\n";
76 for (size_t i = 0; i < m.extent(0); ++i)
77 {
78 for (size_t j = 0; j < m.extent(1); ++j)
79 std::cout << m(i, j) << "\t";
80 std::cout << "\n";
81 }
82 std::cout << "Row-major: m(1, 2) sits at buf[1*4 + 2] = buf["
83 << (1 * m.extent(1) + 2) << "] = " << buf[1 * 4 + 2] << "\n\n";
84}
85
87{
88 std::cout << "[2] Column-major view (Aleph::layout_left) over the same data\n";
89 print_rule();
90
91 // Same flat buffer, no copy: just a different layout policy on top.
92 std::vector<int> buf(2 * 3);
94
95 int next = 0;
96 for (size_t j = 0; j < m.extent(1); ++j) // fastest-varying axis is first
97 for (size_t i = 0; i < m.extent(0); ++i)
98 m(i, j) = next++;
99
100 std::cout << "Column-major fill order produced buf = [";
101 for (size_t k = 0; k < buf.size(); ++k)
102 std::cout << buf[k] << (k + 1 < buf.size() ? ", " : "");
103 std::cout << "]\n";
104 std::cout << "(first axis varies fastest: m(0,0),m(1,0),m(0,1),... -- the\n"
105 " layout a Fortran/BLAS routine would expect from this buffer)\n\n";
106}
107
109{
110 std::cout << "[3] Read-only view over const data\n";
111 print_rule();
112
113 const std::vector<double> readings{98.6, 99.1, 97.9, 100.2};
115
116 double total = 0.0;
117 for (size_t i = 0; i < m.extent(0); ++i)
118 total += m(i);
119 std::cout << "Average of " << m.extent(0) << " read-only samples: "
120 << (total / static_cast<double>(m.extent(0))) << "\n";
121 std::cout << "(m(0) = readings[0]; the view has no write access to \"const double\")\n\n";
122}
123} // namespace
124
125int main()
126{
127 std::cout << "\n=== Aleph::mdspan: zero-copy matrix views ===\n\n";
128
132
133 std::cout << "Done.\n";
134 return 0;
135}
Non-owning multidimensional array view (Aleph::mdspan).
Non-owning view of a contiguous buffer as a multidimensional grid.
Definition ah-mdspan.H:576
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
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
Column-major layout: the first axis varies fastest (Fortran convention).
Definition ah-mdspan.H:467
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k