{ } JustinGeoSci

The dynamic index

The R*-tree that accepts insertions and exact removals, keeps every box exact and tight, and answers the same box and nearest-neighbor queries as the static index.

The dynamic tree

situs::DynamicIndex is the counterpart of the read-only Index for a set that changes. The two types remain separate because their contracts differ: the static index is compact and the fastest to query, and the dynamic tree can change. Both hold (Rect, u32) entries and carry the same guarantee.

DynamicIndex

pub struct DynamicIndex

A dynamic R*-tree (Beckmann, Kriegel, Schneider & Seeger, 1990) 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; it need not be unique, and an entry stored twice is two entries.

The R* heuristics determine the tree’s shape and nothing else. Area, margin, overlap and center distance are computed in f64, while every node’s box is maintained as the exact Rect::union of its children’s boxes. That box is tight rather than merely containing, every leaf is at the same depth, and the stored entries are exactly the multiset inserted. These are the static index’s conditions over a different storage, so the correctness argument applies unchanged. The same operations on the same starting tree produce the same tree, bit for bit.

Two constants fix the node sizes:

Constant Meaning
DynamicIndex::NODE_CAPACITY: usize = 16 Children per node and entries per leaf, the M of the R*-tree.
DynamicIndex::MIN_FILL: usize = 6 The least children in a non-root node, the m of the R*-tree: 40% of the capacity, as the paper recommends.
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)));

DynamicIndex::new

pub fn new() -> DynamicIndex

An empty tree. DynamicIndex also implements Default, which gives the same empty tree.

use situs::DynamicIndex;

let index = DynamicIndex::new();
assert!(index.is_empty());
assert_eq!(index.bounds(), None);
assert_eq!(DynamicIndex::default().len(), 0);

DynamicIndex::bulk_load

pub fn bulk_load(mut entries: Vec<(Rect, u32)>) -> DynamicIndex

Builds the initial tree by STR packing, the static index’s method, with one adjustment. When a level’s last chunk would hold fewer than MIN_FILL entries, the last two chunks are rebalanced so both hold at least that many. The previous chunk is full and the capacity is at least twice the minimum, so the rebalance is always possible. Only the grouping changes rather than the order, and every box is an exact union. A level of one chunk is the root and is exempt.

A bulk-loaded tree is an ordinary dynamic tree: it accepts insertions and removals afterward.

use situs::{DynamicIndex, Rect};

let r = |a, b, c, d| Rect::new(a, b, c, d).unwrap();
// 17 unit cells in a row: one full chunk of 16 and a chunk of 1,
// which the loader rebalances.
let cells: Vec<(Rect, u32)> = (0..17u32)
    .map(|i| (r(f64::from(i), 0.0, f64::from(i) + 1.0, 1.0), i))
    .collect();
let mut index = DynamicIndex::bulk_load(cells);
assert_eq!(index.len(), 17);
assert_eq!(index.bounds(), Some(r(0.0, 0.0, 17.0, 1.0)));

index.insert(r(20.0, 0.0, 21.0, 1.0), 17);
assert_eq!(index.bounds(), Some(r(0.0, 0.0, 21.0, 1.0)));

Insertion and removal

Insertion follows the R*-tree. ChooseSubtree descends by least overlap enlargement immediately above the leaves and by least area enlargement elsewhere; the first overflow at each level of one insertion is treated by forced reinsertion, and later overflows split along the axis the margins choose. Removal follows Guttman’s CondenseTree (1984), with the R*-tree’s reinsertion of whole subtrees at their own level. Both recompute every box they touch by exact union.

DynamicIndex::insert

pub fn insert(&mut self, rect: Rect, id: u32)

Stores one entry. It is infallible: a Rect is already valid, and ids need not be unique.

The box of every node on the way is recomputed by exact union, so bounds stays exact and tight after every call.

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), 1);
index.insert(r(0.0, 0.0, 1.0, 1.0), 1); // the same entry again: two entries
assert_eq!(index.len(), 2);

