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

libbitcoin / libbitcoin-system / 43359831878

13 Nov 2026 03:34AM UTC coverage: 96.281% (+0.03%) from 96.25%
43359831878

push

github

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

Add pool conflict component and outcome algorithm.

386 of 392 new or added lines in 1 file covered. (98.47%)

27055 of 28100 relevant lines covered (96.28%)

3965568.47 hits per line

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

98.47
/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
758 ✔
41
{
42
    return cell < absent;
758 ✔
43
}
44

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

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

55
constexpr uint64_t algorithm::chargeable(const totals_t& totals,
15 ✔
56
    uint32_t bytes_per_sigop) NOEXCEPT
57
{
58
    return std::max(possible_wide_cast<uint64_t>(totals.size),
15 ✔
59
        ceilinged_multiply(possible_wide_cast<uint64_t>(totals.sigops),
15 ✔
60
            possible_wide_cast<uint64_t>(bytes_per_sigop)));
15 ✔
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,
20 ✔
94
    link_t row) NOEXCEPT
95
{
96
    out = {};
20 ✔
97
    const auto add = [&](link_t found) NOEXCEPT
180 ✔
98
    {
99
        if (index(out, found) != maximum_members)
180 ✔
100
            return true;
101

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

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

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

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

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

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

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

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

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

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

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

174
            if (is_one(ones_count(clique)))
49 ✔
175
                continue;
33 ✔
176

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

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

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

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

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

201
            if (ancestors != out.ancestors[member])
41 ✔
202
            {
NEW
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,
2 ✔
213
    const std::span<const link_t>& rows) NOEXCEPT
214
{
215
    mask_t out{};
2 ✔
216
    for (const auto row: rows)
6 ✔
217
    {
218
        const auto member = index(component, row);
4 ✔
219
        if (member != maximum_members)
4 ✔
220
            set_right_into(out, member);
3 ✔
221
    }
222

223
    return out;
2 ✔
224
}
225

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

239
        visited.push_back(row);
1 ✔
240
        pending.push_back(row);
1 ✔
241
    };
3 ✔
242

243
    const auto charge = [&](link_t row) NOEXCEPT
13 ✔
244
    {
245
        out = ceilinged_add(out, chargeable(totals(graph, row),
13 ✔
246
            bytes_per_sigop));
247

248
        const auto slice = edges(graph, row);
13 ✔
249
        for (auto input = slice.first; input < slice.first + slice.count;
28 ✔
250
            ++input)
251
        {
252
            const auto up = parent(graph, input);
15 ✔
253
            if (up != none && is_present(cell(graph, up)))
15 ✔
254
                push(up);
5 ✔
255
        }
256

257
        return out <= limit;
13 ✔
258
    };
3 ✔
259

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

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

272
    return true;
273
}
3 ✔
274

275
// Outcomes.
276
// ----------------------------------------------------------------------------
277

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

284
    return out;
2 ✔
285
}
286

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

296
    return out;
8 ✔
297
}
298

299
template <typename Word>
300
constexpr void algorithm::evaluate(uint64_t& best, uint64_t& best_without,
1 ✔
301
    const std::span<const mask_t>& outcomes, mask_t candidate,
302
    const component_t& component, const graph_t& graph) NOEXCEPT
303
{
304
    best = zero;
1 ✔
305
    best_without = zero;
1 ✔
306
    const auto consider = [&](mask_t outcome, uint64_t fee) NOEXCEPT
9 ✔
307
    {
308
        best = std::max(best, fee);
4 ✔
309
        if (is_zero(outcome & candidate))
4 ✔
310
            best_without = std::max(best_without, fee);
2 ✔
311
    };
312

313
    const auto count = outcomes.size();
1 ✔
314
    size_t index{};
315

316
    if constexpr (is_extended<Word>)
317
    {
318
        constexpr auto lanes = mask_lanes<Word>;
319
        aligned_array<uint64_t, lanes> sums{};
320
        for (; index + lanes <= count; index += lanes)
321
        {
322
            store(sums.data(), fees<Word>(load<Word>(&outcomes[index]),
323
                component, graph));
324

325
            for (size_t lane{}; lane < lanes; ++lane)
326
                consider(outcomes[index + lane], sums[lane]);
327
        }
328
    }
329

330
    for (; index < count; ++index)
5 ✔
331
        consider(outcomes[index], fees<uint64_t>(outcomes[index], component,
8 ✔
332
            graph));
333
}
1 ✔
334

