cocycle 0.1.1

Topological data analysis in pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Native exact bottleneck kernel, adapted from Topp 1.0.1 (MIT).
//!
//! The adaptive gates match Topp commit ffa1da051ca7ac5e313c74cc9fb92a2bcb20c234.
//! Inputs here contain validated finite, strictly off-diagonal points only.
//! The facade owns essential points and input/provenance validation.
//!
//! Copyright (c) 2026 Topp contributors
//!
//! Permission is hereby granted, free of charge, to any person obtaining a copy
//! of this software and associated documentation files (the "Software"), to deal
//! in the Software without restriction, including without limitation the rights
//! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//! copies of the Software, and to permit persons to whom the Software is
//! furnished to do so, subject to the following conditions:
//!
//! The above copyright notice and this permission notice shall be included in all
//! copies or substantial portions of the Software.
//!
//! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//! SOFTWARE.

#[path = "bottleneck/flow.rs"]
mod flow;
#[path = "bottleneck/geometry.rs"]
mod geometry;
#[path = "bottleneck/matching.rs"]
mod matching;

use crate::execution::WorkBudget;
use crate::{Error, Result};

#[cfg(any(test, cocycle_distance_bench))]
#[path = "bottleneck/instrumentation.rs"]
mod instrumentation;
#[cfg(any(test, cocycle_distance_bench))]
pub(crate) use instrumentation::{Diagnostics, Options, Search};
#[cfg(not(any(test, cocycle_distance_bench)))]
#[derive(Clone, Copy, Default)]
struct Options {}
#[cfg(not(any(test, cocycle_distance_bench)))]
#[derive(Default)]
struct Diagnostics {}

const NONE: usize = usize::MAX;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(any(test, cocycle_distance_bench), derive(Default))]
pub(crate) enum Route {
    #[default]
    #[cfg(any(test, cocycle_distance_bench))]
    Empty,
    #[cfg(any(test, cocycle_distance_bench))]
    Identity,
    Multiplicity,
    #[cfg(any(test, cocycle_distance_bench))]
    NoCross,
    MandatorySparse,
    Quickselect,
    Refinement,
}

fn allocation() -> Error {
    Error::AllocationFailed {
        context: "bottleneck workspace",
    }
}

fn size_overflow() -> Error {
    Error::SizeOverflow {
        operation: "bottleneck workspace shape",
    }
}

fn reserve<T>(values: &mut Vec<T>, additional: usize) -> Result<()> {
    values.try_reserve(additional).map_err(|_| allocation())
}

fn filled<T: Clone>(len: usize, value: T) -> Result<Vec<T>> {
    let mut result = Vec::new();
    result.try_reserve_exact(len).map_err(|_| allocation())?;
    result.resize(len, value);
    Ok(result)
}

fn push<T>(values: &mut Vec<T>, value: T) -> Result<()> {
    reserve(values, 1)?;
    values.push(value);
    Ok(())
}

#[cfg(any(test, cocycle_distance_bench))]
fn bytes<T>(values: &Vec<T>) -> usize {
    values.capacity().saturating_mul(std::mem::size_of::<T>())
}

fn cross(a: [f64; 2], b: [f64; 2]) -> f64 {
    (a[0] - b[0]).abs().max((a[1] - b[1]).abs())
}

fn diagonal(point: [f64; 2]) -> Result<f64> {
    // The diagonal is a real-valued locus, not a rounded representable midpoint.
    // In particular an adjacent-float lifetime has half an ULP of diagonal cost.
    // This intentionally fixes Topp C0's midpoint-rounding boundary discrepancy.
    let lifetime = point[1] - point[0];
    let result = if lifetime.is_finite() {
        lifetime * 0.5
    } else {
        point[1] * 0.5 - point[0] * 0.5
    };
    if result == 0.0 {
        return Err(Error::NumericalFailure {
            context: "bottleneck positive diagonal cost underflow",
        });
    }
    Ok(result)
}

struct Prepared<'a> {
    points: &'a [[f64; 2]],
    diagonals: Vec<f64>,
    order: Vec<usize>,
    representatives: Vec<usize>,
    multiplicities: Vec<usize>,
    max_diagonal: f64,
}

