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

libbitcoin / libbitcoin-system / 43468560425

13 Nov 2026 05:01PM UTC coverage: 96.299% (+0.02%) from 96.281%
43468560425

push

github

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

Add pool template algorithm, bound entry, cascade by lanes.

187 of 188 new or added lines in 1 file covered. (99.47%)

27216 of 28262 relevant lines covered (96.3%)

3944008.86 hits per line

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

98.74
/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/intrinsics/intrinsics.hpp>
26
#include <bitcoin/system/math/math.hpp>
27

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

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

37
// Cells.
38
// ----------------------------------------------------------------------------
39

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

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

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

55
constexpr uint64_t algorithm::chargeable(const totals_t& totals,
28 ✔
56
    uint32_t bytes_per_sigop) NOEXCEPT
57
{
58
    return std::max(possible_wide_cast<uint64_t>(totals.size),
28 ✔
59
        ceilinged_multiply(possible_wide_cast<uint64_t>(totals.sigops),
28 ✔
60
            possible_wide_cast<uint64_t>(bytes_per_sigop)));
28 ✔
61
}
62

63
// Candidates.
64
// ----------------------------------------------------------------------------
65

66
constexpr void algorithm::initialize(const graph_t& graph,
3 ✔
67
    const std::span<const link_t>& candidates) NOEXCEPT
68
{
69
    for (const auto row: candidates)
6 ✔
70
        cell(graph, row) = unlabeled;
3 ✔
71

72
    // Relevance closes over the candidates in at most count passes.
73
    for (auto changed = true; changed;)
8 ✔
74
    {
75
        changed = false;
76
        for (const auto row: candidates)
10 ✔
77
        {
78
            if (is_labeled(cell(graph, row)))
5 ✔
79
                continue;
2 ✔
80

81
            if (contested(graph, row) || descended(graph, row))
3 ✔
82
            {
83
                cell(graph, row) = row;
2 ✔
84
                changed = true;
85
            }
86
        }
87
    }
88
}
3 ✔
89

90
// Components (frontier).
91
// ----------------------------------------------------------------------------
92

93
constexpr bool algorithm::collect(component_t& out, const graph_t& graph,
33 ✔
94
    link_t row) NOEXCEPT
95
{
96
    out = {};
33 ✔
97
    const auto add = [&](link_t found) NOEXCEPT
297 ✔
98
    {
99
        if (index(out, found) != maximum_members)
297 ✔
100
            return true;
101

102
        if (out.count == maximum_members)
163 ✔
103
            return false;
104

105
        out.rows[out.count++] = found;
163 ✔
106
        return true;
163 ✔
107
    };
33 ✔
108

109
    if (!add(row))
33 ✔
110
        return false;
111

112
    for (size_t at{}; at < out.count; ++at)
196 ✔
113
    {
114
        const auto member = out.rows[at];
163 ✔
115
        const auto slice = edges(graph, member);
163 ✔
116
        for (auto input = slice.first; input < slice.first + slice.count;
359 ✔
117
            ++input)
118
        {
119
            const auto up = parent(graph, input);
196 ✔
120
            if (up != none && is_labeled(cell(graph, up)) && !add(up))
196 ✔
121
                return false;
×
122

123
            auto added = true;
196 ✔
124
            each_rival(graph, input, [&](link_t other) NOEXCEPT
196 ✔
125
            {
126
                const auto rival_row = owner(graph, other);
137 ✔
127
                if (is_present(cell(graph, rival_row)) && !add(rival_row))
137 ✔
128
                    added = false;
×
129
            });
137 ✔
130

131
            if (!added)
196 ✔
132
                return false;
133
        }
134

135
        auto added = true;
163 ✔
136
        each_child(graph, member, [&](link_t down) NOEXCEPT
163 ✔
137
        {
138
            if (is_present(cell(graph, down)) && !add(down))
64 ✔
139
                added = false;
×
140
        });
64 ✔
141

142
        if (!added)
163 ✔
143
            return false;
144
    }
145

146
    std::sort(out.rows.begin(), std::next(out.rows.begin(), out.count));
33 ✔
147
    return true;
33 ✔
148
}
149

150
constexpr bool algorithm::relate(component_t& out,
21 ✔
151
    const graph_t& graph) NOEXCEPT
152
{
153
    out.contests = zero;
21 ✔
154
    for (size_t member{}; member < out.count; ++member)
127 ✔
155
    {
156
        mask_t direct{};
106 ✔
157
        const auto row = out.rows[member];
106 ✔
158
        const auto slice = edges(graph, row);
106 ✔
159
        for (auto input = slice.first; input < slice.first + slice.count;
233 ✔
160
            ++input)
161
        {
162
            const auto up = index(out, parent(graph, input));
127 ✔
163
            if (up != maximum_members)
127 ✔
164
                set_right_into(direct, up);
42 ✔
165

166
            auto clique = set_right(mask_t{}, member);
127 ✔
167
            each_rival(graph, input, [&](link_t other) NOEXCEPT
127 ✔
168
            {
169
                const auto rival_member = index(out, owner(graph, other));
88 ✔
170
                if (rival_member != maximum_members)
88 ✔
171
                    set_right_into(clique, rival_member);
88 ✔
172
            });
88 ✔
173

174
            if (is_one(ones_count(clique)))
127 ✔
175
                continue;
85 ✔
176

177
            const auto end = std::next(out.cliques.begin(), out.contests);
85 ✔
178
            if (std::find(out.cliques.begin(), end, clique) != end)
85 ✔
179
                continue;
43 ✔
180

181
            if (out.contests == maximum_members)
42 ✔
182
                return false;
×
183

184
            out.cliques[out.contests++] = clique;
42 ✔
185
        }
186

187
        out.ancestors[member] = direct;
106 ✔
188
    }
189

190
    // Ancestry closes within the component in at most count passes.
191
    for (auto changed = true; changed;)
42 ✔
192
    {
193
        changed = false;
194
        for (size_t member{}; member < out.count; ++member)
127 ✔
195
        {
196
            auto ancestors = out.ancestors[member];
106 ✔
197
            for (size_t up{}; up < out.count; ++up)
642 ✔
198
                if (get_right(ancestors, up))
536 ✔
199
                    ancestors |= out.ancestors[up];
42 ✔
200

201
            if (ancestors != out.ancestors[member])
106 ✔
202
            {
203
                out.ancestors[member] = ancestors;
×
204
                changed = true;
205
            }
206
        }
207
    }
208

209
    return true;
210
}
211