335
constexpr bool algorithm::covered(uint64_t best, uint64_t best_without,
4 ✔
336
    uint64_t candidate_bytes, uint64_t rate) NOEXCEPT
337
{
338
    // Compared in satoshis per virtual kilobyte.
339
    const auto added = floored_subtract(best, best_without);
4 ✔
340
    return ceilinged_multiply(added, 1'000_u64) >=
4 ✔
341
        ceilinged_multiply(rate, candidate_bytes);
4 ✔
342
}
343

344
// Transitions (frontier, table columns).
345
// ----------------------------------------------------------------------------
346

347
constexpr void algorithm::relabel(const columns_t& columns,
4 ✔
348
    const component_t& component) NOEXCEPT
349
{
350
    // Rows are sorted by collect, so the label is the first.
351
    const auto label = component.rows.front();
4 ✔
352
    for (size_t member{}; member < component.count; ++member)
22 ✔
353
        columns.cells[component.rows[member]] = label;
18 ✔
354
}
4 ✔
355

356
inline void algorithm::exclude(const columns_t& columns, link_t row) NOEXCEPT
6 ✔
357
{
358
    const graph_t graph{ columns };
6 ✔
359
    std::vector<link_t> pending{ row };
6 ✔
360
    columns.cells[row] = absent;
6 ✔
361

362
    while (!pending.empty())
18 ✔
363
    {
364
        const auto next = pending.back();
12 ✔
365
        pending.pop_back();
12 ✔
366
        each_child(graph, next, [&](link_t down) NOEXCEPT
12 ✔
367
        {
368
            if (is_present(columns.cells[down]))
6 ✔
369
            {
370
                columns.cells[down] = absent;
6 ✔
371
                pending.push_back(down);
6 ✔
372
            }
373
        });
6 ✔
374
    }
375
}
6 ✔
376

377
inline void algorithm::admit(const columns_t& columns, link_t row) NOEXCEPT
1 ✔
378
{
379
    const std_array<link_t, one> rows{ { row } };
1 ✔
380
    columns.cells[row] = unlabeled;
1 ✔
381
    reform(columns, rows);
1 ✔
382
}
1 ✔
383

384
inline void algorithm::confirm(const columns_t& columns, link_t row) NOEXCEPT
5 ✔
385
{
386
    const graph_t graph{ columns };
5 ✔
387
    component_t former{};
5 ✔
388
    const auto labeled = is_labeled(columns.cells[row]) &&
9 ✔
389
        collect(former, graph, row);
4 ✔
390

391
    columns.cells[row] = boundary;
5 ✔
392
    const auto slice = edges(graph, row);
5 ✔
393
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
10 ✔
394
    {
395
        each_rival(graph, input, [&](link_t other) NOEXCEPT
5 ✔
396
        {
397
            const auto rival_row = owner(graph, other);
5 ✔
398
            if (is_present(columns.cells[rival_row]))
5 ✔
399
                exclude(columns, rival_row);
4 ✔
400
        });
5 ✔
401
    }
402

403
    if (!labeled)
5 ✔
404
        return;
1 ✔
405

406
    std::vector<link_t> rest{};
4 ✔
407
    for (size_t member{}; member < former.count; ++member)
24 ✔
408
        if (is_present(columns.cells[former.rows[member]]))
20 ✔
409
            rest.push_back(former.rows[member]);
8 ✔
410

411
    reform(columns, rest);
4 ✔
412
}
4 ✔
413

414
inline void algorithm::unconfirm(const columns_t& columns, link_t row) NOEXCEPT
2 ✔
415
{
416
    const graph_t graph{ columns };
2 ✔
417
    std::vector<link_t> affected{ row };
2 ✔
418
    columns.cells[row] = unlabeled;
2 ✔
419

420
    const auto slice = edges(graph, row);
2 ✔
421
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
4 ✔
422
    {
423
        each_rival(graph, input, [&](link_t other) NOEXCEPT
2 ✔
424
        {
425
            restore(affected, columns, owner(graph, other));
2 ✔
426
        });
2 ✔
427
    }
428

429
    reform(columns, affected);
2 ✔
430
}
2 ✔
431

432
inline void algorithm::revive(const columns_t& columns, link_t row) NOEXCEPT
2 ✔
433
{
434
    std::vector<link_t> affected{};
2 ✔
435
    restore(affected, columns, row);
2 ✔
436
    reform(columns, affected);
2 ✔
437
}
2 ✔
438

439
// Columns (whole table).
440
// ----------------------------------------------------------------------------
441

442
template <typename Word>
443
constexpr void algorithm::gather(std::span<link_t> out,
72 ✔
444
    const std::span<const link_t>& values,
445
    const std::span<const link_t>& indexes) NOEXCEPT
446
{
447
    const auto count = indexes.size();
72 ✔
448
    size_t index{};
449

450
    if constexpr (is_extended<Word>)
451
    {
452
        constexpr auto lanes = link_lanes<Word>;
453
        const auto nones = f::broadcast<Word>(none);
454
        for (; index + lanes <= count; index += lanes)
455
        {
456
            const auto at = load<Word>(&indexes[index]);
457
            const auto missing = f::eq<bits<link_t>>(at, nones);
458
            const auto found = f::gather(values.data(), f::andnot(missing, at));
459
            store(&out[index], f::select(missing, nones, found));
460
        }
461
    }
462

463
    for (; index < count; ++index)
491 ✔
464
        out[index] = (indexes[index] == none) ? none : values[indexes[index]];
419 ✔
465
}
72 ✔
466

467
constexpr void algorithm::initialize(const columns_t& columns) NOEXCEPT
26 ✔
468
{
469
    const graph_t graph{ columns };
26 ✔
470
    const auto count = columns.cells.size();
26 ✔
471
    for (size_t row{}; row < count; ++row)
152 ✔
472
    {
473
        auto& value = columns.cells[row];
126 ✔
474
        if (is_present(value))
126 ✔
475
            value = contested(graph, possible_narrow_cast<link_t>(row)) ?
198 ✔
476
                possible_narrow_cast<link_t>(row) : unlabeled;
73 ✔
477
    }
478
}
26 ✔
479

480
template <typename Word>
481
constexpr bool algorithm::propagate(const columns_t& columns,
71 ✔
482
    std::span<link_t> scratch) NOEXCEPT
483
{
484
    const graph_t graph{ columns };
71 ✔
485
    gather<Word>(scratch, columns.cells, columns.rows);
71 ✔
486

487
    auto changed = false;
488
    const auto count = columns.cells.size();
71 ✔
489
    for (size_t at{}; at < count; ++at)
418 ✔
490
    {
491
        const auto row = possible_narrow_cast<link_t>(at);
347 ✔
492
        const auto own = columns.cells[row];
347 ✔
493
        if (!is_present(own))
347 ✔
494
            continue;
9 ✔
495

496
        auto down = unlabeled;
346 ✔
497
        const auto slice = columns.edges[row];
346 ✔
498
        for (auto input = slice.first; input < slice.first + slice.count;
758 ✔
499
            ++input)
500
        {
501
            down = std::min(down, scratch[input]);
412 ✔
502
            each_rival(graph, input, [&](link_t other) NOEXCEPT
412 ✔
503
            {
504
                down = std::min(down, columns.cells[owner(graph, other)]);
274 ✔
505
            });
506
        }
507

508
        // An uncontested row without a relevant parent is irrelevant.
509
        if (!is_labeled(own) && !is_labeled(down))
346 ✔
510
            continue;
8 ✔
511

512
        auto label = std::min(own, down);
338 ✔
513
        each_child(graph, row, [&](link_t child) NOEXCEPT
338 ✔
514
        {
515
            label = std::min(label, columns.cells[child]);
132 ✔
516
        });
517

518
        if (label != own)
338 ✔
519
        {
520
            columns.cells[row] = label;
112 ✔
521
            changed = true;
522
        }
523
    }
524

525
    return changed;
71 ✔
526
}
527

528
template <typename Word>
529
constexpr bool algorithm::collect(component_t& out, link_t label,
3 ✔
530
    const std::span<const link_t>& cells) NOEXCEPT
531
{
532
    out = {};
3 ✔
533
    const auto rows = cells.size();
3 ✔
534
    const auto add = [&](size_t row) NOEXCEPT
73 ✔
535
    {
536
        if (out.count == maximum_members)
70 ✔
537
            return false;
538

539
        out.rows[out.count++] = possible_narrow_cast<link_t>(row);
69 ✔
540
        return true;
69 ✔
541
    };
542

543
    size_t row{};
544
    if constexpr (is_extended<Word>)
545
    {
546
        constexpr auto lanes = link_lanes<Word>;
547
        const auto wanted = f::broadcast<Word>(label);
548
        for (; row + lanes <= rows; row += lanes)
549
        {
550
            const auto found = f::eq<bits<link_t>>(load<Word>(&cells[row]),
551
                wanted);
552

553
            if (f::any(found))
554
                for (auto lane = row; lane < row + lanes; ++lane)
555
                    if (cells[lane] == label && !add(lane))
556
                        return false;
557
        }
558
    }
559

560
    for (; row < rows; ++row)
77 ✔
561
        if (cells[row] == label && !add(row))
75 ✔
562
            return false;
563

564
    return true;
565
}
566

567
template <typename Word>
568
inline void algorithm::find(std::vector<link_t>& out,
1 ✔
569
    const std::span<const link_t>& values, link_t value) NOEXCEPT
570
{
571
    out.clear();
1 ✔
572
    const auto count = values.size();
1 ✔
573
    size_t index{};
574

575
    if constexpr (is_extended<Word>)
576
    {
577
        constexpr auto lanes = link_lanes<Word>;
578
        const auto wanted = f::broadcast<Word>(value);
579
        for (; index + lanes <= count; index += lanes)
580
        {
581
            const auto found = f::eq<bits<link_t>>(load<Word>(&values[index]),
582
                wanted);
583

584
            if (f::any(found))
585
                for (auto lane = index; lane < index + lanes; ++lane)
586
                    if (values[lane] == value)
587
                        out.push_back(possible_narrow_cast<link_t>(lane));
588
        }
589
    }
590

591
    for (; index < count; ++index)
10 ✔
592
        if (values[index] == value)
9 ✔
593
            out.push_back(possible_narrow_cast<link_t>(index));
5 ✔
594
}
1 ✔
595

596
// Internals.
597
// ----------------------------------------------------------------------------
598

599
constexpr size_t algorithm::rows(const graph_t& graph) NOEXCEPT
1,544 ✔
600
{
601
    return graph.table.cells.size();
1,544 ✔
602
}
603

604
constexpr algorithm::link_t& algorithm::cell(const graph_t& graph,
278 ✔
605
    link_t row) NOEXCEPT
606
{
607
    const auto count = rows(graph);
278 ✔
608
    return row < count ? graph.table.cells[row] :
262 ✔
609
        graph.extra.cells[row - count];
278 ✔
610
}
611

612
constexpr algorithm::edges_t algorithm::edges(const graph_t& graph,
321 ✔
613
    link_t row) NOEXCEPT
614
{
615
    const auto count = rows(graph);
321 ✔
616
    return row < count ? graph.table.edges[row] :
321 ✔
617
        graph.extra.edges[row - count];
11 ✔
618
}
619

620
constexpr const algorithm::totals_t& algorithm::totals(const graph_t& graph,
33 ✔
621
    link_t row) NOEXCEPT
622
{
623
    const auto count = rows(graph);
33 ✔
624
    return row < count ? graph.table.totals[row] :
33 ✔
625
        graph.extra.totals[row - count];
33 ✔
626
}
627

628
constexpr algorithm::link_t algorithm::child(const graph_t& graph,
452 ✔
629
    link_t row) NOEXCEPT
630
{
631
    const auto count = rows(graph);
452 ✔
632
    return row < count ? graph.table.children[row] :
456 ✔
633
        graph.extra.children[row - count];
4 ✔
634
}
635

636
constexpr algorithm::link_t algorithm::parent(const graph_t& graph,
204 ✔
637
    link_t input) NOEXCEPT
638
{
639
    const auto count = graph.table.rows.size();
204 ✔
640
    return input < count ? graph.table.rows[input] :
212 ✔
641
        graph.extra.rows[input - count];
8 ✔
642
}
643

644
constexpr algorithm::link_t algorithm::owner(const graph_t& graph,
671 ✔
645
    link_t input) NOEXCEPT
646
{
647
    // The last row whose first input is not beyond the input.
648
    const auto count = graph.table.rows.size();
671 ✔
649
    const auto& edges = input < count ? graph.table.edges : graph.extra.edges;
671 ✔
650
    const auto after = std::upper_bound(edges.begin(), edges.end(), input,
671 ✔
651
        [](link_t at, const edges_t& edge) NOEXCEPT
1,643 ✔
652
        {
653
            return at < edge.first;
1,643 ✔
654
        });
655

656
    const auto base = input < count ? zero : rows(graph);
671 ✔
657
    return possible_narrow_cast<link_t>(base +
671 ✔
658
        std::distance(edges.begin(), std::prev(after)));
671 ✔
659
}
660

661
constexpr algorithm::link_t algorithm::sibling(const graph_t& graph,
176 ✔
662
    link_t input) NOEXCEPT
663
{
664
    const auto count = graph.table.rows.size();
176 ✔
665
    return input < count ? graph.table.siblings[input] :
176 ✔
NEW
666
        graph.extra.siblings[input - count];
×
667
}
668

669
constexpr algorithm::link_t algorithm::rival(const graph_t& graph,
1,224 ✔
670
    link_t input) NOEXCEPT
671
{
672
    const auto count = graph.table.rows.size();
1,224 ✔
673
    return input < count ? graph.table.rivals[input] :
1,233 ✔
674
        graph.extra.rivals[input - count];
9 ✔
675
}
676

677
template <typename Visitor>
678
constexpr void algorithm::each_child(const graph_t& graph, link_t row,
452 ✔
679
    const Visitor& visit) NOEXCEPT
680
{
681
    for (auto input = child(graph, row); input != none;
628 ✔
682
        input = sibling(graph, input))
176 ✔
683
        visit(owner(graph, input));
176 ✔
684

685
    if (row < rows(graph))
452 ✔
686
        for (const auto& bridge: graph.children)
456 ✔
687
            if (bridge.from == row)
8 ✔
688
                visit(owner(graph, bridge.to));
2 ✔
689
}
452 ✔
690

691
template <typename Visitor>
692
constexpr void algorithm::each_rival(const graph_t& graph, link_t input,
737 ✔
693
    const Visitor& visit) NOEXCEPT
694
{
695
    const auto count = graph.table.rows.size();
737 ✔
696
    std::vector<link_t> seen{ input };
737 ✔
697
    const auto step = [&](link_t next) NOEXCEPT
1,971 ✔
698
    {
699
        if (next == none || std::ranges::find(seen, next) != seen.end())
1,234 ✔
700
            return false;
741 ✔
701

702
        seen.push_back(next);
493 ✔
703
        return true;
493 ✔
704
    };
705

706
    // A table input bridges to the candidate inputs spending its point.
707
    for (auto at = input;;)
708
    {
709
        if (at < count)
1,224 ✔
710
            for (const auto& bridge: graph.rivals)
1,253 ✔
711
                if (bridge.from == at && step(bridge.to))
38 ✔
712
                    visit(bridge.to);
6 ✔
713

714
        at = rival(graph, at);
1,224 ✔
715
        if (!step(at))
1,224 ✔
716
            return;
737 ✔
717

718
        visit(at);
487 ✔
719
    }
720
}
737 ✔
721

722
constexpr bool algorithm::contested(const graph_t& graph, link_t row) NOEXCEPT
143 ✔
723
{
724
    auto out = false;
143 ✔
725
    const auto slice = edges(graph, row);
143 ✔
726
    for (auto input = slice.first; !out && input < slice.first + slice.count;
286 ✔
727
        ++input)
728
    {
729
        each_rival(graph, input, [&](link_t other) NOEXCEPT
143 ✔
730
        {
731
            if (is_present(cell(graph, owner(graph, other))))
85 ✔
732
                out = true;
78 ✔
733
        });
85 ✔
734
    }
735

736
    return out;
143 ✔
737
}
738

739
constexpr bool algorithm::descended(const graph_t& graph, link_t row) NOEXCEPT
14 ✔
740
{
741
    const auto slice = edges(graph, row);
14 ✔
742
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
24 ✔
743
    {
744
        const auto up = parent(graph, input);
14 ✔
745
        if (up != none && is_labeled(cell(graph, up)))
14 ✔
746
            return true;
747
    }
748

749
    return false;
750
}
751

752
constexpr bool algorithm::confirmable(const graph_t& graph,
5 ✔
753
    link_t row) NOEXCEPT
754
{
755
    auto out = true;
5 ✔
756
    const auto slice = edges(graph, row);
5 ✔
757
    for (auto input = slice.first; out && input < slice.first + slice.count;
13 ✔
758
        ++input)
759
    {
760
        const auto up = parent(graph, input);
8 ✔
761
        if (up != none && cell(graph, up) == absent)
8 ✔
762
            return false;
763

764
        each_rival(graph, input, [&](link_t other) NOEXCEPT
8 ✔
765
        {
766
            if (is_boundary(cell(graph, owner(graph, other))))
6 ✔
767
                out = false;
1 ✔
768
        });
6 ✔
769
    }
770

771
    return out;
772
}
773

774
constexpr size_t algorithm::index(const component_t& component,
275 ✔
775
    link_t row) NOEXCEPT
776
{
777
    for (size_t member{}; member < component.count; ++member)
794 ✔
778
        if (component.rows[member] == row)
660 ✔
779
            return member;
780

781
    return maximum_members;
782
}
783

784
constexpr algorithm::mask_t algorithm::losers(const component_t& component,
9 ✔
785
    size_t index) NOEXCEPT
786
{
787
    mask_t out{};
788
    for (size_t contest{}; contest < component.contests; ++contest)
27 ✔
789
    {
790
        const auto clique = component.cliques[contest];
18 ✔
791
        const auto size = ones_count(clique);
18 ✔
792
        const auto digit = index % size;
18 ✔
793
        index /= size;
18 ✔
794

795
        // The winner is the clique's digit-th set bit.
796
        auto winner = clique;
797
        for (size_t cleared{}; cleared < digit; ++cleared)
26 ✔
798
            winner &= sub1(winner);
8 ✔
799

800
        winner &= (mask_t{} - winner);
18 ✔
801
        out |= clique & ~winner;
18 ✔
802
    }
803

804
    return out;
9 ✔
805
}
806

807
inline void algorithm::restore(std::vector<link_t>& out,
6 ✔
808
    const columns_t& columns, link_t row) NOEXCEPT
809
{
810
    const graph_t graph{ columns };
6 ✔
811
    if (columns.cells[row] != absent || !confirmable(graph, row))
6 ✔
812
        return;
2 ✔
813

814
    columns.cells[row] = unlabeled;
4 ✔
815
    out.push_back(row);
4 ✔
816
    each_child(graph, row, [&](link_t down) NOEXCEPT
4 ✔
817
    {
818
        restore(out, columns, down);
2 ✔
819
    });
2 ✔
820
}
821

822
inline void algorithm::reform(const columns_t& columns,
9 ✔
823
    const std::span<const link_t>& rows) NOEXCEPT
824
{
825
    const graph_t graph{ columns };
9 ✔
826
    for (const auto row: rows)
24 ✔
827
        if (is_present(columns.cells[row]))
15 ✔
828
            columns.cells[row] = unlabeled;
15 ✔
829

830
    // Relevance closes over the rows in at most count passes.
831
    for (auto changed = true; changed;)
21 ✔
832
    {
833
        changed = false;
834
        for (const auto row: rows)
33 ✔
835
        {
836
            if (!is_present(columns.cells[row]) ||
21 ✔
837
                is_labeled(columns.cells[row]))
21 ✔
838
                continue;
6 ✔
839

840
            if (contested(graph, row) || descended(graph, row))
15 ✔
841
            {
842
                columns.cells[row] = row;
6 ✔
843
                changed = true;
844
            }
845
        }
846
    }
847

848
    for (const auto row: rows)
24 ✔
849
    {
850
        // A row relabeled by a previous component carries a lower label.
851
        if (columns.cells[row] != row)
15 ✔
852
            continue;
12 ✔
853

854
        component_t component{};
3 ✔
855
        if (collect(component, graph, row))
3 ✔
856
            relabel(columns, component);
3 ✔
857
    }
858
}
9 ✔
859

860
template <typename Word>
861
INLINE constexpr Word algorithm::fees(Word outcomes,
862
    const component_t& component, const graph_t& graph) NOEXCEPT
863
{
864
    constexpr auto lane = bits<mask_t>;
865
    auto sum = f::broadcast<Word>(mask_t{});
866
    for (size_t member{}; member < component.count; ++member)
24 ✔
867
    {
868
        const auto bit = f::broadcast<Word>(set_right(mask_t{}, member));
20 ✔
869
        const auto fee = f::broadcast<Word>(totals(graph,
20 ✔
870
            component.rows[member]).fee);
20 ✔
871
        const auto selected = f::eq<lane>(f::and_(outcomes, bit), bit);
872
        sum = f::add<lane>(sum, f::and_(selected, fee));
873
    }
874

875
    return sum;
876
}
877

878
template <typename Word, typename Integral>
879
INLINE constexpr Word algorithm::load(const Integral* at) NOEXCEPT
880
{
881
    if constexpr (is_extended<Word>)
882
        return f::load<Word>(pointer_cast<const uint8_t>(at));
883
    else
884
        return *at;
885
}
886

887
template <typename Word, typename Integral>
888
INLINE constexpr void algorithm::store(Integral* at, Word word) NOEXCEPT
889
{
890
    if constexpr (is_extended<Word>)
891
        f::store(pointer_cast<uint8_t>(at), word);
892
    else
893
        *at = word;
894
}
895

896
BC_POP_WARNING()
897
BC_POP_WARNING()
898
BC_POP_WARNING()
899
BC_POP_WARNING()
900

901
} // namespace pool
902
} // namespace system
903
} // namespace libbitcoin
904

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