java-diff-utils-rs 0.1.0-alpha.2

Experimental Rust port of the core diffing and patching behavior of java-diff-utils
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
//! Histogram diff algorithm (Patience / low-occurrence anchor based diff).
//!
//! Ported to match the behavior of JGit / `java-diff-utils` `HistogramDiff`.
//! This algorithm selects elements with low occurrence counts as anchors to split sequences
//! recursively, falling back to Myers' algorithm when no low-occurrence anchors remain.

use crate::algorithm::{
    change::{Change, DeltaType},
    diff_algorithm_factory::DiffAlgorithmFactory,
    diff_algorithm_listener::DiffAlgorithmListener,
    myers::myers_linear::MyersDiffWithLinearSpace,
    DiffAlgorithm,
};

/// Default maximum occurrence count for an element to be considered as a pivot anchor.
pub const DEFAULT_MAX_CHAIN_LENGTH: usize = 64;

/// Histogram diff algorithm implementation.
pub struct HistogramDiff<T> {
    max_chain_length: usize,
    equalizer: Option<Box<dyn Fn(&T, &T) -> bool>>,
}

impl<T> Default for HistogramDiff<T> {
    fn default() -> Self {
        Self {
            max_chain_length: DEFAULT_MAX_CHAIN_LENGTH,
            equalizer: None,
        }
    }
}

impl<T> HistogramDiff<T> {
    /// Creates a new `HistogramDiff` with default max chain length (64).
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets a custom maximum chain length threshold.
    #[must_use]
    pub fn with_max_chain_length(mut self, max_chain_length: usize) -> Self {
        self.max_chain_length = max_chain_length;
        self
    }

    /// Sets a custom element equality predicate.
    #[must_use]
    pub fn with_equalizer<F>(mut self, equalizer: F) -> Self
    where
        F: Fn(&T, &T) -> bool + 'static,
    {
        self.equalizer = Some(Box::new(equalizer));
        self
    }
}

impl<T: PartialEq> DiffAlgorithm<T> for HistogramDiff<T> {
    fn diff_with_listener(
        &self,
        source: &[T],
        target: &[T],
        listener: &mut dyn DiffAlgorithmListener,
    ) -> Vec<Change> {
        let eq: &dyn Fn(&T, &T) -> bool = match &self.equalizer {
            Some(f) => f.as_ref(),
            None => &|a, b| a == b,
        };
        compute_diff_full(source, target, eq, self.max_chain_length, Some(listener))
    }
}

/// Computes the diff between two slices using HistogramDiff and default equality.
pub fn compute_diff<T: PartialEq>(source: &[T], target: &[T]) -> Vec<Change> {
    compute_diff_with(source, target, |a, b| a == b)
}

/// Computes the diff between two slices using HistogramDiff and a custom equalizer.
pub fn compute_diff_with<T, F>(source: &[T], target: &[T], equalizer: F) -> Vec<Change>
where
    T: PartialEq,
    F: Fn(&T, &T) -> bool,
{
    compute_diff_full(
        source,
        target,
        &equalizer,
        DEFAULT_MAX_CHAIN_LENGTH,
        Option::<&mut dyn DiffAlgorithmListener>::None,
    )
}

/// Full histogram diff entry point with workspace and listener support.
pub fn compute_diff_full<T, F, L>(
    source: &[T],
    target: &[T],
    equalizer: &F,
    max_chain_length: usize,
    mut listener: Option<&mut L>,
) -> Vec<Change>
where
    T: PartialEq,
    F: Fn(&T, &T) -> bool + ?Sized,
    L: DiffAlgorithmListener + ?Sized,
{
    if source.is_empty() && target.is_empty() {
        return Vec::new();
    }

    if let Some(l) = listener.as_deref_mut() {
        l.diff_start();
    }

    let mut script = Vec::new();
    let total_steps = source.len() + target.len();

    histogram_rec(
        source,
        target,
        0,
        source.len(),
        0,
        target.len(),
        equalizer,
        max_chain_length,
        &mut script,
        listener.as_deref_mut(),
        total_steps,
    );

    normalize_replacements(&mut script);

    if let Some(l) = listener {
        l.diff_end();
    }

    script
}

#[derive(Clone, Copy)]
struct MatchAnchor {
    src_idx: usize,
    tgt_idx: usize,
    len: usize,
}