impl<'a> Prepared<'a> {
    fn new<const CONTROLLED: bool>(
        points: &'a [[f64; 2]],
        budget: &mut WorkBudget<'_, CONTROLLED>,
    ) -> Result<Self> {
        budget.step_by(points.len())?;
        let mut diagonals = filled(points.len(), 0.0)?;
        let mut order = filled(points.len(), 0)?;
        let mut max_diagonal: f64 = 0.0;
        for (index, &point) in points.iter().enumerate() {
            if CONTROLLED && index % 256 == 0 {
                budget.step_by((points.len() - index).min(256))?;
            }
            diagonals[index] = diagonal(point)?;
            max_diagonal = max_diagonal.max(diagonals[index]);
            order[index] = index;
        }
        // Equal-coordinate groups remain contiguous; the final index tie break
        // makes ordering deterministic without an allocating stable sort.
        budget.step_by(order.len())?;
        order.sort_unstable_by(|&a, &b| {
            points[a][0]
                .partial_cmp(&points[b][0])
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| points[a][1].total_cmp(&points[b][1]))
                .then(a.cmp(&b))
        });
        let mut representatives = Vec::new();
        let mut multiplicities = Vec::new();
        reserve(&mut representatives, points.len())?;
        reserve(&mut multiplicities, points.len())?;
        for (position, &index) in order.iter().enumerate() {
            if CONTROLLED && position % 256 == 0 {
                budget.step_by((order.len() - position).min(256))?;
            }
            if representatives
                .last()
                .is_some_and(|&old| points[old] == points[index])
            {
                // A group cannot exceed the borrowed slice's length.
                if let Some(count) = multiplicities.last_mut() {
                    *count += 1;
                }
            } else {
                representatives.push(index);
                multiplicities.push(1);
            }
        }
        Ok(Self {
            points,
            diagonals,
            order,
            representatives,
            multiplicities,
            max_diagonal,
        })
    }

    #[inline]
    fn window(&self, birth: f64, radius: f64) -> std::ops::Range<usize> {
        let lower = (birth - radius).next_down();
        let upper = (birth + radius).next_up();
        let begin = self
            .order
            .partition_point(|&index| self.points[index][0] < lower);
        let end = self
            .order
            .partition_point(|&index| self.points[index][0] <= upper);
        begin..end
    }

    #[cfg(any(test, cocycle_distance_bench))]
    fn bytes(&self) -> usize {
        bytes(&self.diagonals)
            .saturating_add(bytes(&self.order))
            .saturating_add(bytes(&self.representatives))
            .saturating_add(bytes(&self.multiplicities))
    }
}

struct Pair<'a, 'p> {
    first: &'p Prepared<'a>,
    second: &'p Prepared<'a>,
    size: usize,
    dense: Vec<f64>,
}

impl<'a, 'p> Pair<'a, 'p> {
    // Keep dense preparation in the specialized caller.
    #[inline(always)]
    fn new<const CONTROLLED: bool>(
        first: &'p Prepared<'a>,
        second: &'p Prepared<'a>,
        dense: bool,
        budget: &mut WorkBudget<'_, CONTROLLED>,
    ) -> Result<Self> {
        budget.check()?;
        let size = first
            .points
            .len()
            .checked_add(second.points.len())
            .ok_or_else(size_overflow)?;
        let mut result = Self {
            first,
            second,
            size,
            dense: Vec::new(),
        };
        if dense {
            let count = first
                .points
                .len()
                .checked_mul(second.points.len())
                .ok_or_else(size_overflow)?;
            reserve(&mut result.dense, count)?;
            for &a in first.points {
                budget.step_by(second.points.len())?;
                for &b in second.points {
                    result.dense.push(cross(a, b));
                }
            }
        }
        Ok(result)
    }

    // Specialized matchers also need to inline this per-edge lookup.
    #[inline(always)]
    fn cross(&self, left: usize, right: usize) -> f64 {
        if self.dense.is_empty() {
            cross(self.first.points[left], self.second.points[right])
        } else {
            self.dense[left * self.second.points.len() + right]
        }
    }

