Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
packed_matrix_test.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
56#include <tpl_packed_matrix.H>
57
58#include <gtest/gtest.h>
59
60#include <array>
61#include <compare>
62#include <cstddef>
63#include <cstdint>
64#include <limits>
65#include <random>
66#include <stdexcept>
67#include <string>
68#include <type_traits>
69#include <utility>
70
71#include <tpl_array.H>
72
73using namespace Aleph;
74using std::int64_t;
75using std::uint64_t;
76
77namespace
78{
79 constexpr int64_t Min = std::numeric_limits<int64_t>::min();
80 constexpr int64_t Max = std::numeric_limits<int64_t>::max();
81 constexpr size_t No_Index = std::numeric_limits<size_t>::max();
82
83 // ------------------------------------------------------------------
84 // What the compiler works out from the ranges
85 // ------------------------------------------------------------------
86
87 using Book = Packed_Matrix<between(-1, 3), between(0, 15), between(0, 1000),
88 between(-500, 500), bits(1), between(0, 99999)>;
89
90 static_assert(Book::Row::num_cols == 6 and Book::cols() == 6);
91 static_assert(Book::Row::num_bits == 45 and sizeof(Book::Row) == 8);
92 static_assert(Book::Row::column_bits(0) == 3 and Book::Row::column_bits(3) == 10);
93 static_assert(Book::Row::column_min(3) == -500 and Book::Row::column_max(3) == 500);
94
95 // A row is trivial, so that an array of rows does not touch the slots it
96 // only reserves
97 static_assert(std::is_trivially_default_constructible_v<Book::Row>);
98 static_assert(std::is_trivially_copyable_v<Book::Row>);
99 static_assert(std::is_trivially_default_constructible_v<Packed_Row<signed_bits(64), bits(2)>>);
100
101 // The word of a row is the smallest that holds its bits
102 static_assert(sizeof(Packed_Row<between(7, 7)>) == 1); // 0 bits
103 static_assert(sizeof(Packed_Row<bits(3), bits(2), bits(3)>) == 1); // 8
104 static_assert(sizeof(Packed_Row<bits(3), bits(3), bits(3)>) == 2); // 9
105 static_assert(sizeof(Packed_Row<bits(3), bits(10), bits(3)>) == 2); // 16
106 static_assert(sizeof(Packed_Row<bits(3), bits(11), bits(3)>) == 4); // 17
107 static_assert(sizeof(Packed_Row<bits(3), bits(26), bits(3)>) == 4); // 32
108 static_assert(sizeof(Packed_Row<bits(3), bits(27), bits(3)>) == 8); // 33
109 static_assert(sizeof(Packed_Row<bits(3), bits(58), bits(3)>) == 8); // 64
110 // Beyond 64 bits, words of 64, and a column never straddles two of them
111 static_assert(sizeof(Packed_Row<bits(3), bits(59), bits(3)>) == 16); // 3 + 59 | 3
112 static_assert(sizeof(Packed_Row<bits(3), bits(61), bits(3)>) == 16); // 3 + 61 | 3
113 static_assert(sizeof(Packed_Row<bits(3), bits(62), bits(3)>) == 24); // 3 | 62 | 3
114 static_assert(sizeof(Packed_Row<bits(40), bits(40), bits(20)>) == 16); // 40 | 40 + 20
115 static_assert(sizeof(Packed_Row<bits(40), bits(40), bits(30)>) == 24); // 40 | 40 | 30
116 static_assert(sizeof(Packed_Row<signed_bits(64), signed_bits(64)>) == 16);
117
118 // One word of that size, not several smaller ones
119 static_assert(alignof(Packed_Row<bits(3), bits(2), bits(3)>) == 1);
120 static_assert(alignof(Packed_Row<bits(3), bits(3), bits(3)>) == 2);
121 static_assert(alignof(Packed_Row<bits(3), bits(11), bits(3)>) == 4);
122 static_assert(alignof(Packed_Row<bits(3), bits(27), bits(3)>) == 8);
123 static_assert(alignof(Packed_Row<bits(3), bits(59), bits(3)>) == 8);
124
125 // A column alone in its row, at the widths where the word changes
126 static_assert(sizeof(Packed_Row<bits(8)>) == 1 and sizeof(Packed_Row<bits(9)>) == 2);
127 static_assert(sizeof(Packed_Row<bits(16)>) == 2 and sizeof(Packed_Row<bits(17)>) == 4);
128 static_assert(sizeof(Packed_Row<bits(32)>) == 4 and sizeof(Packed_Row<bits(33)>) == 8);
129 static_assert(sizeof(Packed_Row<bits(63)>) == 8 and sizeof(Packed_Row<signed_bits(64)>) == 8);
130
138 constexpr bool columns_without_bits()
139 {
140 Packed_Row<between(7, 7), bits(40)> first{}; // at the start of a word of 64 bits
141 first(0) = 7;
142 first(1) = 5;
143 Packed_Row<between(7, 7), signed_bits(64)> before_full{}; // before a column that fills the word
144 before_full(1) = Min;
145 before_full(0) = 7;
146 Packed_Row<signed_bits(64), between(7, 7)> after_full{}; // after it
147 after_full(0) = Max;
148 after_full(1) = 7;
149 Packed_Row<bits(64 - 1), bits(1), between(7, 7), bits(3)> between_words{}; // between two words
150 between_words(1) = 1;
151 between_words(2) = 7;
152 between_words(3) = 5;
153 Packed_Row<between(7, 7), between(-3, -3), between(0, 0)> nothing{}; // a row without bits
154 nothing(1) = -3;
155 return first(0) == 7 and first(1) == 5 and before_full(0) == 7 and before_full(1) == Min and
156 after_full(0) == Max and after_full(1) == 7 and between_words(0) == 0 and
157 between_words(1) == 1 and between_words(2) == 7 and between_words(3) == 5 and
158 nothing(0) == 7 and nothing(1) == -3 and nothing(2) == 0 and
159 first == Packed_Row<between(7, 7), bits(40)>::make(7, 5);
160 }
161 static_assert(columns_without_bits());
162
163 // The bits of a column: the fewest that tell its values apart
164 static_assert(Packed_Row<between(7, 7)>::column_bits(0) == 0);
165 static_assert(Packed_Row<between(0, 1)>::column_bits(0) == 1);
166 static_assert(Packed_Row<between(-1, 3)>::column_bits(0) == 3); // 5 values
167 static_assert(Packed_Row<between(-4, 3)>::column_bits(0) == 3); // 8 values
168 static_assert(Packed_Row<between(-4, 4)>::column_bits(0) == 4); // 9 values
169 static_assert(Packed_Row<between(-500, 500)>::column_bits(0) == 10); // 1001 values
170 static_assert(Packed_Row<between(Min, -1)>::column_bits(0) == 63);
171 static_assert(Packed_Row<between(0, Max)>::column_bits(0) == 63);
172 static_assert(Packed_Row<between(-1, Max)>::column_bits(0) == 64);
173 static_assert(Packed_Row<between(Min, Max)>::column_bits(0) == 64);
174
175 // The builders of columns
176 static_assert(bits(0).lo == 0 and bits(0).hi == 0);
177 static_assert(bits(1).lo == 0 and bits(1).hi == 1);
178 static_assert(bits(63).lo == 0 and bits(63).hi == Max);
179 static_assert(signed_bits(1).lo == -1 and signed_bits(1).hi == 0);
180 static_assert(signed_bits(8).lo == -128 and signed_bits(8).hi == 127);
181 static_assert(signed_bits(64).lo == Min and signed_bits(64).hi == Max);
182 static_assert(between(-3, 9).lo == -3 and between(-3, 9).hi == 9);
183
185 template <class Row, class... Vs>
186 concept Makes = requires (Vs... vs) { Row::make(vs...); };
187
189 template <class Row, class... Vs>
190 concept Fast_Makes = requires (Vs... vs) { Row::fast_make(vs...); };
191
192 // make() takes one value per column, each convertible to std::int64_t
193 using Three = Packed_Row<between(-1, 3), bits(2), signed_bits(2)>;
195 static_assert(not Makes<Three, int, int>);
196 static_assert(not Makes<Three, int, int, int, int>);
200
202 struct Rvalue_Only
203 {
205 operator int64_t() && { return 1; }
206 };
207
209 struct Throws
210 {
212 operator int64_t() const { throw std::runtime_error("conversion"); }
213 };
214
215 // Only integers of at most 64 bits: not a floating point number, which
216 // could be out of the range of std::int64_t, nor a class, whose
217 // conversion may need an rvalue, throw or give another value for a copy
218 static_assert(Makes<Three, bool, char, uint64_t>);
219 static_assert(Fast_Makes<Three, signed char, unsigned, long long>);
220 static_assert(not Makes<Three, double, int, int> and not Fast_Makes<Three, int, float, int>);
221 static_assert(not Makes<Three, Rvalue_Only, int, int>);
222 static_assert(not Fast_Makes<Three, Rvalue_Only, int, int>);
223 static_assert(not Makes<Three, Throws, int, int> and not Fast_Makes<Three, Throws, int, int>);
224 static_assert(noexcept(Three::fast_make(uint64_t{0}, 0, 0)));
225
226 // The unchecked accesses do not throw; the checked ones may
227 static_assert(noexcept(std::declval<Three &>()(0) = 1));
228 static_assert(noexcept(std::declval<Three &>().fast_write(0, 1)));
229 static_assert(noexcept(std::declval<const Three &>()(0)));
230 static_assert(not noexcept(std::declval<Three &>()[0] = 1));
231 static_assert(not noexcept(std::declval<Three &>().write(0, 1)));
232 static_assert(not noexcept(std::declval<const Three &>()[0]));
233
234 // A proxy only for a row or a matrix that outlives it: on a temporary
235 // one, the accesses give the value, and a row cannot be written
236 static_assert(std::is_same_v<decltype(std::declval<Three &>()(0)), Three::Fast_Cell>);
237 static_assert(std::is_same_v<decltype(std::declval<Three &>()[0]), Three::Cell>);
238 static_assert(std::is_same_v<decltype(std::declval<const Three &>()(0)), int64_t>);
239 static_assert(std::is_same_v<decltype(Three::lowest()(0)), int64_t>);
240 static_assert(std::is_same_v<decltype(Three::lowest()[0]), int64_t>);
241 static_assert(std::is_same_v<decltype(std::declval<Book &>()(0, 0)), Book::Row::Fast_Cell>);
242 static_assert(std::is_same_v<decltype(std::declval<Book &>()(0)), Book::Row &>);
243 static_assert(std::is_same_v<decltype(std::declval<Book &>()[0]), Book::Row &>);
244 static_assert(std::is_same_v<decltype(std::declval<Book>()(0, 0)), int64_t>);
245 static_assert(std::is_same_v<decltype(std::declval<Book>()(0)), Book::Row>);
246 static_assert(std::is_same_v<decltype(std::declval<Book>()[0]), Book::Row>);
247 static_assert(std::is_same_v<decltype(std::declval<const Book &>()(0)), const Book::Row &>);
248 static_assert(std::is_same_v<decltype(std::declval<const Book &>()[0]), const Book::Row &>);
249 static_assert(std::is_same_v<decltype(std::declval<Book>()(0)(0)), int64_t>);
250 static_assert(std::is_same_v<decltype(std::declval<Book>()[0][0]), int64_t>);
251
255 constexpr bool row_at_compile_time()
256 {
257 Packed_Row<between(-1, 3), between(0, 15), signed_bits(64), between(7, 7)> a{}, b{};
258 if (a(0) != -1 or a(1) != 0 or a(2) != Min or a(3) != 7) // the lowest values
259 return false;
260 a(0) = 3;
261 a[1] = 15;
262 a.fast_write(2, Max);
263 a.write(3, 7);
264 if (a.fast_read(0) != 3 or a.read(1) != 15 or a[2] != Max or a(3) != 7)
265 return false;
266 a(2) = Min;
267 a(2) += 5;
268 --a(1);
269 a(0) = 2;
270 ++a[0]; // the checked steps, inside the range
271 a[0] -= 1;
272 a(0) += 1;
273 return a(2) == Min + 5 and a(1) == 14 and a(0) == 3 and b < a and not (a == b);
274 }
275
276 // The lowest value of the column is the zero of its bits, and the first
277 // column is the most significant
278 constexpr auto low = Three::lowest();
279 static_assert(low(0) == -1 and low(1) == 0 and low(2) == -2 and low == Three{});
280 static_assert(Three::make(0, 0, -2) > Three::make(-1, 3, 1));
281 static_assert(Three::make(0, 1, -2) > Three::make(0, 0, 1));
282 static_assert(Three::make(0, 1, -1) > Three::make(0, 1, -2));
283 static_assert(Three::make(3, 3, 1) == Three::fast_make(3, 3, 1));
284 static_assert(Three::valid(0, -1) and Three::valid(0, 3));
285 static_assert(not Three::valid(0, -2) and not Three::valid(0, 4));
286 static_assert(not Three::valid(3, 0) and not Three::valid(No_Index, 0)); // no such column
287
288 // ------------------------------------------------------------------
289 // Helpers
290 // ------------------------------------------------------------------
291
301 [[gnu::noinline]] size_t opaque(const size_t n)
302 {
303 volatile size_t value = n;
304 return value;
305 }
306
312 template <class Row>
313 Array<int64_t> corner_values(const size_t j)
314 {
315 const int64_t lo = Row::column_min(j), hi = Row::column_max(j);
316 const uint64_t span = static_cast<uint64_t>(hi) - static_cast<uint64_t>(lo);
317 Array<int64_t> values;
318 values.append(lo);
319 if (span >= 1)
320 values.append(hi);
321 if (span >= 2)
322 {
323 values.append(lo + 1);
324 values.append(hi - 1);
325 }
326 if (span >= 4)
327 values.append(static_cast<int64_t>(static_cast<uint64_t>(lo) + span/2));
328 return values;
329 }
330
336 template <class Row>
337 Array<int64_t> invalid_values(const size_t j)
338 {
339 const int64_t lo = Row::column_min(j), hi = Row::column_max(j);
340 Array<int64_t> values;
341 if (lo > Min)
342 {
343 values.append(lo - 1);
344 if (lo - 1 > Min)
345 values.append(Min);
346 }
347 if (hi < Max)
348 {
349 values.append(hi + 1);
350 if (hi + 1 < Max)
351 values.append(Max);
352 }
353 return values;
354 }
355
363 template <class Row, size_t... I>
364 Row row_of(const int64_t * v, const bool checked, std::index_sequence<I...>)
365 {
366 return checked ? Row::make(v[I]...) : Row::fast_make(v[I]...);
367 }
368
372 template <class M>
373 struct Model
374 {
375 using Row = typename M::Row;
376
377 size_t num_rows;
378 Array<int64_t> values;
379
383 explicit Model(const size_t n) : num_rows(n), values(n*Row::num_cols, int64_t{0})
384 {
385 for (size_t i = 0; i < n; ++i)
386 for (size_t j = 0; j < Row::num_cols; ++j)
387 at(i, j) = Row::column_min(j);
388 }
389
395 int64_t & at(const size_t i, const size_t j) { return values(i*Row::num_cols + j); }
396
403 void expect_equal_but(const M & m, const size_t i, const size_t j, const char * where)
404 {
405 for (size_t r = 0; r < num_rows; ++r)
406 for (size_t c = 0; c < Row::num_cols; ++c)
407 if (r != i or c != j)
408 {
409 ASSERT_EQ(m(r, c), at(r, c)) << where << ": (" << r << ", " << c << ")";
410 }
411 }
412
417 void expect_equal(M & m, const char * where)
418 {
419 const M & cm = m;
420 ASSERT_EQ(cm.rows(), num_rows) << where;
421 for (size_t i = 0; i < num_rows; ++i)
422 for (size_t j = 0; j < Row::num_cols; ++j)
423 {
424 const int64_t expected = at(i, j);
425 ASSERT_EQ(cm(i, j), expected) << where << ": (" << i << ", " << j << ")";
426 ASSERT_EQ(cm.fast_read(i, j), expected) << where;
427 ASSERT_EQ(cm.read(i, j), expected) << where;
428 ASSERT_EQ(cm[i][j], expected) << where;
429 ASSERT_EQ(cm(i)(j), expected) << where;
430 ASSERT_EQ(cm(i).fast_read(j), expected) << where;
431 ASSERT_EQ(cm[i].read(j), expected) << where;
432 ASSERT_EQ(static_cast<int64_t>(m(i, j)), expected) << where; // through the proxies
433 ASSERT_EQ(static_cast<int64_t>(m[i][j]), expected) << where;
434 }
435 }
436 };
437
438 // The ways of writing an entry
439 enum class Path
440 {
441 fast_write, write, call, subscript, row_call, row_subscript, row_fast_write, row_write,
442 row_copy, last
443 };
444
453 template <class M>
454 void write_through(M & m, const Path path, const size_t i, const size_t j, const int64_t v)
455 {
456 switch (path)
457 {
458 case Path::fast_write: m.fast_write(i, j, v); break;
459 case Path::write: m.write(i, j, v); break;
460 case Path::call: m(i, j) = v; break;
461 case Path::subscript: m[i][j] = v; break;
462 case Path::row_call: m(i)(j) = v; break;
463 case Path::row_subscript: m(i)[j] = v; break;
464 case Path::row_fast_write: m(i).fast_write(j, v); break;
465 case Path::row_write: m[i].write(j, v); break;
466 case Path::row_copy:
467 {
468 typename M::Row r = m(i);
469 r(j) = v;
470 m[i] = r;
471 break;
472 }
473 case Path::last: break;
474 }
475 }
476
477 // ------------------------------------------------------------------
478 // The typed suite: one schema per class of row
479 // ------------------------------------------------------------------
480
481 template <class M>
482 class PackedMatrixSchemas : public ::testing::Test {};
483
484 using Schemas = ::testing::Types<
485 Packed_Matrix<between(-1, 3), bits(4), bits(1)>, // 8 bits: a full byte
486 Packed_Matrix<between(-1, 3), bits(4), bits(2)>, // 9 bits
487 Packed_Matrix<bits(7), signed_bits(9)>, // 16 bits, full
488 Packed_Matrix<bits(7), signed_bits(9), bits(1)>, // 17 bits
489 Packed_Matrix<bits(20), between(0, 1000), signed_bits(2)>, // 32 bits, full
490 Packed_Matrix<bits(20), between(0, 1000), signed_bits(3)>, // 33 bits
491 Book, // 45 bits
492 Packed_Matrix<bits(63), bits(1)>, // 64 bits, full
493 Packed_Matrix<signed_bits(64)>, // one column of 64 bits
494 Packed_Matrix<between(Min, Max), between(-1, Max)>, // two of 64 bits
495 Packed_Matrix<bits(63), bits(2)>, // 65 bits: the second opens a word
496 Packed_Matrix<signed_bits(64), bits(1), between(7, 7), bits(63), signed_bits(1),
497 between(Min, -1), bits(33), bits(31)>, // four words
498 Packed_Matrix<between(7, 7)>, // no bits at all
499 Packed_Matrix<between(-5, -5), bits(3), between(9, 9)>>; // columns without bits at the ends
500 TYPED_TEST_SUITE(PackedMatrixSchemas, Schemas);
501
502 TYPED_TEST(PackedMatrixSchemas, StartsAtTheLowestValues)
503 {
504 using M = TypeParam;
505 for (const size_t n : {size_t{0}, size_t{1}, size_t{5}})
506 {
507 M m(n);
508 Model<M> model(n);
509 model.expect_equal(m, "built");
510 for (const auto & row : m)
511 {
512 EXPECT_TRUE(row == M::Row::lowest());
513 EXPECT_TRUE(row == typename M::Row{});
514 }
515 }
516 }
517
518 TYPED_TEST(PackedMatrixSchemas, EveryWayOfReadingSeesEveryWayOfWriting)
519 {
520 using M = TypeParam;
521 using Row = typename M::Row;
522 for (int p = 0; p < static_cast<int>(Path::last); ++p)
523 {
524 const Path path = static_cast<Path>(p);
525 M m(3);
526 Model<M> model(3);
527 // Every column goes through its corners in the middle row, twice
528 // over, so that each value is also written over each other one;
529 // the whole matrix is compared after each write: a write that
530 // spilled into another column or row would show
531 for (int round = 0; round < 2; ++round)
532 for (size_t j = 0; j < Row::num_cols; ++j)
533 {
534 const Array<int64_t> values = corner_values<Row>(j);
535 for (size_t k = 0; k < values.size(); ++k)
536 {
537 write_through(m, path, 1, j, values(k));
538 model.at(1, j) = values(k);
539 model.expect_equal(m, "after a write");
540 }
541 }
542 // The other rows, at the highest values this time
543 for (const size_t i : {size_t{0}, size_t{2}})
544 for (size_t j = 0; j < Row::num_cols; ++j)
545 {
546 write_through(m, path, i, j, Row::column_max(j));
547 model.at(i, j) = Row::column_max(j);
548 }
549 model.expect_equal(m, "all rows");
550 }
551 }
552
553 TYPED_TEST(PackedMatrixSchemas, CheckedWritesRefuseValuesOutOfRange)
554 {
555 using M = TypeParam;
556 using Row = typename M::Row;
557 M m(2);
558 Model<M> model(2);
559 for (size_t j = 0; j < Row::num_cols; ++j) // away from the lowest values
560 {
561 m(1, j) = Row::column_max(j);
562 model.at(1, j) = Row::column_max(j);
563 }
564 for (size_t j = 0; j < Row::num_cols; ++j)
565 {
566 const Array<int64_t> bad = invalid_values<Row>(j);
567 for (size_t k = 0; k < bad.size(); ++k)
568 {
569 const int64_t v = bad(k);
570 EXPECT_FALSE(Row::valid(j, v));
571 for (const size_t i : {size_t{0}, size_t{1}})
572 {
573 EXPECT_THROW(m.write(i, j, v), std::domain_error);
574 EXPECT_THROW(m[i][j] = v, std::domain_error);
575 EXPECT_THROW(m[i].write(j, v), std::domain_error);
576 EXPECT_THROW(m(i)[j] = v, std::domain_error);
577 }
578 // A whole row with that one value wrong
579 int64_t values[Row::num_cols];
580 for (size_t c = 0; c < Row::num_cols; ++c)
581 values[c] = Row::column_max(c);
582 values[j] = v;
583 const auto columns = std::make_index_sequence<Row::num_cols>();
584 EXPECT_THROW((void) row_of<Row>(values, true, columns), std::domain_error);
585 }
586 const Array<int64_t> good = corner_values<Row>(j);
587 for (size_t k = 0; k < good.size(); ++k)
588 EXPECT_TRUE(Row::valid(j, good(k)));
589 }
590 model.expect_equal(m, "after the refused writes"); // nothing changed
591 }
592
593 TYPED_TEST(PackedMatrixSchemas, CheckedAccessesRefuseIndexesOutOfRange)
594 {
595 using M = TypeParam;
596 using Row = typename M::Row;
597 for (const size_t n : {size_t{0}, size_t{1}, size_t{4}})
598 {
599 M m(n);
600 const M & cm = m;
601 Model<M> model(n);
602 const int64_t v = Row::column_min(0);
603 for (const size_t i : {n, n + 1, No_Index})
604 {
605 EXPECT_THROW((void) cm.read(i, 0), std::out_of_range);
606 EXPECT_THROW(m.write(i, 0, v), std::out_of_range);
607 EXPECT_THROW((void) cm[i], std::out_of_range);
608 EXPECT_THROW((void) m[i], std::out_of_range);
609 EXPECT_THROW(m[i][0] = v, std::out_of_range);
610 }
611 for (const size_t j : {Row::num_cols, Row::num_cols + 1, No_Index})
612 {
613 EXPECT_FALSE(Row::valid(j, v));
614 EXPECT_THROW((void) cm.read(n, j), std::out_of_range); // both wrong
615 if (n == 0)
616 continue;
617 EXPECT_THROW((void) cm.read(n - 1, j), std::out_of_range);
618 EXPECT_THROW(m.write(n - 1, j, v), std::out_of_range);
619 EXPECT_THROW((void) cm[n - 1][j], std::out_of_range);
620 EXPECT_THROW(m[n - 1][j] = v, std::out_of_range);
621 EXPECT_THROW((void) cm(n - 1)[j], std::out_of_range);
622 EXPECT_THROW((void) cm(n - 1).read(j), std::out_of_range);
623 EXPECT_THROW(m(n - 1).write(j, v), std::out_of_range);
624 }
625 model.expect_equal(m, "after the refused accesses");
626 }
627 }
628
629 TYPED_TEST(PackedMatrixSchemas, CompoundOperatorsStayInsideTheColumn)
630 {
631 using M = TypeParam;
632 using Row = typename M::Row;
633 M m(2);
634 Model<M> model(2);
635 for (size_t j = 0; j < Row::num_cols; ++j)
636 {
637 const int64_t lo = Row::column_min(j), hi = Row::column_max(j);
638 if (lo == Min and hi == Max)
639 {
640 // The column holds every std::int64_t: the steps wrap around
641 --m[1][j];
642 EXPECT_EQ(m.read(1, j), Max);
643 ++m[1][j];
644 EXPECT_EQ(m.read(1, j), Min);
645 m(1, j) = hi;
646 }
647 else
648 {
649 // At the ends, the checked operators refuse to leave the range
650 EXPECT_THROW(--m[1][j], std::domain_error);
651 EXPECT_THROW(m[1][j] -= 1, std::domain_error);
652 EXPECT_EQ(m.read(1, j), lo);
653 m(1, j) = hi;
654 EXPECT_THROW(++m[1][j], std::domain_error);
655 EXPECT_THROW(m[1][j] += 1, std::domain_error);
656 }
657 model.at(1, j) = hi;
658 model.expect_equal(m, "after the refused steps");
659 if (lo == hi)
660 continue;
661 // One step down and up again, through each of the proxies
662 --m(1, j);
663 EXPECT_EQ(m.read(1, j), hi - 1);
664 ++m[1][j];
665 EXPECT_EQ(m.read(1, j), hi);
666 m[1][j] -= 1;
667 m(1, j) += 1;
668 EXPECT_EQ(m.read(1, j), hi);
669 // The whole range in one step, in unsigned arithmetic
670 const uint64_t span = static_cast<uint64_t>(hi) - static_cast<uint64_t>(lo);
671 if (span <= static_cast<uint64_t>(Max))
672 {
673 m(1, j) -= static_cast<int64_t>(span);
674 EXPECT_EQ(m.read(1, j), lo);
675 m[1][j] += static_cast<int64_t>(span);
676 }
677 // A column assigned from another entry of the same column
678 m(0, j) = m(1, j);
679 m[0][j] = m[1][j];
680 model.at(0, j) = hi;
681 model.expect_equal(m, "after the steps");
682 }
683 }
684
685 TYPED_TEST(PackedMatrixSchemas, RowsAreValues)
686 {
687 using M = TypeParam;
688 using Row = typename M::Row;
689 constexpr size_t n = Row::num_cols;
690 const auto columns = std::make_index_sequence<Row::num_cols>();
691
692 // Rows whose columns are all at the same corner, and rows with one
693 // column apart from the others
695 Array<Array<int64_t>> tuples;
696 auto add = [&] (const int64_t * values)
697 {
698 const Row checked = row_of<Row>(values, true, columns);
699 const Row fast = row_of<Row>(values, false, columns);
700 ASSERT_TRUE(checked == fast);
701 Array<int64_t> tuple;
702 for (size_t j = 0; j < n; ++j)
703 {
704 ASSERT_EQ(checked(j), values[j]);
705 ASSERT_EQ(checked[j], values[j]);
706 tuple.append(values[j]);
707 }
708 rows.append(checked);
709 tuples.append(tuple);
710 };
711 int64_t values[Row::num_cols];
712 for (int corner = 0; corner < 5; ++corner)
713 {
714 for (size_t j = 0; j < n; ++j)
715 {
716 const Array<int64_t> c = corner_values<Row>(j);
717 values[j] = c(static_cast<size_t>(corner) % c.size());
718 }
719 add(values);
720 for (size_t j = 0; j < n; ++j) // column j at its lowest, the rest as they are
721 {
722 const int64_t kept = values[j];
723 values[j] = Row::column_min(j);
724 add(values);
725 values[j] = kept;
726 }
727 }
728
729 // Rows compare as their tuples do
730 for (size_t a = 0; a < rows.size(); ++a)
731 for (size_t b = 0; b < rows.size(); ++b)
732 {
733 std::strong_ordering expected = std::strong_ordering::equal;
734 for (size_t j = 0; j < n and expected == 0; ++j)
735 expected = tuples(a)(j) <=> tuples(b)(j);
736 ASSERT_TRUE((rows(a) <=> rows(b)) == expected) << a << " against " << b;
737 ASSERT_EQ(rows(a) == rows(b), expected == 0);
738 ASSERT_EQ(rows(a) < rows(b), expected < 0);
739 }
740
741 // A copy is independent of its origin
742 Row copy = rows(0);
743 for (size_t j = 0; j < n; ++j)
744 copy(j) = Row::column_max(j);
745 for (size_t j = 0; j < n; ++j)
746 EXPECT_EQ(rows(0)(j), tuples(0)(j));
747 }
748
749 TYPED_TEST(PackedMatrixSchemas, CopiesMovesSwapsAndFills)
750 {
751 using M = TypeParam;
752 using Row = typename M::Row;
753 const size_t n = 6;
754 M m(n);
755 Model<M> model(n);
756 for (size_t i = 0; i < n; ++i)
757 for (size_t j = 0; j < Row::num_cols; ++j)
758 {
759 const Array<int64_t> c = corner_values<Row>(j);
760 m(i, j) = c((i + j) % c.size());
761 model.at(i, j) = c((i + j) % c.size());
762 }
763
764 M copy(m);
765 EXPECT_TRUE(copy == m);
766 model.expect_equal(copy, "copy");
767
768 // The copy does not share its rows
769 Model<M> lowest(n);
770 copy.fill(Row::lowest());
771 lowest.expect_equal(copy, "filled copy");
772 model.expect_equal(m, "origin of the copy");
773 const bool all_alike = Row::num_bits == 0; // a schema without bits has one row only
774 EXPECT_EQ(copy == m, all_alike);
775
776 // Two matrices that differ in one entry only, whichever its row
777 for (size_t j = 0; j < Row::num_cols; ++j)
778 {
779 if (Row::column_min(j) == Row::column_max(j))
780 continue;
781 for (const size_t i : {size_t{0}, n/2, n - 1})
782 {
783 M changed(m);
784 EXPECT_TRUE(changed == m);
785 changed(i, j) = m(i, j) == Row::column_min(j) ? Row::column_max(j) : Row::column_min(j);
786 EXPECT_FALSE(changed == m) << "row " << i << ", column " << j;
787 EXPECT_TRUE(changed != m);
788 EXPECT_FALSE(m == changed);
789 }
790 }
791
792 M assigned(1);
793 assigned = m;
794 model.expect_equal(assigned, "assigned");
795
796 M moved(std::move(assigned));
797 model.expect_equal(moved, "moved");
798
799 M target;
800 EXPECT_EQ(target.rows(), 0u);
801 target = std::move(moved);
802 model.expect_equal(target, "move-assigned");
803
804 M other(2, target(n - 1)); // every row equal to a given one
805 EXPECT_EQ(other.rows(), 2u);
806 EXPECT_TRUE(other(0) == target(n - 1) and other(1) == target(n - 1));
807 other.swap(target);
808 model.expect_equal(other, "swapped");
809 EXPECT_EQ(target.rows(), 2u);
810 EXPECT_FALSE(other == target); // not as many rows
811
812 // The rows are contiguous and in order
813 const M & cm = other;
814 ASSERT_EQ(cm.end() - cm.begin(), static_cast<std::ptrdiff_t>(n));
815 EXPECT_EQ(cm.data(), cm.begin());
816 EXPECT_EQ(other.data(), other.begin());
817 size_t i = 0;
818 for (const Row & row : cm)
819 {
820 EXPECT_EQ(&row, &cm(i));
821 EXPECT_TRUE(row == cm[i]);
822 ++i;
823 }
824 EXPECT_EQ(i, n);
825 for (size_t k = 0; k < n; ++k) // a hint: it changes nothing
826 cm.prefetch(k);
827 model.expect_equal(other, "prefetched");
828 for (Row & row : other) // and can be written in place
829 row = Row::lowest();
830 lowest.expect_equal(other, "written through the range");
831 }
832
833 TYPED_TEST(PackedMatrixSchemas, RandomWritesAgainstAPlainMatrix)
834 {
835 using M = TypeParam;
836 using Row = typename M::Row;
837 for (const unsigned seed : {1u, 2u, 3u})
838 {
839 std::mt19937_64 rng(seed);
840 const size_t n = 17;
841 M m(n);
842 Model<M> model(n);
843 for (int step = 0; step < 3000; ++step)
844 {
845 const size_t i = rng() % n, j = rng() % Row::num_cols;
846 const int64_t lo = Row::column_min(j), hi = Row::column_max(j);
847 const uint64_t span = static_cast<uint64_t>(hi) - static_cast<uint64_t>(lo);
848 const uint64_t whole = std::numeric_limits<uint64_t>::max();
849 const uint64_t offset = span == whole ? rng() : rng() % (span + 1);
850 const int64_t v = static_cast<int64_t>(static_cast<uint64_t>(lo) + offset);
851 const Path path = static_cast<Path>(rng() % static_cast<unsigned>(Path::last));
852 write_through(m, path, i, j, v);
853 model.at(i, j) = v;
854 // The row that was written, and its neighbours in the array
855 for (const size_t r : {i, (i + 1) % n, (i + n - 1) % n})
856 for (size_t c = 0; c < Row::num_cols; ++c)
857 ASSERT_EQ(m.read(r, c), model.at(r, c)) << "seed " << seed << ", step " << step;
858 }
859 model.expect_equal(m, "after the random writes");
860 }
861 }
862
863 TYPED_TEST(PackedMatrixSchemas, AMatrixWithoutRows)
864 {
865 using M = TypeParam;
866 M m, also_empty(opaque(0));
867 const M & cm = m;
868 EXPECT_EQ(m.rows(), 0u);
869 EXPECT_EQ(m.begin(), m.end());
870 EXPECT_EQ(cm.begin(), cm.end());
871 EXPECT_EQ(m.data(), nullptr);
872 EXPECT_EQ(cm.data(), nullptr);
873 EXPECT_TRUE(m == also_empty);
874 m.fill(M::Row::lowest()); // nothing to fill
875 M copy(also_empty);
876 EXPECT_EQ(copy.rows(), 0u);
877 EXPECT_TRUE(copy == m);
878 M one(1);
879 EXPECT_FALSE(one == m);
880 m.swap(one);
881 EXPECT_EQ(m.rows(), 1u);
882 EXPECT_EQ(one.rows(), 0u);
883 }
884
885 // ------------------------------------------------------------------
886 // Every width of a column, between two guards
887 // ------------------------------------------------------------------
888
893 template <Bit_Column C>
894 void check_between_guards(const unsigned width)
895 {
896 using Row = Packed_Row<bits(3), C, bits(3)>;
897 ASSERT_EQ(Row::column_bits(1), width);
898 ASSERT_EQ(Row::num_bits, 6u + width);
899 ASSERT_EQ(Row::column_min(1), C.lo);
900 ASSERT_EQ(Row::column_max(1), C.hi);
901 const Array<int64_t> values = corner_values<Row>(1);
902 for (const int64_t guard : {int64_t{0}, int64_t{7}, int64_t{5}})
903 for (size_t k = 0; k < values.size(); ++k)
904 {
905 Row r = Row::lowest();
906 r(0) = guard;
907 r(2) = 7 - guard;
908 r(1) = values(k);
909 ASSERT_EQ(r(1), values(k)) << "width " << width;
910 ASSERT_EQ(r(0), guard) << "width " << width;
911 ASSERT_EQ(r(2), 7 - guard) << "width " << width;
912 ASSERT_TRUE(r == Row::make(guard, values(k), 7 - guard));
913 // The guards written after the column, at both ends of theirs
914 r(0) = 7 - guard;
915 r(2) = guard;
916 ASSERT_EQ(r(1), values(k)) << "width " << width;
917 r[1] = Row::column_min(1); // and the column cleared under them
918 ASSERT_EQ(r(0), 7 - guard);
919 ASSERT_EQ(r(2), guard);
920 ASSERT_EQ(r(1), Row::column_min(1));
921 }
922 const Array<int64_t> bad = invalid_values<Row>(1);
923 for (size_t k = 0; k < bad.size(); ++k)
924 {
925 ASSERT_FALSE(Row::valid(1, bad(k)));
926 ASSERT_THROW((void) Row::make(0, bad(k), 0), std::domain_error);
927 }
928 }
929
934 template <Bit_Column C>
935 void check_alone(const unsigned width)
936 {
937 using Row = Packed_Row<C>;
938 ASSERT_EQ(Row::num_bits, width);
939 const size_t bytes = width <= 8 ? 1 : width <= 16 ? 2 : width <= 32 ? 4 : 8;
940 ASSERT_EQ(sizeof(Row), bytes) << "width " << width;
941 ASSERT_EQ(alignof(Row), sizeof(Row));
942 const Array<int64_t> values = corner_values<Row>(0);
943 Row r = Row::lowest();
944 ASSERT_EQ(r(0), C.lo);
945 for (size_t k = 0; k < values.size(); ++k)
946 for (size_t over = 0; over < values.size(); ++over) // each value over each other one
947 {
948 r(0) = values(over);
949 r[0] = values(k);
950 ASSERT_EQ(r(0), values(k)) << "width " << width;
951 ASSERT_TRUE(r == Row::make(values(k)));
952 ASSERT_TRUE(r == Row::fast_make(values(k)));
953 }
954 }
955
959 template <size_t... W>
960 void check_every_width(std::index_sequence<W...>)
961 {
962 (check_between_guards<bits(W)>(W), ...); // 0 to 63 bits, from zero
963 (check_between_guards<signed_bits(W + 1)>(W + 1), ...); // 1 to 64 bits, around zero
964 (check_alone<bits(W)>(W), ...);
965 (check_alone<signed_bits(W + 1)>(W + 1), ...);
966 }
967
968 TEST(PackedRowWidths, EveryWidthBetweenTwoGuards)
969 {
970 check_every_width(std::make_index_sequence<64>());
971 }
972
973 TEST(PackedRowWidths, RangesThatAreNotPowersOfTwo)
974 {
975 check_between_guards<between(-1, 3)>(3);
976 check_between_guards<between(5, 6)>(1);
977 check_between_guards<between(-500, 500)>(10);
978 check_between_guards<between(1000000, 1000255)>(8);
979 check_between_guards<between(Min, Min + 2)>(2);
980 check_between_guards<between(Max - 2, Max)>(2);
981 check_between_guards<between(Min, -1)>(63);
982 check_between_guards<between(Min + 1, Max)>(64);
983 check_between_guards<between(Min, Max - 1)>(64);
984 check_between_guards<between(-1, Max)>(64);
985 check_between_guards<between(42, 42)>(0);
986 check_between_guards<between(Min, Min)>(0);
987 check_between_guards<between(Max, Max)>(0);
988 }
989
990 // ------------------------------------------------------------------
991 // The order of the rows, pair by pair
992 // ------------------------------------------------------------------
993
1001 template <class Row>
1002 size_t check_order_of_every_pair(const Array<int64_t> & a, const Array<int64_t> & b,
1003 const Array<int64_t> & c)
1004 {
1005 struct Entry
1006 {
1007 Row row;
1008 int64_t tuple[3];
1009 };
1010 Array<Entry> entries;
1011 for (size_t i = 0; i < a.size(); ++i)
1012 for (size_t j = 0; j < b.size(); ++j)
1013 for (size_t k = 0; k < c.size(); ++k)
1014 entries.append(Entry{Row::make(a(i), b(j), c(k)), {a(i), b(j), c(k)}});
1015 for (size_t x = 0; x < entries.size(); ++x)
1016 for (size_t y = 0; y < entries.size(); ++y)
1017 {
1018 std::strong_ordering expected = std::strong_ordering::equal;
1019 for (size_t j = 0; j < 3 and expected == 0; ++j)
1020 expected = entries(x).tuple[j] <=> entries(y).tuple[j];
1021 EXPECT_TRUE((entries(x).row <=> entries(y).row) == expected);
1022 EXPECT_EQ(entries(x).row == entries(y).row, expected == 0);
1023 EXPECT_EQ(entries(x).row != entries(y).row, expected != 0);
1024 EXPECT_EQ(entries(x).row < entries(y).row, expected < 0);
1025 EXPECT_EQ(entries(x).row >= entries(y).row, expected >= 0);
1026 }
1027 return entries.size();
1028 }
1029
1035 Array<int64_t> all_of(const int64_t lo, const int64_t hi)
1036 {
1037 Array<int64_t> values;
1038 for (int64_t v = lo; v <= hi; ++v)
1039 values.append(v);
1040 return values;
1041 }
1042
1043 TEST(PackedRowOrder, EveryPairOfRowsOfOneWord)
1044 {
1045 // Every row of the schema: 5*4*4 tuples
1046 using Row = Packed_Row<between(-1, 3), bits(2), signed_bits(2)>;
1047 EXPECT_EQ(check_order_of_every_pair<Row>(all_of(-1, 3), all_of(0, 3), all_of(-2, 1)), 80u);
1048 }
1049
1050 TEST(PackedRowOrder, EveryPairOfRowsOfSeveralWords)
1051 {
1052 // Three words; the corners of each column
1053 using Row = Packed_Row<signed_bits(64), bits(63), between(-1, 1)>;
1054 const size_t n =
1055 check_order_of_every_pair<Row>(corner_values<Row>(0), corner_values<Row>(1), all_of(-1, 1));
1056 EXPECT_EQ(n, 5u*5u*3u);
1057 }
1058
1059 TEST(PackedRowOrder, EveryPairOfRowsWithColumnsThatShareAWord)
1060 {
1061 // 40 | 40 + 20: the last two columns share the second word
1062 using Row = Packed_Row<bits(40), signed_bits(40), bits(20)>;
1063 const size_t n = check_order_of_every_pair<Row>(corner_values<Row>(0), corner_values<Row>(1),
1064 corner_values<Row>(2));
1065 EXPECT_EQ(n, 125u);
1066 }
1067
1068 // ------------------------------------------------------------------
1069 // The builders of columns, at run time
1070 // ------------------------------------------------------------------
1071
1077 [[gnu::noinline]] Bit_Column between_at_run_time(const int64_t lo, const int64_t hi)
1078 {
1079 return between(lo, hi);
1080 }
1081
1082 TEST(PackedColumns, TheBuildersValidateTheirArguments)
1083 {
1084 EXPECT_THROW((void) between_at_run_time(3, -1), std::domain_error);
1085 EXPECT_THROW((void) between_at_run_time(Max, Min), std::domain_error);
1086 EXPECT_THROW((void) between_at_run_time(1, 0), std::domain_error);
1087 EXPECT_NO_THROW((void) between_at_run_time(0, 0));
1088 EXPECT_NO_THROW((void) between_at_run_time(Min, Max));
1089 volatile unsigned n = 64;
1090 EXPECT_THROW((void) bits(n), std::domain_error);
1091 n = 65;
1092 EXPECT_THROW((void) signed_bits(n), std::domain_error);
1093 n = 0;
1094 EXPECT_THROW((void) signed_bits(n), std::domain_error);
1095 EXPECT_NO_THROW((void) bits(n));
1096 n = 63;
1097 EXPECT_EQ(bits(n).hi, Max);
1098 n = 64;
1099 EXPECT_EQ(signed_bits(n).lo, Min);
1100 EXPECT_EQ(signed_bits(n).hi, Max);
1101 for (unsigned width = 1; width <= 62; ++width)
1102 {
1103 n = width;
1104 EXPECT_EQ(bits(n).lo, 0);
1105 EXPECT_EQ(bits(n).hi, (int64_t{1} << width) - 1);
1106 EXPECT_EQ(signed_bits(n + 1).lo, -(int64_t{1} << width));
1107 EXPECT_EQ(signed_bits(n + 1).hi, (int64_t{1} << width) - 1);
1108 }
1109 }
1110
1111 TEST(PackedRowMake, UnsignedValuesAboveTheHighestInt64AreRefused)
1112 {
1113 using Wide = Packed_Row<signed_bits(64)>;
1114 constexpr uint64_t above = uint64_t{1} << 63; // the lowest that does not fit
1115 // they would wrap to a value of the column
1116 EXPECT_THROW((void) Wide::make(std::numeric_limits<uint64_t>::max()), std::domain_error);
1117 EXPECT_THROW((void) Wide::make(above), std::domain_error);
1118 EXPECT_EQ(Wide::make(above - 1)(0), Max);
1119 EXPECT_EQ(Wide::make(uint64_t{0})(0), 0);
1120
1121 using Small = Packed_Row<between(0, 100), bits(1)>;
1122 EXPECT_EQ(Small::make(uint64_t{42}, true)(0), 42);
1123 EXPECT_EQ(Small::make(42u, false)(1), 0);
1124 EXPECT_THROW((void) Small::make(uint64_t{101}, true), std::domain_error);
1125 EXPECT_THROW((void) Small::make(std::numeric_limits<uint64_t>::max(), true), std::domain_error);
1126 EXPECT_THROW((void) Small::make(-1, true), std::domain_error);
1127 }
1128
1129 TEST(PackedRowTemporaries, TheAccessesOfATemporaryGiveValues)
1130 {
1131 // the value outlives the temporary row or matrix that it comes from
1132 auto a = Three::make(3, 2, 1)(0);
1133 auto b = Three::make(3, 2, 1)[1];
1134 auto c = Book(4, Book::Row::make(3, 15, 7, -500, 1, 99999))(2, 3);
1135 auto d = Book(4, Book::Row::make(3, 15, 7, -500, 1, 99999))[3][5];
1136 static_assert(std::is_same_v<decltype(a), int64_t> and std::is_same_v<decltype(b), int64_t>);
1137 static_assert(std::is_same_v<decltype(c), int64_t> and std::is_same_v<decltype(d), int64_t>);
1138 EXPECT_EQ(a, 3);
1139 EXPECT_EQ(b, 2);
1140 EXPECT_EQ(c, -500);
1141 EXPECT_EQ(d, 99999);
1142
1143 // A row of a temporary matrix is a copy, which a reference keeps alive
1144 // after the matrix is gone (AddressSanitizer saw a reference to the
1145 // freed rows when it was not)
1146 const auto & row = Book(3, Book::Row::make(-1, 4, 999, 0, 0, 7))[2];
1147 const auto & unchecked = Book(3, Book::Row::make(2, 5, 1, -1, 1, 8))(1);
1148 EXPECT_EQ(row[0], -1);
1149 EXPECT_EQ(row(2), 999);
1150 EXPECT_EQ(row(5), 7);
1151 EXPECT_EQ(unchecked(0), 2);
1152 EXPECT_EQ(unchecked[3], -1);
1153 EXPECT_THROW((void) Book(3)[3], std::out_of_range);
1154
1155 // the proxies of a row or a matrix that is not temporary still write
1156 Three r = Three::lowest();
1157 r(0) = 3;
1158 r[1] = 2;
1159 EXPECT_TRUE(r == Three::make(3, 2, -2));
1160 Book m(2);
1161 m(1, 3) = 7;
1162 m[0][2] = 8;
1163 m(1)(4) = 1;
1164 m[0][5] += 9;
1165 EXPECT_EQ(m(1, 3), 7);
1166 EXPECT_EQ(m(0, 2), 8);
1167 EXPECT_EQ(m(1, 4), 1);
1168 EXPECT_EQ(m(0, 5), 9);
1169 }
1170
1171 TEST(PackedColumns, AConstantExpressionOfARow)
1172 {
1173 static_assert(row_at_compile_time());
1174 EXPECT_TRUE(row_at_compile_time()); // and the same at run time
1175 }
1176
1177 // ------------------------------------------------------------------
1178 // Rows of more columns than the operands of a fold that Clang takes
1179 // ------------------------------------------------------------------
1180
1191 constexpr Bit_Column wide_column(const size_t j)
1192 {
1193 const unsigned w = static_cast<unsigned>(j % 65);
1194 const int64_t k = static_cast<int64_t>(j);
1195 if (w == 0)
1196 return between(k, k);
1197 switch (j % 3)
1198 {
1199 case 0: return signed_bits(w);
1200 case 1: return w == 64 ? between(Min, Max) : bits(w);
1201 default: return w == 64 ? between(-1, Max) : between(-k, -k + (int64_t{1} << (w - 1)));
1202 }
1203 }
1204
1208 template <class S>
1209 struct Wide_Of;
1210
1214 template <size_t... J>
1215 struct Wide_Of<std::index_sequence<J...>>
1216 {
1217 using Row = Packed_Row<wide_column(J)...>;
1218 using Matrix = Packed_Matrix<wide_column(J)...>;
1219 };
1220
1222 constexpr size_t Wide_Cols = 257;
1223
1225 constexpr auto wide_indexes = std::make_index_sequence<Wide_Cols>();
1226
1227 using Wide = Wide_Of<std::make_index_sequence<Wide_Cols>>::Row;
1228 using Wide_Matrix = Wide_Of<std::make_index_sequence<Wide_Cols>>::Matrix;
1229
1230 static_assert(Wide::num_cols == Wide_Cols and Wide_Matrix::cols() == Wide_Cols);
1231 static_assert(std::is_same_v<Wide_Matrix::Row, Wide>);
1232 static_assert(std::is_trivially_default_constructible_v<Wide>);
1233 static_assert(std::is_trivially_copyable_v<Wide>);
1234
1241 constexpr size_t wide_words()
1242 {
1243 size_t words = 1, free = 64;
1244 for (size_t j = 0; j < Wide_Cols; ++j)
1245 {
1246 const size_t width = j % 65;
1247 if (width > free)
1248 {
1249 ++words;
1250 free = 64;
1251 }
1252 free -= width;
1253 }
1254 return words;
1255 }
1256 static_assert(sizeof(Wide) == 8*wide_words());
1257
1259 template <size_t>
1260 using Int = int;
1261
1266 template <size_t... I>
1267 constexpr bool wide_takes(std::index_sequence<I...>)
1268 {
1269 return Makes<Wide, Int<I>...> and Fast_Makes<Wide, Int<I>...>;
1270 }
1271
1276 template <size_t... I>
1277 constexpr bool wide_takes_any(std::index_sequence<I...>)
1278 {
1279 return Makes<Wide, Int<I>...> or Fast_Makes<Wide, Int<I>...>;
1280 }
1281
1282 // One value per column, neither one less nor one more
1283 static_assert(wide_takes(std::make_index_sequence<Wide_Cols>()));
1284 static_assert(not wide_takes_any(std::make_index_sequence<Wide_Cols - 1>()));
1285 static_assert(not wide_takes_any(std::make_index_sequence<Wide_Cols + 1>()));
1286
1293 template <size_t K, class T, size_t... I>
1294 constexpr bool wide_makes(std::index_sequence<I...>)
1295 {
1296 return Makes<Wide, std::conditional_t<I == K, T, int>...>;
1297 }
1298
1305 template <size_t K, class T, size_t... I>
1306 constexpr bool wide_fast_makes(std::index_sequence<I...>)
1307 {
1308 return Fast_Makes<Wide, std::conditional_t<I == K, T, int>...>;
1309 }
1310
1315 template <size_t... K>
1316 constexpr bool wide_refuses_a_double_anywhere(std::index_sequence<K...>)
1317 {
1318 const bool refused[] = {(not wide_makes<K, double>(wide_indexes) and
1319 not wide_fast_makes<K, double>(wide_indexes))...};
1320 for (const bool r : refused)
1321 if (not r)
1322 return false;
1323 return true;
1324 }
1325
1326 // Any integer of at most 64 bits in any column, the last ones included...
1327 static_assert(wide_makes<0, bool>(wide_indexes) and wide_fast_makes<0, uint64_t>(wide_indexes));
1328 static_assert(wide_makes<256, uint64_t>(wide_indexes));
1329 static_assert(wide_fast_makes<256, signed char>(wide_indexes));
1330 // ... and nothing else in any of them
1331 static_assert(wide_refuses_a_double_anywhere(wide_indexes));
1332 static_assert(not wide_makes<1, float>(wide_indexes) and not wide_fast_makes<1, float>(wide_indexes));
1333 static_assert(not wide_makes<255, Rvalue_Only>(wide_indexes));
1334 static_assert(not wide_fast_makes<255, Rvalue_Only>(wide_indexes));
1335 static_assert(not wide_makes<256, Throws>(wide_indexes));
1336 static_assert(not wide_fast_makes<256, Throws>(wide_indexes));
1337 static_assert(not wide_makes<256, std::nullptr_t>(wide_indexes));
1338
1340 constexpr size_t Wide_Patterns = 7;
1341
1348 void wide_values(const size_t pattern, int64_t (&v)[Wide_Cols])
1349 {
1350 for (size_t j = 0; j < Wide_Cols; ++j)
1351 if (pattern == 5)
1352 v[j] = Wide::column_min(j);
1353 else if (pattern == 6)
1354 v[j] = Wide::column_max(j);
1355 else
1356 {
1357 const Array<int64_t> c = corner_values<Wide>(j);
1358 v[j] = c((pattern + j) % c.size());
1359 }
1360 }
1361
1362 TEST(PackedRowWide, EveryColumnHasItsRangeAndItsBits)
1363 {
1364 size_t bits_of_the_row = 0;
1365 for (size_t j = 0; j < Wide_Cols; ++j)
1366 {
1367 EXPECT_EQ(Wide::column_bits(j), j % 65) << "column " << j;
1368 EXPECT_EQ(Wide::column_min(j), wide_column(j).lo) << "column " << j;
1369 EXPECT_EQ(Wide::column_max(j), wide_column(j).hi) << "column " << j;
1370 EXPECT_EQ(Wide::lowest()(j), wide_column(j).lo) << "column " << j;
1371 bits_of_the_row += j % 65;
1372 }
1373 EXPECT_EQ(Wide::num_bits, bits_of_the_row);
1374 EXPECT_EQ(sizeof(Wide), 8*wide_words());
1375 EXPECT_TRUE(Wide{} == Wide::lowest());
1376 }
1377
1378 TEST(PackedRowWide, MakeAndFastMakePlaceEveryValueInItsColumn)
1379 {
1380 int64_t v[Wide_Cols];
1381 for (size_t pattern = 0; pattern < Wide_Patterns; ++pattern)
1382 {
1383 wide_values(pattern, v);
1384 const Wide checked = row_of<Wide>(v, true, wide_indexes);
1385 const Wide fast = row_of<Wide>(v, false, wide_indexes);
1386 Wide one_by_one = Wide::lowest(); // the columns written one at a time
1387 for (size_t j = 0; j < Wide_Cols; ++j)
1388 one_by_one(j) = v[j];
1389 ASSERT_TRUE(checked == fast) << "pattern " << pattern;
1390 ASSERT_TRUE(checked == one_by_one) << "pattern " << pattern;
1391 for (size_t j = 0; j < Wide_Cols; ++j)
1392 {
1393 ASSERT_EQ(checked(j), v[j]) << "pattern " << pattern << ", column " << j;
1394 ASSERT_EQ(checked[j], v[j]) << "pattern " << pattern << ", column " << j;
1395 }
1396 }
1397 }
1398
1399 TEST(PackedRowWide, MakeChecksEveryValueAgainstItsOwnColumn)
1400 {
1401 int64_t v[Wide_Cols];
1402 wide_values(6, v); // every column at its highest value
1403 size_t refused = 0;
1404 for (size_t j = 0; j < Wide_Cols; ++j)
1405 {
1406 const Array<int64_t> bad = invalid_values<Wide>(j);
1407 for (size_t k = 0; k < bad.size(); ++k)
1408 {
1409 const int64_t kept = v[j];
1410 v[j] = bad(k);
1411 try
1412 {
1413 (void) row_of<Wide>(v, true, wide_indexes);
1414 ADD_FAILURE() << "column " << j << " took " << bad(k);
1415 }
1416 catch (const std::domain_error & e)
1417 {
1418 // The column whose range the value is out of, not another
1419 const std::string what = e.what();
1420 EXPECT_TRUE(what.ends_with(" of column " + std::to_string(j))) << what;
1421 ++refused;
1422 }
1423 v[j] = kept;
1424 }
1425 }
1426 EXPECT_GT(refused, 3*Wide_Cols); // nearly every column has four values to refuse
1427
1428 // With two values out of range, make() reports the first one
1429 const std::pair<size_t, size_t> pairs[] = {{0, 256}, {255, 256}, {3, 200}};
1430 for (const auto & [first, second] : pairs)
1431 {
1432 const int64_t kept_first = v[first], kept_second = v[second];
1433 v[first] = invalid_values<Wide>(first)(0);
1434 v[second] = invalid_values<Wide>(second)(0);
1435 try
1436 {
1437 (void) row_of<Wide>(v, true, wide_indexes);
1438 ADD_FAILURE() << "columns " << first << " and " << second;
1439 }
1440 catch (const std::domain_error & e)
1441 {
1442 const std::string what = e.what();
1443 EXPECT_TRUE(what.ends_with(" of column " + std::to_string(first))) << what;
1444 }
1445 v[first] = kept_first;
1446 v[second] = kept_second;
1447 }
1448 }
1449
1455 constexpr int64_t wide_end(const size_t j)
1456 {
1457 return j % 2 == 1 ? wide_column(j).hi : wide_column(j).lo;
1458 }
1459
1464 template <size_t... J>
1465 constexpr bool wide_row_at_compile_time(std::index_sequence<J...>)
1466 {
1467 const Wide checked = Wide::make(wide_end(J)...);
1468 if (not (checked == Wide::fast_make(wide_end(J)...)))
1469 return false;
1470 for (size_t j = 0; j < Wide_Cols; ++j)
1471 if (checked(j) != wide_end(j))
1472 return false;
1473 return true;
1474 }
1475
1476 TEST(PackedRowWide, AConstantExpressionOfAWideRow)
1477 {
1478 static_assert(wide_row_at_compile_time(wide_indexes));
1479 EXPECT_TRUE(wide_row_at_compile_time(wide_indexes)); // and the same at run time
1480 }
1481
1482 TEST(PackedRowWide, RowsCompareAsTheirTuplesWhereverTheyDiffer)
1483 {
1484 // Two rows that agree up to column j and differ there: the one with the
1485 // lower value there is the lower row, whatever the columns after it
1486 int64_t middle[Wide_Cols];
1487 wide_values(2, middle);
1488 size_t compared = 0;
1489 for (size_t j = 0; j < Wide_Cols; ++j)
1490 {
1491 if (Wide::column_min(j) == Wide::column_max(j))
1492 continue;
1493 Wide low = row_of<Wide>(middle, false, wide_indexes), high = low;
1494 low(j) = Wide::column_min(j);
1495 high(j) = Wide::column_max(j);
1496 for (size_t c = j + 1; c < Wide_Cols; ++c)
1497 {
1498 low(c) = Wide::column_max(c);
1499 high(c) = Wide::column_min(c);
1500 }
1501 ASSERT_TRUE(low < high) << "column " << j;
1502 ASSERT_TRUE((low <=> high) == std::strong_ordering::less) << "column " << j;
1503 ASSERT_TRUE((high <=> low) == std::strong_ordering::greater) << "column " << j;
1504 ASSERT_FALSE(low == high) << "column " << j;
1505 ASSERT_TRUE((low <=> low) == std::strong_ordering::equal) << "column " << j;
1506 ++compared;
1507 }
1508 EXPECT_EQ(compared, Wide_Cols - 4); // all but the four columns without bits
1509 }
1510
1511 TEST(PackedMatrixWide, EveryWayOfWritingAgainstAPlainMatrix)
1512 {
1513 std::mt19937_64 rng(Wide_Cols);
1514 const size_t n = 5;
1515 Wide_Matrix m(n);
1516 Model<Wide_Matrix> model(n);
1517 model.expect_equal(m, "built");
1518 for (int step = 0; step < 3000; ++step)
1519 {
1520 const size_t i = rng() % n, j = rng() % Wide_Cols;
1521 const int64_t lo = Wide::column_min(j), hi = Wide::column_max(j);
1522 const uint64_t span = static_cast<uint64_t>(hi) - static_cast<uint64_t>(lo);
1523 const uint64_t whole = std::numeric_limits<uint64_t>::max();
1524 const uint64_t offset = span == whole ? rng() : rng() % (span + 1);
1525 const int64_t v = static_cast<int64_t>(static_cast<uint64_t>(lo) + offset);
1526 const Path path = static_cast<Path>(rng() % static_cast<unsigned>(Path::last));
1527 write_through(m, path, i, j, v);
1528 model.at(i, j) = v;
1529 for (size_t c = 0; c < Wide_Cols; ++c) // the row that was written
1530 ASSERT_EQ(m.read(i, c), model.at(i, c)) << "step " << step << ", column " << c;
1531 }
1532 model.expect_equal(m, "after the random writes");
1533
1534 // Whole rows, built with make() and fast_make()
1535 int64_t v[Wide_Cols];
1536 for (size_t pattern = 0; pattern < Wide_Patterns; ++pattern)
1537 {
1538 wide_values(pattern, v);
1539 const size_t i = pattern % n;
1540 m(i) = row_of<Wide>(v, pattern % 2 == 0, wide_indexes);
1541 for (size_t j = 0; j < Wide_Cols; ++j)
1542 model.at(i, j) = v[j];
1543 model.expect_equal(m, "after a whole row");
1544 }
1545
1546 for (size_t i = 0; i < n; ++i) // a row of many cache lines: the hint changes nothing
1547 m.prefetch(i);
1548 model.expect_equal(m, "prefetched");
1549
1550 Wide_Matrix copy(m);
1551 EXPECT_TRUE(copy == m);
1552 copy(n - 1, Wide_Cols - 1) = m(n - 1, Wide_Cols - 1) == 0 ? 1 : 0;
1553 EXPECT_FALSE(copy == m);
1554 }
1555
1556 // ------------------------------------------------------------------
1557 // Columns given as an array
1558 // ------------------------------------------------------------------
1559
1563 constexpr std::array<Bit_Column, Wide_Cols> wide_columns_array()
1564 {
1565 std::array<Bit_Column, Wide_Cols> columns{};
1566 for (size_t j = 0; j < Wide_Cols; ++j)
1567 columns[j] = wide_column(j);
1568 return columns;
1569 }
1570
1572 constexpr auto wide_columns = wide_columns_array();
1573
1574 // The same types as with one template argument per column, from a
1575 // variable or from a call, and more columns than a fold takes
1576 static_assert(std::is_same_v<Packed_Row_Of<wide_columns>, Wide>);
1577 static_assert(std::is_same_v<Packed_Matrix_Of<wide_columns>, Wide_Matrix>);
1578 static_assert(std::is_same_v<Packed_Row_Of<wide_columns_array()>, Wide>);
1579
1581 using Mixed = Packed_Matrix<bits(1), bits(8), bits(11), signed_bits(16), between(-1, 3),
1582 between(7, 7), signed_bits(64), bits(0)>;
1583
1585 constexpr std::array mixed_columns{bits(1), bits(8), bits(11),
1586 signed_bits(16), between(-1, 3), between(7, 7),
1587 signed_bits(64), bits(0)};
1588
1590 constexpr Bit_Column mixed_c_array[] = {bits(1), bits(8), bits(11),
1591 signed_bits(16), between(-1, 3), between(7, 7),
1592 signed_bits(64), bits(0)};
1593
1594 static_assert(std::is_same_v<Packed_Matrix_Of<mixed_columns>, Mixed>);
1595 static_assert(std::is_same_v<Packed_Row_Of<mixed_columns>, Mixed::Row>);
1596 static_assert(std::is_same_v<Packed_Matrix_Of<std::to_array(mixed_c_array)>, Mixed>);
1597 static_assert(std::is_same_v<Packed_Matrix_Of<std::array{bits(1), bits(8), bits(11),
1598 signed_bits(16), between(-1, 3),
1599 between(7, 7), signed_bits(64),
1600 bits(0)}>,
1601 Mixed>); // written in place
1602 static_assert(std::is_same_v<Packed_Matrix_Of<std::array{signed_bits(64)}>,
1603 Packed_Matrix<signed_bits(64)>>); // one column
1604
1605 // Two arrays with the same columns give the same type; in another order,
1606 // another type
1607 constexpr auto mixed_copy = mixed_columns;
1608 static_assert(std::is_same_v<Packed_Matrix_Of<mixed_copy>, Packed_Matrix_Of<mixed_columns>>);
1609 static_assert(not std::is_same_v<Packed_Matrix_Of<std::array{bits(8), bits(1)}>,
1610 Packed_Matrix_Of<std::array{bits(1), bits(8)}>>);
1611
1613 template <auto Columns>
1614 concept Row_Of_Takes = requires { typename Packed_Row_Of<Columns>; };
1615
1617 template <auto Columns>
1618 concept Matrix_Of_Takes = requires { typename Packed_Matrix_Of<Columns>; };
1619
1620 // Only a std::array of Bit_Column, of 1 to 65535 of them
1622 static_assert(not Row_Of_Takes<mixed_c_array> and not Matrix_Of_Takes<mixed_c_array>); // a pointer
1623 static_assert(not Row_Of_Takes<std::array<Bit_Column, 0>{}>);
1624 static_assert(not Matrix_Of_Takes<std::array<Bit_Column, 0>{}>);
1625 static_assert(not Matrix_Of_Takes<std::array{1, 2, 3}>);
1626 static_assert(not Matrix_Of_Takes<bits(3)> and not Matrix_Of_Takes<42>);
1627 static_assert(packed_matrix_detail::Column_Array<std::array<Bit_Column, 1>>);
1628 static_assert(packed_matrix_detail::Column_Array<std::array<Bit_Column, 65535>>);
1629 static_assert(not packed_matrix_detail::Column_Array<std::array<Bit_Column, 65536>>);
1630 static_assert(packed_matrix_detail::Column_Array<const std::array<Bit_Column, 3>>);
1631
1641 template <auto Columns, size_t N>
1645
1647 static_assert(not Columns_At_Gives_A_Row<std::array{1, 2, 3}, 3>);
1648 static_assert(not Columns_At_Gives_A_Row<std::array<Bit_Column, 0>{}, 0>);
1649 static_assert(not Columns_At_Gives_A_Row<mixed_c_array, 8>); // a pointer, which [] would take
1650 static_assert(not Columns_At_Gives_A_Row<bits(3), 1>);
1651
1656 template <size_t N>
1657 constexpr std::array<Bit_Column, N> small_columns()
1658 {
1659 std::array<Bit_Column, N> columns{};
1660 for (size_t j = 0; j < N; ++j)
1661 {
1662 const unsigned width = 1 + static_cast<unsigned>((j*7 + j/3) % 16);
1663 columns[j] = j % 4 == 3 ? signed_bits(width) : bits(width);
1664 }
1665 return columns;
1666 }
1667
1668 TEST(PackedMatrixOf, ManySmallColumnsBuiltByCode)
1669 {
1670 constexpr auto columns = small_columns<300>();
1671 using M = Packed_Matrix_Of<columns>;
1672 using Row = M::Row;
1673 static_assert(M::cols() == 300 and Row::num_cols == 300);
1674 for (size_t j = 0; j < M::cols(); ++j)
1675 {
1676 ASSERT_EQ(Row::column_min(j), columns[j].lo) << "column " << j;
1677 ASSERT_EQ(Row::column_max(j), columns[j].hi) << "column " << j;
1678 }
1679
1680 M m(3);
1681 Model<M> model(3);
1682 for (size_t j = 0; j < M::cols(); ++j) // one entry at a time
1683 {
1684 m[1][j] = columns[j].hi;
1685 model.at(1, j) = columns[j].hi;
1686 }
1687 model.expect_equal(m, "the middle row at its highest values");
1688
1689 Row r{}; // a whole row, written on a copy
1690 for (size_t j = 0; j < M::cols(); ++j)
1691 {
1692 const int64_t v = j % 2 == 1 ? columns[j].hi : columns[j].lo + 1;
1693 r(j) = v;
1694 model.at(2, j) = v;
1695 }
1696 m(2) = r;
1697 model.expect_equal(m, "a row written on a copy");
1698 }
1699
1700 // ------------------------------------------------------------------
1701 // What the unchecked writes do with a value out of range
1702 // ------------------------------------------------------------------
1703
1704# ifdef NDEBUG
1705 // Without assertions, the value is masked: its column is left with an
1706 // unspecified value and no other column changes
1707 TEST(PackedMatrixUnchecked, AValueOutOfRangeStaysInItsColumn)
1708 {
1709 Book m(3);
1710 Model<Book> model(3);
1711 for (size_t i = 0; i < 3; ++i)
1712 for (size_t j = 0; j < Book::cols(); ++j)
1713 {
1714 m(i, j) = Book::Row::column_max(j);
1715 model.at(i, j) = Book::Row::column_max(j);
1716 }
1717 for (size_t j = 0; j < Book::cols(); ++j)
1718 {
1719 const Array<int64_t> bad = invalid_values<Book::Row>(j);
1720 for (size_t k = 0; k < bad.size(); ++k)
1721 {
1722 m.fast_write(1, j, bad(k));
1723 model.expect_equal_but(m, 1, j, "after an invalid fast_write()");
1724 m(1, j) = bad(k);
1725 model.expect_equal_but(m, 1, j, "after an invalid write through the proxy");
1726 m(1, j) = Book::Row::column_max(j); // a valid value puts it right again
1727 model.expect_equal(m, "after an invalid unchecked write");
1728
1729 int64_t values[6];
1730 for (size_t c = 0; c < 6; ++c)
1731 values[c] = Book::Row::column_max(c);
1732 values[j] = bad(k);
1733 const Book::Row row = row_of<Book::Row>(values, false, std::make_index_sequence<6>());
1734 for (size_t c = 0; c < 6; ++c)
1735 if (c != j)
1736 {
1737 EXPECT_EQ(row(c), values[c]);
1738 }
1739 }
1740 }
1741 }
1742# else
1743 // With assertions, the unchecked accesses stop at a wrong argument
1744 TEST(PackedMatrixUncheckedDeathTest, AssertionsCatchWrongArguments)
1745 {
1746 Book m(2);
1747 const Book & cm = m;
1748 EXPECT_DEATH(m(0, 1) = 16, "valid"); // a value
1749 EXPECT_DEATH((void) Book::Row::fast_make(0, 0, 1001, 0, 0, 0), "valid"); // a value of a row
1750 EXPECT_DEATH((void) cm(0).fast_read(6), "num_cols"); // a column
1751 EXPECT_DEATH((void) Packed_Row<signed_bits(64)>::fast_make(std::numeric_limits<uint64_t>::max()),
1752 "fits_in_int64"); // not an int64_t
1753 }
1754# endif
1755}
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
TYPED_TEST(ArcHeapLifecycle, ClearNullsTheHandles)
long double w
Definition btreepic.C:153
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t row
Definition ca-c-api.h:115
size_t * rows
Definition ca-c-api.h:112
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Sparse matrix with generic row and column domains.
Definition al-matrix.H:70
void swap(ODhashTable &other) noexcept
Swaps this table with other in constant time.
Definition tpl_odhash.H:470
Matrix whose columns are bit fields: an Aleph::Array of Packed_Row.
Tuple of small integers packed into the fewest whole words.
Path on a graph.
Definition tpl_graph.H:3832
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.
Whether Columns_At gives a row for a value, with no constraint in front of it.
Whether a row can be built with fast_make() from values of the given types.
Whether a row can be built with make() from values of the given types.
Whether Packed_Matrix_Of takes a value as its columns.
Whether Packed_Row_Of takes a value as its columns.
#define TEST(name)
static mt19937 rng
#define N
Definition fib.C:294
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
static mpfr_t y
Definition mpfr_mul_d.c:3
@ I
Infected (and infectious).
Main namespace for Aleph-w library functions.
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
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.
typename packed_matrix_detail::Columns_At< Columns, std::make_index_sequence< Columns.size()> >::Row Packed_Row_Of
Packed_Row whose columns are the entries of an array.
bool between(const Point &a, const Point &b, const Point &c)
constexpr Bit_Column signed_bits(const unsigned n)
Column of n bits with sign: the integers of [-2^(n-1), 2^(n-1) - 1].
double add(double a, double b)
STL namespace.
Inclusive range of the integers that a column holds.
std::int64_t hi
Highest value of the column; not less than lo.
std::int64_t lo
Lowest value of the column.
The row and the matrix of the columns of an array, by their indexes.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Matrix whose columns are bit fields sized from integer ranges.