#[allow(clippy::too_many_arguments)]
fn histogram_rec<T, F, L>(
    source: &[T],
    target: &[T],
    mut src_start: usize,
    mut src_end: usize,
    mut tgt_start: usize,
    mut tgt_end: usize,
    equalizer: &F,
    max_chain_length: usize,
    script: &mut Vec<Change>,
    mut listener: Option<&mut L>,
    total_steps: usize,
) where
    T: PartialEq,
    F: Fn(&T, &T) -> bool + ?Sized,
    L: DiffAlgorithmListener + ?Sized,
{
    // Fast path: trim matching prefix
    while src_start < src_end
        && tgt_start < tgt_end
        && equalizer(&source[src_start], &target[tgt_start])
    {
        src_start += 1;
        tgt_start += 1;
    }

    // Fast path: trim matching suffix
    while src_end > src_start
        && tgt_end > tgt_start
        && equalizer(&source[src_end - 1], &target[tgt_end - 1])
    {
        src_end -= 1;
        tgt_end -= 1;
    }

    let src_len = src_end - src_start;
    let tgt_len = tgt_end - tgt_start;

    if src_len == 0 && tgt_len == 0 {
        return;
    }

    if let Some(l) = listener.as_deref_mut() {
        l.diff_step(src_start + tgt_start, total_steps);
    }

    if should_fallback_to_myers(source, src_start, src_end, target, tgt_start, tgt_end, equalizer) {
        let fallback_algo = MyersDiffWithLinearSpace::new();
        let sub_source = &source[src_start..src_end];
        let sub_target = &target[tgt_start..tgt_end];

        let sub_changes = fallback_algo.diff_with_listener(
            sub_source,
            sub_target,
            &mut crate::algorithm::diff_algorithm_listener::NoOpListener,
        );

        for c in sub_changes {
            push_change(
                script,
                c.delta_type,
                src_start + c.start_original,
                src_start + c.end_original,
                tgt_start + c.start_revised,
                tgt_start + c.end_revised,
            );
        }
        return;
    }

    // Base cases: purely insertion or deletion
    if src_len == 0 {
        push_change(
            script,
            DeltaType::Insert,
            src_start,
            src_start,
            tgt_start,
            tgt_end,
        );
        return;
    }
    if tgt_len == 0 {
        push_change(
            script,
            DeltaType::Delete,
            src_start,
            src_end,
            tgt_start,
            tgt_start,
        );
        return;
    }

    let src_distinct = distinct_count(source, src_start, src_end, equalizer);
    let tgt_distinct = distinct_count(target, tgt_start, tgt_end, equalizer);

    if (src_distinct > 32 || tgt_distinct > 32)
        && (has_high_frequency_value(source, src_start, src_end, equalizer, max_chain_length)
            || has_high_frequency_value(target, tgt_start, tgt_end, equalizer, max_chain_length))
    {
        let fallback_algo = MyersDiffWithLinearSpace::new();
        let sub_source = &source[src_start..src_end];
        let sub_target = &target[tgt_start..tgt_end];

        let sub_changes = fallback_algo.diff_with_listener(
            sub_source,
            sub_target,
            &mut crate::algorithm::diff_algorithm_listener::NoOpListener,
        );

        for c in sub_changes {
            push_change(
                script,
                c.delta_type,
                src_start + c.start_original,
                src_start + c.end_original,
                tgt_start + c.start_revised,
                tgt_start + c.end_revised,
            );
        }
        return;
    }

    // Try finding the lowest-occurrence anchor in the target range
    if let Some(anchor) = find_best_anchor(
        source,
        target,
        src_start,
        src_end,
        tgt_start,
        tgt_end,
        equalizer,
        max_chain_length,
    ) {
        // Divide and conquer: Left subregion
        histogram_rec(
            source,
            target,
            src_start,
            anchor.src_idx,
            tgt_start,
            anchor.tgt_idx,
            equalizer,
            max_chain_length,
            script,
            listener.as_deref_mut(),
            total_steps,
        );

        // Right subregion (anchor region is skipped as it is equal)
        histogram_rec(
            source,
            target,
            anchor.src_idx + anchor.len,
            src_end,
            anchor.tgt_idx + anchor.len,
            tgt_end,
            equalizer,
            max_chain_length,
            script,
            listener,
            total_steps,
        );
    } else {
        // Fallback to linear Myers on this subregion
        let fallback_algo = MyersDiffWithLinearSpace::new();
        let sub_source = &source[src_start..src_end];
        let sub_target = &target[tgt_start..tgt_end];

        let sub_changes = fallback_algo.diff_with_listener(
            sub_source,
            sub_target,
            &mut crate::algorithm::diff_algorithm_listener::NoOpListener,
        );

        for c in sub_changes {
            push_change(
                script,
                c.delta_type,
                src_start + c.start_original,
                src_start + c.end_original,
                tgt_start + c.start_revised,
                tgt_start + c.end_revised,
            );
        }
    }
}

