jixel 0.5.11

JPEG XL encoder
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
 * // Copyright (c) Radzivon Bartoshyk 10/2026. All rights reserved.
 * //
 * // Redistribution and use in source and binary forms, with or without modification,
 * // are permitted provided that the following conditions are met:
 * //
 * // 1.  Redistributions of source code must retain the above copyright notice, this
 * // list of conditions and the following disclaimer.
 * //
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
 * // this list of conditions and the following disclaimer in the documentation
 * // and/or other materials provided with the distribution.
 * //
 * // 3.  Neither the name of the copyright holder nor the names of its
 * // contributors may be used to endorse or promote products derived from
 * // this software without specific prior written permission.
 * //
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//! Visiting orders for point sets coded as one absolute position followed by
//! signed deltas (spline starts, patch occurrences): an open Hamiltonian path
//! priced by the hybrid-uint tokens it emits.

use crate::coder_scratch::CoderScratch;
use crate::entropy::{pack_signed, uint_encode};
use crate::shared_sort::SharedSort;
use crate::thread_pool::ThreadPool;
use std::cmp::Reverse;
use std::sync::Mutex;

const ALPHABET: usize = 128;
const HELD_KARP_MAX: usize = 9;
const NEIGHBORS: usize = 8;
const OR_OPT_SEGMENT: usize = 3;
const MAX_ROUNDS: usize = 8;
const MAX_POINTS: usize = 1 << 16;
const MIN_GAIN: f64 = 1e-3;
const SMALL_VALUES: usize = 4096;

pub(crate) type Pos = (i32, i32);

/// Bits of a value in one context: the token's share of a histogram plus the
/// raw bits below it.
#[derive(Clone)]
pub(crate) struct Prices {
    tokens: [f32; ALPHABET],
    small: Vec<f32>,
}

impl Prices {
    pub(crate) fn learn(values: impl IntoIterator<Item = u32>) -> Self {
        let mut counts = [0u32; ALPHABET];
        let mut total = 0u32;
        let mut top = 0usize;
        for v in values {
            let t = (uint_encode(v).0 as usize).min(ALPHABET - 1);
            counts[t] += 1;
            total += 1;
            top = top.max(t);
        }
        // Half a count on every token up to just past the largest seen; each
        // token beyond that costs a bit more than the last.
        let span = (top + 3).min(ALPHABET);
        let denom = total as f32 + 0.5 * span as f32;
        let mut bits = [0f32; ALPHABET];
        for (t, b) in bits.iter_mut().enumerate() {
            *b = if t < span {
                (denom / (counts[t] as f32 + 0.5)).log2()
            } else {
                (denom / 0.5).log2() + (t + 1 - span) as f32
            };
        }
        let small = (0..SMALL_VALUES as u32)
            .map(|v| Self::encoded_bits(&bits, v))
            .collect();
        Self {
            tokens: bits,
            small,
        }
    }

    fn encoded_bits(tokens: &[f32; ALPHABET], value: u32) -> f32 {
        let (token, nbits, _) = uint_encode(value);
        tokens[(token as usize).min(ALPHABET - 1)] + nbits as f32
    }

    #[inline]
    pub(crate) fn bits(&self, value: u32) -> f32 {
        match self.small.get(value as usize) {
            Some(&bits) => bits,
            None => Self::encoded_bits(&self.tokens, value),
        }
    }
}

/// Prices of the first (absolute) position and of every later delta.
pub(crate) struct PathCost<'a> {
    pub(crate) first: &'a Prices,
    pub(crate) delta: &'a Prices,
}

impl PathCost<'_> {
    #[inline]
    fn first(&self, p: Pos) -> f32 {
        self.first.bits(p.0 as u32) + self.first.bits(p.1 as u32)
    }

    #[inline]
    fn step(&self, a: Pos, b: Pos) -> f32 {
        self.delta.bits(pack_signed(b.0 - a.0)) + self.delta.bits(pack_signed(b.1 - a.1))
    }

    fn of(&self, points: &[Pos], order: &[usize]) -> f64 {
        let Some(&head) = order.first() else {
            return 0.0;
        };
        self.first(points[head]) as f64
            + order
                .array_windows::<2>()
                .map(|&[a, b]| self.step(points[a], points[b]) as f64)
                .sum::<f64>()
    }
}

