• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-system / 43642063456

14 Nov 2026 09:17PM UTC coverage: 96.305% (+0.006%) from 96.299%
43642063456

push

github

web-flow
Merge pull request #2174 from evoskuil/master

Add pool projection and fee estimate.

176 of 178 new or added lines in 1 file covered. (98.88%)

27285 of 28332 relevant lines covered (96.3%)

3932606.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.72
/include/bitcoin/system/impl/pool/algorithm.ipp
1
/**
2
 * Copyright (c) 2011-2026 libbitcoin developers
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#ifndef LIBBITCOIN_SYSTEM_POOL_ALGORITHM_IPP
20
#define LIBBITCOIN_SYSTEM_POOL_ALGORITHM_IPP
21

22
#include <span>
23
#include <bitcoin/system/data/data.hpp>
24
#include <bitcoin/system/define.hpp>
25
#include <bitcoin/system/endian/endian.hpp>
26
#include <bitcoin/system/intrinsics/intrinsics.hpp>
27
#include <bitcoin/system/math/math.hpp>
28

29
namespace libbitcoin {
30
namespace system {
31
namespace pool {
32

33
BC_PUSH_WARNING(NO_USE_OF_SPAN)
34
BC_PUSH_WARNING(NO_ARRAY_INDEXING)
35
BC_PUSH_WARNING(NO_DYNAMIC_ARRAY_INDEXING)
36
BC_PUSH_WARNING(NO_POINTER_ARITHMETIC)
37

38
// Cells.
39
// ----------------------------------------------------------------------------
40

41
constexpr bool algorithm::is_present(link_t cell) NOEXCEPT
1,314 ✔
42
{
43
    return cell < absent;
1,314 ✔
44
}
45

46
constexpr bool algorithm::is_labeled(link_t cell) NOEXCEPT
863 ✔
47
{
48
    return cell < unlabeled;
863 ✔
49
}
50

51
constexpr bool algorithm::is_boundary(link_t cell) NOEXCEPT
96 ✔
52
{
53
    return cell == boundary;
96 ✔
54
}
55

56
constexpr uint64_t algorithm::chargeable(const totals_t& totals,
139 ✔
57
    uint32_t bytes_per_sigop) NOEXCEPT
58
{
59
    const auto sigops = totals.sigops;
139 ✔
60
    const auto cost = ceilinged_multiply<uint64_t>(sigops, bytes_per_sigop);
139 ✔
61
    return greater(totals.size, cost);
139 ✔
62
}
63

64
constexpr uint64_t algorithm::fee_rate(const totals_t& totals,
93 ✔
65
    uint32_t bytes_per_sigop) NOEXCEPT
66
{
67
    const auto size = greater(chargeable(totals, bytes_per_sigop), one);
93 ✔
68
    return ceilinged_multiply(totals.fee, 1'000_u64) / size;
93 ✔
69
}
70

71
// Column values.
72
// ----------------------------------------------------------------------------
73

74
constexpr algorithm::link_t algorithm::to_column(link_t value) NOEXCEPT
3,166 ✔
75
{
76
    return native_to_little_end(value);
3,166 ✔
77
}
78

79
constexpr algorithm::edges_t algorithm::to_column(
285 ✔
80
    const edges_t& value) NOEXCEPT
81
{
82
    return { to_column(value.first), to_column(value.count) };
285 ✔
83
}
84

85
constexpr algorithm::totals_t algorithm::to_column(
281 ✔
86
    const totals_t& value) NOEXCEPT
87
{
88
    return
89
    {
90
        native_to_little_end(value.fee),
281 ✔
91
        native_to_little_end(value.size),
281 ✔
92
        native_to_little_end(value.sigops)
281 ✔
93
    };
281 ✔
94
}
95

96
constexpr algorithm::link_t algorithm::from_column(link_t value) NOEXCEPT
14,134 ✔
97
{
98
    return native_from_little_end(value);
14,134 ✔
99
}
100

101
constexpr algorithm::edges_t algorithm::from_column(
1,473 ✔
102
    const edges_t& value) NOEXCEPT
103
{
104
    return { from_column(value.first), from_column(value.count) };
1,473 ✔
105
}
106

107
constexpr algorithm::totals_t algorithm::from_column(
1,801 ✔
108
    const totals_t& value) NOEXCEPT
109
{
110
    return
111
    {
112
        native_from_little_end(value.fee),
1,801 ✔
113
        native_from_little_end(value.size),
1,801 ✔
114
        native_from_little_end(value.sigops)
1,801 ✔
115
    };
1,801 ✔
116
}
117

118
// Candidates.
119
// ----------------------------------------------------------------------------
120

121
constexpr void algorithm::initialize(const graph_t& graph,
3 ✔
122
    const std::span<const link_t>& candidates) NOEXCEPT
123
{
124
    for (const auto row: candidates)
6 ✔
125
        set_cell(graph, row, unlabeled);
3 ✔
126

127
    // Relevance closes over the candidates in at most count passes.
128
    for (auto changed = true; changed;)
8 ✔
129
    {
130
        changed = false;
131
        for (const auto row: candidates)
10 ✔
132
        {
133
            if (is_labeled(cell(graph, row)))
5 ✔
134
                continue;
2 ✔
135

136
            if (contested(graph, row) || descended(graph, row))
3 ✔
137
            {
138
                set_cell(graph, row, row);
2 ✔
139
                changed = true;
140
            }
141
        }
142
    }
143
}
3 ✔
144

145
// Components (frontier).
146
// ----------------------------------------------------------------------------
147

148
constexpr bool algorithm::collect(component_t& out, const graph_t& graph,
33 ✔
149
    link_t row) NOEXCEPT
150
{
151
    out = {};
33 ✔
152
    const auto add = [&](link_t found) NOEXCEPT
297 ✔
153
    {
154
        if (index(out, found) != maximum_members)
297 ✔
155
            return true;
156

157
        if (out.count == maximum_members)
163 ✔
158
            return false;
159

160
        out.rows[out.count++] = found;
163 ✔
161
        return true;
163 ✔
162
    };
33 ✔
163

164
    if (!add(row))
33 ✔
165
        return false;
166

167
    for (size_t at{}; at < out.count; ++at)
196 ✔
168
    {
169
        const auto member = out.rows[at];
163 ✔
170
        const auto slice = edges(graph, member);
163 ✔
171
        for (auto input = slice.first; input < slice.first + slice.count;
359 ✔
172
            ++input)
173
        {
174
            const auto up = parent(graph, input);
196 ✔
175
            if (up != none && is_labeled(cell(graph, up)) && !add(up))
196 ✔
176
                return false;
×
177

178
            auto added = true;
196 ✔
179
            each_rival(graph, input, [&](link_t other) NOEXCEPT
196 ✔
180
            {
181
                const auto rival_row = owner(graph, other);
137 ✔
182
                if (is_present(cell(graph, rival_row)) && !add(rival_row))
137 ✔
183
                    added = false;
×
184
            });
137 ✔
185

186
            if (!added)
196 ✔
187
                return false;
188
        }
189

190
        auto added = true;
163 ✔
191
        each_child(graph, member, [&](link_t down) NOEXCEPT
163 ✔
192
        {
193
            if (is_present(cell(graph, down)) && !add(down))
64 ✔
194
                added = false;
×
195
        });
64 ✔
196

197
        if (!added)
163 ✔
198
            return false;
199
    }
200

201
    std::sort(out.rows.begin(), std::next(out.rows.begin(), out.count));
33 ✔
202
    return true;
33 ✔
203
}
204

205
constexpr bool algorithm::relate(component_t& out,
21 ✔
206
    const graph_t& graph) NOEXCEPT
207
{
208
    out.contests = zero;
21 ✔
209
    for (size_t member{}; member < out.count; ++member)
127 ✔
210
    {
211
        mask_t direct{};
106 ✔
212
        const auto row = out.rows[member];
106 ✔
213
        const auto slice = edges(graph, row);
106 ✔
214
        for (auto input = slice.first; input < slice.first + slice.count;
233 ✔
215
            ++input)
216
        {
217
            const auto up = index(out, parent(graph, input));
127 ✔
218
            if (up != maximum_members)
127 ✔
219
                set_right_into(direct, up);
42 ✔
220

221
            auto clique = set_right(mask_t{}, member);
127 ✔
222
            each_rival(graph, input, [&](link_t other) NOEXCEPT
127 ✔
223
            {
224
                const auto rival_member = index(out, owner(graph, other));
88 ✔
225
                if (rival_member != maximum_members)
88 ✔
226
                    set_right_into(clique, rival_member);
88 ✔
227
            });
88 ✔
228

229
            if (is_one(ones_count(clique)))
127 ✔
230
                continue;
85 ✔
231

232
            const auto end = std::next(out.cliques.begin(), out.contests);
85 ✔
233
            if (std::find(out.cliques.begin(), end, clique) != end)
85 ✔
234
                continue;
43 ✔
235

236
            if (out.contests == maximum_members)
42 ✔
237
                return false;
×
238

239
            out.cliques[out.contests++] = clique;
42 ✔
240
        }
241

242
        out.ancestors[member] = direct;
106 ✔
243
    }
244

245
    // Ancestry closes within the component in at most count passes.
246
    for (auto changed = true; changed;)
42 ✔
247
    {
248
        changed = false;
249
        for (size_t member{}; member < out.count; ++member)
127 ✔
250
        {
251
            auto ancestors = out.ancestors[member];
106 ✔
252
            for (size_t up{}; up < out.count; ++up)
642 ✔
253
                if (get_right(ancestors, up))
536 ✔
254
                    ancestors |= out.ancestors[up];
42 ✔
255

256
            if (ancestors != out.ancestors[member])
106 ✔
257
            {
258
                out.ancestors[member] = ancestors;
×
259
                changed = true;
260
            }
261
        }
262
    }
263

264
    return true;
265
}
266

267
constexpr algorithm::mask_t algorithm::members(const component_t& component,
5 ✔
268
    const std::span<const link_t>& rows) NOEXCEPT
269
{
270
    mask_t out{};
5 ✔
271
    for (const auto row: rows)
12 ✔
272
    {
273
        const auto member = index(component, row);
7 ✔
274
        if (member != maximum_members)
7 ✔
275
            set_right_into(out, member);
6 ✔
276
    }
277

278
    return out;
5 ✔
279
}
280

281
inline bool algorithm::ancestry(totals_t& out, const graph_t& graph,
4 ✔
282
    const component_t& component, const totals_t& limit) NOEXCEPT
283
{
284
    out = {};
4 ✔
285
    std::vector<link_t> visited{};
4 ✔
286
    std::vector<link_t> pending{};
4 ✔
287
    const auto push = [&](link_t row) NOEXCEPT
7 ✔
288
    {
289
        if (index(component, row) != maximum_members ||
7 ✔
290
            std::ranges::find(visited, row) != visited.end())
1 ✔
291
            return;
6 ✔
292

293
        visited.push_back(row);
1 ✔
294
        pending.push_back(row);
1 ✔
295
    };
4 ✔
296

297
    const auto charge = [&](link_t row) NOEXCEPT
18 ✔
298
    {
299
        const auto& add = totals(graph, row);
18 ✔
300
        out.fee = ceilinged_add(out.fee, add.fee);
18 ✔
301
        out.size = ceilinged_add(out.size, add.size);
18 ✔
302
        out.sigops = ceilinged_add(out.sigops, add.sigops);
18 ✔
303

304
        const auto slice = edges(graph, row);
18 ✔
305
        for (auto input = slice.first; input < slice.first + slice.count;
39 ✔
306
            ++input)
307
        {
308
            const auto up = parent(graph, input);
21 ✔
309
            if (up != none && is_present(cell(graph, up)))
21 ✔
310
                push(up);
7 ✔
311
        }
312

313
        return out.size <= limit.size && out.sigops <= limit.sigops;
18 ✔
314
    };
4 ✔
315

316
    for (size_t member{}; member < component.count; ++member)
19 ✔
317
        if (!charge(component.rows[member]))
17 ✔
318
            return false;
319

320
    while (!pending.empty())
3 ✔
321
    {
322
        const auto row = pending.back();
1 ✔
323
        pending.pop_back();
1 ✔
324
        if (!charge(row))
1 ✔
325
            return false;
326
    }
327

328
    return true;
329
}
4 ✔
330

331
// Outcomes.
332
// ----------------------------------------------------------------------------
333

334
constexpr size_t algorithm::outcomes(const component_t& component) NOEXCEPT
8 ✔
335
{
336
    size_t out{ one };
337
    for (size_t contest{}; contest < component.contests; ++contest)
24 ✔
338
        out = ceilinged_multiply(out, ones_count(component.cliques[contest]));
16 ✔
339

340
    return out;
8 ✔
341
}
342

343
constexpr algorithm::mask_t algorithm::outcome(const component_t& component,
168 ✔
344
    size_t index) NOEXCEPT
345
{
346
    const auto lost = losers(component, index);
168 ✔
347
    auto out = bit_and(unmask_right<mask_t>(component.count), bit_not(lost));
168 ✔
348
    for (size_t member{}; member < component.count; ++member)
1,008 ✔
349
        if (is_nonzero(bit_and(component.ancestors[member], lost)))
840 ✔
350
            out = set_right(out, member, false);
205 ✔
351

352
    return out;
168 ✔
353
}
354

355
template <typename Word>
356
constexpr void algorithm::evaluate(uint64_t& best, uint64_t& best_without,
7 ✔
357
    const std::span<const mask_t>& outcomes, mask_t candidate,
358
    const component_t& component, const graph_t& graph) NOEXCEPT
359
{
360
    best = zero;
7 ✔
361
    best_without = zero;
7 ✔
362
    each_fee<Word>(zero, outcomes, component, graph,
7 ✔
363
        [&](size_t index, uint64_t fee) NOEXCEPT
291 ✔
364
        {
365
            best = std::max(best, fee);
142 ✔
366
            if (is_zero(bit_and(outcomes[index], candidate)))
142 ✔
367
                best_without = std::max(best_without, fee);
68 ✔
368
        });
369
}
7 ✔
370

371
constexpr bool algorithm::covered(uint64_t best, uint64_t best_without,
4 ✔
372
    uint64_t candidate_bytes, uint64_t rate) NOEXCEPT
373
{
374
    // Compared in satoshis per virtual kilobyte.
375
    const auto added = floored_subtract(best, best_without);
4 ✔
376
    return ceilinged_multiply(added, 1'000_u64) >=
4 ✔
377
        ceilinged_multiply(rate, candidate_bytes);
4 ✔
378
}
379

380
// Transitions (frontier, table columns).
381
// ----------------------------------------------------------------------------
382

383
constexpr void algorithm::relabel(const columns_t& columns,
4 ✔
384
    const component_t& component) NOEXCEPT
385
{
386
    // Rows are sorted by collect, so the label is the first.
387
    const auto label = to_column(component.rows.front());
4 ✔
388
    for (size_t member{}; member < component.count; ++member)
22 ✔
389
        columns.cells[component.rows[member]] = label;
18 ✔
390
}
4 ✔
391

392
inline void algorithm::exclude(const columns_t& columns, link_t row) NOEXCEPT
6 ✔
393
{
394
    const graph_t graph{ columns };
6 ✔
395
    std::vector<link_t> pending{ row };
6 ✔
396
    set_cell(graph, row, absent);
6 ✔
397

398
    while (!pending.empty())
18 ✔
399
    {
400
        const auto next = pending.back();
12 ✔
401
        pending.pop_back();
12 ✔
402
        each_child(graph, next, [&](link_t down) NOEXCEPT
12 ✔
403
        {
404
            if (is_present(cell(graph, down)))
6 ✔
405
            {
406
                set_cell(graph, down, absent);
6 ✔
407
                pending.push_back(down);
6 ✔
408
            }
409
        });
6 ✔
410
    }
411
}
6 ✔
412

413
inline void algorithm::admit(const columns_t& columns, link_t row) NOEXCEPT
1 ✔
414
{
415
    const std_array<link_t, one> rows{ { row } };
1 ✔
416
    columns.cells[row] = to_column(unlabeled);
1 ✔
417
    reform(columns, rows);
1 ✔
418
}
1 ✔
419

420
inline void algorithm::confirm(const columns_t& columns, link_t row) NOEXCEPT
5 ✔
421
{
422
    const graph_t graph{ columns };
5 ✔
423
    component_t former{};
5 ✔
424
    const auto labeled = is_labeled(cell(graph, row)) &&
9 ✔
425
        collect(former, graph, row);
4 ✔
426

427
    set_cell(graph, row, boundary);
5 ✔
428
    const auto slice = edges(graph, row);
5 ✔
429
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
10 ✔
430
    {
431
        each_rival(graph, input, [&](link_t other) NOEXCEPT
5 ✔
432
        {
433
            const auto rival_row = owner(graph, other);
5 ✔
434
            if (is_present(cell(graph, rival_row)))
5 ✔
435
                exclude(columns, rival_row);
4 ✔
436
        });
5 ✔
437
    }
438

439
    if (!labeled)
5 ✔
440
        return;
1 ✔
441

442
    std::vector<link_t> rest{};
4 ✔
443
    for (size_t member{}; member < former.count; ++member)
24 ✔
444
        if (is_present(cell(graph, former.rows[member])))
20 ✔
445
            rest.push_back(former.rows[member]);
8 ✔
446

447
    reform(columns, rest);
4 ✔
448
}
4 ✔
449

450
inline void algorithm::unconfirm(const columns_t& columns, link_t row) NOEXCEPT
2 ✔
451
{
452
    const graph_t graph{ columns };
2 ✔
453
    std::vector<link_t> affected{ row };
2 ✔
454
    set_cell(graph, row, unlabeled);
2 ✔
455

456
    const auto slice = edges(graph, row);
2 ✔
457
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
4 ✔
458
    {
459
        each_rival(graph, input, [&](link_t other) NOEXCEPT
2 ✔
460
        {
461
            restore(affected, columns, owner(graph, other));
2 ✔
462
        });
2 ✔
463
    }
464

465
    reform(columns, affected);
2 ✔
466
}
2 ✔
467

468
inline void algorithm::revive(const columns_t& columns, link_t row) NOEXCEPT
2 ✔
469
{
470
    std::vector<link_t> affected{};
2 ✔
471
    restore(affected, columns, row);
2 ✔
472
    reform(columns, affected);
2 ✔
473
}
2 ✔
474

475
// Template (frontier, table columns).
476
// ----------------------------------------------------------------------------
477

478
template <typename Word>
479
inline void algorithm::resolve(std::span<mark> marks,
7 ✔
480
    const columns_t& columns, size_t maximum_outcomes) NOEXCEPT
481
{
482
    const graph_t graph{ columns };
7 ✔
483
    const auto count = columns.cells.size();
7 ✔
484
    for (size_t row{}; row < count; ++row)
40 ✔
485
    {
486
        const auto present = is_present(from_column(columns.cells[row]));
33 ✔
487
        marks[row] = present ? mark::unmarked : mark::excluded;
34 ✔
488
    }
489

490
    std::vector<mask_t> masks{};
7 ✔
491
    for (size_t at{}; at < count; ++at)
40 ✔
492
    {
493
        // A component is resolved at its label, its lowest member row.
494
        const auto row = possible_narrow_cast<link_t>(at);
33 ✔
495
        const auto label = cell(graph, row);
33 ✔
496
        if (!is_labeled(label) || label != row)
33 ✔
497
            continue;
28 ✔
498

499
        component_t component{};
6 ✔
500
        const auto collected = collect(component, graph, row);
6 ✔
501
        const auto related = collected && relate(component, graph);
6 ✔
502
        const auto limit = related ? maximum_outcomes : zero;
6 ✔
503
        const auto total = std::min(outcomes(component), limit);
6 ✔
504

505
        if (is_zero(total))
6 ✔
506
        {
507
            forfeit(marks, graph, row);
1 ✔
508
            continue;
1 ✔
509
        }
510

511
        masks.resize(total);
5 ✔
512
        for (size_t index{}; index < total; ++index)
23 ✔
513
            masks[index] = outcome(component, index);
18 ✔
514

515
        const auto chosen = masks[best<Word>(masks, component, graph)];
5 ✔
516
        for (size_t member{}; member < component.count; ++member)
30 ✔
517
            if (!get_right(chosen, member))
25 ✔
518
                discard(marks, graph, component.rows[member]);
11 ✔
519
    }
520
}
7 ✔
521

522
inline void algorithm::pack(std::vector<link_t>& out, std::span<mark> marks,
9 ✔
523
    const columns_t& columns, const totals_t& budget, size_t depth,
524
    uint32_t bytes_per_sigop) NOEXCEPT
525
{
526
    takes_t takes{};
9 ✔
527
    pack_(out, takes, marks, columns, budget, depth, bytes_per_sigop);
9 ✔
528
}
9 ✔
529

530
inline void algorithm::project(std::vector<link_t>& out,
22 ✔
531
    projection_t& projection, const columns_t& columns,
532
    const totals_t& budget, size_t depth, uint32_t bytes_per_sigop) NOEXCEPT
533
{
534
    takes_t takes{};
22 ✔
535
    auto& marks = projection.marks;
22 ✔
536
    pack_(out, takes, marks, columns, budget, depth, bytes_per_sigop);
22 ✔
537

538
    const auto left = std::ranges::find(marks, mark::unmarked) != marks.end();
22 ✔
539
    projection.exhausted = takes.empty() || !left;
22 ✔
540
    if (!takes.empty())
22 ✔
541
        projection.blocks.push_back(std::move(takes));
20 ✔
542
}
22 ✔
543

544
// Estimate (projected blocks).
545
// ----------------------------------------------------------------------------
546

547
constexpr uint64_t algorithm::above(const std::span<const take_t>& takes,
874 ✔
548
    uint64_t rate) NOEXCEPT
549
{
550
    uint64_t size{};
551
    for (const auto& take: takes)
2,468 ✔
552
        if (take.rate >= rate)
1,594 ✔
553
            size = ceilinged_add(size, take.size);
876 ✔
554

555
    return size;
874 ✔
556
}
557

558
template <typename Arrivals>
559
inline uint64_t algorithm::estimate(const projection_t& projection,
9 ✔
560
    size_t count, uint64_t space, const Arrivals& arrivals) NOEXCEPT
561
{
562
    uint64_t low{};
563
    auto high = max_uint64;
564
    if (!reaches(projection, count, space, high, arrivals))
9 ✔
565
        return high;
566

567
    // A rate that is taken implies that each higher rate is taken.
568
    while (low < high)
455 ✔
569
    {
570
        const auto middle = low + to_half(high - low);
448 ✔
571
        if (reaches(projection, count, space, middle, arrivals))
448 ✔
572
            high = middle;
573
        else
574
            low = add1(middle);
16 ✔
575
    }
576

577
    return low;
578
}
579

580
// Columns (whole table).
581
// ----------------------------------------------------------------------------
582

583
template <typename Word>
584
constexpr void algorithm::gather(std::span<link_t> out,
135 ✔
585
    const std::span<const link_t>& values,
586
    const std::span<const link_t>& indexes) NOEXCEPT
587
{
588
    gather_<Word>(zero, out, values, indexes);
135 ✔
589
}
135 ✔
590

591
constexpr void algorithm::initialize(const columns_t& columns) NOEXCEPT
45 ✔
592
{
593
    const graph_t graph{ columns };
45 ✔
594
    const auto count = columns.cells.size();
45 ✔
595
    for (size_t row{}; row < count; ++row)
266 ✔
596
    {
597
        const auto link = possible_narrow_cast<link_t>(row);
221 ✔
598
        if (is_present(cell(graph, link)))
221 ✔
599
            set_cell(graph, link, contested(graph, link) ? link : unlabeled);
310 ✔
600
    }
601
}
45 ✔
602

603
template <typename Word>
604
constexpr bool algorithm::propagate(const columns_t& columns,
128 ✔
605
    std::span<link_t> scratch) NOEXCEPT
606
{
607
    const graph_t graph{ columns };
128 ✔
608
    gather<Word>(scratch, columns.cells, columns.rows);
128 ✔
609

610
    auto changed = false;
611
    const auto count = columns.cells.size();
128 ✔
612
    for (size_t at{}; at < count; ++at)
760 ✔
613
    {
614
        const auto row = possible_narrow_cast<link_t>(at);
632 ✔
615
        const auto own = cell(graph, row);
632 ✔
616
        if (!is_present(own))
632 ✔
617
            continue;
9 ✔
618

619
        auto down = unlabeled;
631 ✔
620
        const auto slice = edges(graph, row);
631 ✔
621
        for (auto input = slice.first; input < slice.first + slice.count;
1,385 ✔
622
            ++input)
623
        {
624
            down = std::min(down, from_column(scratch[input]));
754 ✔
625
            each_rival(graph, input, [&](link_t other) NOEXCEPT
1,758 ✔
626
            {
627
                down = std::min(down, cell(graph, owner(graph, other)));
502 ✔
628
            });
629
        }
630

631
        // An uncontested row without a relevant parent is irrelevant.
632
        if (!is_labeled(own) && !is_labeled(down))
631 ✔
633
            continue;
8 ✔
634

635
        auto label = std::min(own, down);
623 ✔
636
        each_child(graph, row, [&](link_t child) NOEXCEPT
1,115 ✔
637
        {
638
            label = std::min(label, cell(graph, child));
246 ✔
639
        });
640

641
        if (label != own)
623 ✔
642
        {
643
            set_cell(graph, row, label);
207 ✔
644
            changed = true;
645
        }
646
    }
647

648
    return changed;
128 ✔
649
}
650

651
template <typename Word>
652
constexpr bool algorithm::collect(component_t& out, link_t label,
9 ✔
653
    const std::span<const link_t>& cells) NOEXCEPT
654
{
655
    out = {};
9 ✔
656
    return collect_<Word>(zero, out, label, cells);
9 ✔
657
}
658

659
template <typename Word>
660
inline void algorithm::find(std::vector<link_t>& out,
7 ✔
661
    const std::span<const link_t>& values, link_t value) NOEXCEPT
662
{
663
    out.clear();
7 ✔
664
    find_<Word>(zero, out, values, value);
7 ✔
665
}
7 ✔
666

667
// Internals.
668
// ----------------------------------------------------------------------------
669

670
constexpr size_t algorithm::rows(const graph_t& graph) NOEXCEPT
7,698 ✔
671
{
672
    return graph.table.cells.size();
7,698 ✔
673
}
674

675
constexpr algorithm::link_t algorithm::cell(const graph_t& graph,
2,280 ✔
676
    link_t row) NOEXCEPT
677
{
678
    const auto count = rows(graph);
2,280 ✔
679
    return from_column(row < count ? graph.table.cells[row] :
2,291 ✔
680
        graph.extra.cells[row - count]);
11 ✔
681
}
682

683
constexpr void algorithm::set_cell(const graph_t& graph, link_t row,
476 ✔
684
    link_t value) NOEXCEPT
685
{
686
    const auto count = rows(graph);
476 ✔
687
    auto& stored = row < count ? graph.table.cells[row] :
471 ✔
688
        graph.extra.cells[row - count];
476 ✔
689

690
    stored = to_column(value);
476 ✔
691
}
476 ✔
692

693
constexpr algorithm::edges_t algorithm::edges(const graph_t& graph,
1,473 ✔
694
    link_t row) NOEXCEPT
695
{
696
    const auto count = rows(graph);
1,473 ✔
697
    return from_column(row < count ? graph.table.edges[row] :
1,473 ✔
698
        graph.extra.edges[row - count]);
11 ✔
699
}
700

701
constexpr algorithm::totals_t algorithm::totals(const graph_t& graph,
1,801 ✔
702
    link_t row) NOEXCEPT
703
{
704
    const auto count = rows(graph);
1,801 ✔
705
    return from_column(row < count ? graph.table.totals[row] :
1,801 ✔
NEW
706
        graph.extra.totals[row - count]);
×
707
}
708

709
constexpr algorithm::link_t algorithm::child(const graph_t& graph,
830 ✔
710
    link_t row) NOEXCEPT
711
{
712
    const auto count = rows(graph);
830 ✔
713
    return from_column(row < count ? graph.table.children[row] :
834 ✔
714
        graph.extra.children[row - count]);
4 ✔
715
}
716

717
constexpr algorithm::link_t algorithm::parent(const graph_t& graph,
658 ✔
718
    link_t input) NOEXCEPT
719
{
720
    const auto count = graph.table.rows.size();
658 ✔
721
    return from_column(input < count ? graph.table.rows[input] :
666 ✔
722
        graph.extra.rows[input - count]);
8 ✔
723
}
724

725
constexpr algorithm::link_t algorithm::owner(const graph_t& graph,
1,213 ✔
726
    link_t input) NOEXCEPT
727
{
728
    // The last row whose first input is not beyond the input.
729
    const auto count = graph.table.rows.size();
1,213 ✔
730
    const auto& edges = input < count ? graph.table.edges : graph.extra.edges;
1,213 ✔
731
    const auto after = std::upper_bound(edges.begin(), edges.end(), input,
1,213 ✔
732
        [](link_t at, const edges_t& edge) NOEXCEPT
2,991 ✔
733
        {
734
            return at < from_column(edge.first);
2,991 ✔
735
        });
736

737
    const auto base = input < count ? zero : rows(graph);
1,213 ✔
738
    const auto offset = std::distance(edges.begin(), std::prev(after));
1,213 ✔
739
    return possible_narrow_cast<link_t>(base + offset);
1,213 ✔
740
}
741

742
constexpr algorithm::link_t algorithm::sibling(const graph_t& graph,
325 ✔
743
    link_t input) NOEXCEPT
744
{
745
    const auto count = graph.table.rows.size();
325 ✔
746
    return from_column(input < count ? graph.table.siblings[input] :
325 ✔
NEW
747
        graph.extra.siblings[input - count]);
×
748
}
749

750
constexpr algorithm::link_t algorithm::rival(const graph_t& graph,
2,216 ✔
751
    link_t input) NOEXCEPT
752
{
753
    const auto count = graph.table.rows.size();
2,216 ✔
754
    return from_column(input < count ? graph.table.rivals[input] :
2,225 ✔
755
        graph.extra.rivals[input - count]);
9 ✔
756
}
757

758
template <typename Visitor>
759
constexpr void algorithm::each_child(const graph_t& graph, link_t row,
830 ✔
760
    const Visitor& visit) NOEXCEPT
761
{
762
    for (auto input = child(graph, row); input != none;
1,155 ✔
763
        input = sibling(graph, input))
325 ✔
764
        visit(owner(graph, input));
325 ✔
765

766
    if (row < rows(graph))
830 ✔
767
        for (const auto& bridge: graph.children)
834 ✔
768
            if (bridge.from == row)
8 ✔
769
                visit(owner(graph, bridge.to));
2 ✔
770
}
830 ✔
771

772
template <typename Visitor>
773
constexpr void algorithm::each_rival(const graph_t& graph, link_t input,
1,336 ✔
774
    const Visitor& visit) NOEXCEPT
775
{
776
    const auto count = graph.table.rows.size();
1,336 ✔
777
    std::vector<link_t> seen{ input };
1,336 ✔
778
    const auto step = [&](link_t next) NOEXCEPT
3,562 ✔
779
    {
780
        if (next == none || std::ranges::find(seen, next) != seen.end())
2,226 ✔
781
            return false;
1,340 ✔
782

783
        seen.push_back(next);
886 ✔
784
        return true;
886 ✔
785
    };
786

787
    // A table input bridges to the candidate inputs spending its point.
788
    for (auto at = input;;)
789
    {
790
        if (at < count)
2,216 ✔
791
            for (const auto& bridge: graph.rivals)
2,245 ✔
792
                if (bridge.from == at && step(bridge.to))
38 ✔
793
                    visit(bridge.to);
6 ✔
794

795
        at = rival(graph, at);
2,216 ✔
796
        if (!step(at))
2,216 ✔
797
            return;
1,336 ✔
798

799
        visit(at);
880 ✔
800
    }
801
}
1,336 ✔
802

803
constexpr bool algorithm::contested(const graph_t& graph, link_t row) NOEXCEPT
238 ✔
804
{
805
    auto out = false;
238 ✔
806
    const auto slice = edges(graph, row);
238 ✔
807
    for (auto input = slice.first; !out && input < slice.first + slice.count;
476 ✔
808
        ++input)
809
    {
810
        each_rival(graph, input, [&](link_t other) NOEXCEPT
238 ✔
811
        {
812
            if (is_present(cell(graph, owner(graph, other))))
142 ✔
813
                out = true;
135 ✔
814
        });
142 ✔
815
    }
816

817
    return out;
238 ✔
818
}
819

820
constexpr bool algorithm::descended(const graph_t& graph, link_t row) NOEXCEPT
14 ✔
821
{
822
    const auto slice = edges(graph, row);
14 ✔
823
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
24 ✔
824
    {
825
        const auto up = parent(graph, input);
14 ✔
826
        if (up != none && is_labeled(cell(graph, up)))
14 ✔
827
            return true;
828
    }
829

830
    return false;
831
}
832

833
constexpr bool algorithm::confirmable(const graph_t& graph,
5 ✔
834
    link_t row) NOEXCEPT
835
{
836
    auto out = true;
5 ✔
837
    const auto slice = edges(graph, row);
5 ✔
838
    for (auto input = slice.first; out && input < slice.first + slice.count;
13 ✔
839
        ++input)
840
    {
841
        const auto up = parent(graph, input);
8 ✔
842
        if (up != none && cell(graph, up) == absent)
8 ✔
843
            return false;
844

845
        each_rival(graph, input, [&](link_t other) NOEXCEPT
8 ✔
846
        {
847
            if (is_boundary(cell(graph, owner(graph, other))))
6 ✔
848
                out = false;
1 ✔
849
        });
6 ✔
850
    }
851

852
    return out;
853
}
854

855
constexpr size_t algorithm::index(const component_t& component,
527 ✔
856
    link_t row) NOEXCEPT
857
{
858
    for (size_t member{}; member < component.count; ++member)
1,561 ✔
859
        if (component.rows[member] == row)
1,310 ✔
860
            return member;
861

862
    return maximum_members;
863
}
864

865
constexpr algorithm::mask_t algorithm::losers(const component_t& component,
169 ✔
866
    size_t index) NOEXCEPT
867
{
868
    mask_t out{};
869
    for (size_t contest{}; contest < component.contests; ++contest)
507 ✔
870
    {
871
        const auto clique = component.cliques[contest];
338 ✔
872
        const auto size = ones_count(clique);
338 ✔
873
        const auto digit = index % size;
338 ✔
874
        index /= size;
338 ✔
875

876
        // The winner is the clique's digit-th set bit.
877
        auto winner = clique;
878
        for (size_t cleared{}; cleared < digit; ++cleared)
499 ✔
879
            winner &= sub1(winner);
161 ✔
880

881
        winner &= twos_complement(winner);
338 ✔
882
        out |= bit_and(clique, bit_not(winner));
338 ✔
883
    }
884

885
    return out;
169 ✔
886
}
887

888
template <typename Word>
889
constexpr size_t algorithm::best(const std::span<const mask_t>& outcomes,
12 ✔
890
    const component_t& component, const graph_t& graph) NOEXCEPT
891
{
892
    size_t out{};
12 ✔
893
    uint64_t most{};
12 ✔
894
    each_fee<Word>(zero, outcomes, component, graph,
12 ✔
895
        [&](size_t index, uint64_t fee) NOEXCEPT
332 ✔
896
        {
897
            if (fee > most)
160 ✔
898
            {
899
                most = fee;
23 ✔
900
                out = index;
23 ✔
901
            }
902
        });
903

904
    return out;
12 ✔
905
}
906

907
inline void algorithm::discard(std::span<mark> marks, const graph_t& graph,
16 ✔
908
    link_t row) NOEXCEPT
909
{
910
    std::vector<link_t> pending{ row };
16 ✔
911
    marks[row] = mark::excluded;
16 ✔
912

913
    while (!pending.empty())
39 ✔
914
    {
915
        const auto next = pending.back();
23 ✔
916
        pending.pop_back();
23 ✔
917
        each_child(graph, next, [&](link_t down) NOEXCEPT
23 ✔
918
        {
919
            if (marks[down] != mark::excluded)
7 ✔
920
            {
921
                marks[down] = mark::excluded;
7 ✔
922
                pending.push_back(down);
7 ✔
923
            }
924
        });
7 ✔
925
    }
926
}
16 ✔
927

928
inline void algorithm::forfeit(std::span<mark> marks, const graph_t& graph,
1 ✔
929
    link_t row) NOEXCEPT
930
{
931
    std::vector<link_t> found{ row };
1 ✔
932
    const auto add = [&](link_t next) NOEXCEPT
8 ✔
933
    {
934
        if (std::ranges::find(found, next) == found.end())
8 ✔
935
            found.push_back(next);
4 ✔
936
    };
9 ✔
937

938
    for (size_t at{}; at < found.size(); ++at)
6 ✔
939
    {
940
        const auto member = found[at];
5 ✔
941
        const auto slice = edges(graph, member);
5 ✔
942
        for (auto input = slice.first; input < slice.first + slice.count;
11 ✔
943
            ++input)
944
        {
945
            const auto up = parent(graph, input);
6 ✔
946
            if (up != none && is_labeled(cell(graph, up)))
6 ✔
947
                add(up);
2 ✔
948

949
            each_rival(graph, input, [&](link_t other) NOEXCEPT
6 ✔
950
            {
951
                const auto rival_row = owner(graph, other);
4 ✔
952
                if (is_present(cell(graph, rival_row)))
4 ✔
953
                    add(rival_row);
4 ✔
954
            });
4 ✔
955
        }
956

957
        each_child(graph, member, [&](link_t down) NOEXCEPT
5 ✔
958
        {
959
            if (is_present(cell(graph, down)))
2 ✔
960
                add(down);
2 ✔
961
        });
2 ✔
962
    }
963

964
    for (const auto member: found)
6 ✔
965
        discard(marks, graph, member);
5 ✔
966
}
1 ✔
967

968
inline void algorithm::pack_(std::vector<link_t>& out, takes_t& takes,
31 ✔
969
    std::span<mark> marks, const columns_t& columns, const totals_t& budget,
970
    size_t depth, uint32_t bytes_per_sigop) NOEXCEPT
971
{
972
    struct score_t
973
    {
974
        link_t row{};
975
        uint64_t rate{};
976
        uint64_t fee{};
977
        size_t count{};
978
    };
979

980
    const graph_t graph{ columns };
31 ✔
981
    const auto count = columns.cells.size();
31 ✔
982
    std::vector<score_t> scores{};
31 ✔
983
    std::vector<link_t> rows{};
31 ✔
984
    totals_t used{};
31 ✔
985
    out.clear();
31 ✔
986
    takes.clear();
31 ✔
987

988
    const auto available = [&]() NOEXCEPT -> totals_t
207 ✔
989
    {
990
        return
991
        {
992
            {},
993
            floored_subtract(budget.size, used.size),
207 ✔
994
            floored_subtract(budget.sigops, used.sigops)
207 ✔
995
        };
207 ✔
996
    };
31 ✔
997

998
    for (auto taken = true; taken;)
105 ✔
999
    {
1000
        taken = false;
1001
        scores.clear();
74 ✔
1002
        for (size_t at{}; at < count; ++at)
308 ✔
1003
        {
1004
            totals_t sum{};
234 ✔
1005
            const auto row = possible_narrow_cast<link_t>(at);
234 ✔
1006
            if (marks[row] == mark::unmarked &&
351 ✔
1007
                chunk(rows, sum, marks, graph, row, depth, available()))
261 ✔
1008
            {
1009
                const auto rate = fee_rate(sum, bytes_per_sigop);
90 ✔
1010
                scores.push_back({ row, rate, sum.fee, rows.size() });
90 ✔
1011
            }
1012
        }
1013

1014
        std::sort(scores.begin(), scores.end(),
74 ✔
1015
            [](const score_t& left, const score_t& right) NOEXCEPT
88 ✔
1016
            {
1017
                return left.rate == right.rate ? left.row < right.row :
88 ✔
1018
                    left.rate > right.rate;
88 ✔
1019
            });
1020

1021
        for (const auto& score: scores)
164 ✔
1022
        {
1023
            totals_t sum{};
90 ✔
1024
            const auto space = available();
90 ✔
1025
            if (marks[score.row] != mark::unmarked ||
176 ✔
1026
                !chunk(rows, sum, marks, graph, score.row, depth, space) ||
86 ✔
1027
                rows.size() != score.count ||
160 ✔
1028
                sum.fee != score.fee)
44 ✔
1029
                continue;
46 ✔
1030

1031
            for (const auto row: rows)
92 ✔
1032
                marks[row] = mark::taken;
48 ✔
1033

1034
            out.insert(out.end(), rows.begin(), rows.end());
44 ✔
1035
            takes.push_back({ score.rate, chargeable(sum, bytes_per_sigop) });
44 ✔
1036
            used.fee = ceilinged_add(used.fee, sum.fee);
44 ✔
1037
            used.size = ceilinged_add(used.size, sum.size);
44 ✔
1038
            used.sigops = ceilinged_add(used.sigops, sum.sigops);
44 ✔
1039
            taken = true;
1040
        }
1041
    }
1042

1043
    std::sort(out.begin(), out.end());
31 ✔
1044
}
31 ✔
1045

1046
template <typename Arrivals>
1047
inline bool algorithm::reaches(const projection_t& projection, size_t count,
457 ✔
1048
    uint64_t space, uint64_t rate, const Arrivals& arrivals) NOEXCEPT
1049
{
1050
    const auto& blocks = projection.blocks;
457 ✔
1051
    const auto arriving = arrivals(rate);
457 ✔
1052
    uint64_t carry{};
1053

1054
    // A block that leaves rows is full, so its takes are its capacity.
1055
    for (size_t block{}; block < count; ++block)
499 ✔
1056
    {
1057
        std::span<const take_t> takes{};
481 ✔
1058
        if (block < blocks.size())
481 ✔
1059
            takes = blocks[block];
415 ✔
1060

1061
        const auto last = add1(block) >= blocks.size();
481 ✔
1062
        const auto full = !takes.empty() && !(last && projection.exhausted);
481 ✔
1063
        const auto capacity = full ? above(takes, zero) : space;
390 ✔
1064
        const auto demand = ceilinged_add(carry, above(takes, rate));
481 ✔
1065
        if (demand < capacity)
481 ✔
1066
            return true;
439 ✔
1067

1068
        const auto excess = floored_subtract(demand, capacity);
42 ✔
1069
        carry = ceilinged_add(excess, arriving);
42 ✔
1070
    }
1071

1072
    return false;
1073
}
1074

1075
inline bool algorithm::chunk(std::vector<link_t>& out, totals_t& sum,
203 ✔
1076
    const std::span<const mark>& marks, const graph_t& graph, link_t row,
1077
    size_t depth, const totals_t& available) NOEXCEPT
1078
{
1079
    sum = {};
203 ✔
1080
    out.assign(one, row);
203 ✔
1081

1082
    // Each level holds the untaken parents of the previous level.
1083
    for (size_t level{}, begin{}; begin < out.size(); ++level)
484 ✔
1084
    {
1085
        const auto end = out.size();
1086
        for (auto at = begin; at < end; ++at)
567 ✔
1087
        {
1088
            const auto slice = edges(graph, out[at]);
286 ✔
1089
            for (auto input = slice.first;
567 ✔
1090
                input < slice.first + slice.count; ++input)
567 ✔
1091
            {
1092
                const auto up = parent(graph, input);
286 ✔
1093
                if (up == none || marks[up] == mark::taken ||
286 ✔
1094
                    is_boundary(cell(graph, up)))
88 ✔
1095
                    continue;
198 ✔
1096

1097
                if (marks[up] == mark::excluded)
88 ✔
1098
                    return false;
5 ✔
1099

1100
                if (std::ranges::find(out, up) != out.end())
86 ✔
1101
                    continue;
×
1102

1103
                if (level == depth)
86 ✔
1104
                    return false;
1105

1106
                out.push_back(up);
83 ✔
1107
            }
1108
        }
1109

1110
        begin = end;
1111
    }
1112

1113
    std::sort(out.begin(), out.end());
198 ✔
1114
    auto cut = out.begin();
198 ✔
1115
    for (; cut != out.end(); ++cut)
393 ✔
1116
    {
1117
        const auto& add = totals(graph, *cut);
273 ✔
1118
        const auto size = ceilinged_add(sum.size, add.size);
273 ✔
1119
        const auto sigops = ceilinged_add(sum.sigops, add.sigops);
273 ✔
1120
        if (size > available.size || sigops > available.sigops)
273 ✔
1121
            break;
1122

1123
        sum.fee = ceilinged_add(sum.fee, add.fee);
195 ✔
1124
        sum.size = size;
195 ✔
1125
        sum.sigops = sigops;
195 ✔
1126
    }
1127

1128
    out.erase(cut, out.end());
198 ✔
1129
    return !out.empty();
198 ✔
1130
}
1131

1132
inline void algorithm::restore(std::vector<link_t>& out,
6 ✔
1133
    const columns_t& columns, link_t row) NOEXCEPT
1134
{
1135
    const graph_t graph{ columns };
6 ✔
1136
    if (cell(graph, row) != absent || !confirmable(graph, row))
6 ✔
1137
        return;
2 ✔
1138

1139
    set_cell(graph, row, unlabeled);
4 ✔
1140
    out.push_back(row);
4 ✔
1141
    each_child(graph, row, [&](link_t down) NOEXCEPT
4 ✔
1142
    {
1143
        restore(out, columns, down);
2 ✔
1144
    });
2 ✔
1145
}
1146

1147
inline void algorithm::reform(const columns_t& columns,
9 ✔
1148
    const std::span<const link_t>& rows) NOEXCEPT
1149
{
1150
    const graph_t graph{ columns };
9 ✔
1151
    for (const auto row: rows)
24 ✔
1152
        if (is_present(cell(graph, row)))
15 ✔
1153
            set_cell(graph, row, unlabeled);
15 ✔
1154

1155
    // Relevance closes over the rows in at most count passes.
1156
    for (auto changed = true; changed;)
21 ✔
1157
    {
1158
        changed = false;
1159
        for (const auto row: rows)
33 ✔
1160
        {
1161
            if (!is_present(cell(graph, row)) || is_labeled(cell(graph, row)))
21 ✔
1162
                continue;
6 ✔
1163

1164
            if (contested(graph, row) || descended(graph, row))
15 ✔
1165
            {
1166
                set_cell(graph, row, row);
6 ✔
1167
                changed = true;
1168
            }
1169
        }
1170
    }
1171

1172
    for (const auto row: rows)
24 ✔
1173
    {
1174
        // A row relabeled by a previous component carries a lower label.
1175
        if (cell(graph, row) != row)
15 ✔
1176
            continue;
12 ✔
1177

1178
        component_t component{};
3 ✔
1179
        if (collect(component, graph, row))
3 ✔
1180
            relabel(columns, component);
3 ✔
1181
    }
1182
}
9 ✔
1183

1184
template <typename Word>
1185
INLINE constexpr Word algorithm::fees(Word outcomes,
1186
    const component_t& component, const graph_t& graph) NOEXCEPT
1187
{
1188
    constexpr auto lane = bits<mask_t>;
1189
    auto sum = f::broadcast<Word>(mask_t{});
1190
    for (size_t member{}; member < component.count; ++member)
1,812 ✔
1191
    {
1192
        const auto bit = f::broadcast<Word>(set_right(mask_t{}, member));
1,510 ✔
1193
        const auto& total = totals(graph, component.rows[member]);
1,510 ✔
1194
        const auto fee = f::broadcast<Word>(total.fee);
1,510 ✔
1195
        const auto selected = f::eq<lane>(f::and_(outcomes, bit), bit);
1196
        sum = f::add<lane>(sum, f::and_(selected, fee));
1197
    }
1198

1199
    return sum;
1200
}
1201

1202
template <typename Word, typename Visitor>
1203
constexpr void algorithm::each_fee(size_t index,
31 ✔
1204
    const std::span<const mask_t>& outcomes, const component_t& component,
1205
    const graph_t& graph, const Visitor& visit) NOEXCEPT
1206
{
1207
    const auto count = outcomes.size();
19 ✔
1208
    if constexpr (is_extended<Word>)
1209
    {
1210
        if constexpr (have<Word>)
1211
        {
1212
            constexpr auto lanes = mask_lanes<Word>;
1213
            aligned_array<uint64_t, lanes> sums{};
1214
            for (; index + lanes <= count; index += lanes)
1215
            {
1216
                const auto word = load<Word>(&outcomes[index]);
1217
                store(sums.data(), fees<Word>(word, component, graph));
1218

1219
                for (size_t lane{}; lane < lanes; ++lane)
1220
                    visit(index + lane, sums[lane]);
1221
            }
1222
        }
1223

1224
        using next = narrower<Word, mask_t>;
1225
        each_fee<next>(index, outcomes, component, graph, visit);
12 ✔
1226
    }
1227
    else
1228
    {
1229
        for (; index < count; ++index)
321 ✔
1230
            visit(index, fees<mask_t>(outcomes[index], component, graph));
604 ✔
1231
    }
1232
}
31 ✔
1233

1234
template <typename Word>
1235
constexpr void algorithm::gather_(size_t index, std::span<link_t> out,
159 ✔
1236
    const std::span<const link_t>& values,
1237
    const std::span<const link_t>& indexes) NOEXCEPT
1238
{
1239
    const auto count = indexes.size();
135 ✔
1240
    if constexpr (is_extended<Word>)
1241
    {
1242
        if constexpr (have<Word> && is_little_endian)
1243
        {
1244
            constexpr auto lanes = link_lanes<Word>;
1245
            const auto nones = f::broadcast<Word>(none);
1246
            for (; index + lanes <= count; index += lanes)
1247
            {
1248
                const auto at = load<Word>(&indexes[index]);
1249
                const auto missing = f::eq<bits<link_t>>(at, nones);
1250
                const auto valid = f::andnot(missing, at);
1251
                const auto found = f::gather(values.data(), valid);
1252

1253
                store(&out[index], f::select(missing, nones, found));
1254
            }
1255
        }
1256

1257
        gather_<narrower<Word, link_t>>(index, out, values, indexes);
24 ✔
1258
    }
1259
    else
1260
    {
1261
        for (; index < count; ++index)
1,178 ✔
1262
        {
1263
            const auto at = from_column(indexes[index]);
1,043 ✔
1264
            out[index] = (at == none) ? none : values[at];
1,043 ✔
1265
        }
1266
    }
1267
}
159 ✔
1268

1269
template <typename Word>
1270
constexpr bool algorithm::collect_(size_t index, component_t& out,
15 ✔
1271
    link_t label, const std::span<const link_t>& cells) NOEXCEPT
1272
{
1273
    const auto count = cells.size();
9 ✔
1274
    const auto stored = to_column(label);
9 ✔
1275
    const auto add = [&](size_t row) NOEXCEPT
241 ✔
1276
    {
1277
        if (out.count == maximum_members)
226 ✔
1278
            return false;
1279

1280
        out.rows[out.count++] = possible_narrow_cast<link_t>(row);
225 ✔
1281
        return true;
225 ✔
1282
    };
1283

1284
    if constexpr (is_extended<Word>)
1285
    {
1286
        if constexpr (have<Word>)
1287
        {
1288
            constexpr auto lanes = link_lanes<Word>;
1289
            const auto wanted = f::broadcast<Word>(stored);
1290
            for (; index + lanes <= count; index += lanes)
1291
            {
1292
                const auto word = load<Word>(&cells[index]);
1293
                const auto found = f::eq<bits<link_t>>(word, wanted);
1294

1295
                if (f::any(found))
1296
                    for (auto row = index; row < index + lanes; ++row)
1297
                        if (cells[row] == stored && !add(row))
1298
                            return false;
1299
            }
1300
        }
1301

1302
        return collect_<narrower<Word, link_t>>(index, out, label, cells);
6 ✔
1303
    }
1304
    else
1305
    {
1306
        for (; index < count; ++index)
557 ✔
1307
            if (cells[index] == stored && !add(index))
549 ✔
1308
                return false;
1309

1310
        return true;
1311
    }
1312
}
1313

1314
template <typename Word>
1315
inline void algorithm::find_(size_t index, std::vector<link_t>& out,
13 ✔
1316
    const std::span<const link_t>& values, link_t value) NOEXCEPT
1317
{
1318
    const auto count = values.size();
7 ✔
1319
    const auto stored = to_column(value);
7 ✔
1320
    if constexpr (is_extended<Word>)
1321
    {
1322
        if constexpr (have<Word>)
1323
        {
1324
            constexpr auto lanes = link_lanes<Word>;
1325
            const auto wanted = f::broadcast<Word>(stored);
1326
            for (; index + lanes <= count; index += lanes)
1327
            {
1328
                const auto word = load<Word>(&values[index]);
1329
                const auto found = f::eq<bits<link_t>>(word, wanted);
1330

1331
                if (f::any(found))
1332
                    for (auto row = index; row < index + lanes; ++row)
1333
                        if (values[row] == stored)
1334
                            out.push_back(possible_narrow_cast<link_t>(row));
1335
            }
1336
        }
1337

1338
        find_<narrower<Word, link_t>>(index, out, values, value);
6 ✔
1339
    }
1340
    else
1341
    {
1342
        for (; index < count; ++index)
298 ✔
1343
            if (values[index] == stored)
291 ✔
1344
                out.push_back(possible_narrow_cast<link_t>(index));
71 ✔
1345
    }
1346
}
13 ✔
1347

1348
template <typename Word, typename Integral>
1349
INLINE constexpr Word algorithm::load(const Integral* at) NOEXCEPT
1350
{
1351
    if constexpr (is_extended<Word>)
1352
        return f::load<Word>(pointer_cast<const uint8_t>(at));
1353
    else
1354
        return *at;
1355
}
1356

1357
template <typename Word, typename Integral>
1358
INLINE constexpr void algorithm::store(Integral* at, Word word) NOEXCEPT
1359
{
1360
    if constexpr (is_extended<Word>)
1361
        f::store(pointer_cast<uint8_t>(at), word);
1362
    else
1363
        *at = word;
1364
}
1365

1366
BC_POP_WARNING()
1367
BC_POP_WARNING()
1368
BC_POP_WARNING()
1369
BC_POP_WARNING()
1370

1371
} // namespace pool
1372
} // namespace system
1373
} // namespace libbitcoin
1374

1375
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc