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

libbitcoin / libbitcoin-system / 46197992762

28 Nov 2026 10:42PM UTC coverage: 96.418% (+0.005%) from 96.413%
46197992762

push

github

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

Fix performance test chain weight field.

29500 of 30596 relevant lines covered (96.42%)

3605327.83 hits per line

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

96.73
/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 <ranges>
23
#include <span>
24
#include <unordered_set>
25
#include <bitcoin/system/data/data.hpp>
26
#include <bitcoin/system/define.hpp>
27
#include <bitcoin/system/endian/endian.hpp>
28
#include <bitcoin/system/intrinsics/intrinsics.hpp>
29
#include <bitcoin/system/math/math.hpp>
30

31
namespace libbitcoin {
32
namespace system {
33
namespace pool {
34

35
BC_PUSH_WARNING(NO_USE_OF_SPAN)
36
BC_PUSH_WARNING(NO_ARRAY_INDEXING)
37
BC_PUSH_WARNING(NO_DYNAMIC_ARRAY_INDEXING)
38
BC_PUSH_WARNING(NO_POINTER_ARITHMETIC)
39

40
// Cells.
41
// ----------------------------------------------------------------------------
42

43
constexpr bool algorithm::is_present(link_t cell) NOEXCEPT
832,140 ✔
44
{
45
    return cell < absent;
832,140 ✔
46
}
47

48
constexpr bool algorithm::is_labeled(link_t cell) NOEXCEPT
450,526 ✔
49
{
50
    return cell < unlabeled;
450,526 ✔
51
}
52

53
constexpr bool algorithm::is_boundary(link_t cell) NOEXCEPT
338,569 ✔
54
{
55
    return cell == boundary;
338,569 ✔
56
}
57

58
constexpr bool algorithm::is_absent(link_t cell) NOEXCEPT
446,747 ✔
59
{
60
    return cell >= absent && cell < boundary;
446,747 ✔
61
}
62

63
constexpr algorithm::link_t algorithm::raised(link_t cell,
21,554 ✔
64
    link_t cause) NOEXCEPT
65
{
66
    return bit_or(is_absent(cell) ? cell : absent, cause);
21,554 ✔
67
}
68

69
constexpr algorithm::link_t algorithm::lowered(link_t cell,
78,748 ✔
70
    link_t cause) NOEXCEPT
71
{
72
    if (!is_absent(cell))
78,748 ✔
73
        return cell;
74

75
    const auto left = bit_and(cell, bit_not(cause));
22,174 ✔
76
    return left == absent ? unlabeled : left;
22,174 ✔
77
}
78

79
// Sigop cost at bytes per sigop is weight.
80
constexpr uint64_t algorithm::chargeable(const totals_t& totals,
1,060 ✔
81
    uint32_t bytes_per_sigop) NOEXCEPT
82
{
83
    const auto sigop = ceilinged_multiply<uint64_t>(totals.sigops, bytes_per_sigop);
1,060 ✔
84
    return greater(totals.weight, sigop);
1,060 ✔
85
}
86

87
constexpr uint64_t algorithm::chargeable(mask_t outcome,
142 ✔
88
    const component_t& component, const graph_t& graph,
89
    uint32_t bytes_per_sigop) NOEXCEPT
90
{
91
    uint64_t bytes{};
92
    for (size_t member{}; member < component.count; ++member)
852 ✔
93
    {
94
        if (!get_right(outcome, member))
710 ✔
95
            continue;
420 ✔
96

97
        const auto row = totals(graph, component.rows[member]);
290 ✔
98
        bytes = ceilinged_add(bytes, chargeable(row, bytes_per_sigop));
290 ✔
99
    }
100

101
    return bytes;
142 ✔
102
}
103

104
constexpr uint64_t algorithm::fee_rate(const totals_t& totals,
3 ✔
105
    uint32_t bytes_per_sigop) NOEXCEPT
106
{
107
    return fee_rate(totals.fee, chargeable(totals, bytes_per_sigop));
3 ✔
108
}
109

110
constexpr uint64_t algorithm::fee_rate(uint64_t fee, uint64_t weight) NOEXCEPT
254 ✔
111
{
112
    return ceilinged_multiply(fee, per_weight) / greater(weight, 1_u64);
254 ✔
113
}
114

115
// Column values.
116
// ----------------------------------------------------------------------------
117

118
constexpr algorithm::link_t algorithm::to_column(link_t value) NOEXCEPT
222,550 ✔
119
{
120
    return native_to_little_end(value);
222,550 ✔
121
}
122

123
constexpr algorithm::edges_t algorithm::to_column(
3,313 ✔
124
    const edges_t& value) NOEXCEPT
125
{
126
    return { to_column(value.first), to_column(value.count) };
3,313 ✔
127
}
128

129
constexpr algorithm::totals_t algorithm::to_column(
3,312 ✔
130
    const totals_t& value) NOEXCEPT
131
{
132
    return
133
    {
134
        native_to_little_end(value.fee),
3,312 ✔
135
        native_to_little_end(value.weight),
3,312 ✔
136
        native_to_little_end(value.sigops)
3,312 ✔
137
    };
3,312 ✔
138
}
139

140
constexpr algorithm::link_t algorithm::from_column(link_t value) NOEXCEPT
9,947,734 ✔
141
{
142
    return native_from_little_end(value);
9,947,734 ✔
143
}
144

145
constexpr algorithm::edges_t algorithm::from_column(
2,310,123 ✔
146
    const edges_t& value) NOEXCEPT
147
{
148
    return { from_column(value.first), from_column(value.count) };
2,310,123 ✔
149
}
150

151
constexpr algorithm::totals_t algorithm::from_column(
1,816 ✔
152
    const totals_t& value) NOEXCEPT
153
{
154
    return
155
    {
156
        native_from_little_end(value.fee),
1,816 ✔
157
        native_from_little_end(value.weight),
1,816 ✔
158
        native_from_little_end(value.sigops)
1,816 ✔
159
    };
1,816 ✔
160
}
161

162
// Candidates.
163
// ----------------------------------------------------------------------------
164

165
constexpr void algorithm::initialize(const graph_t& graph,
3 ✔
166
    const std::span<const link_t>& candidates) NOEXCEPT
167
{
168
    for (const auto row: candidates)
6 ✔
169
        set_cell(graph, row, unlabeled);
3 ✔
170

171
    // Relevance closes over the candidates in at most count passes.
172
    for (auto changed = true; changed;)
8 ✔
173
    {
174
        changed = false;
175
        for (const auto row: candidates)
10 ✔
176
        {
177
            if (is_labeled(cell(graph, row)))
5 ✔
178
                continue;
2 ✔
179

180
            if (contested(graph, row) || descended(graph, row))
3 ✔
181
            {
182
                set_cell(graph, row, row);
2 ✔
183
                changed = true;
184
            }
185
        }
186
    }
187
}
3 ✔
188

189
// Components (frontier).
190
// ----------------------------------------------------------------------------
191

192
constexpr bool algorithm::collect(component_t& out, const graph_t& graph,
2,223 ✔
193
    link_t row) NOEXCEPT
194
{
195
    out = {};
2,223 ✔
196
    std_array<link_t, maximum_members> sorted{};
2,223 ✔
197
    const auto add = [&](link_t found) NOEXCEPT
97,914 ✔
198
    {
199
        const auto end = std::next(sorted.begin(), out.count);
97,914 ✔
200
        const auto at = std::lower_bound(sorted.begin(), end, found);
97,914 ✔
201
        if (at != end && *at == found)
97,914 ✔
202
            return true;
203

204
        if (out.count == maximum_members)
52,642 ✔
205
            return false;
206

207
        std::move_backward(at, end, std::next(end));
52,065 ✔
208
        *at = found;
52,065 ✔
209
        out.rows[out.count++] = found;
52,065 ✔
210
        return true;
52,065 ✔
211
    };
2,223 ✔
212

213
    if (!add(row))
2,223 ✔
214
        return false;
215

216
    for (size_t at{}; at < out.count; ++at)
39,583 ✔
217
        if (!each_member(graph, out.rows[at], add))
37,937 ✔
218
            return false;
219

220
    out.rows = sorted;
1,646 ✔
221
    return true;
1,646 ✔
222
}
223

224
constexpr bool algorithm::relate(component_t& out,
37 ✔
225
    const graph_t& graph) NOEXCEPT
226
{
227
    const auto first = out.rows.begin();
37 ✔
228
    const auto last = std::next(first, out.count);
37 ✔
229
    const auto locate = [&](link_t row) NOEXCEPT
374 ✔
230
    {
231
        const auto at = std::lower_bound(first, last, row);
374 ✔
232
        return at != last && *at == row ?
374 ✔
233
            to_unsigned(std::distance(first, at)) : maximum_members;
225 ✔
234
    };
37 ✔
235

236
    out.contests = zero;
37 ✔
237
    for (size_t member{}; member < out.count; ++member)
222 ✔
238
    {
239
        mask_t direct{};
185 ✔
240
        const auto row = out.rows[member];
185 ✔
241
        const auto slice = edges(graph, row);
185 ✔
242
        for (auto input = slice.first; input < slice.first + slice.count;
407 ✔
243
            ++input)
244
        {
245
            const auto up = locate(parent(graph, input));
222 ✔
246
            if (up != maximum_members)
222 ✔
247
                set_right_into(direct, up);
73 ✔
248

249
            auto clique = set_right(mask_t{}, member);
222 ✔
250
            each_rival(graph, input, [&](link_t other) NOEXCEPT
222 ✔
251
            {
252
                const auto rival_member = locate(owner(graph, other));
152 ✔
253
                if (rival_member != maximum_members)
152 ✔
254
                    set_right_into(clique, rival_member);
152 ✔
255
            });
152 ✔
256

257
            if (is_one(ones_count(clique)))
222 ✔
258
                continue;
148 ✔
259

260
            const auto end = std::next(out.cliques.begin(), out.contests);
149 ✔
261
            if (std::find(out.cliques.begin(), end, clique) != end)
149 ✔
262
                continue;
75 ✔
263

264
            if (out.contests == maximum_members)
74 ✔
265
                return false;
×
266

267
            out.cliques[out.contests++] = clique;
74 ✔
268
        }
269

270
        out.ancestors[member] = direct;
185 ✔
271
    }
272

273
    // Ancestry closes within the component in at most count passes.
274
    for (auto changed = true; changed;)
74 ✔
275
    {
276
        changed = false;
277
        for (size_t member{}; member < out.count; ++member)
222 ✔
278
        {
279
            auto ancestors = out.ancestors[member];
185 ✔
280
            for (auto ups = ancestors; is_nonzero(ups); ups &= sub1(ups))
258 ✔
281
                ancestors |= out.ancestors[right_zeros(ups)];
73 ✔
282

283
            if (ancestors != out.ancestors[member])
185 ✔
284
            {
285
                out.ancestors[member] = ancestors;
×
286
                changed = true;
287
            }
288
        }
289
    }
290

291
    return true;
292
}
293

294
constexpr algorithm::mask_t algorithm::members(const component_t& component,
1 ✔
295
    const std::span<const link_t>& rows) NOEXCEPT
296
{
297
    mask_t out{};
1 ✔
298
    for (const auto row: rows)
4 ✔
299
    {
300
        const auto member = index(component, row);
3 ✔
301
        if (member != maximum_members)
3 ✔
302
            set_right_into(out, member);
2 ✔
303
    }
304

305
    return out;
1 ✔
306
}
307

308
inline bool algorithm::ancestry(totals_t& out, const graph_t& graph,
4 ✔
309
    const component_t& component, const totals_t& limit) NOEXCEPT
310
{
311
    out = {};
4 ✔
312
    std::vector<link_t> visited{};
4 ✔
313
    std::vector<link_t> pending{};
4 ✔
314
    const auto push = [&](link_t row) NOEXCEPT
7 ✔
315
    {
316
        if (index(component, row) != maximum_members ||
7 ✔
317
            std::ranges::find(visited, row) != visited.end())
1 ✔
318
            return;
6 ✔
319

320
        visited.push_back(row);
1 ✔
321
        pending.push_back(row);
1 ✔
322
    };
4 ✔
323

324
    const auto charge = [&](link_t row) NOEXCEPT
18 ✔
325
    {
326
        const auto& add = totals(graph, row);
18 ✔
327
        out.fee = ceilinged_add(out.fee, add.fee);
18 ✔
328
        out.weight = ceilinged_add(out.weight, add.weight);
18 ✔
329
        out.sigops = ceilinged_add(out.sigops, add.sigops);
18 ✔
330

331
        const auto slice = edges(graph, row);
18 ✔
332
        for (auto input = slice.first; input < slice.first + slice.count;
39 ✔
333
            ++input)
334
        {
335
            const auto up = parent(graph, input);
21 ✔
336
            if (up != none && is_present(cell(graph, up)))
21 ✔
337
                push(up);
7 ✔
338
        }
339

340
        return out.weight <= limit.weight && out.sigops <= limit.sigops;
18 ✔
341
    };
4 ✔
342

343
    for (size_t member{}; member < component.count; ++member)
19 ✔
344
        if (!charge(component.rows[member]))
17 ✔
345
            return false;
346

347
    while (!pending.empty())
3 ✔
348
    {
349
        const auto row = pending.back();
1 ✔
350
        pending.pop_back();
1 ✔
351
        if (!charge(row))
1 ✔
352
            return false;
353
    }
354

355
    return true;
356
}
4 ✔
357

358
template <typename Word>
359
inline bool algorithm::family(uint64_t& fee, uint64_t& bytes,
4 ✔
360
    const graph_t& graph, const std::span<const link_t>& rows,
361
    uint64_t limit, uint64_t rate, uint32_t bytes_per_sigop,
362
    size_t maximum_outcomes) NOEXCEPT
363
{
364
    fee = 0_u64;
4 ✔
365
    bytes = 0_u64;
4 ✔
366
    std::vector<link_t> found{};
4 ✔
367
    std::unordered_set<link_t> visited{};
4 ✔
368
    const auto push = [&](link_t row) NOEXCEPT
53 ✔
369
    {
370
        if (row == none || !is_present(cell(graph, row)) ||
49 ✔
371
            !visited.insert(row).second)
34 ✔
372
            return true;
29 ✔
373

374
        found.push_back(row);
20 ✔
375
        const auto size = chargeable(totals(graph, row), bytes_per_sigop);
20 ✔
376
        bytes = ceilinged_add(bytes, size);
20 ✔
377
        return bytes <= limit;
20 ✔
378
    };
379

380
    for (const auto row: rows)
8 ✔
381
        if (!push(row))
4 ✔
382
            return false;
×
383

384
    for (size_t at{}; at < found.size(); ++at)
21 ✔
385
    {
386
        auto within = true;
18 ✔
387
        const auto row = found[at];
18 ✔
388
        const auto slice = edges(graph, row);
18 ✔
389
        for (auto input = slice.first; input < slice.first + slice.count;
40 ✔
390
            ++input)
391
        {
392
            within = within && push(parent(graph, input));
22 ✔
393
            each_rival(graph, input, [&](link_t other) NOEXCEPT
22 ✔
394
            {
395
                within = within && push(owner(graph, other));
15 ✔
396
            });
397
        }
398

399
        each_child(graph, row, [&](link_t down) NOEXCEPT
18 ✔
400
        {
401
            within = within && push(down);
8 ✔
402
        });
403

404
        if (!within)
18 ✔
405
            return false;
1 ✔
406
    }
407

408
    // Each component pays its best outcome, then each other row its fee.
409
    std::vector<mask_t> masks{};
3 ✔
410
    std::unordered_set<link_t> resolved{};
3 ✔
411
    for (const auto row: found)
13 ✔
412
    {
413
        if (!is_labeled(cell(graph, row)) || resolved.contains(row))
11 ✔
414
            continue;
8 ✔
415

416
        component_t component{};
3 ✔
417
        if (!collect(component, graph, row) || !relate(component, graph))
3 ✔
418
            return false;
1 ✔
419

420
        leaves(masks, component, graph, maximum_outcomes, rate,
3 ✔
421
            bytes_per_sigop);
422
        if (masks.empty())
3 ✔
423
            return false;
424

425
        fee = ceilinged_add(fee, evaluate<Word>(masks, component, graph));
2 ✔
426
        for (size_t member{}; member < component.count; ++member)
12 ✔
427
            resolved.insert(component.rows[member]);
10 ✔
428
    }
429

430
    for (const auto row: found)
12 ✔
431
        if (!resolved.contains(row))
10 ✔
432
            fee = ceilinged_add(fee, totals(graph, row).fee);
×
433

434
    return true;
2 ✔
435
}
4 ✔
436

437
// Outcomes.
438
// ----------------------------------------------------------------------------
439

440
constexpr size_t algorithm::outcomes(const component_t& component) NOEXCEPT
3 ✔
441
{
442
    size_t out{ one };
443
    for (size_t contest{}; contest < component.contests; ++contest)
9 ✔
444
        out = ceilinged_multiply(out, ones_count(component.cliques[contest]));
6 ✔
445

446
    return out;
3 ✔
447
}
448

449
constexpr algorithm::mask_t algorithm::outcome(const component_t& component,
230 ✔
450
    size_t index) NOEXCEPT
451
{
452
    const auto lost = losers(component, index);
230 ✔
453
    auto out = bit_and(unmask_right<mask_t>(component.count), bit_not(lost));
230 ✔
454
    for (size_t member{}; member < component.count; ++member)
1,380 ✔
455
        if (is_nonzero(bit_and(component.ancestors[member], lost)))
1,150 ✔
456
            out = set_right(out, member, false);
281 ✔
457

458
    return out;
230 ✔
459
}
460

461
template <typename Word>
462
constexpr uint64_t algorithm::evaluate(const std::span<const mask_t>& outcomes,
9 ✔
463
    const component_t& component, const graph_t& graph) NOEXCEPT
464
{
465
    uint64_t best{};
9 ✔
466
    each_fee<Word>(zero, outcomes, component, graph,
9 ✔
467
        [&](size_t, uint64_t fee) NOEXCEPT
157 ✔
468
        {
469
            best = std::max(best, fee);
148 ✔
470
        });
471

472
    return best;
9 ✔
473
}
474

475
template <typename Word>
476
constexpr void algorithm::range(size_t& least, size_t& greatest,
5 ✔
477
    const std::span<const mask_t>& outcomes, const component_t& component,
478
    const weights_t& weights) NOEXCEPT
479
{
480
    least = zero;
5 ✔
481
    greatest = zero;
5 ✔
482
    auto low = max_int64;
5 ✔
483
    auto high = min_int64;
5 ✔
484
    each_sum<Word>(zero, outcomes, weights, component.count,
5 ✔
485
        [&](size_t index, uint64_t sum) NOEXCEPT
151 ✔
486
        {
487
            const auto value = to_signed(sum);
73 ✔
488
            if (value < low)
73 ✔
489
            {
490
                low = value;
12 ✔
491
                least = index;
12 ✔
492
            }
493

494
            if (value > high)
73 ✔
495
            {
496
                high = value;
4 ✔
497
                greatest = index;
4 ✔
498
            }
499
        });
500
}
5 ✔
501

502
inline void algorithm::leaves(std::vector<mask_t>& out,
15 ✔
503
    const component_t& component, const graph_t& graph, size_t limit,
504
    uint64_t rate, uint32_t bytes_per_sigop) NOEXCEPT
505
{
506
    out.clear();
15 ✔
507
    if (is_zero(limit))
15 ✔
508
        return;
509

510
    const auto all = unmask_right<mask_t>(component.count);
12 ✔
511
    const auto contests = component.contests;
12 ✔
512
    if (is_zero(contests))
12 ✔
513
    {
514
        out.push_back(all);
×
515
        return;
×
516
    }
517

518
    std_array<mask_t, maximum_members> below{};
12 ✔
519
    for (size_t member{}; member < component.count; ++member)
72 ✔
520
        for (size_t up{}; up < component.count; ++up)
360 ✔
521
            if (get_right(component.ancestors[member], up))
300 ✔
522
                set_right_into(below[up], member);
25 ✔
523

524
    // Losers and their descendants join the dead.
525
    const auto kill = [&](mask_t dead, mask_t losers) NOEXCEPT
63 ✔
526
    {
527
        dead |= losers;
63 ✔
528
        for (; is_nonzero(losers); losers &= sub1(losers))
126 ✔
529
            dead |= below[right_zeros(losers)];
63 ✔
530

531
        return dead;
63 ✔
532
    };
12 ✔
533

534
    // Dead winners of a clique yield one outcome, so only the first is tried.
535
    std_array<mask_t, add1(maximum_members)> deads{};
12 ✔
536
    std_array<mask_t, add1(maximum_members)> rests{};
12 ✔
537
    std_array<bool, add1(maximum_members)> spent{};
12 ✔
538
    size_t level{};
539
    rests[level] = component.cliques[level];
12 ✔
540
    while (true)
541
    {
542
        if (level == contests)
130 ✔
543
        {
544
            if (out.size() == limit)
41 ✔
545
            {
546
                out.push_back(greedy(component, graph, rate, bytes_per_sigop));
4 ✔
547
                return;
4 ✔
548
            }
549

550
            out.push_back(bit_and(all, bit_not(deads[level])));
37 ✔
551
            --level;
37 ✔
552
            continue;
37 ✔
553
        }
554

555
        const auto clique = component.cliques[level];
89 ✔
556
        auto& rest = rests[level];
89 ✔
557
        auto descend = false;
558
        while (!descend && is_nonzero(rest))
153 ✔
559
        {
560
            const auto winner = bit_and(rest, twos_complement(rest));
64 ✔
561
            rest &= sub1(rest);
64 ✔
562
            if (is_nonzero(bit_and(winner, deads[level])))
64 ✔
563
            {
564
                if (spent[level])
13 ✔
565
                    continue;
1 ✔
566

567
                spent[level] = true;
12 ✔
568
            }
569

570
            deads[add1(level)] = kill(deads[level],
63 ✔
571
                bit_and(clique, bit_not(winner)));
572
            descend = true;
573
        }
574

575
        if (descend)
89 ✔
576
        {
577
            ++level;
63 ✔
578
            if (level < contests)
63 ✔
579
            {
580
                rests[level] = component.cliques[level];
22 ✔
581
                spent[level] = false;
22 ✔
582
            }
583

584
            continue;
63 ✔
585
        }
586

587
        if (is_zero(level))
26 ✔
588
            return;
589

590
        --level;
18 ✔
591
    }
592
}
593

594
constexpr uint64_t algorithm::chargeable(const component_t& component,
2 ✔
595
    const graph_t& graph, uint32_t bytes_per_sigop) NOEXCEPT
596
{
597
    uint64_t bytes{};
598
    for (size_t member{}; member < component.count; ++member)
12 ✔
599
    {
600
        const auto row = totals(graph, component.rows[member]);
10 ✔
601
        bytes = ceilinged_add(bytes, chargeable(row, bytes_per_sigop));
10 ✔
602
    }
603

604
    return bytes;
2 ✔
605
}
606

607
constexpr bool algorithm::covered(uint64_t fee, uint64_t weight,
4 ✔
608
    uint64_t rate) NOEXCEPT
609
{
610
    return ceilinged_multiply(fee, per_weight) >=
4 ✔
611
        ceilinged_multiply(rate, weight);
4 ✔
612
}
613

614
// Transitions (frontier, table columns).
615
// ----------------------------------------------------------------------------
616

617
constexpr void algorithm::relabel(const columns_t& columns,
645 ✔
618
    const component_t& component) NOEXCEPT
619
{
620
    // Rows are sorted by collect, so the label is the first.
621
    const auto label = to_column(component.rows.front());
645 ✔
622
    for (size_t member{}; member < component.count; ++member)
7,493 ✔
623
        columns.cells[component.rows[member]] = label;
6,848 ✔
624
}
645 ✔
625

626
inline bool algorithm::admit(const columns_t& columns, link_t row,
3 ✔
627
    bool conflict) NOEXCEPT
628
{
629
    walk_t walk{};
3 ✔
630
    const graph_t graph{ columns };
3 ✔
631
    set_cell(graph, row, conflict ? bit_or(absent, conflicted) : unlabeled);
3 ✔
632

633
    // Its children are the rows adopted by it.
634
    walk.affected.push_back(row);
3 ✔
635
    walk.pending.push_back(row);
3 ✔
636
    each_child(graph, row, [&](link_t down) NOEXCEPT
3 ✔
637
    {
638
        walk.pending.push_back(down);
1 ✔
639
    });
1 ✔
640

641
    return spread(walk, columns);
6 ✔
642
}
3 ✔
643

644
inline bool algorithm::confirm(const columns_t& columns,
172 ✔
645
    const std::span<const link_t>& rows,
646
    const std::span<const link_t>& conflicts) NOEXCEPT
647
{
648
    walk_t walk{};
172 ✔
649
    const graph_t graph{ columns };
172 ✔
650
    for (const auto row: rows)
1,464 ✔
651
        change(walk, columns, row, boundary);
1,292 ✔
652

653
    for (const auto row: conflicts)
818 ✔
654
        if (!is_boundary(cell(graph, row)))
646 ✔
655
            change(walk, columns, row, raised(cell(graph, row), conflicted));
524 ✔
656

657
    return spread(walk, columns);
344 ✔
658
}
172 ✔
659

660
inline bool algorithm::unconfirm(const columns_t& columns,
90 ✔
661
    const std::span<const link_t>& rows,
662
    const std::span<const link_t>& conflicts) NOEXCEPT
663
{
664
    walk_t walk{};
90 ✔
665
    const graph_t graph{ columns };
90 ✔
666
    for (const auto row: rows)
755 ✔
667
    {
668
        if (!is_boundary(cell(graph, row)))
665 ✔
669
            continue;
8 ✔
670

671
        change(walk, columns, row, unlabeled);
657 ✔
672
        walk.pending.push_back(row);
657 ✔
673
    }
674

675
    for (const auto row: conflicts)
423 ✔
676
        change(walk, columns, row, lowered(cell(graph, row), conflicted));
333 ✔
677

678
    return spread(walk, columns);
180 ✔
679
}
90 ✔
680

681
inline void algorithm::rivals(std::vector<link_t>& out,
4 ✔
682
    const columns_t& columns, const std::span<const link_t>& rows) NOEXCEPT
683
{
684
    const graph_t graph{ columns };
4 ✔
685
    for (const auto row: rows)
10 ✔
686
    {
687
        const auto slice = edges(graph, row);
6 ✔
688
        for (auto input = slice.first; input < slice.first + slice.count;
13 ✔
689
            ++input)
690
        {
691
            each_rival(graph, input, [&](link_t other) NOEXCEPT
7 ✔
692
            {
693
                out.push_back(owner(graph, other));
5 ✔
694
            });
5 ✔
695
        }
696
    }
697
}
4 ✔
698

699
// Template (frontier, table columns).
700
// ----------------------------------------------------------------------------
701

702
inline void algorithm::owners(std::vector<link_t>& out,
3 ✔
703
    const columns_t& columns) NOEXCEPT
704
{
705
    out.resize(columns.rows.size());
3 ✔
706
    for (size_t row{}; row < columns.edges.size(); ++row)
16 ✔
707
    {
708
        const auto slice = from_column(columns.edges[row]);
13 ✔
709
        const auto first = std::min<size_t>(slice.first, out.size());
13 ✔
710
        const auto last = std::min(first + slice.count, out.size());
13 ✔
711
        std::fill(std::next(out.begin(), first), std::next(out.begin(), last),
26 ✔
712
            possible_narrow_cast<link_t>(row));
26 ✔
713
    }
714
}
3 ✔
715

716
template <typename Word>
717
inline void algorithm::resolve(std::span<mark> marks,
9 ✔
718
    const columns_t& columns, size_t maximum_outcomes, uint64_t rate,
719
    uint32_t bytes_per_sigop) NOEXCEPT
720
{
721
    constexpr auto policy = execution::par;
9 ✔
722
    const graph_t graph{ columns };
9 ✔
723
    const auto count = columns.cells.size();
9 ✔
724
    std::transform(policy, columns.cells.begin(), columns.cells.end(),
9 ✔
725
        marks.begin(), [](link_t value) NOEXCEPT
43 ✔
726
        {
727
            return is_present(from_column(value)) ? mark::unmarked :
43 ✔
728
                mark::excluded;
43 ✔
729
        });
730

731
    // Components and the descendants they discard are disjoint in rows.
732
    const auto workers = execution::thread_pool::get().size();
9 ✔
733
    const auto width = std::max(ceilinged_divide(count, workers), one);
9 ✔
734
    std::vector<size_t> ranges(ceilinged_divide(count, width));
9 ✔
735
    std::iota(ranges.begin(), ranges.end(), zero);
9 ✔
736
    std::for_each(policy, ranges.cbegin(), ranges.cend(),
9 ✔
737
        [&](size_t range) NOEXCEPT
27 ✔
738
        {
739
            std::vector<mask_t> masks{};
27 ✔
740
            std::vector<link_t> pending{};
27 ✔
741
            const auto first = range * width;
27 ✔
742
            const auto last = std::min(first + width, count);
27 ✔
743
            for (auto at = first; at < last; ++at)
70 ✔
744
            {
745
                // A component is resolved at its label, its lowest member row.
746
                const auto row = possible_narrow_cast<link_t>(at);
43 ✔
747
                const auto label = cell(graph, row);
43 ✔
748
                if (!is_labeled(label) || label != row)
43 ✔
749
                    continue;
36 ✔
750

751
                component_t component{};
8 ✔
752
                const auto collected = collect(component, graph, row);
8 ✔
753
                const auto related = collected && relate(component, graph);
8 ✔
754
                const auto limit = related ? maximum_outcomes : zero;
8 ✔
755
                leaves(masks, component, graph, limit, rate, bytes_per_sigop);
8 ✔
756
                if (masks.empty())
8 ✔
757
                {
758
                    forfeit(pending, marks, graph, row);
1 ✔
759
                    continue;
1 ✔
760
                }
761

762
                const auto chosen = masks[best<Word>(masks, component, graph,
7 ✔
763
                    rate, bytes_per_sigop)];
764

765
                for (size_t member{}; member < component.count; ++member)
42 ✔
766
                    if (!get_right(chosen, member))
35 ✔
767
                        discard(pending, marks, graph,
14 ✔
768
                            component.rows[member]);
769
            }
770
        });
27 ✔
771
}
9 ✔
772

773
inline void algorithm::pack(std::vector<link_t>& out, std::span<mark> marks,
16 ✔
774
    const columns_t& columns, const totals_t& budget, size_t depth,
775
    uint32_t bytes_per_sigop) NOEXCEPT
776
{
777
    takes_t takes{};
16 ✔
778
    packing_t kept{};
16 ✔
779
    pack_(out, takes, kept, marks, columns, budget, depth, bytes_per_sigop);
16 ✔
780
}
16 ✔
781

782
inline void algorithm::project(std::vector<link_t>& out,
30 ✔
783
    projection_t& projection, const columns_t& columns,
784
    const totals_t& budget, size_t depth, uint32_t bytes_per_sigop) NOEXCEPT
785
{
786
    takes_t takes{};
30 ✔
787
    auto& marks = projection.marks;
30 ✔
788
    pack_(out, takes, projection.packing, marks, columns, budget, depth,
30 ✔
789
        bytes_per_sigop);
790

791
    const auto left = std::ranges::find(marks, mark::unmarked) != marks.end();
30 ✔
792
    projection.exhausted = takes.empty() || !left;
30 ✔
793
    if (!takes.empty())
30 ✔
794
        projection.blocks.push_back(std::move(takes));
28 ✔
795
}
30 ✔
796

797
// Estimate (projected blocks).
798
// ----------------------------------------------------------------------------
799

800
constexpr uint64_t algorithm::above(const std::span<const take_t>& takes,
3 ✔
801
    uint64_t rate) NOEXCEPT
802
{
803
    uint64_t size{};
804
    for (const auto& take: takes)
12 ✔
805
        if (take.rate >= rate)
9 ✔
806
            size = ceilinged_add(size, take.weight);
5 ✔
807

808
    return size;
3 ✔
809
}
810

811
template <typename Arrivals>
812
inline uint64_t algorithm::estimate(const projection_t& projection,
15 ✔
813
    size_t count, uint64_t space, uint64_t room,
814
    const Arrivals& arrivals) NOEXCEPT
815
{
816
    uint64_t low{};
817
    auto high = max_uint64;
818
    const auto sized = levels(projection, count);
15 ✔
819
    if (!reaches(projection, sized, count, space, room, high, arrivals))
15 ✔
820
        return high;
821

822
    // A rate that has room implies that each higher rate has.
823
    while (low < high)
780 ✔
824
    {
825
        const auto middle = low + to_half(high - low);
768 ✔
826
        if (reaches(projection, sized, count, space, room, middle, arrivals))
768 ✔
827
            high = middle;
828
        else
829
            low = add1(middle);
40 ✔
830
    }
831

832
    return low;
833
}
15 ✔
834

835
// Columns (whole table).
836
// ----------------------------------------------------------------------------
837

838
template <typename Word>
839
constexpr bool algorithm::collect(component_t& out, link_t label,
9 ✔
840
    const std::span<const link_t>& cells) NOEXCEPT
841
{
842
    out = {};
9 ✔
843
    return collect_<Word>(zero, out, label, cells);
9 ✔
844
}
845

846
template <typename Word>
847
inline void algorithm::find(std::vector<link_t>& out,
7 ✔
848
    const std::span<const link_t>& values, link_t value) NOEXCEPT
849
{
850
    out.clear();
7 ✔
851
    find_<Word>(zero, out, values, value);
7 ✔
852
}
7 ✔
853

854
// Internals.
855
// ----------------------------------------------------------------------------
856

857
constexpr size_t algorithm::rows(const graph_t& graph) NOEXCEPT
5,761,051 ✔
858
{
859
    return graph.table.cells.size();
5,761,051 ✔
860
}
861

862
constexpr algorithm::link_t algorithm::cell(const graph_t& graph,
1,499,241 ✔
863
    link_t row) NOEXCEPT
864
{
865
    const auto count = rows(graph);
1,499,241 ✔
866
    return from_column(row < count ? graph.table.cells[row] :
1,499,252 ✔
867
        graph.extra.cells[row - count]);
11 ✔
868
}
869

870
constexpr void algorithm::set_cell(const graph_t& graph, link_t row,
64,106 ✔
871
    link_t value) NOEXCEPT
872
{
873
    const auto count = rows(graph);
64,106 ✔
874
    auto& stored = row < count ? graph.table.cells[row] :
64,101 ✔
875
        graph.extra.cells[row - count];
64,106 ✔
876

877
    stored = to_column(value);
64,106 ✔
878
}
64,106 ✔
879

880
constexpr algorithm::edges_t algorithm::edges(const graph_t& graph,
2,310,110 ✔
881
    link_t row) NOEXCEPT
882
{
883
    const auto count = rows(graph);
2,310,110 ✔
884
    return from_column(row < count ? graph.table.edges[row] :
2,310,110 ✔
885
        graph.extra.edges[row - count]);
11 ✔
886
}
887

888
constexpr algorithm::totals_t algorithm::totals(const graph_t& graph,
1,816 ✔
889
    link_t row) NOEXCEPT
890
{
891
    const auto count = rows(graph);
1,816 ✔
892
    return from_column(row < count ? graph.table.totals[row] :
1,816 ✔
893
        graph.extra.totals[row - count]);
×
894
}
895

896
constexpr algorithm::link_t algorithm::child(const graph_t& graph,
41,629 ✔
897
    link_t row) NOEXCEPT
898
{
899
    const auto count = rows(graph);
41,629 ✔
900
    return from_column(row < count ? graph.table.children[row] :
41,633 ✔
901
        graph.extra.children[row - count]);
4 ✔
902
}
903

904
constexpr algorithm::link_t algorithm::parent(const graph_t& graph,
664,994 ✔
905
    link_t input) NOEXCEPT
906
{
907
    const auto count = graph.table.rows.size();
664,994 ✔
908
    return from_column(input < count ? graph.table.rows[input] :
665,002 ✔
909
        graph.extra.rows[input - count]);
8 ✔
910
}
911

912
constexpr algorithm::link_t algorithm::owner(const graph_t& graph,
214,842 ✔
913
    link_t input) NOEXCEPT
914
{
915
    const auto count = graph.table.rows.size();
214,842 ✔
916
    if (input < count && !graph.table.owners.empty())
214,842 ✔
917
        return graph.table.owners[input];
16 ✔
918

919
    // The last row whose first input is not beyond the input.
920
    const auto& edges = input < count ? graph.table.edges : graph.extra.edges;
214,826 ✔
921
    const auto after = std::upper_bound(edges.begin(), edges.end(), input,
214,826 ✔
922
        [](link_t at, const edges_t& edge) NOEXCEPT
1,764,475 ✔
923
        {
924
            return at < from_column(edge.first);
1,764,475 ✔
925
        });
926

927
    const auto base = input < count ? zero : rows(graph);
214,826 ✔
928
    const auto offset = std::distance(edges.begin(), std::prev(after));
214,826 ✔
929
    return possible_narrow_cast<link_t>(base + offset);
214,826 ✔
930
}
931

932
constexpr algorithm::link_t algorithm::sibling(const graph_t& graph,
57,756 ✔
933
    link_t input) NOEXCEPT
934
{
935
    const auto count = graph.table.rows.size();
57,756 ✔
936
    return from_column(input < count ? graph.table.siblings[input] :
57,756 ✔
937
        graph.extra.siblings[input - count]);
×
938
}
939

940
constexpr algorithm::link_t algorithm::rival(const graph_t& graph,
1,134,165 ✔
941
    link_t input) NOEXCEPT
942
{
943
    const auto count = graph.table.rows.size();
1,134,165 ✔
944
    return from_column(input < count ? graph.table.rivals[input] :
1,134,179 ✔
945
        graph.extra.rivals[input - count]);
14 ✔
946
}
947

948
template <typename Visitor>
949
constexpr void algorithm::each_child(const graph_t& graph, link_t row,
41,629 ✔
950
    const Visitor& visit) NOEXCEPT
951
{
952
    for (auto input = child(graph, row); input != none;
99,385 ✔
953
        input = sibling(graph, input))
57,756 ✔
954
        visit(owner(graph, input));
57,756 ✔
955

956
    if (row < rows(graph))
41,629 ✔
957
        for (const auto& bridge: graph.children)
41,633 ✔
958
            if (bridge.from == row)
8 ✔
959
                visit(owner(graph, bridge.to));
2 ✔
960
}
41,629 ✔
961

962
template <typename Visitor>
963
constexpr void algorithm::each_rival(const graph_t& graph, link_t input,
504,280 ✔
964
    const Visitor& visit) NOEXCEPT
965
{
966
    // A candidate input enters the ring of its table rival, which excludes it.
967
    const auto count = graph.table.rows.size();
504,280 ✔
968
    const auto next = rival(graph, input);
504,280 ✔
969
    const auto start = (input >= count && next < count) ? next : input;
504,280 ✔
970
    auto at = start;
971

972
    do
973
    {
974
        if (at != input)
629,885 ✔
975
            visit(at);
125,609 ✔
976

977
        // A table input bridges to the candidate inputs spending its point.
978
        if (at < count)
629,885 ✔
979
            for (const auto& bridge: graph.rivals)
629,918 ✔
980
                if (bridge.from == at && bridge.to != input)
38 ✔
981
                    visit(bridge.to);
6 ✔
982

983
        at = rival(graph, at);
629,885 ✔
984
    }
985
    while (at != none && at != start);
629,885 ✔
986
}
504,280 ✔
987

988
template <typename Visitor>
989
constexpr bool algorithm::each_member(const graph_t& graph, link_t row,
37,942 ✔
990
    const Visitor& visit) NOEXCEPT
991
{
992
    auto all = true;
37,942 ✔
993
    const auto slice = edges(graph, row);
37,942 ✔
994
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
119,754 ✔
995
    {
996
        const auto up = parent(graph, input);
81,812 ✔
997
        if (up != none && is_labeled(cell(graph, up)))
81,812 ✔
998
            all = all && visit(up);
33,925 ✔
999

1000
        each_rival(graph, input, [&](link_t other) NOEXCEPT
123,944 ✔
1001
        {
1002
            const auto rival_row = owner(graph, other);
21,066 ✔
1003
            if (is_present(cell(graph, rival_row)))
21,066 ✔
1004
                all = all && visit(rival_row);
18,667 ✔
1005
        });
1006
    }
1007

1008
    each_child(graph, row, [&](link_t down) NOEXCEPT
146,956 ✔
1009
    {
1010
        if (is_present(cell(graph, down)))
54,507 ✔
1011
            all = all && visit(down);
44,512 ✔
1012
    });
1013

1014
    return all;
37,942 ✔
1015
}
1016

1017
constexpr bool algorithm::contested(const graph_t& graph, link_t row) NOEXCEPT
5,253 ✔
1018
{
1019
    auto out = false;
5,253 ✔
1020
    const auto slice = edges(graph, row);
5,253 ✔
1021
    for (auto input = slice.first; !out && input < slice.first + slice.count;
14,864 ✔
1022
        ++input)
1023
    {
1024
        each_rival(graph, input, [&](link_t other) NOEXCEPT
9,611 ✔
1025
        {
1026
            if (is_present(cell(graph, owner(graph, other))))
2,229 ✔
1027
                out = true;
1,621 ✔
1028
        });
2,229 ✔
1029
    }
1030

1031
    return out;
5,253 ✔
1032
}
1033

1034
constexpr bool algorithm::descended(const graph_t& graph, link_t row) NOEXCEPT
4,139 ✔
1035
{
1036
    const auto slice = edges(graph, row);
4,139 ✔
1037
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
10,067 ✔
1038
    {
1039
        const auto up = parent(graph, input);
7,391 ✔
1040
        if (up != none && is_labeled(cell(graph, up)))
7,391 ✔
1041
            return true;
1042
    }
1043

1044
    return false;
1045
}
1046

1047
constexpr size_t algorithm::index(const component_t& component,
11 ✔
1048
    link_t row) NOEXCEPT
1049
{
1050
    for (size_t member{}; member < component.count; ++member)
31 ✔
1051
        if (component.rows[member] == row)
28 ✔
1052
            return member;
1053

1054
    return maximum_members;
1055
}
1056

1057
constexpr algorithm::mask_t algorithm::losers(const component_t& component,
231 ✔
1058
    size_t index) NOEXCEPT
1059
{
1060
    mask_t out{};
1061
    for (size_t contest{}; contest < component.contests; ++contest)
693 ✔
1062
    {
1063
        const auto clique = component.cliques[contest];
462 ✔
1064
        const auto size = ones_count(clique);
462 ✔
1065
        const auto digit = index % size;
462 ✔
1066
        index /= size;
462 ✔
1067

1068
        // The winner is the clique's digit-th set bit.
1069
        auto winner = clique;
1070
        for (size_t cleared{}; cleared < digit; ++cleared)
683 ✔
1071
            winner &= sub1(winner);
221 ✔
1072

1073
        winner &= twos_complement(winner);
462 ✔
1074
        out |= bit_and(clique, bit_not(winner));
462 ✔
1075
    }
1076

1077
    return out;
231 ✔
1078
}
1079

1080
template <typename Word>
1081
constexpr size_t algorithm::best(const std::span<const mask_t>& outcomes,
21 ✔
1082
    const component_t& component, const graph_t& graph, uint64_t rate,
1083
    uint32_t bytes_per_sigop) NOEXCEPT
1084
{
1085
    size_t out{};
21 ✔
1086
    uint64_t most{};
21 ✔
1087
    uint64_t least{};
21 ✔
1088
    auto first = true;
21 ✔
1089
    each_fee<Word>(zero, outcomes, component, graph,
21 ✔
1090
        [&](size_t index, uint64_t fee) NOEXCEPT
639 ✔
1091
        {
1092
            const auto size = is_zero(rate) ? 0_u64 : chargeable(
309 ✔
1093
                outcomes[index], component, graph, bytes_per_sigop);
1094

1095
            if (first || worth(fee, size, most, least, rate))
309 ✔
1096
            {
1097
                first = false;
41 ✔
1098
                most = fee;
41 ✔
1099
                least = size;
41 ✔
1100
                out = index;
41 ✔
1101
            }
1102
        });
1103

1104
    return out;
21 ✔
1105
}
1106

1107
constexpr algorithm::mask_t algorithm::greedy(const component_t& component,
6 ✔
1108
    const graph_t& graph, uint64_t rate, uint32_t bytes_per_sigop) NOEXCEPT
1109
{
1110
    struct value_t
1111
    {
1112
        uint64_t fee;
1113
        uint64_t bytes;
1114
        size_t member;
1115
    };
1116

1117
    std_array<value_t, maximum_members> values{};
6 ✔
1118
    for (size_t member{}; member < component.count; ++member)
35 ✔
1119
    {
1120
        const auto row = totals(graph, component.rows[member]);
29 ✔
1121
        values[member] = { row.fee, chargeable(row, bytes_per_sigop), member };
29 ✔
1122
    }
1123

1124
    const auto end = std::next(values.begin(), component.count);
6 ✔
1125
    std::sort(values.begin(), end, [rate](const value_t& left,
6 ✔
1126
        const value_t& right) NOEXCEPT
1127
    {
1128
        if (worth(left.fee, left.bytes, right.fee, right.bytes, rate))
51 ✔
1129
            return true;
1130

1131
        if (worth(right.fee, right.bytes, left.fee, left.bytes, rate))
24 ✔
1132
            return false;
1133

1134
        return left.member < right.member;
2 ✔
1135
    });
1136

1137
    mask_t won{};
1138
    mask_t lost{};
1139
    mask_t contested{};
1140
    for (size_t contest{}; contest < component.contests; ++contest)
18 ✔
1141
        contested |= component.cliques[contest];
12 ✔
1142

1143
    for (auto value = values.begin(); value != end; ++value)
35 ✔
1144
    {
1145
        const auto need = set_right(component.ancestors[value->member], value->member);
29 ✔
1146
        if (is_nonzero(bit_and(need, lost)))
29 ✔
1147
            continue;
12 ✔
1148

1149
        auto taken = true;
1150
        mask_t losers{};
1151
        for (size_t contest{}; taken && contest < component.contests;
51 ✔
1152
            ++contest)
1153
        {
1154
            const auto clique = component.cliques[contest];
34 ✔
1155
            const auto mine = bit_and(clique, need);
34 ✔
1156
            if (is_zero(mine))
34 ✔
1157
                continue;
16 ✔
1158

1159
            const auto others = bit_and(clique, bit_not(need));
18 ✔
1160
            taken = is_one(ones_count(mine)) && is_zero(bit_and(others, won));
18 ✔
1161
            losers |= others;
18 ✔
1162
        }
1163

1164
        if (!taken)
17 ✔
1165
            continue;
×
1166

1167
        won |= need;
17 ✔
1168
        lost |= losers;
17 ✔
1169
    }
1170

1171
    const auto all = unmask_right<mask_t>(component.count);
6 ✔
1172
    auto out = bit_or(won, bit_and(all, bit_not(bit_or(lost, contested))));
6 ✔
1173
    for (size_t member{}; member < component.count; ++member)
35 ✔
1174
        if (is_nonzero(bit_and(component.ancestors[member], lost)))
29 ✔
1175
            out = set_right(out, member, false);
5 ✔
1176

1177
    return out;
6 ✔
1178
}
1179

1180
constexpr bool algorithm::worth(uint64_t fee, uint64_t weight,
367 ✔
1181
    uint64_t other_fee, uint64_t other_weight, uint64_t rate) NOEXCEPT
1182
{
1183
    // Fee less the rate on weight.
1184
    const auto value = ceilinged_add(ceilinged_multiply(fee, per_weight),
367 ✔
1185
        ceilinged_multiply(rate, other_weight));
1186
    const auto other = ceilinged_add(ceilinged_multiply(other_fee, per_weight),
367 ✔
1187
        ceilinged_multiply(rate, weight));
1188
    return value > other;
367 ✔
1189
}
1190

1191
inline void algorithm::discard(std::vector<link_t>& pending,
19 ✔
1192
    std::span<mark> marks, const graph_t& graph, link_t row) NOEXCEPT
1193
{
1194
    pending.assign(one, row);
19 ✔
1195
    marks[row] = mark::excluded;
19 ✔
1196

1197
    while (!pending.empty())
47 ✔
1198
    {
1199
        const auto next = pending.back();
28 ✔
1200
        pending.pop_back();
28 ✔
1201
        each_child(graph, next, [&](link_t down) NOEXCEPT
28 ✔
1202
        {
1203
            if (marks[down] != mark::excluded)
9 ✔
1204
            {
1205
                marks[down] = mark::excluded;
9 ✔
1206
                pending.push_back(down);
9 ✔
1207
            }
1208
        });
9 ✔
1209
    }
1210
}
19 ✔
1211

1212
inline void algorithm::forfeit(std::vector<link_t>& pending,
1 ✔
1213
    std::span<mark> marks, const graph_t& graph, link_t row) NOEXCEPT
1214
{
1215
    std::vector<link_t> found{ row };
1 ✔
1216
    const auto add = [&](link_t next) NOEXCEPT
8 ✔
1217
    {
1218
        if (std::ranges::find(found, next) == found.end())
8 ✔
1219
            found.push_back(next);
4 ✔
1220

1221
        return true;
8 ✔
1222
    };
1 ✔
1223

1224
    for (size_t at{}; at < found.size(); ++at)
6 ✔
1225
        each_member(graph, found[at], add);
5 ✔
1226

1227
    for (const auto member: found)
6 ✔
1228
        discard(pending, marks, graph, member);
5 ✔
1229
}
1 ✔
1230

1231
inline void algorithm::pack_(std::vector<link_t>& out, takes_t& takes,
46 ✔
1232
    packing_t& kept, std::span<mark> marks, const columns_t& columns,
1233
    const totals_t& budget, size_t depth, uint32_t bytes_per_sigop) NOEXCEPT
1234
{
1235
    const auto same = [](const totals_t& left, const totals_t& right) NOEXCEPT
47 ✔
1236
    {
1237
        return left.fee == right.fee && left.weight == right.weight &&
47 ✔
1238
            left.sigops == right.sigops;
47 ✔
1239
    };
1240

1241
    // What is kept is of the rows and the limits it was made for, with no
1242
    // row before the first unmarked row met by a walk.
1243
    const graph_t graph{ columns };
46 ✔
1244
    const auto count = columns.cells.size();
46 ✔
1245
    const auto current = kept.rows == count && same(kept.budget, budget) &&
14 ✔
1246
        kept.depth == depth && kept.bytes_per_sigop == bytes_per_sigop;
59 ✔
1247

1248
    if (!current)
1249
    {
1250
        const auto end = std::next(marks.begin(), count);
33 ✔
1251
        const auto start = std::find(marks.begin(), end, mark::unmarked);
33 ✔
1252
        const auto offset = to_unsigned(std::distance(marks.begin(), start));
33 ✔
1253
        kept = {};
33 ✔
1254
        kept.rows = count;
33 ✔
1255
        kept.budget = budget;
33 ✔
1256
        kept.depth = depth;
33 ✔
1257
        kept.bytes_per_sigop = bytes_per_sigop;
33 ✔
1258
        kept.base = possible_narrow_cast<link_t>(offset);
33 ✔
1259
        kept.places.resize(count - offset);
33 ✔
1260
    }
1261

1262
    const auto base = kept.base;
46 ✔
1263
    const auto live = kept.places.size();
46 ✔
1264
    auto& places = kept.places;
1265
    auto& open = kept.open;
46 ✔
1266
    auto& joined = kept.joined;
46 ✔
1267
    auto& ring = kept.ring;
46 ✔
1268
    auto& partial = kept.partial;
46 ✔
1269
    auto& firsts = kept.offers;
46 ✔
1270
    std::vector<offer_t> others{};
46 ✔
1271
    std::vector<link_t> closed{};
46 ✔
1272
    std::vector<link_t> rows{};
46 ✔
1273
    std::vector<link_t> group{};
46 ✔
1274
    std::vector<link_t> fresh{};
46 ✔
1275
    std::vector<link_t> lost{};
46 ✔
1276
    std::vector<link_t> descent{};
46 ✔
1277
    std::vector<link_t> seeds{};
46 ✔
1278
    std::vector<link_t> work{};
46 ✔
1279
    std::vector<link_t> drop{};
46 ✔
1280
    visits_t seen{ std::vector<link_t>(live), base };
46 ✔
1281
    visits_t below{ std::vector<link_t>(live), base };
46 ✔
1282
    visits_t walk{ std::vector<link_t>(live), base };
46 ✔
1283
    flow_t flow{};
46 ✔
1284
    size_t offers{};
46 ✔
1285
    auto heaped = false;
46 ✔
1286
    totals_t used{};
46 ✔
1287
    out.clear();
46 ✔
1288
    takes.clear();
46 ✔
1289

1290
    const auto fits = [](const totals_t& sum, const totals_t& space) NOEXCEPT
239 ✔
1291
    {
1292
        return sum.weight <= space.weight && sum.sigops <= space.sigops;
239 ✔
1293
    };
1294

1295
    const auto unmarked = [&](link_t row) NOEXCEPT
643 ✔
1296
    {
1297
        return marks[row] == mark::unmarked;
643 ✔
1298
    };
46 ✔
1299

1300
    const auto available = [&]() NOEXCEPT -> totals_t
435 ✔
1301
    {
1302
        return
1303
        {
1304
            {},
1305
            floored_subtract(budget.weight, used.weight),
435 ✔
1306
            floored_subtract(budget.sigops, used.sigops)
435 ✔
1307
        };
435 ✔
1308
    };
46 ✔
1309

1310
    const auto alone = [&](link_t row) NOEXCEPT
56 ✔
1311
    {
1312
        return fits(totals(graph, row), available());
56 ✔
1313
    };
46 ✔
1314

1315
    // Chargeable size of the rows, each charged alone.
1316
    const auto charged = [&](const std::vector<link_t>& members) NOEXCEPT
219 ✔
1317
    {
1318
        uint64_t bytes{};
1319
        for (const auto member: members)
517 ✔
1320
            bytes = ceilinged_add(bytes, chargeable(totals(graph, member),
298 ✔
1321
                bytes_per_sigop));
1322

1323
        return bytes;
219 ✔
1324
    };
46 ✔
1325

1326
    // An unmarked row of no more than the value is left (least on top).
1327
    const auto remains = [&](std::vector<least_t>& least,
246 ✔
1328
        uint32_t value) NOEXCEPT
1329
    {
1330
        while (!least.empty() && !unmarked(least.front().second))
329 ✔
1331
        {
1332
            std::pop_heap(least.begin(), least.end(), std::greater<>{});
83 ✔
1333
            least.pop_back();
83 ✔
1334
        }
1335

1336
        return !least.empty() && least.front().first <= value;
246 ✔
1337
    };
46 ✔
1338

1339
    // The highest rate is on top, a bound over offers of equal rate, the
1340
    // lowest row of equal rates, and the latest offer of the same row.
1341
    const auto lower = [](const offer_t& left, const offer_t& right) NOEXCEPT
265 ✔
1342
    {
1343
        if (left.rate != right.rate)
265 ✔
1344
            return left.rate < right.rate;
40 ✔
1345

1346
        const auto left_bound = left.count == none;
225 ✔
1347
        const auto right_bound = right.count == none;
225 ✔
1348
        if (left_bound != right_bound)
225 ✔
1349
            return right_bound;
1350

1351
        return left.row == right.row ? left.order < right.order :
225 ✔
1352
            left.row > right.row;
1353
    };
1354

1355
    // The first offers are kept and heaped at once, the others as offered.
1356
    const auto push = [&](offer_t&& score) NOEXCEPT
188 ✔
1357
    {
1358
        score.order = offers++;
188 ✔
1359
        if (!heaped)
188 ✔
1360
        {
1361
            firsts.push_back(score);
58 ✔
1362
            return;
58 ✔
1363
        }
1364

1365
        others.push_back(score);
130 ✔
1366
        std::push_heap(others.begin(), others.end(), lower);
130 ✔
1367
    };
46 ✔
1368

1369
    const auto score_row = [&](link_t row) NOEXCEPT
92 ✔
1370
    {
1371
        totals_t sum{};
92 ✔
1372
        if (chunk(rows, sum, walk, marks, graph, row, depth, available()))
92 ✔
1373
            push({ row, {}, {}, {}, fee_rate(sum.fee, charged(rows)), sum });
86 ✔
1374
    };
138 ✔
1375

1376
    // Each row of group offers its chunk (unless every, only rows below take),
1377
    // none of which fits unless one of the rows does.
1378
    const auto score_rows = [&](bool every) NOEXCEPT
55 ✔
1379
    {
1380
        if (!std::ranges::any_of(group, alone))
55 ✔
1381
            return;
1382

1383
        for (const auto member: group)
129 ✔
1384
            if (every || visited(below, member))
92 ✔
1385
                score_row(member);
92 ✔
1386
    };
46 ✔
1387

1388
    // The closure offered with its sum, held for the take.
1389
    const auto hold = [&](const totals_t& sum) NOEXCEPT
68 ✔
1390
    {
1391
        auto& held = heaped ? closed : kept.closed;
68 ✔
1392
        const auto first = held.size();
68 ✔
1393
        const auto size = possible_narrow_cast<link_t>(rows.size());
68 ✔
1394
        const auto rate = fee_rate(sum.fee, charged(rows));
68 ✔
1395
        held.insert(held.end(), rows.begin(), rows.end());
68 ✔
1396
        push({ rows.front(), size, first, {}, rate, sum });
68 ✔
1397
    };
114 ✔
1398

1399
    // Least rate leaf of the closure and its descendants leave the work.
1400
    const auto shed = [&]() NOEXCEPT
35 ✔
1401
    {
1402
        auto leaf = none;
35 ✔
1403
        totals_t least{};
35 ✔
1404
        for (const auto row: rows)
121 ✔
1405
        {
1406
            auto parent_of_row = false;
86 ✔
1407
            each_child(graph, row, [&](link_t down) NOEXCEPT
86 ✔
1408
            {
1409
                parent_of_row = parent_of_row ||
54 ✔
1410
                    std::ranges::find(rows, down) != rows.end();
52 ✔
1411
            });
54 ✔
1412

1413
            if (parent_of_row)
86 ✔
1414
                continue;
49 ✔
1415

1416
            const auto own = totals(graph, row);
37 ✔
1417
            const auto bytes = chargeable(own, bytes_per_sigop);
37 ✔
1418
            const auto least_bytes = chargeable(least, bytes_per_sigop);
37 ✔
1419
            if (leaf == none || ceilinged_multiply(own.fee, least_bytes) <=
37 ✔
1420
                ceilinged_multiply(least.fee, bytes))
2 ✔
1421
            {
1422
                leaf = row;
37 ✔
1423
                least = own;
37 ✔
1424
            }
1425
        }
1426

1427
        drop.assign(one, leaf);
35 ✔
1428
        for (size_t at{}; at < drop.size(); ++at)
73 ✔
1429
            each_child(graph, drop[at], [&](link_t down) NOEXCEPT
38 ✔
1430
            {
1431
                if (std::ranges::find(drop, down) == drop.end())
3 ✔
1432
                    drop.push_back(down);
3 ✔
1433
            });
3 ✔
1434

1435
        std::erase_if(work, [&](link_t row) NOEXCEPT
35 ✔
1436
        {
1437
            return std::ranges::find(drop, row) != drop.end();
86 ✔
1438
        });
1439
    };
81 ✔
1440

1441
    // Whole cluster over the space offers its best closure that fits.
1442
    const auto fit = [&]() NOEXCEPT
32 ✔
1443
    {
1444
        totals_t sum{};
32 ✔
1445
        work.assign(group.begin(), group.end());
32 ✔
1446
        while (!work.empty())
67 ✔
1447
        {
1448
            closure(rows, sum, flow, graph, work, bytes_per_sigop);
66 ✔
1449
            if (rows.empty())
66 ✔
1450
                return;
31 ✔
1451

1452
            if (fits(sum, available()))
66 ✔
1453
            {
1454
                hold(sum);
31 ✔
1455
                return;
31 ✔
1456
            }
1457

1458
            shed();
35 ✔
1459
        }
1460
    };
46 ✔
1461

1462
    // Whole cluster offers its best closure (itself if one row), else each of
1463
    // its rows its chunk, of partial cluster of take only those below take.
1464
    const auto propose = [&](bool whole, bool every) NOEXCEPT
92 ✔
1465
    {
1466
        totals_t sum{};
92 ✔
1467
        if (whole && is_one(group.size()))
92 ✔
1468
        {
1469
            rows.assign(one, group.front());
44 ✔
1470
            sum = totals(graph, group.front());
44 ✔
1471
        }
1472
        else if (whole)
48 ✔
1473
        {
1474
            closure(rows, sum, flow, graph, group, bytes_per_sigop);
41 ✔
1475
        }
1476

1477
        if (whole && fits(sum, available()))
85 ✔
1478
        {
1479
            hold(sum);
37 ✔
1480
            return;
37 ✔
1481
        }
1482

1483
        if (whole && !is_one(group.size()))
55 ✔
1484
            fit();
32 ✔
1485

1486
        score_rows(every || whole);
55 ✔
1487
    };
46 ✔
1488

1489
    // Cluster of row below take,
1490
    // false if over maximum_members with nothing offered.
1491
    const auto offer = [&](link_t row) NOEXCEPT
36 ✔
1492
    {
1493
        const auto whole = cluster(group, walk, marks, graph, row);
36 ✔
1494
        if (group.size() > maximum_members)
36 ✔
1495
            return false;
1496

1497
        for (const auto member: group)
86 ✔
1498
            visit(seen, member);
50 ✔
1499

1500
        propose(whole, false);
36 ✔
1501
        return true;
36 ✔
1502
    };
46 ✔
1503

1504
    // The first place of the cluster of the place.
1505
    const auto lowest = [&](link_t place) NOEXCEPT
196 ✔
1506
    {
1507
        while (joined[place] != place)
229 ✔
1508
        {
1509
            joined[place] = joined[joined[place]];
33 ✔
1510
            place = joined[place];
33 ✔
1511
        }
1512

1513
        return place;
196 ✔
1514
    };
46 ✔
1515

1516
    // Rows of the ring of the first place of a cluster.
1517
    const auto gather = [&](link_t place) NOEXCEPT
89 ✔
1518
    {
1519
        group.clear();
89 ✔
1520
        auto member = place;
1521
        do
1522
        {
1523
            group.push_back(open[member]);
209 ✔
1524
            member = ring[member];
209 ✔
1525
        }
1526
        while (member != place);
209 ✔
1527
    };
135 ✔
1528

1529
    // Each place is joined to an earlier place of its cluster (the first to
1530
    // itself) and is in a ring of the places of its cluster, which is partial
1531
    // (at its first place) if a row of it has an excluded parent. Each
1532
    // cluster of the places then makes its first offer.
1533
    const auto form = [&](const auto& set) NOEXCEPT
46 ✔
1534
    {
1535
        for (const auto place: set)
168 ✔
1536
        {
1537
            joined[place] = ring[place] = place;
122 ✔
1538
            partial[place] = false;
122 ✔
1539
        }
1540

1541
        for (const auto place: set)
168 ✔
1542
        {
1543
            const auto slice = edges(graph, open[place]);
122 ✔
1544
            for (auto input = slice.first; input < slice.first + slice.count;
247 ✔
1545
                ++input)
1546
            {
1547
                const auto up = parent(graph, input);
125 ✔
1548
                if (up == none || is_boundary(cell(graph, up)) ||
125 ✔
1549
                    marks[up] == mark::taken)
78 ✔
1550
                    continue;
62 ✔
1551

1552
                auto low = lowest(place);
67 ✔
1553
                if (marks[up] == mark::excluded)
67 ✔
1554
                {
1555
                    partial[low] = true;
4 ✔
1556
                    continue;
4 ✔
1557
                }
1558

1559
                auto high = lowest(places[up - base]);
63 ✔
1560
                if (high == low)
63 ✔
1561
                    continue;
×
1562

1563
                if (high < low)
63 ✔
1564
                    std::swap(low, high);
63 ✔
1565

1566
                joined[high] = low;
63 ✔
1567
                std::swap(ring[low], ring[high]);
63 ✔
1568
                partial[low] = partial[low] || partial[high];
64 ✔
1569
            }
1570
        }
1571

1572
        for (const auto place: set)
168 ✔
1573
        {
1574
            if (joined[place] != place)
122 ✔
1575
                continue;
89 ✔
1576

1577
            gather(place);
59 ✔
1578
            if (is_one(group.size()))
59 ✔
1579
            {
1580
                propose(!partial[place], true);
26 ✔
1581
                continue;
26 ✔
1582
            }
1583

1584
            // No offer of cluster exceeds the rate of its best row.
1585
            uint64_t most{};
33 ✔
1586
            for (const auto member: group)
129 ✔
1587
            {
1588
                const auto own = totals(graph, member);
96 ✔
1589
                most = std::max(most, fee_rate(own.fee,
96 ✔
1590
                    chargeable(own, bytes_per_sigop)));
1591
            }
1592

1593
            push({ group.front(), none, {}, {}, most, {} });
33 ✔
1594
        }
1595
    };
92 ✔
1596

1597
    // Cluster of bound proposes as when formed, with the whole budget.
1598
    const auto unfold = [&](link_t row) NOEXCEPT
30 ✔
1599
    {
1600
        const auto place = lowest(places[row - base]);
30 ✔
1601
        gather(place);
30 ✔
1602
        const auto spent = used;
30 ✔
1603
        used = {};
30 ✔
1604
        propose(!partial[place] && group.size() <= maximum_members, true);
30 ✔
1605
        used = spent;
30 ✔
1606
    };
76 ✔
1607

1608
    if (!current)
46 ✔
1609
    {
1610
        // The unmarked rows in order, each with its place among them.
1611
        const auto start = std::next(marks.begin(), count - live);
33 ✔
1612
        const auto end = std::next(marks.begin(), count);
33 ✔
1613
        const auto total = std::count(start, end, mark::unmarked);
33 ✔
1614
        const auto all = possible_narrow_cast<link_t>(to_unsigned(total));
33 ✔
1615
        open.resize(all);
33 ✔
1616
        joined.resize(all);
33 ✔
1617
        ring.resize(all);
33 ✔
1618
        partial.resize(all);
33 ✔
1619
        kept.sizes.resize(all);
33 ✔
1620
        kept.sigops.resize(all);
33 ✔
1621

1622
        link_t place{};
1623
        for (auto at = count - live; at < count; ++at)
150 ✔
1624
        {
1625
            const auto row = possible_narrow_cast<link_t>(at);
117 ✔
1626
            if (!unmarked(row))
117 ✔
1627
                continue;
7 ✔
1628

1629
            const auto own = totals(graph, row);
110 ✔
1630
            places[row - base] = place;
110 ✔
1631
            open[place] = row;
110 ✔
1632
            kept.sizes[place] = { own.weight, row };
110 ✔
1633
            kept.sigops[place] = { own.sigops, row };
110 ✔
1634
            ++place;
110 ✔
1635
        }
1636

1637
        std::make_heap(kept.sizes.begin(), kept.sizes.end(),
33 ✔
1638
            std::greater<>{});
1639
        std::make_heap(kept.sigops.begin(), kept.sigops.end(),
33 ✔
1640
            std::greater<>{});
1641
        form(std::views::iota(link_t{}, all));
33 ✔
1642
    }
1643
    else
1644
    {
1645
        // A cluster that lost rows to the last block is formed again from
1646
        // those left, and the first offers of the others are kept.
1647
        restart(seen);
13 ✔
1648
        for (const auto row: kept.taken)
35 ✔
1649
        {
1650
            const auto first = lowest(places[row - base]);
22 ✔
1651
            if (visit(seen, open[first]))
22 ✔
1652
                lost.push_back(first);
13 ✔
1653
        }
1654

1655
        std::erase_if(firsts, [&](const offer_t& score) NOEXCEPT
13 ✔
1656
        {
1657
            return visited(seen, open[lowest(places[score.row - base])]);
14 ✔
1658
        });
1659

1660
        for (auto& score: firsts)
14 ✔
1661
            score.order = offers++;
1 ✔
1662

1663
        for (const auto first: lost)
26 ✔
1664
        {
1665
            auto member = first;
13 ✔
1666
            do
1667
            {
1668
                if (unmarked(open[member]))
34 ✔
1669
                    fresh.push_back(member);
12 ✔
1670

1671
                member = ring[member];
34 ✔
1672
            }
1673
            while (member != first);
34 ✔
1674
        }
1675

1676
        form(fresh);
13 ✔
1677
    }
1678

1679
    std::make_heap(firsts.begin(), firsts.end(), lower);
46 ✔
1680
    auto left = firsts.size();
46 ✔
1681
    heaped = true;
46 ✔
1682

1683
    // A first offer leaves the heap and is kept.
1684
    while (is_nonzero(left) || !others.empty())
155 ✔
1685
    {
1686
        // A take holds an unmarked row, so none is left when none fits.
1687
        const auto space = available();
136 ✔
1688
        if (!remains(kept.sizes, space.weight) ||
246 ✔
1689
            !remains(kept.sigops, space.sigops))
110 ✔
1690
            break;
1691

1692
        const auto first = is_nonzero(left) && (others.empty() ||
109 ✔
1693
            lower(others.front(), firsts.front()));
12 ✔
1694

1695
        offer_t score{};
109 ✔
1696
        if (first)
109 ✔
1697
        {
1698
            std::pop_heap(firsts.begin(), std::next(firsts.begin(), left),
100 ✔
1699
                lower);
1700
            score = firsts[--left];
50 ✔
1701
        }
1702
        else
1703
        {
1704
            std::pop_heap(others.begin(), others.end(), lower);
59 ✔
1705
            score = others.back();
59 ✔
1706
            others.pop_back();
59 ✔
1707
        }
1708

1709
        if (score.count == none)
109 ✔
1710
        {
1711
            unfold(score.row);
30 ✔
1712
            continue;
45 ✔
1713
        }
1714

1715
        totals_t sum{};
79 ✔
1716
        if (is_nonzero(score.count))
79 ✔
1717
        {
1718
            const auto& from = first ? kept.closed : closed;
46 ✔
1719
            const auto held = std::span<const link_t>{ from }.subspan(
46 ✔
1720
                score.first, score.count);
46 ✔
1721

1722
            if (!std::ranges::all_of(held, unmarked))
46 ✔
1723
                continue;
14 ✔
1724

1725
            // Closure that no longer fits falls back to chunks of its cluster.
1726
            if (!fits(score.sum, space))
32 ✔
1727
            {
1728
                if (cluster(group, walk, marks, graph, score.row) &&
×
1729
                    !is_one(group.size()) && group.size() <= maximum_members)
×
1730
                    fit();
×
1731

1732
                score_rows(true);
×
1733
                continue;
×
1734
            }
1735

1736
            rows.assign(held.begin(), held.end());
32 ✔
1737
            sum = score.sum;
32 ✔
1738
        }
1739
        else
1740
        {
1741
            if (!unmarked(score.row) || !chunk(rows, sum, walk, marks, graph,
33 ✔
1742
                score.row, depth, space))
1743
                continue;
×
1744

1745
            // A changed chunk competes again at its current rate.
1746
            if (!same(sum, score.sum))
33 ✔
1747
            {
1748
                const auto rate = fee_rate(sum.fee, charged(rows));
1 ✔
1749
                push({ score.row, {}, {}, {}, rate, sum });
1 ✔
1750
                continue;
1 ✔
1751
            }
1 ✔
1752
        }
1753

1754
        for (const auto row: rows)
145 ✔
1755
            marks[row] = mark::taken;
81 ✔
1756

1757
        out.insert(out.end(), rows.begin(), rows.end());
64 ✔
1758
        takes.push_back({ score.rate, charged(rows) });
64 ✔
1759
        used.weight = ceilinged_add(used.weight, sum.weight);
64 ✔
1760
        used.sigops = ceilinged_add(used.sigops, sum.sigops);
64 ✔
1761

1762
        // Untaken descendants of take have changed chunks, and untaken
1763
        // remainder of its cluster competes again from its children.
1764
        restart(seen);
64 ✔
1765
        restart(below);
64 ✔
1766
        seeds.clear();
64 ✔
1767
        descent.assign(rows.begin(), rows.end());
64 ✔
1768
        const auto taken = descent.size();
64 ✔
1769
        for (size_t at{}; at < descent.size(); ++at)
195 ✔
1770
        {
1771
            each_child(graph, descent[at], [&](link_t down) NOEXCEPT
131 ✔
1772
            {
1773
                if (!unmarked(down) || !visit(below, down))
68 ✔
1774
                    return;
18 ✔
1775

1776
                descent.push_back(down);
50 ✔
1777
                if (marks[descent[at]] == mark::taken)
50 ✔
1778
                    seeds.push_back(down);
36 ✔
1779
            });
1780
        }
1781

1782
        auto walked = true;
1783
        for (const auto seed: seeds)
100 ✔
1784
            if (!visited(seen, seed) && !offer(seed))
36 ✔
1785
                walked = false;
1786

1787
        // Each descendant that no walk met is of a cluster not walked.
1788
        if (!walked)
64 ✔
1789
            for (auto at = taken; at < descent.size(); ++at)
×
1790
                if (!visited(seen, descent[at]))
×
1791
                    score_row(descent[at]);
×
1792
    }
1793

1794
    kept.taken = out;
46 ✔
1795
    std::sort(out.begin(), out.end());
46 ✔
1796
}
79 ✔
1797

1798
// Takes are in about descending order of rate as taken.
1799
inline std::vector<algorithm::takes_t> algorithm::levels(
15 ✔
1800
    const projection_t& projection, size_t count) NOEXCEPT
1801
{
1802
    const auto& blocks = projection.blocks;
15 ✔
1803
    std::vector<takes_t> out(std::min(count, blocks.size()));
15 ✔
1804
    for (size_t block{}; block < out.size(); ++block)
31 ✔
1805
    {
1806
        auto& level = out[block];
16 ✔
1807
        level.assign(blocks[block].rbegin(), blocks[block].rend());
16 ✔
1808
        if (!std::ranges::is_sorted(level, {}, &take_t::rate))
16 ✔
1809
            std::ranges::sort(level, {}, &take_t::rate);
1 ✔
1810

1811
        uint64_t size{};
1812
        for (auto take = level.rbegin(); take != level.rend(); ++take)
45 ✔
1813
            take->weight = size = ceilinged_add(size, take->weight);
29 ✔
1814
    }
1815

1816
    return out;
15 ✔
1817
}
×
1818

1819
template <typename Arrivals>
1820
inline bool algorithm::reaches(const projection_t& projection,
783 ✔
1821
    const std::vector<takes_t>& levels, size_t count, uint64_t space,
1822
    uint64_t room, uint64_t rate, const Arrivals& arrivals) NOEXCEPT
1823
{
1824
    const auto blocks = projection.blocks.size();
783 ✔
1825
    uint64_t arriving{};
1826
    uint64_t carry{};
1827

1828
    // A block that leaves rows is full, so its takes are its capacity.
1829
    for (size_t block{}; block < count; ++block)
814 ✔
1830
    {
1831
        std::span<const take_t> level{};
813 ✔
1832
        if (block < levels.size())
813 ✔
1833
            level = levels[block];
747 ✔
1834

1835
        const auto from = std::ranges::lower_bound(level, rate, {},
813 ✔
1836
            &take_t::rate);
1837

1838
        const auto last = add1(block) >= blocks;
813 ✔
1839
        const auto full = !level.empty() && !(last && projection.exhausted);
813 ✔
1840
        const auto capacity = full ? level.front().weight : space;
716 ✔
1841
        const auto over = from == level.end() ? 0_u64 : from->weight;
813 ✔
1842
        const auto demand = ceilinged_add(carry, over);
813 ✔
1843
        if (floored_subtract(capacity, demand) >= room)
813 ✔
1844
            return true;
740 ✔
1845

1846
        // Arrivals join what the first block leaves, and each block after.
1847
        if (add1(block) == count)
73 ✔
1848
            break;
1849

1850
        if (is_zero(block))
31 ✔
1851
            arriving = arrivals(rate);
31 ✔
1852

1853
        const auto excess = floored_subtract(demand, capacity);
31 ✔
1854
        carry = ceilinged_add(excess, arriving);
31 ✔
1855
    }
1856

1857
    return false;
1858
}
1859

1860
inline void algorithm::restart(visits_t& visits) NOEXCEPT
302 ✔
1861
{
1862
    if (is_nonzero(++visits.walk))
302 ✔
1863
        return;
1864

1865
    std::ranges::fill(visits.walks, link_t{});
×
1866
    visits.walk = 1_u32;
×
1867
}
1868

1869
inline bool algorithm::visited(const visits_t& visits, link_t row) NOEXCEPT
52 ✔
1870
{
1871
    return visits.walks[row - visits.first] == visits.walk;
52 ✔
1872
}
1873

1874
inline bool algorithm::visit(visits_t& visits, link_t row) NOEXCEPT
381 ✔
1875
{
1876
    auto& walk = visits.walks[row - visits.first];
381 ✔
1877
    if (walk == visits.walk)
381 ✔
1878
        return false;
1879

1880
    walk = visits.walk;
358 ✔
1881
    return true;
358 ✔
1882
}
1883

1884
inline bool algorithm::chunk(std::vector<link_t>& out, totals_t& sum,
125 ✔
1885
    visits_t& visits, const std::span<const mark>& marks,
1886
    const graph_t& graph, link_t row, size_t depth,
1887
    const totals_t& available) NOEXCEPT
1888
{
1889
    sum = {};
125 ✔
1890
    out.assign(one, row);
125 ✔
1891
    restart(visits);
125 ✔
1892
    visit(visits, row);
125 ✔
1893

1894
    // Each level holds the untaken parents of the previous level.
1895
    for (size_t level{}, begin{}; begin < out.size(); ++level)
312 ✔
1896
    {
1897
        const auto end = out.size();
1898
        for (auto at = begin; at < end; ++at)
380 ✔
1899
        {
1900
            const auto slice = edges(graph, out[at]);
193 ✔
1901
            for (auto input = slice.first;
383 ✔
1902
                input < slice.first + slice.count; ++input)
383 ✔
1903
            {
1904
                const auto up = parent(graph, input);
196 ✔
1905
                if (up == none || marks[up] == mark::taken ||
196 ✔
1906
                    is_boundary(cell(graph, up)))
76 ✔
1907
                    continue;
120 ✔
1908

1909
                if (marks[up] == mark::excluded)
76 ✔
1910
                    return false;
6 ✔
1911

1912
                if (!visit(visits, up))
70 ✔
1913
                    continue;
×
1914

1915
                if (level == depth)
70 ✔
1916
                    return false;
1917

1918
                out.push_back(up);
70 ✔
1919
            }
1920
        }
1921

1922
        begin = end;
1923
    }
1924

1925
    std::sort(out.begin(), out.end());
119 ✔
1926
    auto cut = out.begin();
119 ✔
1927
    for (; cut != out.end(); ++cut)
272 ✔
1928
    {
1929
        const auto& add = totals(graph, *cut);
185 ✔
1930
        const auto size = ceilinged_add(sum.weight, add.weight);
185 ✔
1931
        const auto sigops = ceilinged_add(sum.sigops, add.sigops);
185 ✔
1932
        if (size > available.weight || sigops > available.sigops)
185 ✔
1933
            break;
1934

1935
        sum.fee = ceilinged_add(sum.fee, add.fee);
153 ✔
1936
        sum.weight = size;
153 ✔
1937
        sum.sigops = sigops;
153 ✔
1938
    }
1939

1940
    out.erase(cut, out.end());
119 ✔
1941
    return !out.empty();
119 ✔
1942
}
1943

1944
inline bool algorithm::cluster(std::vector<link_t>& out, visits_t& visits,
36 ✔
1945
    const std::span<const mark>& marks, const graph_t& graph,
1946
    link_t row) NOEXCEPT
1947
{
1948
    out.assign(one, row);
36 ✔
1949
    restart(visits);
36 ✔
1950
    visit(visits, row);
36 ✔
1951
    auto whole = true;
1952
    const auto push = [&](link_t found) NOEXCEPT
64 ✔
1953
    {
1954
        if (marks[found] == mark::unmarked && visit(visits, found))
64 ✔
1955
            out.push_back(found);
14 ✔
1956
    };
100 ✔
1957

1958
    for (size_t at{}; at < out.size(); ++at)
86 ✔
1959
    {
1960
        if (out.size() > maximum_members)
50 ✔
1961
            return false;
×
1962

1963
        const auto member = out[at];
50 ✔
1964
        const auto slice = edges(graph, member);
50 ✔
1965
        for (auto input = slice.first; input < slice.first + slice.count;
103 ✔
1966
            ++input)
1967
        {
1968
            const auto up = parent(graph, input);
53 ✔
1969
            if (up == none || is_boundary(cell(graph, up)))
53 ✔
1970
                continue;
×
1971

1972
            if (marks[up] == mark::excluded)
53 ✔
1973
                whole = false;
1974
            else
1975
                push(up);
50 ✔
1976
        }
1977

1978
        each_child(graph, member, [&](link_t down) NOEXCEPT
50 ✔
1979
        {
1980
            push(down);
14 ✔
1981
        });
14 ✔
1982
    }
1983

1984
    return whole && out.size() <= maximum_members;
36 ✔
1985
}
1986

1987
inline void algorithm::closure(std::vector<link_t>& out, totals_t& sum,
107 ✔
1988
    flow_t& flow, const graph_t& graph, const std::vector<link_t>& cluster,
1989
    uint32_t bytes_per_sigop) NOEXCEPT
1990
{
1991
    // A node is a row in ascending order, a link a node to a parent node.
1992
    const auto count = cluster.size();
107 ✔
1993
    const auto end = std::next(flow.rows.begin(), count);
107 ✔
1994
    std::copy(cluster.begin(), cluster.end(), flow.rows.begin());
107 ✔
1995
    std::sort(flow.rows.begin(), end);
107 ✔
1996

1997
    size_t links{};
1998
    uint64_t fee{};
1999
    uint64_t size{};
2000
    for (size_t node{}; node < count; ++node)
344 ✔
2001
    {
2002
        const auto row = flow.rows[node];
237 ✔
2003
        const auto own = totals(graph, row);
237 ✔
2004
        flow.fees[node] = own.fee;
237 ✔
2005
        flow.sizes[node] = chargeable(own, bytes_per_sigop);
237 ✔
2006
        fee = ceilinged_add(fee, flow.fees[node]);
237 ✔
2007
        size = ceilinged_add(size, flow.sizes[node]);
237 ✔
2008

2009
        mask_t parents{};
237 ✔
2010
        const auto slice = edges(graph, row);
237 ✔
2011
        for (auto input = slice.first; input < slice.first + slice.count;
474 ✔
2012
            ++input)
2013
        {
2014
            const auto up = parent(graph, input);
237 ✔
2015
            const auto it = std::lower_bound(flow.rows.begin(), end, up);
237 ✔
2016
            if (it != end && *it == up)
237 ✔
2017
                set_right_into(parents, to_unsigned(std::distance(
130 ✔
2018
                    flow.rows.begin(), it)));
2019
        }
2020

2021
        flow.parents[node] = parents;
237 ✔
2022
        flow.links[node] = links;
237 ✔
2023
        links += ones_count(parents);
237 ✔
2024
    }
2025

2026
    // No capacity exceeds the fee of the cluster times its size.
2027
    auto best = is_multiply_overflow(fee, size) ?
107 ✔
2028
        closure_(flow, flow.wide, count, links, fee, size) :
×
2029
        closure_(flow, flow.narrow, count, links, fee, size);
107 ✔
2030

2031
    out.clear();
107 ✔
2032
    sum = {};
107 ✔
2033
    for (; is_nonzero(best); best &= sub1(best))
343 ✔
2034
    {
2035
        const auto row = flow.rows[right_zeros(best)];
236 ✔
2036
        const auto own = totals(graph, row);
236 ✔
2037
        out.push_back(row);
236 ✔
2038
        sum.fee = ceilinged_add(sum.fee, own.fee);
236 ✔
2039
        sum.weight = ceilinged_add(sum.weight, own.weight);
236 ✔
2040
        sum.sigops = ceilinged_add(sum.sigops, own.sigops);
236 ✔
2041
    }
2042
}
107 ✔
2043

2044
// Dinkelbach: maximum weight closure at rate of best closure
2045
// found (fee times its size less size times its fee, a
2046
// minimum cut) is positive until no closure has higher rate.
2047
template <typename Capacity>
2048
inline algorithm::mask_t algorithm::closure_(flow_t& flow,
107 ✔
2049
    residual_t<Capacity>& residual, size_t count, size_t links, uint64_t fee,
2050
    uint64_t size) NOEXCEPT
2051
{
2052
    const auto link = [&](size_t node, size_t up) NOEXCEPT
255 ✔
2053
    {
2054
        const auto before = unmask_right<mask_t>(up);
148 ✔
2055
        return flow.links[node] + ones_count(bit_and(flow.parents[node],
148 ✔
2056
            before));
148 ✔
2057
    };
2058

2059
    const auto weigh = [&](mask_t nodes, uint64_t& fees,
287 ✔
2060
        uint64_t& sizes) NOEXCEPT
2061
    {
2062
        fees = 0_u64;
90 ✔
2063
        sizes = 0_u64;
90 ✔
2064
        for (; is_nonzero(nodes); nodes &= sub1(nodes))
91 ✔
2065
        {
2066
            const auto node = right_zeros(nodes);
1 ✔
2067
            fees = ceilinged_add(fees, flow.fees[node]);
1 ✔
2068
            sizes = ceilinged_add(sizes, flow.sizes[node]);
1 ✔
2069
        }
2070
    };
2071

2072
    // Source feeds each node its fee at the size, each node feeds sink its size
2073
    // at the fee, and a node feeds its parents without limit. Nodes reached
2074
    // from source when no more can flow are the closure, and a node passes at
2075
    // once what its own two limits allow.
2076
    const auto cut = [&]() NOEXCEPT
197 ✔
2077
    {
2078
        residual.flows.assign(links, Capacity{});
90 ✔
2079
        for (size_t node{}; node < count; ++node)
311 ✔
2080
        {
2081
            const auto gain = Capacity{ flow.fees[node] } * size;
×
2082
            const auto cost = Capacity{ flow.sizes[node] } * fee;
×
2083
            const auto both = std::min(gain, cost);
221 ✔
2084
            residual.source[node] = gain - both;
×
2085
            residual.sink[node] = cost - both;
×
2086
            flow.fed[node] = {};
221 ✔
2087
        }
2088

2089
        while (true)
114 ✔
2090
        {
2091
            // Path to each node reached is by node before it, source if count.
2092
            mask_t reached{};
204 ✔
2093
            size_t head{};
2094
            size_t tail{};
2095
            for (size_t node{}; node < count; ++node)
739 ✔
2096
            {
2097
                if (residual.source[node] != Capacity{})
535 ✔
2098
                {
2099
                    set_right_into(reached, node);
124 ✔
2100
                    flow.before[node] = count;
124 ✔
2101
                    flow.queue[tail++] = node;
124 ✔
2102
                }
2103
            }
2104

2105
            auto last = count;
2106
            while (head < tail && last == count)
362 ✔
2107
            {
2108
                const auto node = flow.queue[head++];
×
2109
                if (residual.sink[node] != Capacity{})
272 ✔
2110
                {
2111
                    last = node;
2112
                    break;
2113
                }
2114

2115
                // Flow moves to a parent, or back to a child that fed it.
2116
                const auto next = bit_or(flow.parents[node], flow.fed[node]);
158 ✔
2117
                for (auto more = bit_and(next, bit_not(reached));
158 ✔
2118
                    is_nonzero(more); more &= sub1(more))
306 ✔
2119
                {
2120
                    const auto other = right_zeros(more);
148 ✔
2121
                    set_right_into(reached, other);
148 ✔
2122
                    flow.before[other] = node;
148 ✔
2123
                    flow.queue[tail++] = other;
148 ✔
2124
                }
2125
            }
2126

2127
            if (last == count)
204 ✔
2128
                return reached;
90 ✔
2129

2130
            auto amount = residual.sink[last];
114 ✔
2131
            auto node = last;
2132
            for (; flow.before[node] != count; node = flow.before[node])
262 ✔
2133
            {
2134
                const auto from = flow.before[node];
2135
                if (!get_right(flow.parents[from], node))
148 ✔
2136
                    amount = std::min(amount,
×
2137
                        residual.flows[link(node, from)]);
×
2138
            }
2139

2140
            amount = std::min(amount, residual.source[node]);
114 ✔
2141
            residual.source[node] -= amount;
×
2142
            residual.sink[last] -= amount;
×
2143
            for (node = last; flow.before[node] != count;
262 ✔
2144
                node = flow.before[node])
148 ✔
2145
            {
2146
                const auto from = flow.before[node];
2147
                if (get_right(flow.parents[from], node))
148 ✔
2148
                {
2149
                    residual.flows[link(from, node)] += amount;
×
2150
                    set_right_into(flow.fed[node], from);
148 ✔
2151
                    continue;
148 ✔
2152
                }
2153

2154
                auto& back = residual.flows[link(node, from)];
×
2155
                back -= amount;
×
2156
                if (back == Capacity{})
×
2157
                    flow.fed[from] = set_right(flow.fed[from], node, false);
×
2158
            }
2159
        }
2160
    };
2161

2162
    auto best = unmask_right<mask_t>(count);
107 ✔
2163
    while (count > one && is_nonzero(size))
108 ✔
2164
    {
2165
        uint64_t found_fee{};
90 ✔
2166
        uint64_t found_size{};
90 ✔
2167
        const auto found = cut();
90 ✔
2168
        weigh(found, found_fee, found_size);
90 ✔
2169
        if (is_zero(found_size) || Capacity{ found_fee } * size <=
90 ✔
2170
            Capacity{ found_size } * fee)
1 ✔
2171
            break;
2172

2173
        best = found;
2174
        fee = found_fee;
1 ✔
2175
        size = found_size;
1 ✔
2176
    }
2177

2178
    return best;
107 ✔
2179
}
2180

2181
inline algorithm::link_t algorithm::resolved(const graph_t& graph,
4,353 ✔
2182
    link_t row) NOEXCEPT
2183
{
2184
    const auto own = cell(graph, row);
4,353 ✔
2185
    if (is_boundary(own))
4,353 ✔
2186
        return own;
2187

2188
    auto next = reduced(own);
3,758 ✔
2189
    const auto slice = edges(graph, row);
3,758 ✔
2190
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
12,026 ✔
2191
    {
2192
        const auto up = parent(graph, input);
8,268 ✔
2193
        if (up != none && is_absent(cell(graph, up)))
8,268 ✔
2194
            next = raised(next, ancestor);
2,206 ✔
2195

2196
        each_rival(graph, input, [&](link_t other) NOEXCEPT
8,268 ✔
2197
        {
2198
            if (is_boundary(cell(graph, owner(graph, other))))
1,557 ✔
2199
                next = raised(next, rivaled);
488 ✔
2200
        });
1,557 ✔
2201
    }
2202

2203
    return next;
3,758 ✔
2204
}
2205

2206
// Children of row depend on its absence, and its rivals on its confirmation.
2207
// Labeled row that leaves its component leaves rest of it to relabel, and row
2208
// made present joins one.
2209
inline void algorithm::change(walk_t& walk, const columns_t& columns,
7,159 ✔
2210
    link_t row, link_t next) NOEXCEPT
2211
{
2212
    const graph_t graph{ columns };
7,159 ✔
2213
    const auto own = cell(graph, row);
7,159 ✔
2214
    if (next == own)
7,159 ✔
2215
        return;
5,462 ✔
2216

2217
    if (is_labeled(own) && !is_present(next))
5,153 ✔
2218
    {
2219
        component_t former{};
1,290 ✔
2220
        if (collect(former, graph, row))
1,290 ✔
2221
            walk.affected.insert(walk.affected.end(), former.rows.cbegin(),
960 ✔
2222
                std::next(former.rows.cbegin(), former.count));
960 ✔
2223
        else
2224
            walk.complete = false;
330 ✔
2225
    }
2226

2227
    set_cell(graph, row, next);
5,153 ✔
2228
    if (is_present(next) && !is_present(own))
5,153 ✔
2229
        walk.affected.push_back(row);
1,787 ✔
2230

2231
    if (is_absent(next) != is_absent(own))
5,153 ✔
2232
    {
2233
        each_child(graph, row, [&](link_t down) NOEXCEPT
3,333 ✔
2234
        {
2235
            walk.pending.push_back(down);
3,094 ✔
2236
        });
3,094 ✔
2237
    }
2238

2239
    if (is_boundary(next) == is_boundary(own))
5,153 ✔
2240
        return;
2241

2242
    const auto slice = edges(graph, row);
1,697 ✔
2243
    for (auto input = slice.first; input < slice.first + slice.count; ++input)
5,040 ✔
2244
    {
2245
        each_rival(graph, input, [&](link_t other) NOEXCEPT
3,343 ✔
2246
        {
2247
            walk.pending.push_back(owner(graph, other));
598 ✔
2248
        });
598 ✔
2249
    }
2250
}
2251

2252
inline bool algorithm::spread(walk_t& walk, const columns_t& columns) NOEXCEPT
265 ✔
2253
{
2254
    const graph_t graph{ columns };
265 ✔
2255
    while (!walk.pending.empty())
4,618 ✔
2256
    {
2257
        const auto row = walk.pending.back();
4,353 ✔
2258
        walk.pending.pop_back();
4,353 ✔
2259
        change(walk, columns, row, resolved(graph, row));
4,353 ✔
2260
    }
2261

2262
    return reform(columns, walk.affected) && walk.complete;
265 ✔
2263
}
2264

2265
inline bool algorithm::reform(const columns_t& columns,
265 ✔
2266
    const std::span<const link_t>& rows) NOEXCEPT
2267
{
2268
    const graph_t graph{ columns };
265 ✔
2269
    for (const auto row: rows)
10,140 ✔
2270
        if (is_present(cell(graph, row)))
9,875 ✔
2271
            set_cell(graph, row, unlabeled);
6,975 ✔
2272

2273
    // Relevance closes over the rows in at most count passes.
2274
    for (auto changed = true; changed;)
730 ✔
2275
    {
2276
        changed = false;
2277
        for (const auto row: rows)
19,932 ✔
2278
        {
2279
            if (!is_present(cell(graph, row)) || is_labeled(cell(graph, row)))
19,467 ✔
2280
                continue;
14,217 ✔
2281

2282
            if (contested(graph, row) || descended(graph, row))
5,250 ✔
2283
            {
2284
                set_cell(graph, row, row);
2,575 ✔
2285
                changed = true;
2286
            }
2287
        }
2288
    }
2289

2290
    auto complete = true;
2291
    for (const auto row: rows)
10,140 ✔
2292
    {
2293
        // A row relabeled by a previous component carries a lower label.
2294
        if (cell(graph, row) != row)
9,875 ✔
2295
            continue;
8,984 ✔
2296

2297
        component_t component{};
891 ✔
2298
        if (collect(component, graph, row))
891 ✔
2299
            relabel(columns, component);
644 ✔
2300
        else
2301
            complete = false;
2302
    }
2303

2304
    return complete;
265 ✔
2305
}
2306

2307
template <typename Word>
2308
INLINE constexpr Word algorithm::sums(Word outcomes, const weights_t& weights,
2309
    size_t count) NOEXCEPT
2310
{
2311
    constexpr auto lane = bits<mask_t>;
2312
    auto sum = f::broadcast<Word>(mask_t{});
2313
    for (size_t member{}; member < count; ++member)
3,180 ✔
2314
    {
2315
        const auto bit = f::broadcast<Word>(set_right(mask_t{}, member));
2,650 ✔
2316
        const auto weight = f::broadcast<Word>(weights[member]);
2,650 ✔
2317
        const auto selected = f::eq<lane>(f::and_(outcomes, bit), bit);
2318
        sum = f::add<lane>(sum, f::and_(selected, weight));
2319
    }
2320

2321
    return sum;
2322
}
2323

2324
template <typename Word, typename Visitor>
2325
constexpr void algorithm::each_fee(size_t index,
30 ✔
2326
    const std::span<const mask_t>& outcomes, const component_t& component,
2327
    const graph_t& graph, const Visitor& visit) NOEXCEPT
2328
{
2329
    weights_t fees{};
30 ✔
2330
    for (size_t member{}; member < component.count; ++member)
180 ✔
2331
        fees[member] = totals(graph, component.rows[member]).fee;
150 ✔
2332

2333
    each_sum<Word>(index, outcomes, fees, component.count, visit);
30 ✔
2334
}
30 ✔
2335

2336
template <typename Word, typename Visitor>
2337
constexpr void algorithm::each_sum(size_t index,
59 ✔
2338
    const std::span<const mask_t>& outcomes, const weights_t& weights,
2339
    size_t members, const Visitor& visit) NOEXCEPT
2340
{
2341
    const auto count = outcomes.size();
35 ✔
2342
    if constexpr (is_extended<Word>)
2343
    {
2344
        if constexpr (have<Word>)
2345
        {
2346
            constexpr auto lanes = mask_lanes<Word>;
2347
            aligned_array<uint64_t, lanes> totals{};
2348
            for (; index + lanes <= count; index += lanes)
2349
            {
2350
                const auto word = load<Word>(&outcomes[index]);
2351
                store(totals.data(), sums<Word>(word, weights, members));
2352

2353
                for (size_t lane{}; lane < lanes; ++lane)
2354
                    visit(index + lane, totals[lane]);
2355
            }
2356
        }
2357

2358
        using next = narrower<Word, mask_t>;
2359
        each_sum<next>(index, outcomes, weights, members, visit);
24 ✔
2360
    }
2361
    else
2362
    {
2363
        for (; index < count; ++index)
565 ✔
2364
            visit(index, sums<mask_t>(outcomes[index], weights, members));
1,060 ✔
2365
    }
2366
}
59 ✔
2367

2368
template <typename Word>
2369
INLINE constexpr Word algorithm::native(Word word) NOEXCEPT
2370
{
2371
    if constexpr (is_little_endian)
2372
        return word;
2373
    else
2374
        return f::byteswap<link_t>(word);
2375
}
2376

2377
template <typename Word>
2378
constexpr bool algorithm::collect_(size_t index, component_t& out,
15 ✔
2379
    link_t label, const std::span<const link_t>& cells) NOEXCEPT
2380
{
2381
    const auto count = cells.size();
9 ✔
2382
    const auto stored = to_column(label);
9 ✔
2383
    const auto add = [&](size_t row) NOEXCEPT
241 ✔
2384
    {
2385
        if (out.count == maximum_members)
226 ✔
2386
            return false;
2387

2388
        out.rows[out.count++] = possible_narrow_cast<link_t>(row);
225 ✔
2389
        return true;
225 ✔
2390
    };
2391

2392
    if constexpr (is_extended<Word>)
2393
    {
2394
        if constexpr (have<Word>)
2395
        {
2396
            constexpr auto lanes = link_lanes<Word>;
2397
            const auto wanted = f::broadcast<Word>(stored);
2398
            for (; index + lanes <= count; index += lanes)
2399
            {
2400
                const auto word = load<Word>(&cells[index]);
2401
                const auto found = f::eq<bits<link_t>>(word, wanted);
2402

2403
                if (f::any(found))
2404
                    for (auto row = index; row < index + lanes; ++row)
2405
                        if (cells[row] == stored && !add(row))
2406
                            return false;
2407
            }
2408
        }
2409

2410
        return collect_<narrower<Word, link_t>>(index, out, label, cells);
6 ✔
2411
    }
2412
    else
2413
    {
2414
        for (; index < count; ++index)
557 ✔
2415
            if (cells[index] == stored && !add(index))
549 ✔
2416
                return false;
2417

2418
        return true;
2419
    }
2420
}
2421

2422
template <typename Word>
2423
inline void algorithm::find_(size_t index, std::vector<link_t>& out,
13 ✔
2424
    const std::span<const link_t>& values, link_t value) NOEXCEPT
2425
{
2426
    const auto count = values.size();
7 ✔
2427
    const auto stored = to_column(value);
7 ✔
2428
    if constexpr (is_extended<Word>)
2429
    {
2430
        if constexpr (have<Word>)
2431
        {
2432
            constexpr auto lanes = link_lanes<Word>;
2433
            const auto wanted = f::broadcast<Word>(stored);
2434
            for (; index + lanes <= count; index += lanes)
2435
            {
2436
                const auto word = load<Word>(&values[index]);
2437
                const auto found = f::eq<bits<link_t>>(word, wanted);
2438

2439
                if (f::any(found))
2440
                    for (auto row = index; row < index + lanes; ++row)
2441
                        if (values[row] == stored)
2442
                            out.push_back(possible_narrow_cast<link_t>(row));
2443
            }
2444
        }
2445

2446
        find_<narrower<Word, link_t>>(index, out, values, value);
6 ✔
2447
    }
2448
    else
2449
    {
2450
        for (; index < count; ++index)
298 ✔
2451
            if (values[index] == stored)
291 ✔
2452
                out.push_back(possible_narrow_cast<link_t>(index));
71 ✔
2453
    }
2454
}
13 ✔
2455

2456
template <typename Word, typename Integral>
2457
INLINE constexpr Word algorithm::load(const Integral* at) NOEXCEPT
2458
{
2459
    if constexpr (is_extended<Word>)
2460
        return f::load<Word>(pointer_cast<const uint8_t>(at));
2461
    else
2462
        return *at;
2463
}
2464

2465
template <typename Word, typename Integral>
2466
INLINE constexpr void algorithm::store(Integral* at, Word word) NOEXCEPT
2467
{
2468
    if constexpr (is_extended<Word>)
2469
        f::store(pointer_cast<uint8_t>(at), word);
2470
    else
2471
        *at = word;
2472
}
2473

2474
BC_POP_WARNING()
2475
BC_POP_WARNING()
2476
BC_POP_WARNING()
2477
BC_POP_WARNING()
2478

2479
} // namespace pool
2480
} // namespace system
2481
} // namespace libbitcoin
2482

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