/// Visiting orders of point sets that share one context for first positions
/// and one for deltas (the same one when `one_context`). Sets not `movable`
/// keep `given`. The start is the given orders or serpentine sweeps, each
/// judged under prices learned from itself; then `PRICE_ROUNDS` times the
/// prices are relearned and every movable set is reordered under them.
pub(crate) fn order_sets(
    sets: &[Vec<Pos>],
    movable: &[bool],
    given: Vec<Vec<usize>>,
    one_context: bool,
    workers: Option<(&ThreadPool, &mut CoderScratch)>,
) -> Vec<Vec<usize>> {
    const PRICE_ROUNDS: usize = 2;
    const NEIGHBOR_CACHE_BYTES: usize = 4 * 1024 * 1024;
    // Large dot dictionaries have several expensive sets. Limit the number
    // of simultaneous searches over sets above `SMALL_SET_POINTS` to bound
    // their transient workspace.
    const MAX_SEARCH_THREADS: usize = 4;
    const PARALLEL_SET_POINTS: usize = 512;
    const SMALL_SET_POINTS: usize = 16_384;
    let mut workers = workers.filter(|(pool, _)| {
        pool.num_threads() > 1
            && sets
                .iter()
                .zip(movable)
                .filter(|(points, movable)| {
                    **movable && (PARALLEL_SET_POINTS..=MAX_POINTS).contains(&points.len())
                })
                .nth(1)
                .is_some()
    });
    let sweeps: Vec<Vec<Vec<usize>>> = sets
        .iter()
        .zip(movable)
        .map(|(points, &m)| {
            if m && points.len() > HELD_KARP_MAX {
                serpentine_sweeps(points)
            } else {
                Vec::new()
            }
        })
        .collect();
    let mut orders = given;
    let mut best = self_priced(sets, &orders, one_context);
    for k in 0..SWEEPS.len() {
        let candidate: Vec<Vec<usize>> = orders
            .iter()
            .zip(&sweeps)
            .map(|(order, sweeps)| sweeps.get(k).unwrap_or(order).clone())
            .collect();
        let bits = self_priced(sets, &candidate, one_context);
        if bits < best {
            (best, orders) = (bits, candidate);
        }
    }
    // Neighbor order depends only on the points. Retain it for repricing,
    // with a cap on storage across all sets in a dictionary.
    let mut neighbors_by_set: Vec<Option<NeighborLists>> = (0..sets.len()).map(|_| None).collect();
    let remaining_bytes = Mutex::new(NEIGHBOR_CACHE_BYTES);
    for round in 0..PRICE_ROUNDS {
        let (first, delta) = learn(sets, &orders, one_context);
        let cost = PathCost {
            first: &first,
            delta: &delta,
        };
        let search = |r: usize, path: &mut Vec<usize>, cached: &mut Option<NeighborLists>| {
            let mut neighbors = cached.take();
            *path = order(&sets[r], &cost, std::mem::take(path), &mut neighbors);
            if round + 1 < PRICE_ROUNDS
                && let Some(neighbors) = neighbors
            {
                let mut available = remaining_bytes.lock().unwrap();
                if let Some(remaining) = available.checked_sub(neighbors.capacity_bytes()) {
                    *available = remaining;
                    *cached = Some(neighbors);
                }
            }
        };
        let tasks = orders
            .iter_mut()
            .zip(&mut neighbors_by_set)
            .enumerate()
            .filter(|(r, _)| movable[*r]);
        if let Some((pool, scratch)) = &mut workers {
            // Prices are fixed for this round. Independent searches can start
            // with the largest sets and still update results in their own slots.
            let mut tasks: Vec<_> = tasks.collect();
            tasks.shared_sort_unstable_by_key(|(r, _)| Reverse(sets[*r].len()));
            // Only the large sets carry a workspace worth bounding; the many
            // small ones of a dot dictionary use every worker.
            let large = tasks.partition_point(|(r, _)| sets[*r].len() > SMALL_SET_POINTS);
            let (large, small) = tasks.split_at_mut(large);
            for (tasks, threads) in [(large, MAX_SEARCH_THREADS), (small, pool.num_threads())] {
                pool.steal_for_each_mut_with_threads(
                    scratch,
                    tasks,
                    threads,
                    |_, (r, (path, cached)), _| search(*r, path, cached),
                );
            }
        } else {
            for (r, (path, cached)) in tasks {
                search(r, path, cached);
            }
        }
    }
    orders
}

fn learn(sets: &[Vec<Pos>], orders: &[Vec<usize>], one_context: bool) -> (Prices, Prices) {
    let firsts = || {
        orders
            .iter()
            .zip(sets)
            .filter(|(o, _)| !o.is_empty())
            .flat_map(|(o, p)| {
                let (x, y) = p[o[0]];
                [x as u32, y as u32]
            })
    };
    let deltas = || {
        orders.iter().zip(sets).flat_map(|(o, p)| {
            o.array_windows::<2>().flat_map(move |&[a, b]| {
                [pack_signed(p[b].0 - p[a].0), pack_signed(p[b].1 - p[a].1)]
            })
        })
    };
    if one_context {
        let prices = Prices::learn(firsts().chain(deltas()));
        (prices.clone(), prices)
    } else {
        (Prices::learn(firsts()), Prices::learn(deltas()))
    }
}

/// Bits of `orders` under prices learned from themselves.
fn self_priced(sets: &[Vec<Pos>], orders: &[Vec<usize>], one_context: bool) -> f64 {
    let (first, delta) = learn(sets, orders, one_context);
    let cost = PathCost {
        first: &first,
        delta: &delta,
    };
    sets.iter().zip(orders).map(|(p, o)| cost.of(p, o)).sum()
}

/// Cheapest open path through `points` under `cost`: exact for small sets,
/// otherwise 2-opt and Or-opt from `initial` over spatial neighbors.
fn order(
    points: &[Pos],
    cost: &PathCost,
    initial: Vec<usize>,
    neighbors: &mut Option<NeighborLists>,
) -> Vec<usize> {
    debug_assert_eq!(points.len(), initial.len());
    if points.len() <= 1 {
        initial
    } else if points.len() <= HELD_KARP_MAX {
        held_karp(points, cost)
    } else if points.len() <= MAX_POINTS {
        let neighbors = neighbors.get_or_insert_with(|| neighbor_lists(points));
        LocalSearch::new(points, cost, initial, neighbors).run()
    } else {
        initial
    }
}

/// Band heights of the serpentine sweeps, in mean point spacings.
static SWEEPS: [f64; 3] = [1.0, 2.0, 4.0];

/// Boustrophedon orders over horizontal bands: scattered points sorted by
/// row alone zigzag across the whole width, which local moves cannot undo.
fn serpentine_sweeps(points: &[Pos]) -> Vec<Vec<usize>> {
    let (min_x, max_x) = points
        .iter()
        .fold((i32::MAX, i32::MIN), |(a, b), p| (a.min(p.0), b.max(p.0)));
    let (min_y, max_y) = points
        .iter()
        .fold((i32::MAX, i32::MIN), |(a, b), p| (a.min(p.1), b.max(p.1)));
    let area = (max_x - min_x + 1) as f64 * (max_y - min_y + 1) as f64;
    let spacing = (area / points.len() as f64).sqrt().max(1.0);
    SWEEPS
        .iter()
        .map(|&k| {
            let band = (spacing * k).round().max(1.0) as i32;
            let mut path: Vec<usize> = (0..points.len()).collect();
            path.sort_unstable_by_key(|&i| {
                let (x, y) = points[i];
                let row = (y - min_y) / band;
                (row, if row & 1 == 0 { x } else { -x }, y)
            });
            path
        })
        .collect()
}

fn held_karp(points: &[Pos], cost: &PathCost) -> Vec<usize> {
    let n = points.len();
    let full = (1usize << n) - 1;
    let step: Vec<f32> = (0..n * n)
        .map(|k| cost.step(points[k / n], points[k % n]))
        .collect();
    let mut best = vec![f32::INFINITY; (full + 1) * n];
    let mut parent = vec![u8::MAX; (full + 1) * n];
    for i in 0..n {
        best[(1 << i) * n + i] = cost.first(points[i]);
    }
    for mask in 1..=full {
        for last in 0..n {
            let here = best[mask * n + last];
            if mask & (1 << last) == 0 || here == f32::INFINITY {
                continue;
            }
            for next in 0..n {
                if mask & (1 << next) != 0 {
                    continue;
                }
                let to = (mask | (1 << next)) * n + next;
                let c = here + step[last * n + next];
                if c < best[to] {
                    best[to] = c;
                    parent[to] = last as u8;
                }
            }
        }
    }
    let mut last = (0..n)
        .min_by(|&a, &b| best[full * n + a].total_cmp(&best[full * n + b]))
        .unwrap();
    let mut mask = full;
    let mut path = Vec::with_capacity(n);
    loop {
        path.push(last);
        let p = parent[mask * n + last];
        mask &= !(1 << last);
        if p == u8::MAX {
            break;
        }
        last = p as usize;
    }
    path.reverse();
    path
}

struct NeighborLists {
    offsets: Vec<usize>,
    indices: Vec<usize>,
}

impl NeighborLists {
    fn capacity_bytes(&self) -> usize {
        (self.offsets.capacity() + self.indices.capacity()) * size_of::<usize>()
    }
}

/// Up to `NEIGHBORS` nearest points (Chebyshev) of each point plus its
/// neighbors in row-major and column-major order, which keep long runs along
/// one row or column reachable.
fn neighbor_lists(points: &[Pos]) -> NeighborLists {
    let n = points.len();
    let (min_x, max_x) = points
        .iter()
        .fold((i32::MAX, i32::MIN), |(a, b), p| (a.min(p.0), b.max(p.0)));
    let (min_y, max_y) = points
        .iter()
        .fold((i32::MAX, i32::MIN), |(a, b), p| (a.min(p.1), b.max(p.1)));
    let (w, h) = ((max_x - min_x + 1) as i64, (max_y - min_y + 1) as i64);
    let cell = (((w * h) as f64 / n as f64).sqrt().ceil() as i64).max(1);
    let divider = crate::fast_divide::DividerI64::new(cell);
    let (gw, gh) = (
        ((w + cell - 1) / cell) as usize,
        ((h + cell - 1) / cell) as usize,
    );
    let cell_of = |p: Pos| {
        (
            ((p.0 - min_x) as i64 / divider) as usize,
            ((p.1 - min_y) as i64 / divider) as usize,
        )
    };
    let cells: Vec<_> = points.iter().map(|&p| cell_of(p)).collect();
    let mut start = vec![0usize; gw * gh + 1];
    for &(cx, cy) in &cells {
        start[cy * gw + cx + 1] += 1;
    }
    for k in 0..gw * gh {
        start[k + 1] += start[k];
    }
    let mut fill = start.clone();
    let mut members = vec![0usize; n];
    for (i, &(cx, cy)) in cells.iter().enumerate() {
        members[fill[cy * gw + cx]] = i;
        fill[cy * gw + cx] += 1;
    }

    let mut by_row: Vec<usize> = (0..n).collect();
    by_row.shared_sort_unstable_by_key(|&i| (points[i].1, points[i].0));
    let mut by_col: Vec<usize> = (0..n).collect();
    by_col.shared_sort_unstable_by_key(|&i| (points[i].0, points[i].1));
    let mut extra = vec![Vec::new(); n];
    for order in [&by_row, &by_col] {
        for k in 0..n {
            for d in 1..=2 {
                if k >= d {
                    extra[order[k]].push(order[k - d]);
                }
                if k + d < n {
                    extra[order[k]].push(order[k + d]);
                }
            }
        }
    }

    let mut offsets = Vec::with_capacity(n + 1);
    offsets.push(0);
    let mut lists = Vec::with_capacity(n * (NEIGHBORS + 4));
    let mut near: Vec<(i32, usize)> = Vec::with_capacity(NEIGHBORS + 1);
    for (i, &p) in points.iter().enumerate() {
        near.clear();
        let (cx, cy) = cells[i];
        for ring in 0..gw.max(gh) {
            // Points in ring r are at least (r - 1) * cell + 1 away.
            if near.len() == NEIGHBORS && (ring as i64 - 1) * cell >= near[NEIGHBORS - 1].0 as i64 {
                break;
            }
            let (x0, x1) = (cx.saturating_sub(ring), (cx + ring).min(gw - 1));
            let (y0, y1) = (cy.saturating_sub(ring), (cy + ring).min(gh - 1));
            for gy in y0..=y1 {
                for gx in x0..=x1 {
                    if gx.abs_diff(cx).max(gy.abs_diff(cy)) != ring {
                        continue;
                    }
                    for &j in &members[start[gy * gw + gx]..start[gy * gw + gx + 1]] {
                        if j == i {
                            continue;
                        }
                        let d = (points[j].0 - p.0).abs().max((points[j].1 - p.1).abs());
                        if near.len() < NEIGHBORS || d < near[NEIGHBORS - 1].0 {
                            let at = near.partition_point(|&(e, _)| e <= d);
                            near.insert(at, (d, j));
                            near.truncate(NEIGHBORS);
                        }
                    }
                }
            }
        }
        let begin = lists.len();
        lists.extend(near.iter().map(|&(_, j)| j));
        for &j in &extra[i] {
            if !lists[begin..].contains(&j) {
                lists.push(j);
            }
        }
        offsets.push(lists.len());
    }
    NeighborLists {
        offsets,
        indices: lists,
    }
}

struct LocalSearch<'a> {
    points: &'a [Pos],
    cost: &'a PathCost<'a>,
    path: Vec<usize>,
    pos: Vec<usize>,
    /// `forward[k]`: cost of the steps path[0] -> ... -> path[k].
    forward: Vec<f64>,
    /// `backward[k]`: the same steps walked the other way.
    backward: Vec<f64>,
    neighbors: &'a NeighborLists,
    /// Points next to a move of this round or the last; the others had no
    /// improving move and are skipped (don't-look bits).
    looking: Vec<bool>,
    touched: Vec<bool>,
}

impl<'a> LocalSearch<'a> {
    fn new(
        points: &'a [Pos],
        cost: &'a PathCost<'a>,
        path: Vec<usize>,
        neighbors: &'a NeighborLists,
    ) -> Self {
        let n = points.len();
        let mut search = Self {
            points,
            cost,
            path,
            pos: vec![0; n],
            forward: vec![0.0; n],
            backward: vec![0.0; n],
            neighbors,
            looking: vec![true; n],
            touched: vec![false; n],
        };
        search.reindex(0, n);
        search
    }

    /// Refreshes `pos` and the step sums after path[lo..hi] changed: steps up
    /// to path[hi] are repriced, the later sums only shift.
    fn reindex(&mut self, lo: usize, hi: usize) {
        for k in lo..hi {
            self.pos[self.path[k]] = k;
        }
        let n = self.path.len();
        let stop = hi.min(n - 1);
        let (forward, backward) = (self.forward[stop], self.backward[stop]);
        for k in lo.max(1)..=stop {
            let (a, b) = (self.points[self.path[k - 1]], self.points[self.path[k]]);
            self.forward[k] = self.forward[k - 1] + self.cost.step(a, b) as f64;
            self.backward[k] = self.backward[k - 1] + self.cost.step(b, a) as f64;
        }
        let (df, db) = (self.forward[stop] - forward, self.backward[stop] - backward);
        for k in stop + 1..n {
            self.forward[k] += df;
            self.backward[k] += db;
        }
    }

    /// Cost of joining `a` to `b`: the absolute position when nothing comes
    /// before `b`, nothing when nothing follows `a`.
    #[inline]
    fn edge(&self, a: Option<usize>, b: Option<usize>) -> f64 {
        match (a, b) {
            (Some(a), Some(b)) => self.cost.step(self.points[a], self.points[b]) as f64,
            (None, Some(b)) => self.cost.first(self.points[b]) as f64,
            _ => 0.0,
        }
    }

    fn neighbors(&self, i: usize) -> &[usize] {
        let NeighborLists { offsets, indices } = self.neighbors;
        &indices[offsets[i]..offsets[i + 1]]
    }

    fn looks_at(&self, i: usize) -> bool {
        self.looking[i] || self.touched[i]
    }

    fn touch(&mut self, points: impl IntoIterator<Item = Option<usize>>) {
        for i in points.into_iter().flatten() {
            self.touched[i] = true;
        }
    }