    fn upper(&self) -> f64 {
        self.first.max_diagonal.max(self.second.max_diagonal)
    }

    fn lower(&self) -> f64 {
        (self.first.max_diagonal - self.second.max_diagonal).abs()
    }

    fn allowed(&self, left: usize, right: usize, radius: f64) -> bool {
        let n = self.first.points.len();
        let m = self.second.points.len();
        if left < n {
            if right < m {
                self.cross(left, right) <= radius
            } else {
                right == m + left && self.first.diagonals[left] <= radius
            }
        } else if right < m {
            right == left - n && self.second.diagonals[right] <= radius
        } else {
            true
        }
    }

    #[cfg(any(test, cocycle_distance_bench))]
    fn bytes(&self) -> usize {
        self.first
            .bytes()
            .saturating_add(self.second.bytes())
            .saturating_add(bytes(&self.dense))
    }
}

fn prefer_multiplicity(first: &Prepared<'_>, second: &Prepared<'_>, total: usize) -> bool {
    if total < 128 {
        return false;
    }
    let divisor = if total >= 1024 {
        4
    } else if total >= 512 {
        5
    } else {
        6
    };
    first.representatives.len() + second.representatives.len() <= total.div_ceil(divisor)
}

fn identical(first: &Prepared<'_>, second: &Prepared<'_>) -> bool {
    first.multiplicities == second.multiplicities
        && first
            .representatives
            .iter()
            .zip(&second.representatives)
            .all(|(&a, &b)| first.points[a] == second.points[b])
}

fn prefer_mandatory(first: &Prepared<'_>, second: &Prepared<'_>, total: usize, upper: f64) -> bool {
    if total < 256 || upper <= 0.0 {
        return false;
    }
    let small = first
        .diagonals
        .iter()
        .chain(&second.diagonals)
        .filter(|&&value| value <= upper * 0.05)
        .count();
    if small >= total.div_ceil(2) {
        return true;
    }
    let mut low = f64::INFINITY;
    let mut high = f64::NEG_INFINITY;
    for prepared in [first, second] {
        if let (Some(&a), Some(&b)) = (prepared.order.first(), prepared.order.last()) {
            low = low.min(prepared.points[a][0]);
            high = high.max(prepared.points[b][0]);
        }
    }
    let span = high - low;
    let cutoff = if span.is_finite() {
        span * 0.02
    } else {
        high * 0.02 - low * 0.02
    };
    span > 0.0 && upper <= cutoff
}

pub(crate) fn distance<const CONTROLLED: bool>(
    first: &[[f64; 2]],
    second: &[[f64; 2]],
    budget: &mut WorkBudget<'_, CONTROLLED>,
) -> Result<f64> {
    solve(
        first,
        second,
        Options::default(),
        &mut Diagnostics::default(),
        budget,
    )
}

#[cfg(any(test, cocycle_distance_bench))]
pub(crate) fn distance_with_options(
    first: &[[f64; 2]],
    second: &[[f64; 2]],
    _options: Options,
    _stats: &mut Diagnostics,
) -> Result<f64> {
    solve(
        first,
        second,
        _options,
        _stats,
        &mut WorkBudget::unlimited(),
    )
}