struct OccurrenceBucket<'a, T> {
    value: &'a T,
    count: usize,
    first_index: usize,
}

#[allow(clippy::too_many_arguments)]
fn should_fallback_to_myers<T, F>(
    source: &[T],
    src_start: usize,
    src_end: usize,
    target: &[T],
    tgt_start: usize,
    tgt_end: usize,
    equalizer: &F,
) -> bool
where
    T: PartialEq,
    F: Fn(&T, &T) -> bool + ?Sized,
{
    let src_len = src_end - src_start;
    let tgt_len = tgt_end - tgt_start;
    if src_len == 0 || tgt_len == 0 {
        return false;
    }

    let distinct_source = distinct_count(source, src_start, src_end, equalizer);
    let distinct_target = distinct_count(target, tgt_start, tgt_end, equalizer);

    (distinct_source > 32 || distinct_target > 32)
        && (src_len > 0 && tgt_len > 0)
        && (distinct_source >= 64 || distinct_target >= 64)
}

fn distinct_count<T, F>(sequence: &[T], start: usize, end: usize, equalizer: &F) -> usize
where
    T: PartialEq,
    F: Fn(&T, &T) -> bool + ?Sized,
{
    let mut seen = Vec::new();
    for i in start..end {
        let value = &sequence[i];
        let mut already_seen = false;
        for prev in &seen {
            if equalizer(value, *prev) {
                already_seen = true;
                break;
            }
        }
        if !already_seen {
            seen.push(value);
        }
    }
    seen.len()
}

fn has_high_frequency_value<T, F>(
    sequence: &[T],
    start: usize,
    end: usize,
    equalizer: &F,
    max_chain_length: usize,
) -> bool
where
    T: PartialEq,
    F: Fn(&T, &T) -> bool + ?Sized,
{
    let distinct = distinct_count(sequence, start, end, equalizer);
    if distinct <= 32 {
        return false;
    }

    let mut buckets: Vec<OccurrenceBucket<'_, T>> = Vec::new();

    for i in start..end {
        let value = &sequence[i];
        let mut found = false;

        for bucket in &mut buckets {
            if equalizer(value, bucket.value) {
                bucket.count += 1;
                found = true;
                break;
            }
        }

        if !found {
            buckets.push(OccurrenceBucket {
                value,
                count: 1,
                first_index: i,
            });
        }
    }

    buckets.iter().any(|bucket| bucket.count > max_chain_length)
}

fn find_best_anchor<T, F>(
    source: &[T],
    target: &[T],
    src_start: usize,
    src_end: usize,
    tgt_start: usize,
    tgt_end: usize,
    equalizer: &F,
    max_chain_length: usize,
) -> Option<MatchAnchor>
where
    T: PartialEq,
    F: Fn(&T, &T) -> bool + ?Sized,
{
    let mut source_buckets: Vec<OccurrenceBucket<'_, T>> = Vec::new();
    for i in src_start..src_end {
        let value = &source[i];
        let mut bucket_idx = None;
        for (idx, bucket) in source_buckets.iter_mut().enumerate() {
            if equalizer(value, bucket.value) {
                bucket.count += 1;
                bucket_idx = Some(idx);
                break;
            }
        }

        if bucket_idx.is_none() {
            source_buckets.push(OccurrenceBucket {
                value,
                count: 1,
                first_index: i,
            });
        }
    }

    let mut target_buckets: Vec<OccurrenceBucket<'_, T>> = Vec::new();
    for j in tgt_start..tgt_end {
        let value = &target[j];
        let mut bucket_idx = None;
        for (idx, bucket) in target_buckets.iter_mut().enumerate() {
            if equalizer(value, bucket.value) {
                bucket.count += 1;
                bucket_idx = Some(idx);
                break;
            }
        }

        if bucket_idx.is_none() {
            target_buckets.push(OccurrenceBucket {
                value,
                count: 1,
                first_index: j,
            });
        }
    }

    let mut best_anchor: Option<MatchAnchor> = None;
    let mut lowest_occurrence = max_chain_length + 1;
    let mut best_len = 0usize;

    for source_bucket in &source_buckets {
        let target_bucket = target_buckets
            .iter()
            .find(|bucket| equalizer(source_bucket.value, bucket.value));

        let Some(target_bucket) = target_bucket else {
            continue;
        };

        let occurrence_count = source_bucket.count.min(target_bucket.count);
        if occurrence_count == 0 || occurrence_count > max_chain_length {
            // Large repeated alphabets are still valid anchors in a histogram split;
            // JGit prefers the longest anchor run among the lowest count values instead
            // of treating all repeated values as a no-op. We therefore keep these as
            // candidates during anchor selection and only reject truly empty matches.
            if occurrence_count == 0 {
                continue;
            }
        }

        if occurrence_count == 0 {
            continue;
        }

        let src_idx = source_bucket.first_index;
        let tgt_idx = target_bucket.first_index;
        let mut len = 1usize;
        while (src_idx + len) < src_end
            && (tgt_idx + len) < tgt_end
            && equalizer(&source[src_idx + len], &target[tgt_idx + len])
        {
            len += 1;
        }

        if occurrence_count < lowest_occurrence
            || (occurrence_count == lowest_occurrence && len > best_len)
        {
            lowest_occurrence = occurrence_count;
            best_len = len;
            best_anchor = Some(MatchAnchor {
                src_idx,
                tgt_idx,
                len,
            });

            if occurrence_count == 1 {
                return best_anchor;
            }
        }
    }

    best_anchor
}