212
constexpr algorithm::mask_t algorithm::members(const component_t& component,
5 ✔
213
    const std::span<const link_t>& rows) NOEXCEPT
214
{
215
    mask_t out{};
5 ✔
216
    for (const auto row: rows)
12 ✔
217
    {
218
        const auto member = index(component, row);
7 ✔
219
        if (member != maximum_members)
7 ✔
220
            set_right_into(out, member);
6 ✔
221
    }
222

223
    return out;
5 ✔
224
}
225

226
inline bool algorithm::ancestry(totals_t& out, const graph_t& graph,
4 ✔
227
    const component_t& component, const totals_t& limit) NOEXCEPT
228
{
229
    out = {};
4 ✔
230
    std::vector<link_t> visited{};
4 ✔
231
    std::vector<link_t> pending{};
4 ✔
232
    const auto push = [&](link_t row) NOEXCEPT
7 ✔
233
    {
234
        if (index(component, row) != maximum_members ||
7 ✔
235
            std::ranges::find(visited, row) != visited.end())
1 ✔
236
            return;
6 ✔
237

238
        visited.push_back(row);
1 ✔
239
        pending.push_back(row);
1 ✔
240
    };
4 ✔
241

242
    const auto charge = [&](link_t row) NOEXCEPT
18 ✔
243
    {
244
        const auto& add = totals(graph, row);
18 ✔
245
        out.fee = ceilinged_add(out.fee, add.fee);
18 ✔
246
        out.size = ceilinged_add(out.size, add.size);
18 ✔
247
        out.sigops = ceilinged_add(out.sigops, add.sigops);
18 ✔
248

249
        const auto slice = edges(graph, row);
18 ✔
250
        for (auto input = slice.first; input < slice.first + slice.count;
39 ✔
251
            ++input)
252
        {
253
            const auto up = parent(graph, input);
21 ✔
254
            if (up != none && is_present(cell(graph, up)))
21 ✔
255
                push(up);
7 ✔
256
        }
257

258
        return out.size <= limit.size && out.sigops <= limit.sigops;
18 ✔
259
    };
4 ✔
260

261
    for (size_t member{}; member < component.count; ++member)
19 ✔
262
        if (!charge(component.rows[member]))
17 ✔
263
            return false;
264

265
    while (!pending.empty())
3 ✔
266
    {
267
        const auto row = pending.back();
1 ✔
268
        pending.pop_back();
1 ✔
269
        if (!charge(row))
1 ✔
270
            return false;
271
    }
272

273
    return true;
274
}
4 ✔
275

276
// Outcomes.
277
// ----------------------------------------------------------------------------
278

279
constexpr size_t algorithm::outcomes(const component_t& component) NOEXCEPT
8 ✔
280
{
281
    size_t out{ one };
282
    for (size_t contest{}; contest < component.contests; ++contest)
24 ✔
283
        out = ceilinged_multiply(out, ones_count(component.cliques[contest]));
16 ✔
284

285
    return out;
8 ✔
286
}
287

288
constexpr algorithm::mask_t algorithm::outcome(const component_t& component,
168 ✔
289
    size_t index) NOEXCEPT
290
{
291
    const auto lost = losers(component, index);
168 ✔
292
    auto out = unmask_right<mask_t>(component.count) & ~lost;
168 ✔
293
    for (size_t member{}; member < component.count; ++member)
1,008 ✔
294
        if (!is_zero(component.ancestors[member] & lost))
840 ✔
295
            out = set_right(out, member, false);
205 ✔
296

297
    return out;
168 ✔
298
}
299

300
template <typename Word>
301
constexpr void algorithm::evaluate(uint64_t& best, uint64_t& best_without,
7 ✔
302
    const std::span<const mask_t>& outcomes, mask_t candidate,
303
    const component_t& component, const graph_t& graph) NOEXCEPT
304
{
305
    best = zero;
7 ✔
306
    best_without = zero;
7 ✔
307
    each_fee<Word>(zero, outcomes, component, graph,
7 ✔
308
        [&](size_t index, uint64_t fee) NOEXCEPT
291 ✔
309
        {
310
            best = std::max(best, fee);
142 ✔
311
            if (is_zero(outcomes[index] & candidate))
142 ✔
312
                best_without = std::max(best_without, fee);
68 ✔
313
        });
314
}
7 ✔
315

316
constexpr bool algorithm::covered(uint64_t best, uint64_t best_without,
4 ✔
317
    uint64_t candidate_bytes, uint64_t rate) NOEXCEPT
