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
// src/dynamic.rs
//! The dynamic R\*-tree, [`DynamicIndex`], which accepts insertions and
//! removals.
//!
//! The static [`Index`](crate::Index) is read-only. This tree changes with
//! its contents and retains the same guarantee: a query reports exactly the
//! entries whose boxes meet the window, and a nearest-neighbor search runs
//! the static index's own search and proof.
//!
//! # Shape and contents
//!
//! **The R\* heuristics determine the tree's shape and nothing else.** Area,
//! margin, overlap and center distance (Beckmann, Kriegel, Schneider &
//! Seeger, "The R\*-tree: An Efficient and Robust Access Method for Points
//! and Rectangles", SIGMOD 1990) are computed in `f64`, while the
//! invariants on which the absence of false negatives rests are maintained
//! by exact `min` and `max` alone. The static index makes the same split,
//! with approximate sort keys and exact contents.
//!
//! The tests check three invariants after every insertion and every
//! removal:
//!
//! - **Tight, exact boxes:** Every node's box is the [`Rect::union`] of its
//! children's boxes (its entries' boxes, for a leaf). The box is tight
//! rather than merely containing, so [`DynamicIndex::bounds`] is exact.
//! `union` selects stored values by exact comparison, so no arithmetic
//! touches a stored box.
//! - **Balance:** Every leaf is at the same depth.
//! - **Contents:** The stored entries are exactly the multiset inserted.
//!
//! These are the static index's three conditions over a different storage,
//! and the query is the same search. No heuristic can break them, because
//! every structural change recomputes the boxes it touches by exact union.
//! A degenerate heuristic value, such as an area that overflows to infinity
//! or a NaN from `∞ − ∞`, can only select a worse shape. Every heuristic
//! comparison runs through one total order that places NaN past every
//! number, and no heuristic value is ever stored as a box.
//!
//! # Storage
//!
//! Nodes live in one arena (`Vec`) with a last-in, first-out free list of
//! slot indices. Each node holds its children inline, with room for one more
//! than the capacity, so an overflowing node holds its extra child while the
//! overflow is treated. No node allocates on its own, slots are reused in
//! last-in, first-out order, and every heuristic tie breaks by a fixed rule.
//! The same operations on the same starting tree therefore produce the same
//! arena, bit for bit.
use Vec;
use crateRect;
pub
pub use ;
/// A dynamic R\*-tree over `(Rect, u32)` entries: bulk-loaded, grown by
/// insertion, shrunk by exact removal, queried exactly.
///
/// The `u32` is the caller's identifier, as in the static
/// [`Index`](crate::Index); it need not be unique, and an entry stored
/// twice is two entries.
///
/// ```
/// use situs::{DynamicIndex, Rect};
///
/// let r = |a, b, c, d| Rect::new(a, b, c, d).unwrap();
/// let mut index = DynamicIndex::new();
/// index.insert(r(0.0, 0.0, 1.0, 1.0), 7);
/// index.insert(r(5.0, 5.0, 6.0, 6.0), 9);
///
/// let mut hits = Vec::new();
/// index.query(&r(1.0, 1.0, 2.0, 2.0), |id| hits.push(id));
/// assert_eq!(hits, [7]); // touching at a corner counts
/// assert_eq!(index.bounds(), Some(r(0.0, 0.0, 6.0, 6.0)));
/// ```