Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
packed_matrix_example.cc
Go to the documentation of this file.
1/*
2 Aleph-w: Data structures & Algorithms
3 Copyright (c) 2026 Leandro Rabindranath Leon
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 SOFTWARE.
22*/
23
37#include <tpl_packed_matrix.H>
38#include <tpl_sort_utils.H>
39
40#include <array>
41#include <cstddef>
42#include <cstdint>
43#include <iostream>
44#include <stdexcept>
45
46using namespace Aleph;
47
48namespace
49{
51 enum Col : size_t { side, level, qty, delta, live, price };
52
54 using Book = Packed_Matrix<between(-1, 3), // side: 5 values, 3 bits
55 between(0, 15), // level: 4 bits
56 between(0, 1000), // qty: 1001 values, 10 bits
57 between(-500, 500), // delta: 1001 values, 10 bits
58 bits(1), // live
59 between(0, 99999)>; // price: 17 bits
60
61 static_assert(sizeof(Book::Row) == 8);
62
64 constexpr auto sensor_columns = [] {
65 std::array<Bit_Column, 200> columns{};
66 for (size_t j = 0; j < columns.size(); ++j)
67 switch (j % 4)
68 {
69 case 0: columns[j] = bits(1); break;
70 case 1: columns[j] = bits(8); break;
71 case 2: columns[j] = bits(11); break;
72 default: columns[j] = signed_bits(16); break;
73 }
74 return columns;
75 }();
76
79
83 void print(const Book::Row & row)
84 {
85 std::cout << " (";
86 for (size_t j = 0; j < Book::cols(); ++j)
87 std::cout << (j == 0 ? "" : ", ") << row(j);
88 std::cout << ")\n";
89 }
90}
91
92int main()
93{
94 std::cout << "The layout that the compiler worked out\n";
95 for (size_t j = 0; j < Book::cols(); ++j)
96 std::cout << " column " << j << ": [" << Book::Row::column_min(j) << ", "
97 << Book::Row::column_max(j) << "], width " << Book::Row::column_bits(j) << "\n";
98 std::cout << " a row: " << Book::Row::num_bits << " bits in " << sizeof(Book::Row)
99 << " bytes\n\n";
100
101 Book m(4); // every column starts at its lowest value
102
103 std::cout << "Reading and writing an entry\n";
104 m(0, qty) = 250; // unchecked, as Array::operator()
105 m(0, price) += 42;
106 m[0][delta] = -17; // checked: the indexes and the value
107 m.write(0, level, 9); // the same check, without a proxy
108 const std::int64_t q = m(0, qty);
109 std::cout << " qty " << q << ", price " << m.fast_read(0, price) << ", delta "
110 << m.read(0, delta) << "\n";
111
112 try
113 {
114 m[0][level] = 16; // out of [0, 15]
115 }
116 catch (const std::domain_error & e)
117 {
118 std::cout << " refused: " << e.what() << "\n";
119 }
120 try
121 {
122 m[4][level] = 1; // there are four rows
123 }
124 catch (const std::out_of_range & e)
125 {
126 std::cout << " refused: " << e.what() << "\n";
127 }
128
129 std::cout << "\nWhole rows\n";
130 m(1) = Book::Row::make(3, 2, 100, 0, 1, 500); // validated
131 m(2) = Book::Row::fast_make(-1, 15, 7, 500, 0, 9); // not validated: the fastest write
132 Book::Row row = m(1); // a row is a value
133 row(side) = 0;
134 m(3) = row;
135 for (const Book::Row & r : m)
136 print(r);
137
138 std::cout << "\nSorted: rows compare as their tuples do, at the cost of comparing a word\n";
139 introsort(m.begin(), m.end());
140 for (const Book::Row & r : m)
141 print(r);
142
143 std::cout << "\nMany columns, described by code\n";
144 Sensors s(3);
145 Sensors::Row reading{}; // a whole row, written on a copy
146 for (size_t j = 0; j < Sensors::cols(); ++j)
147 reading(j) = Sensors::Row::column_max(j);
148 s(1) = reading;
149 s[2][199] = -12345; // checked, as any other column
150 std::cout << " " << Sensors::cols() << " columns: " << Sensors::Row::num_bits << " bits in "
151 << sizeof(Sensors::Row) << " bytes per row\n"
152 << " column 199: " << s(0, 199) << " (lowest), " << s(1, 199) << " (highest), "
153 << s(2, 199) << "\n";
154
155 // What must have come out
156 const bool sorted = m(0) < m(1) and m(1) < m(2) and m(2) < m(3);
157 const bool expected =
158 q == 250 and m(0)(side) == -1 and m(3) == Book::Row::make(3, 2, 100, 0, 1, 500);
159 const bool sensors = s(0, 199) == -32768 and s(1, 199) == 32767 and s(2, 199) == -12345 and
160 s(1, 0) == 1 and s(1, 1) == 255 and s(1, 2) == 2047;
161 return sorted and expected and sensors ? 0 : 1;
162}
size_t row
Definition ca-c-api.h:115
Matrix whose columns are bit fields: an Aleph::Array of Packed_Row.
Minimal std::expected-style result type for C++20.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
constexpr std::uint32_t delta
File contains only the cells that changed relative to a baseline.
Main namespace for Aleph-w library functions.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
and
Check uniqueness with explicit hash + equality functors.
typename packed_matrix_detail::Columns_At< Columns, std::make_index_sequence< Columns.size()> >::Matrix Packed_Matrix_Of
Packed_Matrix whose columns are the entries of an array.
bool between(const Point &a, const Point &b, const Point &c)
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
constexpr Bit_Column signed_bits(const unsigned n)
Column of n bits with sign: the integers of [-2^(n-1), 2^(n-1) - 1].
void print(const DynList< Parc< Net > > &sp)
Print a semi-path to stdout.
Definition tpl_net.H:1202
int main()
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Gsl_Compatible_Rng r
Matrix whose columns are bit fields sized from integer ranges.
Comprehensive sorting algorithms and search utilities for Aleph-w.