fn solve<const CONTROLLED: bool>(
    first: &[[f64; 2]],
    second: &[[f64; 2]],
    _options: Options,
    _stats: &mut Diagnostics,
    budget: &mut WorkBudget<'_, CONTROLLED>,
) -> Result<f64> {
    record! { *_stats = Diagnostics::default(); }
    let first = Prepared::new(first, budget)?;
    let second = Prepared::new(second, budget)?;
    let total = first
        .points
        .len()
        .checked_add(second.points.len())
        .ok_or_else(size_overflow)?;
    record! { _stats.workspace(first.bytes().saturating_add(second.bytes())); }
    if first.points.is_empty() || second.points.is_empty() {
        return Ok(first.max_diagonal.max(second.max_diagonal));
    }
    budget.step_by(total)?;
    let duplicates = prefer_multiplicity(&first, &second, total);
    if duplicates && identical(&first, &second) {
        record! { _stats.route = Route::Identity; }
        return Ok(0.0);
    }
    let (first, second) = if total >= 128 && first.points.len() > second.points.len() {
        (&second, &first)
    } else {
        (&first, &second)
    };
    let upper = first.max_diagonal.max(second.max_diagonal);
    let mut route = Route::Quickselect;
    if experiment!(_options.search == Search::Adaptive, true) {
        if duplicates {
            route = Route::Multiplicity;
        } else if total >= 128 {
            let mut window_pairs = 0_usize;
            for (position, &index) in first.order.iter().enumerate() {
                if CONTROLLED && position % 256 == 0 {
                    budget.step_by((first.order.len() - position).min(256))?;
                }
                window_pairs = window_pairs
                    .checked_add(second.window(first.points[index][0], upper).len())
                    .ok_or_else(size_overflow)?;
            }
            if window_pairs == 0 {
                record! { _stats.route = Route::NoCross; }
                return Ok(upper);
            }
            budget.step_by(total)?;
            if prefer_mandatory(first, second, total, upper) {
                route = Route::MandatorySparse;
            } else if first.points.len().min(second.points.len()) >= 32
                && window_pairs as f64 / (first.points.len() as f64 * second.points.len() as f64)
                    >= 0.01
            {
                route = Route::Refinement;
            }
        }
    } else if experiment!(_options.search == Search::Refinement, false) {
        route = Route::Refinement;
    }
    record! { _stats.route = route; }
    let pair = Pair::new(first, second, route == Route::Quickselect, budget)?;
    if route == Route::Refinement {
        return geometry::distance(&pair, _options, _stats, budget);
    }
    let mut radii = candidates(
        &pair,
        route,
        experiment!(_options.clip_candidates, true),
        0.0,
        pair.upper(),
        _stats,
        budget,
    )?;
    let extra_bytes = experiment!(pair.bytes().saturating_add(bytes(&radii)), 0);
    record! { _stats.workspace(extra_bytes); }
    let mut workspace = if route == Route::Quickselect {
        Some(matching::Workspace::new(pair.size)?)
    } else {
        None
    };
    let mut decide = |radius, budget: &mut WorkBudget<'_, CONTROLLED>| -> Result<bool> {
        budget.step()?;
        record! { _stats.threshold_decisions += 1; }
        if radius < pair.lower() {
            return Ok(false);
        }
        match route {
            Route::Multiplicity => flow::within(&pair, radius, true, _stats, extra_bytes, budget),
            Route::MandatorySparse => {
                flow::within(&pair, radius, false, _stats, extra_bytes, budget)
            }
            _ => {
                if pair.size >= 384 {
                    budget.step_by(pair.size)?;
                    let optional_first = pair
                        .first
                        .diagonals
                        .iter()
                        .filter(|&&d| d <= radius)
                        .count();
                    let optional_second = pair
                        .second
                        .diagonals
                        .iter()
                        .filter(|&&d| d <= radius)
                        .count();
                    let fraction = optional_first as f64 * optional_second as f64
                        / (pair.first.points.len() as f64 * pair.second.points.len() as f64);
                    let count = pair
                        .first
                        .points
                        .len()
                        .checked_mul(pair.second.points.len())
                        .ok_or_else(size_overflow)?;
                    let samples = count.min(64);
                    let mut allowed = 0;
                    for sample in 0..samples {
                        // Quotient/remainder form avoids overflowing sample * count.
                        let flat =
                            sample * (count / samples) + sample * (count % samples) / samples;
                        record! { _stats.adjacency_checks += 1; }
                        allowed += usize::from(
                            pair.cross(
                                flat / pair.second.points.len(),
                                flat % pair.second.points.len(),
                            ) <= radius,
                        );
                    }
                    if allowed as f64 / samples as f64 <= 0.03 || fraction >= 0.75 {
                        let allocated = experiment!(
                            extra_bytes.saturating_add(
                                workspace.as_ref().map_or(0, matching::Workspace::bytes),
                            ),
                            0
                        );
                        return flow::within(&pair, radius, false, _stats, allocated, budget);
                    }
                }
                if !experiment!(_options.reuse_scratch, true) {
                    workspace = Some(matching::Workspace::new(pair.size)?);
                }
                workspace
                    .as_mut()
                    .ok_or(Error::InternalInvariant {
                        reason: "quickselect matcher workspace is missing",
                    })?
                    .within(&pair, radius, _stats, extra_bytes, budget)
            }
        }
    };
    #[cfg(any(test, cocycle_distance_bench))]
    if _options.search == Search::Binary {
        budget.step_by(radii.len())?;
        radii.sort_unstable_by(f64::total_cmp);
        radii.dedup();
        let mut lower = radii.partition_point(|&value| value < pair.lower());
        let mut end = radii.len();
        while lower < end {
            let middle = lower + (end - lower) / 2;
            if decide(radii[middle], budget)? {
                end = middle;
            } else {
                lower = middle + 1;
            }
        }
        return radii.get(lower).copied().ok_or(Error::InternalInvariant {
            reason: "bottleneck has no feasible diagonal bound",
        });
    }
    budget.step_by(radii.len())?;
    radii.retain(|&value| value >= pair.lower());
    let mut remaining = radii.as_mut_slice();
    let mut best = pair.upper();
    while !remaining.is_empty() {
        budget.step_by(remaining.len())?;
        let middle = remaining.len() / 2;
        let (_, pivot, _) = remaining.select_nth_unstable_by(middle, f64::total_cmp);
        let pivot = *pivot;
        budget.step_by(remaining.len())?;
        if pivot >= best || decide(pivot, budget)? {
            best = best.min(pivot);
            let mut count = 0;
            for index in 0..remaining.len() {
                if remaining[index] < pivot {
                    remaining.swap(index, count);
                    count += 1;
                }
            }
            remaining = &mut remaining[..count];
        } else {
            let mut count = 0;
            for index in 0..remaining.len() {
                if remaining[index] > pivot {
                    remaining.swap(index, count);
                    count += 1;
                }
            }
            remaining = &mut remaining[..count];
        }
    }
    Ok(best)
}

