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
//! Generic interval tree abstraction
//!
//! Provides a trait that allows switching between disk-backed and in-memory
//! implementations based on dataset size or user preference.
use std::io;
/// Generic interval tree interface
///
/// Implementations can be disk-backed (for large datasets) or in-memory (for speed).
pub trait IntervalTree<K, V>: Send + Sync
where
K: Ord + Copy,
V: Copy,
{
/// Open writer (for disk-backed trees that use background threads)
fn open_writer(&mut self) -> io::Result<()> {
Ok(()) // Default no-op for in-memory trees
}
/// Add an interval [start, end) with associated value
fn add(&mut self, start: K, end: K, value: V) -> io::Result<()>;
/// Close writer (for disk-backed trees with background threads)
fn close_writer(&mut self) -> io::Result<()> {
Ok(()) // Default no-op for in-memory trees
}
/// Query all intervals that overlap with range [start, end)
/// Calls the provided function for each overlapping interval with (index, start, end, value)
fn overlap<F>(&self, start: K, end: K, func: F) -> io::Result<()>
where
F: FnMut(usize, K, K, V);
/// Finalize the tree (sort, index, etc.) before querying
/// Also available as `index()` for compatibility
fn finalize(&mut self) -> io::Result<()>;
/// Index the tree (alias for finalize, for IITree compatibility)
fn index(&mut self) -> io::Result<()> {
self.finalize()
}
/// Get the number of intervals stored
fn len(&self) -> usize;
/// Check if the tree is empty
fn is_empty(&self) -> bool {
self.len() == 0
}
}
/// Disk-backed interval tree using iitree-rs
///
/// Stores all data on disk via memory mapping. Good for datasets that
/// don't fit in RAM or when you want persistent storage.
pub mod disk {
use super::*;
use iitree_rs::IITree;
use std::path::Path;
pub struct DiskBackedTree<K, V>
where
K: Ord + Copy + Default + Send + Sync + 'static,
V: Copy + Default + Send + Sync + 'static,
{
inner: IITree<K, V>,
}
impl<K, V> DiskBackedTree<K, V>
where
K: Ord + Copy + Default + Send + Sync + 'static,
V: Copy + Default + Send + Sync + 'static,
{
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
Ok(DiskBackedTree {
inner: IITree::new(path)?,
})
}
}
impl<K, V> IntervalTree<K, V> for DiskBackedTree<K, V>
where
K: Ord + Copy + Default + Send + Sync + 'static,
V: Copy + Default + Send + Sync + 'static,
{
fn open_writer(&mut self) -> io::Result<()> {
self.inner.open_writer()
}
fn add(&mut self, start: K, end: K, value: V) -> io::Result<()> {
self.inner.add(start, end, value)
}
fn close_writer(&mut self) -> io::Result<()> {
self.inner.close_writer()
}
fn overlap<F>(&self, start: K, end: K, func: F) -> io::Result<()>
where
F: FnMut(usize, K, K, V),
{
self.inner.overlap(start, end, func)
}
fn finalize(&mut self) -> io::Result<()> {
self.inner.index()
}
fn len(&self) -> usize {
self.inner.len()
}
}
}
/// In-memory interval tree using Vec
///
/// Stores all intervals in RAM. Much faster than disk-backed for datasets
/// that fit in memory. Uses simple binary search after sorting.
pub mod memory {
use super::*;
use rayon::prelude::*;
#[derive(Clone)]
struct Interval<K, V> {
start: K,
end: K,
max: K, // Maximum end position in subtree (for augmented tree)
value: V,
}
pub struct InMemoryTree<K, V>
where
K: Ord + Copy,
V: Copy,
{
intervals: Vec<Interval<K, V>>,
indexed: bool,
max_level: usize, // Maximum level in the implicit binary tree
}
impl<K, V> InMemoryTree<K, V>
where
K: Ord + Copy,
V: Copy,
{
pub fn new() -> Self {
InMemoryTree {
intervals: Vec::new(),
indexed: false,
max_level: 0,
}
}
pub fn with_capacity(capacity: usize) -> Self {
InMemoryTree {
intervals: Vec::with_capacity(capacity),
indexed: false,
max_level: 0,
}
}
/// IAITree overlap query with stack-based traversal
/// This is a direct port of the iitree-rs overlap algorithm
/// Uses the augmented max values to prune entire subtrees
fn overlap_iaitree<F>(&self, query_start: K, query_end: K, mut func: F)
where
F: FnMut(usize, K, K, V),
{
let n = self.intervals.len();
if n == 0 {
return;
}
#[derive(Clone, Copy)]
struct StackCell {
k: usize, // level
x: usize, // node index
w: u8, // 0 if left child not processed, 1 if processed
}
let mut stack = [StackCell { k: 0, x: 0, w: 0 }; 64];
let mut t = 0; // stack pointer
// Push root
stack[t] = StackCell {
k: self.max_level,
x: (1 << self.max_level) - 1,
w: 0,
};
t += 1;
// Top-down traversal
while t > 0 {
t -= 1;
let z = stack[t];
if z.k <= 3 {
// Small subtree: linear scan
let i0 = (z.x >> z.k) << z.k;
let i1 = std::cmp::min(i0 + (1 << (z.k + 1)) - 1, n);
for i in i0..i1 {
if self.intervals[i].start >= query_end {
break;
}
if query_start < self.intervals[i].end {
func(
i,
self.intervals[i].start,
self.intervals[i].end,
self.intervals[i].value,
);
}
}
} else if z.w == 0 {
// Left child not processed
let y = z.x - (1 << (z.k - 1));
// Re-add this node with left child marked as processed
stack[t] = StackCell {
k: z.k,
x: z.x,
w: 1,
};
t += 1;
// Push left child if it might overlap
if y >= n || self.intervals[y].max > query_start {
stack[t] = StackCell {
k: z.k - 1,
x: y,
w: 0,
};
t += 1;
}
} else if z.x < n && self.intervals[z.x].start < query_end {
// Process current node and push right child
if query_start < self.intervals[z.x].end {
func(
z.x,
self.intervals[z.x].start,
self.intervals[z.x].end,
self.intervals[z.x].value,
);
}
// Push right child
stack[t] = StackCell {
k: z.k - 1,
x: z.x + (1 << (z.k - 1)),
w: 0,
};
t += 1;
}
}
}
}
impl<K, V> IntervalTree<K, V> for InMemoryTree<K, V>
where
K: Ord + Copy + Send + Sync,
V: Copy + Send + Sync,
{
fn add(&mut self, start: K, end: K, value: V) -> io::Result<()> {
self.intervals.push(Interval {
start,
end,
max: end, // Initially set max to end
value,
});
self.indexed = false;
Ok(())
}
fn overlap<F>(&self, query_start: K, query_end: K, func: F) -> io::Result<()>
where
F: FnMut(usize, K, K, V),
{
if !self.indexed {
return Err(io::Error::new(
io::ErrorKind::Other,
"Tree not indexed - call finalize() first",
));
}
self.overlap_iaitree(query_start, query_end, func);
Ok(())
}
fn finalize(&mut self) -> io::Result<()> {
if self.indexed {
return Ok(());
}
let n = self.intervals.len();
if n == 0 {
self.indexed = true;
self.max_level = 0;
return Ok(());
}
// Sort by start position, then by end (matching iitree-rs behavior)
if n > 10000 {
self.intervals.par_sort_unstable_by(|a, b| {
a.start.cmp(&b.start).then_with(|| a.end.cmp(&b.end))
});
} else {
self.intervals
.sort_unstable_by(|a, b| a.start.cmp(&b.start).then_with(|| a.end.cmp(&b.end)));
}
// Build augmented tree (compute max values bottom-up)
// This is a direct port of iitree-rs index_core()
// Initialize leaves (level 0) - every other node starting from 0
let mut last_i = 0;
let mut last = self.intervals[0].end;
for i in (0..n).step_by(2) {
last_i = i;
self.intervals[i].max = self.intervals[i].end;
last = self.intervals[i].max;
}
// Process internal nodes bottom-up
let mut k = 1;
while (1usize << k) <= n {
let x = 1usize << (k - 1);
let i0 = (x << 1) - 1; // First node at this level
let step = x << 2;
// Traverse all nodes at level k
let mut i = i0;
while i < n {
// max value of left child
let el = self.intervals[i - x].max;
// max value of right child (may not exist)
let er = if i + x < n {
self.intervals[i + x].max
} else {
last
};
// Compute max for this node
let mut e = self.intervals[i].end;
if el > e {
e = el;
}
if er > e {
e = er;
}
self.intervals[i].max = e;
i += step;
}
// Update last_i to point to parent of original last_i
last_i = if (last_i >> k) & 1 != 0 {
last_i - x
} else {
last_i + x
};
if last_i < n {
last = self.intervals[last_i].max;
}
k += 1;
}
self.max_level = k - 1;
self.indexed = true;
Ok(())
}
fn len(&self) -> usize {
self.intervals.len()
}
}
impl<K, V> Default for InMemoryTree<K, V>
where
K: Ord + Copy,
V: Copy,
{
fn default() -> Self {
Self::new()
}
}
}
/// Adaptive tree that can be either disk-backed or in-memory
///
/// This enum allows runtime selection of implementation while maintaining
/// good performance through match-based dispatch (no virtual function overhead).
pub enum AdaptiveTree<K, V>
where
K: Ord + Copy + Default + Send + Sync + 'static,
V: Copy + Default + Send + Sync + 'static,
{
Disk(disk::DiskBackedTree<K, V>),
Memory(memory::InMemoryTree<K, V>),
}
impl<K, V> AdaptiveTree<K, V>
where
K: Ord + Copy + Default + Send + Sync + 'static,
V: Copy + Default + Send + Sync + 'static,
{
/// Create a disk-backed tree
pub fn new_disk<P: AsRef<std::path::Path>>(path: P) -> io::Result<Self> {
Ok(AdaptiveTree::Disk(disk::DiskBackedTree::new(path)?))
}
/// Create a true in-memory tree (no filesystem dependency)
pub fn new_memory() -> io::Result<Self> {
Ok(AdaptiveTree::Memory(memory::InMemoryTree::new()))
}
/// Create an in-memory tree with capacity hint
pub fn new_memory_with_capacity(capacity: usize) -> io::Result<Self> {
Ok(AdaptiveTree::Memory(memory::InMemoryTree::with_capacity(
capacity,
)))
}
}
impl<K, V> IntervalTree<K, V> for AdaptiveTree<K, V>
where
K: Ord + Copy + Default + Send + Sync + 'static,
V: Copy + Default + Send + Sync + 'static,
{
fn open_writer(&mut self) -> io::Result<()> {
match self {
AdaptiveTree::Disk(tree) => tree.open_writer(),
AdaptiveTree::Memory(tree) => tree.open_writer(),
}
}
fn add(&mut self, start: K, end: K, value: V) -> io::Result<()> {
match self {
AdaptiveTree::Disk(tree) => tree.add(start, end, value),
AdaptiveTree::Memory(tree) => tree.add(start, end, value),
}
}
fn close_writer(&mut self) -> io::Result<()> {
match self {
AdaptiveTree::Disk(tree) => tree.close_writer(),
AdaptiveTree::Memory(tree) => tree.close_writer(),
}
}
fn overlap<F>(&self, start: K, end: K, func: F) -> io::Result<()>
where
F: FnMut(usize, K, K, V),
{
match self {
AdaptiveTree::Disk(tree) => tree.overlap(start, end, func),
AdaptiveTree::Memory(tree) => tree.overlap(start, end, func),
}
}
fn finalize(&mut self) -> io::Result<()> {
match self {
AdaptiveTree::Disk(tree) => tree.finalize(),
AdaptiveTree::Memory(tree) => tree.finalize(),
}
}
fn len(&self) -> usize {
match self {
AdaptiveTree::Disk(tree) => tree.len(),
AdaptiveTree::Memory(tree) => tree.len(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tempfile;
#[test]
fn test_in_memory_tree() {
let mut tree = memory::InMemoryTree::new();
tree.add(10, 20, 1).unwrap();
tree.add(15, 25, 2).unwrap();
tree.add(30, 40, 3).unwrap();
tree.finalize().unwrap();
// Query range [17, 18) should find intervals 1 and 2
let mut results = Vec::new();
tree.overlap(17, 18, |_idx, _start, _end, value| {
results.push(value);
})
.unwrap();
assert_eq!(results.len(), 2);
assert!(results.contains(&1));
assert!(results.contains(&2));
// Query range [35, 36) should find interval 3
let mut results = Vec::new();
tree.overlap(35, 36, |_idx, _start, _end, value| {
results.push(value);
})
.unwrap();
assert_eq!(results, vec![3]);
// Query range [5, 6) should find nothing
let mut results = Vec::new();
tree.overlap(5, 6, |_idx, _start, _end, value| {
results.push(value);
})
.unwrap();
assert_eq!(results.len(), 0);
}
#[test]
fn test_memory_vs_disk_equivalence() {
use std::collections::HashSet;
// Create both tree types
let mut mem_tree = memory::InMemoryTree::new();
let disk_path = tempfile::create("test-equiv-", ".iit").unwrap();
let mut disk_tree = disk::DiskBackedTree::new(&disk_path).unwrap();
// Add same intervals to both - including ones with same start but different ends
let intervals = vec![
(10, 20, 1u64),
(10, 25, 2u64), // Same start, different end
(10, 15, 3u64), // Same start, different end
(15, 25, 4u64),
(30, 40, 5u64),
];
disk_tree.open_writer().unwrap();
for (start, end, val) in &intervals {
mem_tree.add(*start, *end, *val).unwrap();
disk_tree.add(*start, *end, *val).unwrap();
}
disk_tree.close_writer().unwrap();
mem_tree.finalize().unwrap();
disk_tree.finalize().unwrap();
// Query both and collect results
let mut mem_results = Vec::new();
let mut disk_results = Vec::new();
mem_tree
.overlap(12, 18, |idx, start, end, val| {
mem_results.push((idx, start, end, val));
})
.unwrap();
disk_tree
.overlap(12, 18, |idx, start, end, val| {
disk_results.push((idx, start, end, val));
})
.unwrap();
// Compare ignoring index (since that might differ due to sort stability)
let mem_set: HashSet<_> = mem_results.iter().map(|(_, s, e, v)| (s, e, v)).collect();
let disk_set: HashSet<_> = disk_results.iter().map(|(_, s, e, v)| (s, e, v)).collect();
assert_eq!(
mem_set, disk_set,
"Memory and disk trees should return the same intervals.\n\
Memory: {:?}\n\
Disk: {:?}",
mem_results, disk_results
);
// Clean up temp file
tempfile::remove(&disk_path);
}
}