    fn run(mut self) -> Vec<usize> {
        for _ in 0..MAX_ROUNDS {
            let reversed = self.two_opt();
            let moved = self.or_opt();
            if !reversed && !moved {
                break;
            }
            std::mem::swap(&mut self.looking, &mut self.touched);
            self.touched.fill(false);
        }
        self.path
    }

    /// Reversing [i..=j] joins i-1 with j, i with j+1 and walks the inside
    /// backwards.
    fn two_opt(&mut self) -> bool {
        let n = self.path.len();
        let mut improved = false;
        let mut candidates = Vec::new();
        for i in 0..n - 1 {
            if !self.looks_at(self.path[i]) && (i == 0 || !self.looks_at(self.path[i - 1])) {
                continue;
            }
            candidates.clear();
            candidates.push(n - 1);
            if i > 0 {
                candidates.extend(
                    self.neighbors(self.path[i - 1])
                        .iter()
                        .map(|&c| self.pos[c])
                        .filter(|&j| j > i),
                );
            }
            candidates.extend(
                self.neighbors(self.path[i])
                    .iter()
                    .map(|&c| self.pos[c])
                    .filter(|&k| k > i + 1)
                    .map(|k| k - 1),
            );
            let prev = i.checked_sub(1).map(|k| self.path[k]);
            let mut best = (MIN_GAIN, 0usize);
            for &j in &candidates {
                let next = self.path.get(j + 1).copied();
                let (head, tail) = (self.path[i], self.path[j]);
                let before = self.edge(prev, Some(head))
                    + (self.forward[j] - self.forward[i])
                    + self.edge(Some(tail), next);
                let after = self.edge(prev, Some(tail))
                    + (self.backward[j] - self.backward[i])
                    + self.edge(Some(head), next);
                if before - after > best.0 {
                    best = (before - after, j);
                }
            }
            if best.0 > MIN_GAIN {
                let j = best.1;
                self.touch([
                    prev,
                    Some(self.path[i]),
                    Some(self.path[j]),
                    self.path.get(j + 1).copied(),
                ]);
                self.path[i..=j].reverse();
                self.reindex(i, j + 1);
                improved = true;
            }
        }
        improved
    }

