Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
moved_from_numeric_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
38#include "moved_from.H"
39
40#include <Aho_Corasick.H>
41#include <Suffix_Structures.H>
42#include <fft.H>
43#include <geom_algorithms.H>
44#include <ntt.H>
45#include <state_search_common.H>
46
47#include <bit>
48#include <cmath>
49#include <cstdint>
50#include <stdexcept>
51#include <string>
52
53using namespace Aleph;
54
55using F = FFT<double>;
56using Complex = F::Complex;
57using N = NTT<>;
59
60namespace
61{
62 // n samples in (0, 1]: no kernel, window or signal made with it is zero
63 Array<double> ramp(const size_t n)
64 {
66 for (size_t i = 0; i < n; ++i)
67 a.append(static_cast<double>(1 + i % 17) / 17.0);
68 return a;
69 }
70
71 // A text of n bytes over the letters a to e
72 std::string text(const size_t n)
73 {
74 std::string s;
75 for (size_t i = 0; i < n; ++i)
76 s += static_cast<char>('a' + (i * 7) % 5);
77 return s;
78 }
79
80 template <typename T>
81 void append_all(Array<T> & out, const Array<T> & items)
82 {
83 for (const T & item : items)
84 out.append(item);
85 }
86
87 // The linear convolution, by its definition
88 Array<double> direct_convolution(const Array<double> & signal, const Array<double> & kernel)
89 {
90 Array<double> out(signal.size() + kernel.size() - 1, 0.0);
91 for (size_t i = 0; i < signal.size(); ++i)
92 for (size_t j = 0; j < kernel.size(); ++j)
93 out(i + j) += signal[i] * kernel[j];
94 return out;
95 }
96
97 // The output from rest of the filter b / a, with a[0] == 1
98 Array<double> direct_iir(const Array<double> & x, const Array<double> & b,
99 const Array<double> & a)
100 {
101 Array<double> y(x.size(), 0.0);
102 for (size_t n = 0; n < x.size(); ++n)
103 {
104 double acc = 0;
105 for (size_t k = 0; k < b.size() and k <= n; ++k)
106 acc += b[k] * x[n - k];
107 for (size_t k = 1; k < a.size() and k <= n; ++k)
108 acc -= a[k] * y[n - k];
109 y(n) = acc;
110 }
111 return y;
112 }
113
114 void expect_near(const Array<double> & got, const Array<double> & expected)
115 {
116 ASSERT_EQ(got.size(), expected.size());
117 for (size_t i = 0; i < got.size(); ++i)
118 EXPECT_NEAR(got[i], expected[i], 1e-9);
119 }
120}
121
122template <> struct Moved_From_Case<F::Plan>
123{
124 // Odd sizes, for the Bluestein (101, 1001) and the mixed-radix (3) plans;
125 // the convolvers below hold power-of-two plans
126 static F::Plan make(const size_t n) { return F::Plan(n | 1); }
127 static bool is_empty(const F::Plan & p) { return p.size() == 0; }
128 static void use(F::Plan & p)
129 {
131 for (size_t i = 0; i < a.size(); ++i)
132 a(i) = Complex(static_cast<double>(i % 7), 1.0);
133 const Array<Complex> back = p.inverse_transform(p.transformed(a));
134 ASSERT_EQ(back.size(), a.size());
135 for (size_t i = 0; i < a.size(); ++i)
136 EXPECT_LT(std::abs(back[i] - a[i]), 1e-9);
137 }
138};
139
140// The convolvers are left empty when they are moved from: they reject every
141// block and signal, and reset() and flush() have nothing to do
142template <> struct Moved_From_Case<F::OverlapAdd>
143{
144 static F::OverlapAdd make(const size_t n)
145 {
146 F::OverlapAdd c(ramp(n / 10 + 1), n);
147 (void) c.process_block(ramp(n)); // leaves a pending tail
148 return c;
149 }
150 static bool is_empty(const F::OverlapAdd & c)
151 {
152 return c.kernel().is_empty() and c.block_size() == 0 and c.fft_size() == 0
153 and c.overlap_size() == 0;
154 }
155 static void use(F::OverlapAdd & c)
156 {
157 if (c.kernel().is_empty())
158 {
159 ThreadPool pool(1);
160 EXPECT_THROW((void) c.process_block(ramp(8)), std::runtime_error);
161 EXPECT_THROW((void) c.pprocess_block(pool, ramp(8)), std::runtime_error);
162 EXPECT_THROW((void) c.convolve(ramp(8)), std::runtime_error);
163 EXPECT_THROW((void) c.pconvolve(pool, ramp(8)), std::runtime_error);
164 c.reset();
165 EXPECT_TRUE(c.flush().is_empty());
166 return;
167 }
168
169 const Array<double> signal = ramp(3 * c.block_size() + 1);
170 const Array<double> expected = direct_convolution(signal, c.kernel());
171 expect_near(c.convolve(signal), expected);
172 c.reset();
173 Array<double> streamed = c.process_block(signal);
174 append_all(streamed, c.flush());
175 expect_near(streamed, expected);
176 }
177};
178
179template <> struct Moved_From_Case<F::OverlapAddBank>
180{
181 static Array<Array<double>> signals(const size_t channels, const size_t n)
182 {
184 for (size_t c = 0; c < channels; ++c)
185 s.append(ramp(n + c));
186 return s;
187 }
188 static F::OverlapAddBank make(const size_t n)
189 {
190 F::OverlapAddBank b(n < 10 ? 1 : 3, ramp(n / 10 + 1), n);
191 (void) b.process_block(signals(b.num_channels(), n)); // leaves pending tails
192 return b;
193 }
194 static bool is_empty(const F::OverlapAddBank & b)
195 {
196 return b.num_channels() == 0 and b.kernel().is_empty() and b.block_size() == 0
197 and b.overlap_size() == 0;
198 }
199 static void use(F::OverlapAddBank & b)
200 {
201 if (b.kernel().is_empty())
202 {
203 ThreadPool pool(1);
204 const Array<Array<double>> none; // as many channels as the bank has
205 EXPECT_THROW((void) b.process_block(none), std::runtime_error);
206 EXPECT_THROW((void) b.pprocess_block(pool, none), std::runtime_error);
207 EXPECT_THROW((void) b.convolve(none), std::runtime_error);
208 EXPECT_THROW((void) b.pconvolve(pool, none), std::runtime_error);
209 b.reset();
210 EXPECT_TRUE(b.flush().is_empty());
211 return;
212 }
213
214 const Array<Array<double>> in = signals(b.num_channels(), 2 * b.block_size() + 1);
215 const Array<Array<double>> out = b.convolve(in);
216 ASSERT_EQ(out.size(), in.size());
217 b.reset();
218 Array<Array<double>> streamed = b.process_block(in);
219 const Array<Array<double>> tails = b.flush();
220 ASSERT_EQ(streamed.size(), in.size());
221 ASSERT_EQ(tails.size(), in.size());
222 for (size_t c = 0; c < in.size(); ++c)
223 {
224 const Array<double> expected = direct_convolution(in[c], b.kernel());
225 expect_near(out[c], expected);
226 append_all(streamed(c), tails[c]);
227 expect_near(streamed[c], expected);
228 }
229 }
230};
231
232template <> struct Moved_From_Case<F::OverlapSave>
233{
234 static F::OverlapSave make(const size_t n)
235 {
236 F::OverlapSave c(ramp(n / 10 + 1), n);
237 (void) c.process_block(ramp(n + n / 2)); // leaves samples pending
238 return c;
239 }
240 static bool is_empty(const F::OverlapSave & c)
241 {
242 return c.kernel().is_empty() and c.block_size() == 0 and c.fft_size() == 0
243 and c.overlap_size() == 0;
244 }
245 static void use(F::OverlapSave & c)
246 {
247 if (c.kernel().is_empty())
248 {
249 EXPECT_THROW((void) c.process_block(ramp(8)), std::runtime_error);
250 EXPECT_THROW((void) c.convolve(ramp(8)), std::runtime_error);
251 c.reset();
252 EXPECT_TRUE(c.flush().is_empty());
253 return;
254 }
255
256 const Array<double> signal = ramp(3 * c.block_size() + 1);
257 const Array<double> expected = direct_convolution(signal, c.kernel());
258 expect_near(c.convolve(signal), expected);
259 Array<double> streamed = c.process_block(signal);
260 append_all(streamed, c.flush());
261 expect_near(streamed, expected);
262 }
263};
264
265template <> struct Moved_From_Case<F::PartitionedConvolver>
266{
267 static F::PartitionedConvolver make(const size_t n)
268 {
269 F::PartitionedConvolver c(ramp(n + 1), n < 10 ? 2 : 32);
270 (void) c.process_block(ramp(n / 2 + 1)); // leaves a state
271 return c;
272 }
273 static bool is_empty(const F::PartitionedConvolver & c)
274 {
275 return c.kernel().is_empty() and c.partition_size() == 0 and c.fft_size() == 0;
276 }
277 static void use(F::PartitionedConvolver & c)
278 {
279 if (c.kernel().is_empty())
280 {
281 EXPECT_THROW((void) c.process_block(ramp(8)), std::runtime_error);
282 EXPECT_THROW((void) c.convolve(ramp(8)), std::runtime_error);
283 c.reset();
284 EXPECT_TRUE(c.flush().is_empty());
285 return;
286 }
287
288 const Array<double> signal = ramp(3 * c.partition_size() + 1);
289 const Array<double> expected = direct_convolution(signal, c.kernel());
290 expect_near(c.convolve(signal), expected);
291 Array<double> streamed = c.process_block(signal);
292 append_all(streamed, c.flush());
293 expect_near(streamed, expected);
294 }
295};
296
297// The processors and the filters are left unconfigured, as built by their
298// default constructors: then every operation says so. The destination of a
299// move goes on as its source would have: the fingerprint is what a copy
300// outputs for the next block, after the samples or the state that make()
301// leaves pending, which use() then resets
302template <> struct Moved_From_Case<F::STFTProcessor>
303{
304 static F::STFTProcessor make(const size_t n)
305 {
306 const size_t frame = n < 10 ? 2 : n / 4;
307 F::STFTOptions o;
308 o.hop_size = frame / 2;
309 F::STFTProcessor s(ramp(frame), o);
310 (void) s.process_block(ramp(n)); // leaves samples pending
311 return s;
312 }
313 static bool is_empty(const F::STFTProcessor & s)
314 {
315 return not s.configured() and s.frame_size() == 0 and s.fft_size() == 0;
316 }
317 static Array<Array<Complex>> fingerprint(const F::STFTProcessor & s)
318 {
319 if (not s.configured())
320 return {};
321 F::STFTProcessor copy(s);
322 return copy.process_block(ramp(3 * s.frame_size()));
323 }
324 static void use(F::STFTProcessor & s)
325 {
326 if (not s.configured())
327 {
328 EXPECT_THROW((void) s.process_block(ramp(8)), std::runtime_error);
329 EXPECT_THROW(s.reset(), std::runtime_error);
330 return;
331 }
332
333 F::STFTOptions o;
334 o.hop_size = s.hop_size();
335 o.fft_size = s.fft_size();
336 o.centered = s.centered();
337 o.pad_end = s.pad_end();
338 const Array<double> signal = ramp(5 * s.frame_size());
339 const Array<Array<Complex>> expected = F::stft(signal, s.window(), o);
340 s.reset();
341 Array<Array<Complex>> frames = s.process_block(signal);
342 append_all(frames, s.flush());
343 ASSERT_EQ(frames.size(), expected.size());
344 for (size_t f = 0; f < frames.size(); ++f)
345 {
346 ASSERT_EQ(frames[f].size(), expected[f].size());
347 for (size_t k = 0; k < frames[f].size(); ++k)
348 EXPECT_LT(std::abs(frames[f][k] - expected[f][k]), 1e-9);
349 }
350 }
351};
352
353template <> struct Moved_From_Case<F::ISTFTProcessor>
354{
355 static F::ISTFTOptions options(const size_t hop)
356 {
357 F::ISTFTOptions o;
358 o.hop_size = hop;
359 return o;
360 }
361 // The frames of n samples; ramp(frame) is the window of every processor
362 static Array<Array<Complex>> spectrogram(const size_t frame, const size_t hop,
363 const size_t fft, const size_t n)
364 {
365 F::STFTOptions o;
366 o.hop_size = hop;
367 o.fft_size = fft;
368 return F::stft(ramp(n), ramp(frame), o);
369 }
370 static F::ISTFTProcessor make(const size_t n)
371 {
372 const size_t frame = n < 10 ? 2 : n / 4;
373 const size_t fft = std::bit_ceil(frame);
374 F::ISTFTProcessor p(fft, ramp(frame), options(frame / 2));
375 (void) p.process_block(spectrogram(frame, frame / 2, fft, n)); // leaves samples pending
376 return p;
377 }
378 static bool is_empty(const F::ISTFTProcessor & p)
379 {
380 return not p.configured() and p.frame_size() == 0 and p.fft_size() == 0;
381 }
382 static Array<double> fingerprint(const F::ISTFTProcessor & p)
383 {
384 if (not p.configured())
385 return {};
386 F::ISTFTProcessor copy(p);
387 return copy.process_block(
388 spectrogram(p.frame_size(), p.hop_size(), p.fft_size(), 3 * p.frame_size()));
389 }
390 static void use(F::ISTFTProcessor & p)
391 {
392 if (not p.configured())
393 {
394 EXPECT_THROW((void) p.process_frame(Array<Complex>({Complex(1.0, 0.0)})),
395 std::runtime_error);
396 EXPECT_THROW((void) p.flush(), std::runtime_error);
397 return;
398 }
399
400 const Array<Array<Complex>> frames =
401 spectrogram(p.frame_size(), p.hop_size(), p.fft_size(), 5 * p.frame_size());
402 const Array<double> expected = F::istft(frames, ramp(p.frame_size()), options(p.hop_size()));
403 p.reset();
404 Array<double> streamed = p.process_block(frames);
405 append_all(streamed, p.flush());
406 expect_near(streamed, expected);
407 }
408};
409
410template <> struct Moved_From_Case<F::LFilter>
411{
412 static F::LFilter make(const size_t n)
413 {
414 const bool small = n < 10;
415 F::LFilter f(small ? Array<double>{0.5, 0.5} : Array<double>{0.2, 0.3, 0.1},
416 small ? Array<double>{1.0, -0.25} : Array<double>{1.0, -0.4, 0.1});
417 (void) f.filter(ramp(n)); // leaves a state
418 return f;
419 }
420 static bool is_empty(const F::LFilter & f)
421 {
422 return not f.configured() and f.order() == 0 and f.state().is_empty();
423 }
424 static Array<double> fingerprint(const F::LFilter & f)
425 {
426 if (not f.configured())
427 return {};
428 F::LFilter copy(f);
429 return copy.filter(ramp(20));
430 }
431 static void use(F::LFilter & f)
432 {
433 if (not f.configured())
434 {
435 EXPECT_THROW((void) f.filter(ramp(8)), std::runtime_error);
436 EXPECT_THROW(f.reset(), std::runtime_error);
437 return;
438 }
439
440 f.reset();
441 const Array<double> signal = ramp(50);
442 expect_near(f.filter(signal),
443 direct_iir(signal, f.coefficients().numerator, f.coefficients().denominator));
444 EXPECT_EQ(f.state().size(), f.order());
445 f.set_state(Array<double>(f.order(), 0.0));
446 }
447};
448
449template <> struct Moved_From_Case<F::SOSFilter>
450{
451 // The section of every cascade
452 static F::BiquadSection section() { return {0.2, 0.3, 0.1, 1.0, -0.4, 0.1}; }
453 static F::SOSFilter make(const size_t n)
454 {
455 F::SOSFilter f(Array<F::BiquadSection>(n < 10 ? 1 : 3, section()));
456 (void) f.filter(ramp(n)); // leaves a state
457 return f;
458 }
459 static bool is_empty(const F::SOSFilter & f)
460 {
461 return not f.configured() and f.num_sections() == 0;
462 }
463 static Array<double> fingerprint(const F::SOSFilter & f)
464 {
465 if (not f.configured())
466 return {};
467 F::SOSFilter copy(f);
468 return copy.filter(ramp(20));
469 }
470 static void use(F::SOSFilter & f)
471 {
472 if (not f.configured())
473 {
474 EXPECT_THROW((void) f.filter(ramp(8)), std::runtime_error);
475 EXPECT_THROW(f.reset(), std::runtime_error);
476 return;
477 }
478
479 f.reset();
480 const Array<double> signal = ramp(50);
481 Array<double> expected = signal;
482 for (size_t i = 0; i < f.num_sections(); ++i)
483 expected = direct_iir(expected, section().numerator(), section().denominator());
484 expect_near(f.filter(signal), expected);
485 for (size_t i = 0; i < f.num_sections(); ++i)
486 EXPECT_EQ(f.state(i).size(), 2u);
487 }
488};
489
490template <> struct Moved_From_Case<N::Plan>
491{
492 // 7 takes the Bluestein path (14 divides MOD - 1), the powers of two the
493 // radix-2 one
494 static N::Plan make(const size_t n) { return N::Plan(n < 10 ? 7 : std::bit_ceil(n)); }
495 static bool is_empty(const N::Plan & p) { return p.size() == 0; }
496 static void use(N::Plan & p)
497 {
499 for (size_t i = 0; i < a.size(); ++i)
500 a(i) = i % 13;
501 const Array<uint64_t> back = p.transformed(p.transformed(a), true);
502 ASSERT_EQ(back.size(), a.size());
503 for (size_t i = 0; i < a.size(); ++i)
504 EXPECT_EQ(back[i], a[i]);
505 if (p.size() < 3)
506 return;
507
509 ASSERT_EQ(product.size(), 3u);
510 EXPECT_EQ(product[0], 3u);
511 EXPECT_EQ(product[1], 10u);
512 EXPECT_EQ(product[2], 8u);
513 }
514};
515
516// The automata are left as built by their default constructors: with a root
517template <> struct Moved_From_Case<Aho_Corasick>
518{
519 static Aho_Corasick make(const size_t n)
520 {
521 Aho_Corasick ac;
522 for (size_t i = 0; i < n; ++i)
523 (void) ac.add_pattern("p" + std::to_string(i));
524 ac.build();
525 return ac;
526 }
527 static bool is_empty(const Aho_Corasick & ac)
528 {
529 return ac.pattern_count() == 0 and not ac.is_built();
530 }
531 static void use(Aho_Corasick & ac)
532 {
533 for (size_t i = 0; i < ac.pattern_count(); ++i)
534 EXPECT_FALSE(ac.pattern(i).empty());
535 const size_t id = ac.add_pattern("zq");
536 ac.build();
537 EXPECT_TRUE(ac.contains_any("xxzqyy"));
538 bool found = false;
539 for (const Aho_Corasick::Match & m : ac.search("xxzqyy"))
540 found = found or (m.pattern_id == id and m.position == 2);
541 EXPECT_TRUE(found);
542 ac.clear();
543 EXPECT_EQ(ac.pattern_count(), 0u);
544 }
545};
546
548{
549 static Suffix_Automaton make(const size_t n)
550 {
552 sam.build(text(n));
553 return sam;
554 }
555 static bool is_empty(const Suffix_Automaton & sam)
556 {
557 return sam.state_count() == 1 and sam.distinct_substring_count() == 0;
558 }
559 static void use(Suffix_Automaton & sam)
560 {
561 for (size_t i = 1; i < sam.state_count(); ++i)
562 EXPECT_GT(sam.states()[i].len, 0u);
563 sam.extend('z');
564 EXPECT_TRUE(sam.contains("z"));
565 sam.build("abcab");
566 EXPECT_EQ(sam.distinct_substring_count(), 12u);
567 EXPECT_EQ(sam.longest_common_substring("xcabx"), "cab");
568 sam.clear();
569 EXPECT_EQ(sam.state_count(), 1u);
570 }
571};
572
574{
575 static Naive_Suffix_Tree make(const size_t n) { return Naive_Suffix_Tree(text(n)); }
576 static bool is_empty(const Naive_Suffix_Tree & t)
577 {
578 return t.text_size() == 0 and t.find_all("").size() == 1;
579 }
580 static void use(Naive_Suffix_Tree & t)
581 {
582 EXPECT_EQ(t.find_all("").size(), t.text_size() + 1);
583 EXPECT_FALSE(t.contains("zz"));
584 for (const Naive_Suffix_Tree::Node & node : t.nodes())
585 for (const size_t child : node.children)
586 EXPECT_LT(child, t.node_count());
587 t.build("banana");
588 EXPECT_TRUE(t.contains("nan"));
589 const Array<size_t> positions = t.find_all("ana");
590 ASSERT_EQ(positions.size(), 2u);
591 EXPECT_EQ(positions[0], 1u);
592 EXPECT_EQ(positions[1], 3u);
593 }
594};
595
596template <> struct Moved_From_Case<RangeTree2D>
597{
598 static RangeTree2D make(const size_t n)
599 {
600 DynList<Point> points;
601 for (size_t i = 0; i < n; ++i)
602 points.append(Point(i, (i * 37) % n));
603 RangeTree2D t;
604 t.build(points);
605 return t;
606 }
607 static bool is_empty(const RangeTree2D & t) { return t.is_empty() and t.size() == 0; }
608 static void use(RangeTree2D & t)
609 {
610 EXPECT_EQ(t.query(-1, 1 << 20, -1, 1 << 20).size(), t.size());
611 EXPECT_EQ(t.debug_snapshot().x_sorted_points.size(), t.size());
612 DynList<Point> points;
613 points.append(Point(1, 2));
614 points.append(Point(3, 4));
615 t.build(points);
616 EXPECT_EQ(t.size(), 2u);
617 EXPECT_EQ(t.query(0, 2, 0, 3).size(), 1u);
618 }
619};
620
621template <> struct Moved_From_Case<AABBTree>
622{
623 static AABBTree make(const size_t n)
624 {
626 for (size_t i = 0; i < n; ++i)
627 entries.append({Rectangle(i, i, i + 2, i + 2), i});
628 AABBTree t;
629 t.build(entries);
630 return t;
631 }
632 static bool is_empty(const AABBTree & t)
633 {
634 return t.is_empty() and t.debug_snapshot().nodes.is_empty();
635 }
636 static void use(AABBTree & t)
637 {
638 EXPECT_EQ(t.query(Rectangle(-1, -1, 1 << 20, 1 << 20)).size(), t.size());
639 EXPECT_EQ(t.query_point(Point(1, 1)).is_empty(), t.is_empty());
640 if (t.is_empty()) // the braces keep GCC 11 and 13 from -Wdangling-else
641 {
642 EXPECT_THROW((void) t.root_bbox(), std::domain_error);
643 }
644 else
645 {
646 EXPECT_TRUE(t.root_bbox().get_xmin() == 0);
647 }
649 one.append({Rectangle(0, 0, 2, 2), 7});
650 t.build(one);
651 const Array<size_t> hits = t.query_point(Point(1, 1));
652 ASSERT_EQ(hits.size(), 1u);
653 EXPECT_EQ(hits[0], 7u);
654 }
655};
656
657template <> struct Moved_From_Case<Collector>
658{
659 static Collector make(const size_t n)
660 {
661 Collector c(n + 5);
662 for (size_t i = 0; i < n; ++i)
663 (void) c(static_cast<int>(i));
664 return c;
665 }
666 static bool is_empty(const Collector & c)
667 {
668 return c.is_empty() and c.size() == 0 and c.solutions().is_empty();
669 }
670 static void use(Collector & c)
671 {
672 EXPECT_EQ(c.size(), c.solutions().size());
673 const size_t before = c.size();
674 (void) c(42);
675 EXPECT_EQ(c.size(), before + 1);
676 EXPECT_EQ(c.solutions().get_last(), 42);
677 c.clear();
678 EXPECT_TRUE(c.is_empty());
679 EXPECT_TRUE(c(1)); // every limit here is above 1
680 c.clear();
681 }
682};
683
684using Numeric = ::testing::Types<F::Plan, F::OverlapAdd, F::OverlapAddBank, F::OverlapSave,
685 F::PartitionedConvolver, F::STFTProcessor, F::ISTFTProcessor,
686 F::LFilter, F::SOSFilter, N::Plan, Aho_Corasick,
688 Collector>;
Multi-pattern string matching with the Aho-Corasick automaton.
Suffix structures: suffix array/LCP, suffix tree, suffix automaton.
size_t size_t int32_t * out
Definition ca-c-api.h:120
Axis-aligned bounding box tree for spatial queries.
Rectangle root_bbox() const
Return the root bounding box (union of all entries).
size_t build(Array< size_t > &idx, const size_t lo, const size_t hi)
bool is_empty() const
Whether the tree is empty.
size_t size() const
Number of entries.
DebugSnapshot debug_snapshot() const
Return the full tree structure for visualization/debug.
Array< size_t > query(const Rectangle &query) const
Find all entries whose bounding box overlaps the query rectangle.
Array< size_t > query_point(const Point &p) const
Find all entries whose bounding box contains the query point.
Aho-Corasick multi-pattern automaton.
const std::string & pattern(const size_t id) const
Return the pattern text by id.
bool contains_any(const std::string_view text) const
Return true if at least one pattern appears in the text.
void clear()
Clear all patterns and reset to an empty automaton.
size_t pattern_count() const noexcept
Return the number of inserted patterns.
bool is_built() const noexcept
Return whether build() has been executed since last insertion.
void build()
Build failure links and transition completion.
size_t add_pattern(std::string pattern)
Add one pattern to the automaton.
Array< Match > search(const std::string_view text) const
Search all patterns in a text.
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
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Precomputed plans for NTT transforms.
Definition ntt.H:466
Array< uint64_t > multiply(const Array< uint64_t > &a, const Array< uint64_t > &b) const
Multiply two polynomials using this plan size.
Definition ntt.H:1214
size_t size() const noexcept
Return the transform size bound to the plan.
Definition ntt.H:1170
Array< uint64_t > transformed(const Array< uint64_t > &input, const bool invert=false) const
Transform an input array and return the result.
Definition ntt.H:1197
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Naive compressed suffix tree (didactic implementation).
Array< size_t > find_all(const std::string_view pattern) const
Return all occurrences of a pattern.
size_t node_count() const noexcept
Return the number of nodes in the tree.
void build(const std::string_view text)
Build or rebuild the suffix tree for text.
size_t text_size() const noexcept
Return the text size (without terminal marker).
bool contains(const std::string_view pattern) const
Return true if the pattern appears in the text.
const Array< Node > & nodes() const noexcept
Return the internal node array for inspection/debugging.
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
Static 2D range tree for orthogonal range queries.
bool is_empty() const noexcept
DynList< Point > query(const Geom_Number &xmin, const Geom_Number &xmax, const Geom_Number &ymin, const Geom_Number &ymax) const
Query: return all points inside [xmin,xmax] × [ymin,ymax].
size_t size() const noexcept
void build(const DynList< Point > &points)
Build the range tree from a point set.
DebugSnapshot debug_snapshot() const
Return structural snapshot for visualization/debug.
An axis-aligned rectangle.
Definition point.H:1789
const Geom_Number & get_xmin() const
Gets the minimum x-coordinate.
Definition point.H:1815
Collector that stores accepted solutions in an Aleph list.
size_t size() const noexcept
Number of collected solutions.
const Container_Type & solutions() const noexcept
Access the collected solutions.
bool is_empty() const noexcept
Return true if no solution has been collected.
void clear() noexcept
Remove all collected solutions.
Suffix automaton (SAM) over byte alphabet.
std::string longest_common_substring(const std::string_view other) const
Find the longest common substring between the built SAM text and other.
void build(const std::string_view text)
Build the SAM from an entire string.
bool contains(const std::string_view pattern) const
Return true if pattern is a substring of the built text.
const Array< State > & states() const noexcept
Access the internal states array for testing/inspection.
size_t distinct_substring_count() const
Count the number of distinct substrings in the built text.
size_t state_count() const noexcept
Return the number of states in the SAM.
void clear()
Reset the automaton to its initial empty state (root only).
void extend(const char ch)
Extend the SAM with a single character.
A reusable thread pool for efficient parallel task execution.
Minimal std::expected-style result type for C++20.
The typed suite of the moved-from tests.
Definition moved_from.H:115
Fast Fourier Transform (FFT) and Digital Signal Processing (DSP) toolkit.
Computational geometry algorithms.
The pattern test of the moved-from objects.
INSTANTIATE_TYPED_TEST_SUITE_P(Numeric, MovedFrom, Numeric)
::testing::Types< F::Plan, F::OverlapAdd, F::OverlapAddBank, F::OverlapSave, F::PartitionedConvolver, F::STFTProcessor, F::ISTFTProcessor, F::LFilter, F::SOSFilter, N::Plan, Aho_Corasick, Suffix_Automaton, Naive_Suffix_Tree, RangeTree2D, AABBTree, Collector > Numeric
F::Complex Complex
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
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.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
static struct argp_option options[]
Definition ntreepic.C:1890
Industrial-grade Number Theoretic Transform core for modular polynomial multiplication.
static void section(const string &title)
Common infrastructure for implicit state-space search.
Array< DebugNode > nodes
Array of all nodes in debug format.
One match occurrence in the searched text.
Array< Point > x_sorted_points
The underlying x-sorted point array.
static AABBTree make(const size_t n)
static bool is_empty(const AABBTree &t)
static Aho_Corasick make(const size_t n)
static bool is_empty(const Aho_Corasick &ac)
static void use(Aho_Corasick &ac)
static Collector make(const size_t n)
static bool is_empty(const Collector &c)
static F::ISTFTProcessor make(const size_t n)
static Array< Array< Complex > > spectrogram(const size_t frame, const size_t hop, const size_t fft, const size_t n)
static F::ISTFTOptions options(const size_t hop)
static void use(F::ISTFTProcessor &p)
static Array< double > fingerprint(const F::ISTFTProcessor &p)
static bool is_empty(const F::ISTFTProcessor &p)
static Array< double > fingerprint(const F::LFilter &f)
static F::LFilter make(const size_t n)
static bool is_empty(const F::LFilter &f)
static bool is_empty(const F::OverlapAddBank &b)
static void use(F::OverlapAddBank &b)
static F::OverlapAddBank make(const size_t n)
static Array< Array< double > > signals(const size_t channels, const size_t n)
static F::OverlapAdd make(const size_t n)
static bool is_empty(const F::OverlapAdd &c)
static F::OverlapSave make(const size_t n)
static bool is_empty(const F::OverlapSave &c)
static F::PartitionedConvolver make(const size_t n)
static void use(F::PartitionedConvolver &c)
static bool is_empty(const F::PartitionedConvolver &c)
static F::Plan make(const size_t n)
static bool is_empty(const F::Plan &p)
static bool is_empty(const F::SOSFilter &f)
static F::SOSFilter make(const size_t n)
static Array< double > fingerprint(const F::SOSFilter &f)
static F::STFTProcessor make(const size_t n)
static Array< Array< Complex > > fingerprint(const F::STFTProcessor &s)
static bool is_empty(const F::STFTProcessor &s)
static void use(F::STFTProcessor &s)
static N::Plan make(const size_t n)
static bool is_empty(const N::Plan &p)
static bool is_empty(const Naive_Suffix_Tree &t)
static void use(Naive_Suffix_Tree &t)
static Naive_Suffix_Tree make(const size_t n)
static RangeTree2D make(const size_t n)
static bool is_empty(const RangeTree2D &t)
static void use(Suffix_Automaton &sam)
static Suffix_Automaton make(const size_t n)
static bool is_empty(const Suffix_Automaton &sam)
How the moved-from tests build, check and use objects of a class.
Definition moved_from.H:92
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k