let mut hits = Vec::new();
index.query(&r(0.5, 0.5, 0.5, 0.5), |id| hits.push(id));
assert_eq!(hits, [1, 1]);

DynamicIndex::remove

pub fn remove(&mut self, rect: &Rect, id: u32) -> bool

Removes one stored entry equal to (rect, id) and returns true, or returns false if there is none. Equality is Rect’s == (bounds compared numerically, so -0.0 equals 0.0) together with the id, the same equality every query uses. If several stored entries are equal, one of them is removed, since they are the same point set with the same id; which one is a deterministic function of the tree’s state.

The search is complete. A stored entry’s box is contained in every ancestor’s box, so every path to a matching entry passes the exact containment test. More than one child may contain rect, so the search descends into every child whose box contains it and backtracks when a subtree holds no match.

The bounds remain exact and tight. Every node remaining on the path has its box recomputed as the exact union of its remaining children, so boxes shrink as entries leave.

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);

assert!(!index.remove(&r(0.0, 0.0, 1.0, 1.0), 9)); // wrong id
assert!(index.remove(&r(5.0, 5.0, 6.0, 6.0), 9));
assert!(!index.remove(&r(5.0, 5.0, 6.0, 6.0), 9)); // already gone
assert_eq!(index.bounds(), Some(r(0.0, 0.0, 1.0, 1.0))); // shrunk

// -0.0 and 0.0 compare equal, so this removes the remaining entry.
assert!(index.remove(&r(-0.0, -0.0, 1.0, 1.0), 7));
assert!(index.is_empty());

Queries

The dynamic tree answers box queries and reports its extent. Its nearest-neighbor operations, nearest_candidates, browse and nearest_exact, run the static index’s own search and are described with it on Nearest neighbors.

DynamicIndex::query

pub fn query(&self, window: &Rect, mut visit: impl FnMut(u32))

Calls visit with the id of every entry whose box meets window, and reports exactly those entries, by the argument that governs the static index. Every node’s box is the exact union of its children’s, so descending only into nodes whose boxes meet the window loses nothing, and every entry’s own box passes the closed overlap test before it is reported.

An entry stored twice is reported twice. The visit order is a deterministic function of the tree’s state rather than a meaningful one. There is no containment shortcut: the query tests every entry it reaches.

use situs::{DynamicIndex, Rect};

let r = |a, b, c, d| Rect::new(a, b, c, d).unwrap();
let mut index = DynamicIndex::new();
for i in 0..40u32 {
    let x = f64::from(i);
    index.insert(r(x, 0.0, x + 1.0, 1.0), i); // unit cells in a row
}
assert!(index.remove(&r(11.0, 0.0, 12.0, 1.0), 11));

let mut hits = Vec::new();
index.query(&r(10.0, 0.5, 12.0, 0.5), |id| hits.push(id));
hits.sort_unstable();
assert_eq!(hits, [9, 10, 12]); // 9 and 12 touch the window's ends; 11 is gone

bounds, len and is_empty

pub fn bounds(&self) -> Option<Rect> pub fn len(&self) -> usize pub fn is_empty(&self) -> bool

bounds is the box containing every entry, or None for an empty tree. It is the root’s box, which is exact and tight: the exact union of every stored entry’s box, recomputed from stored values at every change, never widened and never left stale. len is the number of stored entries, and is_empty is true if the tree holds none.

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), 1);
index.insert(r(9.0, 9.0, 10.0, 10.0), 2);
assert_eq!(index.len(), 2);
assert_eq!(index.bounds(), Some(r(0.0, 0.0, 10.0, 10.0)));

assert!(index.remove(&r(9.0, 9.0, 10.0, 10.0), 2));
assert_eq!(index.bounds(), Some(r(0.0, 0.0, 1.0, 1.0))); // tight, not stale
assert!(index.remove(&r(0.0, 0.0, 1.0, 1.0), 1));
assert_eq!(index.bounds(), None);

What the dynamic tree does not have. join, self_join, the early-exit try_query and the containment shortcut have no dynamic counterparts, and neither does memory_size_for. They are available on the static Index.