    /// Moves [i, i + len) between two points of the remaining path, next to a
    /// neighbor of either end or at either end of the path, keeping or
    /// reversing its direction.
    fn or_opt(&mut self) -> bool {
        let n = self.path.len();
        let mut improved = false;
        for len in 1..=OR_OPT_SEGMENT.min(n - 1) {
            let mut i = 0;
            while i + len <= n {
                let end = i + len;
                let (first, last) = (self.path[i], self.path[end - 1]);
                if !self.looks_at(first) && !self.looks_at(last) {
                    i += 1;
                    continue;
                }
                let prev = i.checked_sub(1).map(|k| self.path[k]);
                let next = self.path.get(end).copied();
                let inside = [
                    self.forward[end - 1] - self.forward[i],
                    self.backward[end - 1] - self.backward[i],
                ];
                let removed =
                    self.edge(prev, Some(first)) + inside[0] + self.edge(Some(last), next)
                        - self.edge(prev, next);
                let reduced = |k: usize| if k < i { k } else { k - len };
                let left_of = |k: usize| -> Option<usize> {
                    let k = if k == end { i } else { k };
                    k.checked_sub(1).map(|k| self.path[k])
                };
                let right_of = |k: usize| -> Option<usize> {
                    let k = if k + 1 == i { end } else { k + 1 };
                    self.path.get(k).copied()
                };
                // (gain, insertion index in the reduced path, reversed, left, right)
                let mut best = (MIN_GAIN, 0usize, false, None, None);
                let mut consider = |left: Option<usize>, right: Option<usize>, at: usize| {
                    let cut = self.edge(left, right);
                    for reversed in [false, true] {
                        if reversed && len == 1 {
                            break;
                        }
                        let (head, tail) = if reversed {
                            (last, first)
                        } else {
                            (first, last)
                        };
                        let added = self.edge(left, Some(head))
                            + inside[reversed as usize]
                            + self.edge(Some(tail), right)
                            - cut;
                        if removed - added > best.0 {
                            best = (removed - added, at, reversed, left, right);
                        }
                    }
                };
                let front = if i == 0 { next } else { Some(self.path[0]) };
                let back = if end == n {
                    prev
                } else {
                    Some(self.path[n - 1])
                };
                consider(None, front, 0);
                consider(back, None, n - len);
                let ends: &[usize] = if len == 1 {
                    &self.neighbors(first)[..0]
                } else {
                    self.neighbors(last)
                };
                for &c in self.neighbors(first).iter().chain(ends) {
                    let k = self.pos[c];
                    if (i..end).contains(&k) {
                        continue;
                    }
                    consider(Some(c), right_of(k), reduced(k) + 1);
                    consider(left_of(k), Some(c), reduced(k));
                }
                if best.0 > MIN_GAIN {
                    let (_, at, reversed, left, right) = best;
                    self.touch([prev, next, Some(first), Some(last), left, right]);
                    // Remove [i, end) and reinsert it at `at` of the reduced
                    // path: only the points in between move.
                    if at >= i {
                        self.path[i..at + len].rotate_left(len);
                    } else {
                        self.path[at..end].rotate_right(len);
                    }
                    if reversed {
                        self.path[at..at + len].reverse();
                    }
                    self.reindex(i.min(at), end.max(at + len));
                    improved = true;
                }
                i += 1;
            }
        }
        improved
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn flat() -> Prices {
        Prices::learn(0..64)
    }

    fn brute_force(points: &[Pos], cost: &PathCost) -> f64 {
        fn go(points: &[Pos], cost: &PathCost, path: &mut Vec<usize>, best: &mut f64) {
            if path.len() == points.len() {
                *best = best.min(cost.of(points, path));
                return;
            }
            for i in 0..points.len() {
                if !path.contains(&i) {
                    path.push(i);
                    go(points, cost, path, best);
                    path.pop();
                }
            }
        }
        let mut best = f64::INFINITY;
        go(points, cost, &mut Vec::new(), &mut best);
        best
    }

    fn scatter(n: usize, seed: u32) -> Vec<Pos> {
        let mut s = seed;
        let mut next = || {
            s ^= s << 13;
            s ^= s >> 17;
            s ^= s << 5;
            s
        };
        (0..n)
            .map(|_| ((next() % 400) as i32, (next() % 300) as i32))
            .collect()
    }

    #[test]
    fn held_karp_matches_brute_force() {
        let prices = flat();
        let cost = PathCost {
            first: &prices,
            delta: &prices,
        };
        for seed in 1..6 {
            let points = scatter(7, seed);
            let path = held_karp(&points, &cost);
            let mut sorted = path.clone();
            sorted.shared_sort();
            assert_eq!(sorted, (0..7).collect::<Vec<_>>());
            assert!((cost.of(&points, &path) - brute_force(&points, &cost)).abs() < 1e-3);
        }
    }

    #[test]
    fn local_search_never_worsens_and_stays_a_permutation() {
        let prices = flat();
        let cost = PathCost {
            first: &prices,
            delta: &prices,
        };
        for seed in 1..4 {
            let points = scatter(300, seed);
            let initial: Vec<usize> = (0..points.len()).collect();
            let path = order(&points, &cost, initial.clone(), &mut None);
            let mut sorted = path.clone();
            sorted.shared_sort();
            assert_eq!(sorted, initial);
            assert!(cost.of(&points, &path) < cost.of(&points, &initial));
        }
    }

    #[test]
    fn rows_are_walked_back_and_forth() {
        // Raster order pays a long jump back at every row end.
        let points: Vec<Pos> = (0..6)
            .flat_map(|y| (0..20).map(move |x| (x * 9, y * 14)))
            .collect();
        let initial: Vec<usize> = (0..points.len()).collect();
        let deltas = initial
            .array_windows::<2>()
            .flat_map(|&[a, b]| {
                [
                    pack_signed(points[b].0 - points[a].0),
                    pack_signed(points[b].1 - points[a].1),
                ]
            })
            .collect::<Vec<_>>();
        let prices = Prices::learn(deltas);
        let cost = PathCost {
            first: &prices,
            delta: &prices,
        };
        let path = order(&points, &cost, initial.clone(), &mut None);
        assert!(cost.of(&points, &path) < cost.of(&points, &initial) - 10.0);
    }
}