Skip to main content

maplike/
rstar.rs

1// SPDX-FileCopyrightText: 2025 maplike contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use rstar::{RTree, RTreeObject};
6
7use crate::{Get, Insert, IntoIter, KeyedCollection, Remove, Set, StableRemove};
8
9impl<K: RTreeObject> KeyedCollection for RTree<K> {
10    type Key = K;
11    type Value = ();
12}
13
14impl<K: RTreeObject + PartialEq> Get<K> for RTree<K> {
15    #[inline(always)]
16    fn get(&self, key: &K) -> Option<&()> {
17        RTree::contains(self, key).then_some(&())
18    }
19}
20
21impl<K: RTreeObject + PartialEq> Set<K> for RTree<K> {
22    #[inline(always)]
23    fn set(&mut self, key: K, _value: ()) {
24        RTree::remove(self, &key);
25        RTree::insert(self, key);
26    }
27}
28
29impl<K: RTreeObject> Insert<K> for RTree<K> {
30    #[inline(always)]
31    fn insert(&mut self, key: K, _value: ()) {
32        RTree::insert(self, key);
33    }
34}
35
36impl<K: RTreeObject + PartialEq> Remove<K> for RTree<K> {
37    #[inline(always)]
38    fn remove(&mut self, key: &K) -> Option<()> {
39        RTree::remove(self, key).map(|_| ())
40    }
41}
42
43impl<K: RTreeObject + PartialEq> StableRemove<K> for RTree<K> {}
44
45pub struct MapIntoIter<K: RTreeObject>(rstar::iterators::IntoIter<K>);
46
47impl<K: RTreeObject> Iterator for MapIntoIter<K> {
48    type Item = (K, ());
49
50    #[inline(always)]
51    fn next(&mut self) -> Option<Self::Item> {
52        self.0.next().map(|k| (k, ()))
53    }
54}
55
56impl<K: RTreeObject> IntoIter<K> for RTree<K> {
57    type IntoIter = MapIntoIter<K>;
58
59    #[inline(always)]
60    fn into_iter(self) -> MapIntoIter<K> {
61        MapIntoIter(IntoIterator::into_iter(self))
62    }
63}