fn candidates<const CONTROLLED: bool>(
    pair: &Pair<'_, '_>,
    route: Route,
    clip: bool,
    lower: f64,
    upper: f64,
    _stats: &mut Diagnostics,
    budget: &mut WorkBudget<'_, CONTROLLED>,
) -> Result<Vec<f64>> {
    let grouped = route == Route::Multiplicity;
    let first_indices = if grouped {
        &pair.first.representatives
    } else {
        &pair.first.order
    };
    let second_indices = if grouped {
        &pair.second.representatives
    } else {
        &pair.second.order
    };
    let mut values = Vec::new();
    push(&mut values, 0.0)?;
    for (prepared, indices) in [(pair.first, first_indices), (pair.second, second_indices)] {
        budget.step_by(indices.len())?;
        for &index in indices {
            let value = prepared.diagonals[index];
            if !clip || (value >= lower && value <= upper) {
                push(&mut values, value)?;
            }
        }
    }
    for &left in first_indices {
        if clip && matches!(route, Route::MandatorySparse | Route::Refinement) {
            let window = pair.second.window(pair.first.points[left][0], upper);
            budget.step_by(window.len().max(1))?;
            for position in window {
                let right = pair.second.order[position];
                record! { _stats.adjacency_checks += 1; }
                let value = pair.cross(left, right);
                if value > lower && value <= upper {
                    push(&mut values, value)?;
                }
            }
        } else {
            budget.step_by(second_indices.len().max(1))?;
            for &right in second_indices {
                let value = pair.cross(left, right);
                if !clip || (value >= lower && value <= upper) {
                    push(&mut values, value)?;
                }
            }
        }
    }
    record! { _stats.candidate_count = _stats.candidate_count.saturating_add(values.len()); }
    Ok(values)
}

#[cfg(test)]
#[path = "bottleneck/tests.rs"]
mod tests;