318
{
319
    // Compared in satoshis per virtual kilobyte.
320
    const auto added = floored_subtract(best, best_without);
4 ✔
321
    return ceilinged_multiply(added, 1'000_u64) >=
4 ✔
322
        ceilinged_multiply(rate, candidate_bytes);
4 ✔
323
}
324

325
// Transitions (frontier, table columns).
326
// ----------------------------------------------------------------------------
327

328
constexpr void algorithm::relabel(const columns_t& columns,
4 ✔
329
    const component_t& component) NOEXCEPT
330
{
331
    // Rows are sorted by collect, so the label is the first.
332
    const auto label = component.rows.front();
4 ✔
333
    for (size_t member{}; member < component.count; ++member)
22 ✔
334
        columns.cells[component.rows[member]] = label;
18 ✔
335
}
4 ✔
336

337
inline void algorithm::exclude(const columns_t& columns, link_t row) NOEXCEPT
6 ✔
338
{
339
    const graph_t graph{ columns };
6 ✔
340
    std::vector<link_t> pending{ row };
6 ✔
341
    columns.cells[row] = absent;
6 ✔
342

343
    while (!pending.empty())
18 ✔
344
    {
345
        const auto next = pending.back();
12 ✔
346
        pending.pop_back();
12 ✔
347
        each_child(graph, next, [&](link_t down) NOEXCEPT
12 ✔
348
        {
349
            if (is_present(columns.cells[down]))
6 ✔
350
            {
351
                columns.cells[down] = absent;
6 ✔
352
                pending.push_back(down);
6 ✔
353
            }
354
        });
6 ✔
355
    }
356
}
6 ✔
357

358
inline void algorithm::admit(const columns_t& columns, link_t row) NOEXCEPT
1 ✔
359
{
360
    const std_array<link_t, one> rows{ { row } };
1 ✔
361
    columns.cells[row] = unlabeled;
1 ✔
362
    reform(columns, rows);
1 ✔
363
}
1 ✔
364

365
inline void algorithm::confirm(const columns_t& columns, link_t row) NOEXCEPT
5 ✔
366
{
367
    const graph_t graph{ columns };
5 ✔
368
    component_t former{};
5 ✔
369
    const auto labeled = is_labeled(columns.cells[row]) &&
9 ✔
370
        collect(former, graph, row);
4 ✔
371

372
    columns.cells[row] = boundary;
5 ✔
373
    const auto slice = edges(graph, row);
5 ✔
374
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
10 ✔
375
    {
376
        each_rival(graph, input, [&](link_t other) NOEXCEPT
5 ✔
377
        {
378
            const auto rival_row = owner(graph, other);
5 ✔
379
            if (is_present(columns.cells[rival_row]))
5 ✔
380
                exclude(columns, rival_row);
4 ✔
381
        });
5 ✔
382
    }
383

384
    if (!labeled)
5 ✔
385
        return;
1 ✔
386

387
    std::vector<link_t> rest{};
4 ✔
388
    for (size_t member{}; member < former.count; ++member)
24 ✔
389
        if (is_present(columns.cells[former.rows[member]]))
20 ✔
390
            rest.push_back(former.rows[member]);
8 ✔
391

392
    reform(columns, rest);
4 ✔
393
}
4 ✔
394

395
inline void algorithm::unconfirm(const columns_t& columns, link_t row) NOEXCEPT
2 ✔
396
{
397
    const graph_t graph{ columns };
2 ✔
398
    std::vector<link_t> affected{ row };
2 ✔
399
    columns.cells[row] = unlabeled;
2 ✔
400

401
    const auto slice = edges(graph, row);
2 ✔
402
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
4 ✔
403
    {
404
        each_rival(graph, input, [&](link_t other) NOEXCEPT
2 ✔
405
        {
406
            restore(affected, columns, owner(graph, other));
2 ✔
407
        });
2 ✔
408
    }
409

410
    reform(columns, affected);
2 ✔
411
}
2 ✔
412

413
inline void algorithm::revive(const columns_t& columns, link_t row) NOEXCEPT
2 ✔
414
{
415
    std::vector<link_t> affected{};
2 ✔
416
    restore(affected, columns, row);
2 ✔
417
    reform(columns, affected);
2 ✔
418
}
2 ✔
419

420
// Template (frontier, table columns).
421
// ----------------------------------------------------------------------------
422

423
template <typename Word>
424
inline void algorithm::resolve(std::span<mark> marks,
7 ✔
425
    const columns_t& columns, size_t maximum_outcomes) NOEXCEPT
426
{
427
    const graph_t graph{ columns };
7 ✔
428
    const auto count = columns.cells.size();
7 ✔
429
    for (size_t row{}; row < count; ++row)
40 ✔
430
        marks[row] = is_present(columns.cells[row]) ? mark::unmarked :
34 ✔
431
            mark::excluded;
432

433
    std::vector<mask_t> masks{};
7 ✔
434
    for (size_t at{}; at < count; ++at)
40 ✔
435
    {
436
        // A component is resolved at its label, its lowest member row.
437
        const auto row = possible_narrow_cast<link_t>(at);
33 ✔
438
        if (!is_labeled(columns.cells[row]) || columns.cells[row] != row)
33 ✔
439
            continue;
28 ✔
440

441
        component_t component{};
6 ✔
442
        const auto total = collect(component, graph, row) &&
12 ✔
443
            relate(component, graph) ? std::min(outcomes(component),
12 ✔
444
                maximum_outcomes) : zero;
445

446
        if (is_zero(total))
6 ✔
447
        {
448
            forfeit(marks, graph, row);
1 ✔
449
            continue;
1 ✔
450
        }
451

452
        masks.resize(total);
5 ✔
453
        for (size_t index{}; index < total; ++index)
23 ✔
454
            masks[index] = outcome(component, index);
18 ✔
455

456
        const auto chosen = masks[best<Word>(masks, component, graph)];
5 ✔
457
        for (size_t member{}; member < component.count; ++member)
30 ✔
458
            if (!get_right(chosen, member))
25 ✔
459
                discard(marks, graph, component.rows[member]);
11 ✔
460
    }
461
}
7 ✔
462

463
inline void algorithm::pack(std::vector<link_t>& out, std::span<mark> marks,
9 ✔
464
    const columns_t& columns, const totals_t& budget, size_t depth,
465
    uint32_t bytes_per_sigop) NOEXCEPT
466
{
467
    struct score_t
468
    {
469
        link_t row{};
470
        uint64_t rate{};
471
        uint64_t fee{};
472
        size_t count{};
473
    };
474

475
    const graph_t graph{ columns };
9 ✔
476
    const auto count = columns.cells.size();
9 ✔
477
    std::vector<score_t> scores{};
9 ✔
478
    std::vector<link_t> rows{};
9 ✔
479
    totals_t used{};
9 ✔
480

481
    const auto available = [&]() NOEXCEPT -> totals_t
61 ✔
482
    {
483
        return
484
        {
485
            {},
486
            floored_subtract(budget.size, used.size),
61 ✔
487
            floored_subtract(budget.sigops, used.sigops)
61 ✔
488
        };
61 ✔
489
    };
9 ✔
490

491
    // Compared in satoshis per virtual kilobyte.
492
    const auto rate = [&](const totals_t& sum) NOEXCEPT
26 ✔
493
    {
494
        return ceilinged_multiply(sum.fee, 1'000_u64) /
26 ✔
495
            std::max(chargeable(sum, bytes_per_sigop), 1_u64);
26 ✔
496
    };
9 ✔
497

498
    for (auto taken = true; taken;)
30 ✔
499
    {
500
        taken = false;
501
        scores.clear();
21 ✔
502
        for (size_t at{}; at < count; ++at)
96 ✔
503
        {
504
            totals_t sum{};
75 ✔
505
            const auto row = possible_narrow_cast<link_t>(at);
75 ✔
506
            if (marks[row] == mark::unmarked &&
114 ✔
507
                chunk(rows, sum, marks, graph, row, depth, available()))
88 ✔
508
                scores.push_back({ row, rate(sum), sum.fee, rows.size() });
26 ✔
509
        }
510

511
        std::sort(scores.begin(), scores.end(),
21 ✔
512
            [](const score_t& left, const score_t& right) NOEXCEPT
22 ✔
513
            {
514
                return left.rate == right.rate ? left.row < right.row :
22 ✔
515
                    left.rate > right.rate;
22 ✔
516
            });
517

518
        for (const auto& score: scores)
47 ✔
519
        {
520
            totals_t sum{};
26 ✔
521
            if (marks[score.row] != mark::unmarked ||
48 ✔
522
                !chunk(rows, sum, marks, graph, score.row, depth,
22 ✔
523
                    available()) ||
22 ✔
524
                rows.size() != score.count || sum.fee != score.fee)
43 ✔
525
                continue;
13 ✔
526

527
            for (const auto row: rows)
30 ✔
528
                marks[row] = mark::taken;
17 ✔
529

530
            used.fee = ceilinged_add(used.fee, sum.fee);
13 ✔
531
            used.size = ceilinged_add(used.size, sum.size);
13 ✔
532
            used.sigops = ceilinged_add(used.sigops, sum.sigops);
13 ✔
533
            taken = true;
534
        }
535
    }
536

537
    out.clear();
9 ✔
538
    for (size_t at{}; at < count; ++at)
42 ✔
539
        if (marks[at] == mark::taken)
33 ✔
540
            out.push_back(possible_narrow_cast<link_t>(at));
17 ✔
541
}
9 ✔
542

543
// Columns (whole table).
544
// ----------------------------------------------------------------------------
545

546
template <typename Word>
547
constexpr void algorithm::gather(std::span<link_t> out,
135 ✔
548
    const std::span<const link_t>& values,
549
    const std::span<const link_t>& indexes) NOEXCEPT
550
{
551
    gather_<Word>(zero, out, values, indexes);
135 ✔
552
}
135 ✔
553

554
constexpr void algorithm::initialize(const columns_t& columns) NOEXCEPT
45 ✔
555
{
556
    const graph_t graph{ columns };
45 ✔
557
    const auto count = columns.cells.size();
45 ✔
558
    for (size_t row{}; row < count; ++row)
266 ✔
559
    {
560
        auto& value = columns.cells[row];
221 ✔
561
        if (is_present(value))
221 ✔
562
            value = contested(graph, possible_narrow_cast<link_t>(row)) ?
350 ✔
563
                possible_narrow_cast<link_t>(row) : unlabeled;
130 ✔
564
    }
565
}
45 ✔
566

567
template <typename Word>
568
constexpr bool algorithm::propagate(const columns_t& columns,
128 ✔
569
    std::span<link_t> scratch) NOEXCEPT
570
{
571
    const graph_t graph{ columns };
128 ✔
572
    gather<Word>(scratch, columns.cells, columns.rows);
128 ✔
573

574
    auto changed = false;
575
    const auto count = columns.cells.size();
128 ✔
576
    for (size_t at{}; at < count; ++at)
760 ✔
577
    {
578
        const auto row = possible_narrow_cast<link_t>(at);
632 ✔
579
        const auto own = columns.cells[row];
632 ✔
580
        if (!is_present(own))
632 ✔
581
            continue;
9 ✔
582

583
        auto down = unlabeled;
631 ✔
584
        const auto slice = columns.edges[row];
631 ✔
585
        for (auto input = slice.first; input < slice.first + slice.count;
1,385 ✔
586
            ++input)
587
        {
588
            down = std::min(down, scratch[input]);
754 ✔
589
            each_rival(graph, input, [&](link_t other) NOEXCEPT
1,758 ✔
590
            {
591
                down = std::min(down, columns.cells[owner(graph, other)]);
502 ✔
592
            });
593
        }
594

595
        // An uncontested row without a relevant parent is irrelevant.
596
        if (!is_labeled(own) && !is_labeled(down))
631 ✔
597
            continue;
8 ✔
598

599
        auto label = std::min(own, down);
623 ✔
600
        each_child(graph, row, [&](link_t child) NOEXCEPT
1,115 ✔
601
        {
602
            label = std::min(label, columns.cells[child]);
246 ✔
603
        });
604

605
        if (label != own)
623 ✔
606
        {
607
            columns.cells[row] = label;
207 ✔
608
            changed = true;
609
        }
610
    }
611

612
    return changed;
128 ✔
613
}
614

615
template <typename Word>
616
constexpr bool algorithm::collect(component_t& out, link_t label,
9 ✔
617
    const std::span<const link_t>& cells) NOEXCEPT
618
{
619
    out = {};
9 ✔
620
    return collect_<Word>(zero, out, label, cells);
9 ✔
621
}
622

623
template <typename Word>
624
inline void algorithm::find(std::vector<link_t>& out,
7 ✔
625
    const std::span<const link_t>& values, link_t value) NOEXCEPT
626
{
627
    out.clear();
7 ✔
628
    find_<Word>(zero, out, values, value);
7 ✔
629
}
7 ✔
630

631
// Internals.
632
// ----------------------------------------------------------------------------
633

634
constexpr size_t algorithm::rows(const graph_t& graph) NOEXCEPT
4,393 ✔
635
{
636
    return graph.table.cells.size();
4,393 ✔
637
}
638

639
constexpr algorithm::link_t& algorithm::cell(const graph_t& graph,
479 ✔
640
    link_t row) NOEXCEPT
641
{
642
    const auto count = rows(graph);
479 ✔
643
    return row < count ? graph.table.cells[row] :
463 ✔
644
        graph.extra.cells[row - count];
479 ✔
645
}
646

647
constexpr algorithm::edges_t algorithm::edges(const graph_t& graph,
642 ✔
648
    link_t row) NOEXCEPT
649
{
650
    const auto count = rows(graph);
642 ✔
651
    return row < count ? graph.table.edges[row] :
642 ✔
652
        graph.extra.edges[row - count];
11 ✔
653
}
654

655
constexpr const algorithm::totals_t& algorithm::totals(const graph_t& graph,
1,604 ✔
656
    link_t row) NOEXCEPT
657
{
658
    const auto count = rows(graph);
1,604 ✔
659
    return row < count ? graph.table.totals[row] :
1,604 ✔
660
        graph.extra.totals[row - count];
1,604 ✔
661
}
662

663
constexpr algorithm::link_t algorithm::child(const graph_t& graph,
830 ✔
664
    link_t row) NOEXCEPT
665
{
666
    const auto count = rows(graph);
830 ✔
667
    return row < count ? graph.table.children[row] :
834 ✔
668
        graph.extra.children[row - count];
4 ✔
669
}
670

671
constexpr algorithm::link_t algorithm::parent(const graph_t& graph,
458 ✔
672
    link_t input) NOEXCEPT
673
{
674
    const auto count = graph.table.rows.size();
458 ✔
675
    return input < count ? graph.table.rows[input] :
466 ✔
676
        graph.extra.rows[input - count];
8 ✔
677
}
678

679
constexpr algorithm::link_t algorithm::owner(const graph_t& graph,
1,213 ✔
680
    link_t input) NOEXCEPT
681
{
682
    // The last row whose first input is not beyond the input.
683
    const auto count = graph.table.rows.size();
1,213 ✔
684
    const auto& edges = input < count ? graph.table.edges : graph.extra.edges;
1,213 ✔
685
    const auto after = std::upper_bound(edges.begin(), edges.end(), input,
1,213 ✔
686
        [](link_t at, const edges_t& edge) NOEXCEPT
2,991 ✔
687
        {
688
            return at < edge.first;
2,991 ✔
689
        });
690

691
    const auto base = input < count ? zero : rows(graph);
1,213 ✔
692
    return possible_narrow_cast<link_t>(base +
1,213 ✔
693
        std::distance(edges.begin(), std::prev(after)));
1,213 ✔
694
}
695

696
constexpr algorithm::link_t algorithm::sibling(const graph_t& graph,
325 ✔
697
    link_t input) NOEXCEPT
698
{
699
    const auto count = graph.table.rows.size();
325 ✔
700
    return input < count ? graph.table.siblings[input] :
325 ✔
701
        graph.extra.siblings[input - count];
×
702
}
703

704
constexpr algorithm::link_t algorithm::rival(const graph_t& graph,
2,216 ✔
705
    link_t input) NOEXCEPT
706
{
707
    const auto count = graph.table.rows.size();
2,216 ✔
708
    return input < count ? graph.table.rivals[input] :
2,225 ✔
709
        graph.extra.rivals[input - count];
9 ✔
710
}
711

712
template <typename Visitor>
713
constexpr void algorithm::each_child(const graph_t& graph, link_t row,
830 ✔
714
    const Visitor& visit) NOEXCEPT
715
{
716
    for (auto input = child(graph, row); input != none;
1,155 ✔
717
        input = sibling(graph, input))
325 ✔
718
        visit(owner(graph, input));
325 ✔
719

720
    if (row < rows(graph))
830 ✔
721
        for (const auto& bridge: graph.children)
834 ✔
722
            if (bridge.from == row)
8 ✔
723
                visit(owner(graph, bridge.to));
2 ✔
724
}
830 ✔
725

726
template <typename Visitor>
727
constexpr void algorithm::each_rival(const graph_t& graph, link_t input,
1,336 ✔
728
    const Visitor& visit) NOEXCEPT
729
{
730
    const auto count = graph.table.rows.size();
1,336 ✔
731
    std::vector<link_t> seen{ input };
1,336 ✔
732
    const auto step = [&](link_t next) NOEXCEPT
3,562 ✔
733
    {
734
        if (next == none || std::ranges::find(seen, next) != seen.end())
2,226 ✔
735
            return false;
1,340 ✔
736

737
        seen.push_back(next);
886 ✔
738
        return true;
886 ✔
739
    };
740

741
    // A table input bridges to the candidate inputs spending its point.
742
    for (auto at = input;;)
743
    {
744
        if (at < count)
2,216 ✔
745
            for (const auto& bridge: graph.rivals)
2,245 ✔
746
                if (bridge.from == at && step(bridge.to))
38 ✔
747
                    visit(bridge.to);
6 ✔
748

749
        at = rival(graph, at);
2,216 ✔
750
        if (!step(at))
2,216 ✔
751
            return;
1,336 ✔
752

753
        visit(at);
880 ✔
754
    }
755
}
1,336 ✔
756

757
constexpr bool algorithm::contested(const graph_t& graph, link_t row) NOEXCEPT
238 ✔
758
{
759
    auto out = false;
238 ✔
760
    const auto slice = edges(graph, row);
238 ✔
761
    for (auto input = slice.first; !out && input < slice.first + slice.count;
476 ✔
762
        ++input)
763
    {
764
        each_rival(graph, input, [&](link_t other) NOEXCEPT
238 ✔
765
        {
766
            if (is_present(cell(graph, owner(graph, other))))
142 ✔
767
                out = true;
135 ✔
768
        });
142 ✔
769
    }
770

771
    return out;
238 ✔
772
}
773

774
constexpr bool algorithm::descended(const graph_t& graph, link_t row) NOEXCEPT
14 ✔
775
{
776
    const auto slice = edges(graph, row);
14 ✔
777
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
24 ✔
778
    {
779
        const auto up = parent(graph, input);
14 ✔
780
        if (up != none && is_labeled(cell(graph, up)))
14 ✔
781
            return true;
782
    }
783

784
    return false;
785
}
786

787
constexpr bool algorithm::confirmable(const graph_t& graph,
5 ✔
788
    link_t row) NOEXCEPT
789
{
790
    auto out = true;
5 ✔
791
    const auto slice = edges(graph, row);
5 ✔
792
    for (auto input = slice.first; out && input < slice.first + slice.count;
13 ✔
793
        ++input)
794
    {
795
        const auto up = parent(graph, input);
8 ✔
796
        if (up != none && cell(graph, up) == absent)
8 ✔
797
            return false;
798

799
        each_rival(graph, input, [&](link_t other) NOEXCEPT
8 ✔
800
        {
801
            if (is_boundary(cell(graph, owner(graph, other))))
6 ✔
802
                out = false;
1 ✔
803
        });
6 ✔
804
    }
805

806
    return out;
807
}
808

809
constexpr size_t algorithm::index(const component_t& component,
527 ✔
810
    link_t row) NOEXCEPT
811
{
812
    for (size_t member{}; member < component.count; ++member)
1,561 ✔
813
        if (component.rows[member] == row)
1,310 ✔
814
            return member;
815

816
    return maximum_members;
817
}
818

819
constexpr algorithm::mask_t algorithm::losers(const component_t& component,
169 ✔
820
    size_t index) NOEXCEPT
821
{
822
    mask_t out{};
823
    for (size_t contest{}; contest < component.contests; ++contest)
507 ✔
824
    {
825
        const auto clique = component.cliques[contest];
338 ✔
826
        const auto size = ones_count(clique);
338 ✔
827
        const auto digit = index % size;
338 ✔
828
        index /= size;
338 ✔
829

830
        // The winner is the clique's digit-th set bit.
831
        auto winner = clique;
832
        for (size_t cleared{}; cleared < digit; ++cleared)
499 ✔
833
            winner &= sub1(winner);
161 ✔
834

835
        winner &= (mask_t{} - winner);
338 ✔
836
        out |= clique & ~winner;
338 ✔
837
    }
838

839
    return out;
169 ✔
840
}
841

842
template <typename Word>
843
constexpr size_t algorithm::best(const std::span<const mask_t>& outcomes,
12 ✔
844
    const component_t& component, const graph_t& graph) NOEXCEPT
845
{
846
    size_t out{};
12 ✔
847
    uint64_t most{};
12 ✔
848
    each_fee<Word>(zero, outcomes, component, graph,
12 ✔
849
        [&](size_t index, uint64_t fee) NOEXCEPT
332 ✔
850
        {
851
            if (fee > most)
160 ✔
852
            {
853
                most = fee;
23 ✔
854
                out = index;
23 ✔
855
            }
856
        });
857

858
    return out;
12 ✔
859
}
860

861
inline void algorithm::discard(std::span<mark> marks, const graph_t& graph,
16 ✔
862
    link_t row) NOEXCEPT
863
{
864
    std::vector<link_t> pending{ row };
16 ✔
865
    marks[row] = mark::excluded;
16 ✔
866

867
    while (!pending.empty())
39 ✔
868
    {
869
        const auto next = pending.back();
23 ✔
870
        pending.pop_back();
23 ✔
871
        each_child(graph, next, [&](link_t down) NOEXCEPT
23 ✔
872
        {
873
            if (marks[down] != mark::excluded)
7 ✔
874
            {
875
                marks[down] = mark::excluded;
7 ✔
876
                pending.push_back(down);
7 ✔
877
            }
878
        });
7 ✔
879
    }
880
}
16 ✔
881

882
inline void algorithm::forfeit(std::span<mark> marks, const graph_t& graph,
1 ✔
883
    link_t row) NOEXCEPT
884
{
885
    std::vector<link_t> found{ row };
1 ✔
886
    const auto add = [&](link_t next) NOEXCEPT
8 ✔
887
    {
888
        if (std::ranges::find(found, next) == found.end())
8 ✔
889
            found.push_back(next);
4 ✔
890
    };
9 ✔
891

892
    for (size_t at{}; at < found.size(); ++at)
6 ✔
893
    {
894
        const auto member = found[at];
5 ✔
895
        const auto slice = edges(graph, member);
5 ✔
896
        for (auto input = slice.first; input < slice.first + slice.count;
11 ✔
897
            ++input)
898
        {
899
            const auto up = parent(graph, input);
6 ✔
900
            if (up != none && is_labeled(cell(graph, up)))
6 ✔
901
                add(up);
2 ✔
902

903
            each_rival(graph, input, [&](link_t other) NOEXCEPT
6 ✔
904
            {
905
                const auto rival_row = owner(graph, other);
4 ✔
906
                if (is_present(cell(graph, rival_row)))
4 ✔
907
                    add(rival_row);
4 ✔
908
            });
4 ✔
909
        }
910

911
        each_child(graph, member, [&](link_t down) NOEXCEPT
5 ✔
912
        {
913
            if (is_present(cell(graph, down)))
2 ✔
914
                add(down);
2 ✔
915
        });
2 ✔
916
    }
917

918
    for (const auto member: found)
6 ✔
919
        discard(marks, graph, member);
5 ✔
920
}
1 ✔
921

922
inline bool algorithm::chunk(std::vector<link_t>& out, totals_t& sum,
61 ✔
923
    const std::span<const mark>& marks, const graph_t& graph, link_t row,
924
    size_t depth, const totals_t& available) NOEXCEPT
925
{
926
    sum = {};
61 ✔
927
    out.assign(one, row);
61 ✔
928

929
    // Each level holds the untaken parents of the previous level.
930
    for (size_t level{}, begin{}; begin < out.size(); ++level)
142 ✔
931
    {
932
        const auto end = out.size();
933
        for (auto at = begin; at < end; ++at)
167 ✔
934
        {
935
            const auto slice = edges(graph, out[at]);
86 ✔
936
            for (auto input = slice.first;
167 ✔
937
                input < slice.first + slice.count; ++input)
167 ✔
938
            {
939
                const auto up = parent(graph, input);
86 ✔
940
                if (up == none || marks[up] == mark::taken ||
86 ✔
941
                    is_boundary(cell(graph, up)))
30 ✔
942
                    continue;
56 ✔
943

944
                if (marks[up] == mark::excluded)
30 ✔
945
                    return false;
5 ✔
946

947
                if (std::ranges::find(out, up) != out.end())
28 ✔
NEW
948
                    continue;
×
949

950
                if (level == depth)
28 ✔
951
                    return false;
952

953
                out.push_back(up);
25 ✔
954
            }
955
        }
956

957
        begin = end;
958
    }
959

960
    std::sort(out.begin(), out.end());
56 ✔
961
    auto cut = out.begin();
56 ✔
962
    for (; cut != out.end(); ++cut)
112 ✔
963
    {
964
        const auto& add = totals(graph, *cut);
76 ✔
965
        const auto size = ceilinged_add(sum.size, add.size);
76 ✔
966
        const auto sigops = ceilinged_add(sum.sigops, add.sigops);
76 ✔
967
        if (size > available.size || sigops > available.sigops)
76 ✔
968
            break;
969

970
        sum.fee = ceilinged_add(sum.fee, add.fee);
56 ✔
971
        sum.size = size;
56 ✔
972
        sum.sigops = sigops;
56 ✔
973
    }
974

975
    out.erase(cut, out.end());
56 ✔
976
    return !out.empty();
56 ✔
977
}
978

979
inline void algorithm::restore(std::vector<link_t>& out,
6 ✔
980
    const columns_t& columns, link_t row) NOEXCEPT
981
{
982
    const graph_t graph{ columns };
6 ✔
983
    if (columns.cells[row] != absent || !confirmable(graph, row))
6 ✔
984
        return;
2 ✔
985

986
    columns.cells[row] = unlabeled;
4 ✔
987
    out.push_back(row);
4 ✔
988
    each_child(graph, row, [&](link_t down) NOEXCEPT
4 ✔
989
    {
990
        restore(out, columns, down);
2 ✔
991
    });
2 ✔
992
}
993

994
inline void algorithm::reform(const columns_t& columns,
9 ✔
995
    const std::span<const link_t>& rows) NOEXCEPT
996
{
997
    const graph_t graph{ columns };
9 ✔
998
    for (const auto row: rows)
24 ✔
999
        if (is_present(columns.cells[row]))
15 ✔
1000
            columns.cells[row] = unlabeled;
15 ✔
1001

1002
    // Relevance closes over the rows in at most count passes.
1003
    for (auto changed = true; changed;)
21 ✔
1004
    {
1005
        changed = false;
1006
        for (const auto row: rows)
33 ✔
1007
        {
1008
            if (!is_present(columns.cells[row]) ||
21 ✔
1009
                is_labeled(columns.cells[row]))
21 ✔
1010
                continue;
6 ✔
1011

1012
            if (contested(graph, row) || descended(graph, row))
15 ✔
1013
            {
1014
                columns.cells[row] = row;
6 ✔
1015
                changed = true;
1016
            }
1017
        }
1018
    }
1019

1020
    for (const auto row: rows)
24 ✔
1021
    {
1022
        // A row relabeled by a previous component carries a lower label.
1023
        if (columns.cells[row] != row)
15 ✔
1024
            continue;
12 ✔
1025

1026
        component_t component{};
3 ✔
1027
        if (collect(component, graph, row))
3 ✔
1028
            relabel(columns, component);
3 ✔
1029
    }
1030
}
9 ✔
1031

1032
template <typename Word>
1033
INLINE constexpr Word algorithm::fees(Word outcomes,
1034
    const component_t& component, const graph_t& graph) NOEXCEPT
1035
{
1036
    constexpr auto lane = bits<mask_t>;
1037
    auto sum = f::broadcast<Word>(mask_t{});
1038
    for (size_t member{}; member < component.count; ++member)
1,812 ✔
1039
    {
1040
        const auto bit = f::broadcast<Word>(set_right(mask_t{}, member));
1,510 ✔
1041
        const auto fee = f::broadcast<Word>(totals(graph,
1,510 ✔
1042
            component.rows[member]).fee);
1,510 ✔
1043
        const auto selected = f::eq<lane>(f::and_(outcomes, bit), bit);
1044
        sum = f::add<lane>(sum, f::and_(selected, fee));
1045
    }
1046

1047
    return sum;
1048
}
1049

1050
template <typename Word, typename Visitor>
1051
constexpr void algorithm::each_fee(size_t index,
31 ✔
1052
    const std::span<const mask_t>& outcomes, const component_t& component,
1053
    const graph_t& graph, const Visitor& visit) NOEXCEPT
1054
{
1055
    const auto count = outcomes.size();
19 ✔
1056
    if constexpr (is_extended<Word>)
1057
    {
1058
        if constexpr (have<Word>)
1059
        {
1060
            constexpr auto lanes = mask_lanes<Word>;
1061
            aligned_array<uint64_t, lanes> sums{};
1062
            for (; index + lanes <= count; index += lanes)
1063
            {
1064
                store(sums.data(), fees<Word>(load<Word>(&outcomes[index]),
1065
                    component, graph));
1066

1067
                for (size_t lane{}; lane < lanes; ++lane)
1068
                    visit(index + lane, sums[lane]);
1069
            }
1070
        }
1071

1072
        each_fee<narrower<Word, mask_t>>(index, outcomes, component, graph,
12 ✔
1073
            visit);
1074
    }
1075
    else
1076
    {
1077
        for (; index < count; ++index)
321 ✔
1078
            visit(index, fees<mask_t>(outcomes[index], component, graph));
604 ✔
1079
    }
1080
}
31 ✔
1081

1082
template <typename Word>
1083
constexpr void algorithm::gather_(size_t index, std::span<link_t> out,
159 ✔
1084
    const std::span<const link_t>& values,
1085
    const std::span<const link_t>& indexes) NOEXCEPT
1086
{
1087
    const auto count = indexes.size();
135 ✔
1088
    if constexpr (is_extended<Word>)
1089
    {
1090
        if constexpr (have<Word>)
1091
        {
1092
            constexpr auto lanes = link_lanes<Word>;
1093
            const auto nones = f::broadcast<Word>(none);
1094
            for (; index + lanes <= count; index += lanes)
1095
            {
1096
                const auto at = load<Word>(&indexes[index]);
1097
                const auto missing = f::eq<bits<link_t>>(at, nones);
1098
                const auto found = f::gather(values.data(),
1099
                    f::andnot(missing, at));
1100

1101
                store(&out[index], f::select(missing, nones, found));
1102
            }
1103
        }
1104

1105
        gather_<narrower<Word, link_t>>(index, out, values, indexes);
24 ✔
1106
    }
1107
    else
1108
    {
1109
        for (; index < count; ++index)
1,178 ✔
1110
            out[index] = (indexes[index] == none) ? none :
1,043 ✔
1111
                values[indexes[index]];
1112
    }
1113
}
159 ✔
1114

1115
template <typename Word>
1116
constexpr bool algorithm::collect_(size_t index, component_t& out,
15 ✔
1117
    link_t label, const std::span<const link_t>& cells) NOEXCEPT
1118
{
1119
    const auto count = cells.size();
9 ✔
1120
    const auto add = [&](size_t row) NOEXCEPT
241 ✔
1121
    {
1122
        if (out.count == maximum_members)
226 ✔
1123
            return false;
1124

1125
        out.rows[out.count++] = possible_narrow_cast<link_t>(row);
225 ✔
1126
        return true;
225 ✔
1127
    };
1128

1129
    if constexpr (is_extended<Word>)
1130
    {
1131
        if constexpr (have<Word>)
1132
        {
1133
            constexpr auto lanes = link_lanes<Word>;
1134
            const auto wanted = f::broadcast<Word>(label);
1135
            for (; index + lanes <= count; index += lanes)
1136
            {
1137
                const auto found = f::eq<bits<link_t>>(
1138
                    load<Word>(&cells[index]), wanted);
1139

1140
                if (f::any(found))
1141
                    for (auto row = index; row < index + lanes; ++row)
1142
                        if (cells[row] == label && !add(row))
1143
                            return false;
1144
            }
1145
        }
1146

1147
        return collect_<narrower<Word, link_t>>(index, out, label, cells);
6 ✔
1148
    }
1149
    else
1150
    {
1151
        for (; index < count; ++index)
557 ✔
1152
            if (cells[index] == label && !add(index))
549 ✔
1153
                return false;
1154

1155
        return true;
1156
    }
1157
}
1158

1159
template <typename Word>
1160
inline void algorithm::find_(size_t index, std::vector<link_t>& out,
13 ✔
1161
    const std::span<const link_t>& values, link_t value) NOEXCEPT
1162
{
1163
    const auto count = values.size();
7 ✔
1164
    if constexpr (is_extended<Word>)
1165
    {
1166
        if constexpr (have<Word>)
1167
        {
1168
            constexpr auto lanes = link_lanes<Word>;
1169
            const auto wanted = f::broadcast<Word>(value);
1170
            for (; index + lanes <= count; index += lanes)
1171
            {
1172
                const auto found = f::eq<bits<link_t>>(
1173
                    load<Word>(&values[index]), wanted);
1174

1175
                if (f::any(found))
1176
                    for (auto row = index; row < index + lanes; ++row)
1177
                        if (values[row] == value)
1178
                            out.push_back(possible_narrow_cast<link_t>(row));
1179
            }
1180
        }
1181

1182
        find_<narrower<Word, link_t>>(index, out, values, value);
6 ✔
1183
    }
1184
    else
1185
    {
1186
        for (; index < count; ++index)
298 ✔
1187
            if (values[index] == value)
291 ✔
1188
                out.push_back(possible_narrow_cast<link_t>(index));
71 ✔
1189
    }
1190
}
13 ✔
1191

1192
template <typename Word, typename Integral>
1193
INLINE constexpr Word algorithm::load(const Integral* at) NOEXCEPT
1194
{
1195
    if constexpr (is_extended<Word>)
1196
        return f::load<Word>(pointer_cast<const uint8_t>(at));
1197
    else
1198
        return *at;
1199
}
1200

1201
template <typename Word, typename Integral>
1202
INLINE constexpr void algorithm::store(Integral* at, Word word) NOEXCEPT
1203
{
1204
    if constexpr (is_extended<Word>)
1205
        f::store(pointer_cast<uint8_t>(at), word);
1206
    else
1207
        *at = word;
1208
}
1209

1210
BC_POP_WARNING()
1211
BC_POP_WARNING()
1212
BC_POP_WARNING()
1213
BC_POP_WARNING()
1214

1215
} // namespace pool
1216
} // namespace system
1217
} // namespace libbitcoin
1218

1219
#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