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
//! Various diff (longest common subsequence) algorithms.
//!
//! The implementations of the algorithms in this module are relatively low
//! level and expose the most generic bounds possible for the algorithm. To
//! use them you would typically use the higher level API if possible but
//! direct access to these algorithms can be useful in some cases.
//!
//! All these algorithms provide a `diff` function which takes two indexable
//! objects (for instance slices) and a [`DiffHook`]. As the
//! diff is generated the diff hook is invoked. Note that the diff hook does
//! not get access to the actual values but only the indexes. This is why the
//! diff hook is not used outside of the raw algorithm implementations as for
//! most situations access to the values is useful of required.
//!
//! The algorithms module really is the most low-level module in similar and
//! generally not the place to start.
//!
//! # Which Algorithm Should You Use?
//!
//! For most users, start with **[`Algorithm::Myers`]**. It's the default for a
//! reason: good general quality, good performance, and robust behavior with
//! deadlines.
//!
//! If you need to tune behavior, use this rule of thumb:
//!
//! - **[`Algorithm::Myers`]** (default):
//! best all-around choice for mixed workloads.
//! - **[`Algorithm::Patience`]**:
//! often more human-readable for refactors and reordered blocks, especially
//! when there are unique lines/tokens to anchor on.
//! - **[`Algorithm::Histogram`]**:
//! good for noisy/repetitive inputs (logs, generated text, repeated lines),
//! as it prefers low-frequency anchors over common noise.
//! - **[`Algorithm::Hunt`]**:
//! useful when matching pairs are relatively sparse and you want
//! Hunt/LCS-style anchoring; can use more memory on highly repetitive input.
//! - **[`Algorithm::Lcs`]**:
//! mainly for small inputs, debugging, or reference behavior. It has
//! `O(N*M)` time/space and is usually not a good default at scale.
//!
//! Trade-offs to keep in mind:
//!
//! - `Patience` can lose its edge when there are few unique anchors.
//! - `Histogram` may prefer readability-oriented anchors over minimal edit
//! scripts.
//! - `Hunt` can degrade on inputs with many repeated matches (`R` grows large).
//! - `Lcs` scales poorly and produces weak approximations when deadlines hit.
//!
//! # Heuristics
//!
//! Algorithm entrypoints in this module (`diff` / `diff_deadline`) are
//! heuristic-enabled. In practice that means they use practical shortcuts to
//! keep difficult inputs fast while still producing useful diff scripts.
//!
//! At a high level, the current heuristics are:
//!
//! - **Shared disjoint-range fast path** (all algorithms):
//! if two large ranges appear to have no common items, we skip expensive
//! search and emit a straight delete+insert replacement.
//! - **Prefix/suffix trimming** (used widely):
//! matching runs at the beginning/end are emitted immediately so each
//! algorithm only works on the changed middle.
//! - **Deadline-aware fallback behavior**:
//! when a deadline is provided, algorithms periodically check it and may fall
//! back to a simpler script instead of running too long.
//! - **Algorithm-local anchor strategies**:
//! - **Patience** anchors on items unique in both sides.
//! - **Histogram** prefers low-frequency anchors and avoids very noisy lines.
//! - **Hunt** uses match lists and longest-increasing anchor chains.
//! - **Myers-specific safeguards**:
//! Myers follows Eugene W. Myers' shortest-edit-script approach: it finds a
//! "middle snake" (a central diagonal run of equal items on an optimal edit
//! path) and recursively diffs the left and right sides around that split.
//! Beyond that classic middle-snake recursion, this implementation adds a
//! "front-anchor peel" for heavily unbalanced shifts (it probes a few small
//! one-sided skips near the start to find a long shared run, emits that
//! prefix anchor early, then recurses on the remaining tail) and an exact
//! small-side fallback when one side is tiny and the other is large.
//!
//! Some heuristic-enabled entrypoints may require stricter trait bounds than
//! their raw counterpart (for example, shared heuristics that build hash-based
//! lookups require [`Hash`] + [`Eq`]). If your values need to be computed lazily
//! or compared via a derived key, wrap them in [`CachedLookup`]
//! first. In the remaining cases, `diff_deadline_raw` is the compatibility path
//! with minimal bounds.
//!
//! If you want to skip shared heuristics, each algorithm module provides
//! `diff_deadline_raw`, which keeps that algorithm's minimal intrinsic bounds.
//!
//! The top-level dispatcher [`diff_deadline`] always calls the
//! heuristic-enabled entrypoints and never calls raw variants.
//!
//! # Sequence Adapters
//!
//! Two helpers are available when your input is not already a plain slice:
//!
//! - [`CachedLookup`]: lazily computes and caches sequence items on first access.
//! - [`IdentifyDistinct`]: eagerly remaps values to dense integer IDs.
//!
//! # Example
//!
//! This is a simple example that shows how you can calculate the difference
//! between two sequences and capture the ops into a vector.
//!
//! ```rust
//! use similar::algorithms::{Algorithm, Replace, Capture, diff_slices};
//!
//! let a = vec![1, 2, 3, 4, 5];
//! let b = vec![1, 2, 3, 4, 7];
//! let mut d = Replace::new(Capture::new());
//! diff_slices(Algorithm::Myers, &mut d, &a, &b).unwrap();
//! let ops = d.into_inner().into_ops();
//! ```
//!
//! The above example is equivalent to using
//! [`capture_diff_slices`](crate::capture_diff_slices).
pub use crateCachedLookup;
pub
use Vec;
use Hash;
use ;
use crateInstant;
pub use Capture;
pub use Compact;
pub use ;
pub use Replace;
pub use IdentifyDistinct;
pub use crateAlgorithm;
/// Creates a diff between old and new with the given algorithm.
///
/// Diffs `old`, between indices `old_range` and `new` between indices `new_range`.
/// Creates a diff between old and new with the given algorithm with deadline.
///
/// Diffs `old`, between indices `old_range` and `new` between indices `new_range`.
///
/// This diff is done with an optional deadline that defines the maximal
/// execution time permitted before it bails and falls back to an approximation.
/// Note that not all algorithms behave well if they reach the deadline (LCS
/// for instance produces a very simplistic diff when the deadline is reached
/// in all cases).
/// Shortcut for diffing slices with a specific algorithm.
/// Shortcut for diffing slices with a specific algorithm.