fn push_change(
    script: &mut Vec<Change>,
    delta_type: DeltaType,
    src_start: usize,
    src_end: usize,
    tgt_start: usize,
    tgt_end: usize,
) {
    if let Some(last) = script.last_mut() {
        if last.delta_type == delta_type {
            match delta_type {
                DeltaType::Delete if last.end_original == src_start => {
                    last.end_original = src_end;
                    return;
                }
                DeltaType::Insert if last.end_revised == tgt_start => {
                    last.end_revised = tgt_end;
                    return;
                }
                _ => {}
            }
        }
    }

    script.push(Change {
        delta_type,
        start_original: src_start,
        end_original: src_end,
        start_revised: tgt_start,
        end_revised: tgt_end,
    });
}

fn normalize_replacements(script: &mut Vec<Change>) {
    let mut normalized: Vec<Change> = Vec::with_capacity(script.len());

    for change in script.drain(..) {
        if let Some(previous) = normalized.last_mut() {
            let replacement = match (previous.delta_type, change.delta_type) {
                (DeltaType::Insert, DeltaType::Delete)
                    if previous.start_original == change.start_original
                        && previous.end_revised == change.start_revised => Some(Change {
                        delta_type: DeltaType::Change,
                        start_original: change.start_original,
                        end_original: change.end_original,
                        start_revised: previous.start_revised,
                        end_revised: change.end_revised,
                    }),
                (DeltaType::Delete, DeltaType::Insert)
                    if previous.end_original == change.start_original
                        && previous.end_revised == change.start_revised => Some(Change {
                        delta_type: DeltaType::Change,
                        start_original: previous.start_original,
                        end_original: previous.end_original,
                        start_revised: previous.start_revised,
                        end_revised: change.end_revised,
                    }),
                _ => None,
            };

            if let Some(replacement) = replacement {
                *previous = replacement;
                continue;
            }
        }

        normalized.push(change);
    }

    *script = normalized;
}

/// Factory for creating `HistogramDiff` algorithm instances.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct HistogramDiffFactory {
    pub max_chain_length: usize,
}

impl HistogramDiffFactory {
    pub fn new() -> Self {
        Self {
            max_chain_length: DEFAULT_MAX_CHAIN_LENGTH,
        }
    }

    pub fn with_max_chain_length(max_chain_length: usize) -> Self {
        Self { max_chain_length }
    }
}

impl<T: PartialEq + 'static> DiffAlgorithmFactory<T> for HistogramDiffFactory {
    fn create(&self) -> Box<dyn DiffAlgorithm<T>>
    where
        T: PartialEq + 'static,
    {
        Box::new(HistogramDiff::new().with_max_chain_length(self.max_chain_length))
    }

    fn create_with_equalizer(
        &self,
        equalizer: Box<dyn Fn(&T, &T) -> bool + 'static>,
    ) -> Box<dyn DiffAlgorithm<T>> {
        Box::new(
            HistogramDiff::new()
                .with_max_chain_length(self.max_chain_length)
                .with_equalizer(move |a: &T, b: &T| equalizer(a, b)),
        )
    }
}