iset/
entry.rs

1use core::{
2    fmt,
3    ops::Range,
4};
5use super::{
6    IntervalMap, Interval,
7    ix::{IndexType, DefaultIx},
8};
9
10/// A view into a vacant entry in an `IntervalMap`. It is part of the [Entry](enum.Entry.html) enum.
11pub struct VacantEntry<'a, T, V, Ix: IndexType = DefaultIx> {
12    tree: &'a mut IntervalMap<T, V, Ix>,
13    parent: Ix,
14    left_side: bool,
15    interval: Interval<T>,
16}
17
18impl<'a, T, V, Ix: IndexType> VacantEntry<'a, T, V, Ix>
19where T: Copy,
20{
21    pub(super) fn new(tree: &'a mut IntervalMap<T, V, Ix>, parent: Ix, left_side: bool, interval: Interval<T>) -> Self {
22        Self { tree, parent, left_side, interval }
23    }
24
25    /// Returns the interval that was used for the search.
26    pub fn interval(&self) -> Range<T> {
27        self.interval.to_range()
28    }
29}
30
31impl<'a, T, V, Ix: IndexType> VacantEntry<'a, T, V, Ix>
32where T: Copy + PartialOrd,
33{
34    /// Inserts a new value in the IntervalMap, and returns a mutable reference to it.
35    pub fn insert(self, value: V) -> &'a mut V {
36        self.tree.insert_at(self.parent, self.left_side, self.interval, value)
37    }
38}
39
40impl<'a, T, V, Ix: IndexType> fmt::Debug for VacantEntry<'a, T, V, Ix>
41where T: PartialOrd + Copy + fmt::Debug,
42{
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        write!(f, "VacantEntry({:?})", self.interval.to_range())
45    }
46}
47
48/// A view into an occupied entry in an `IntervalMap`. It is part of the [Entry](enum.Entry.html) enum.
49pub struct OccupiedEntry<'a, T, V, Ix: IndexType = DefaultIx> {
50    tree: &'a mut IntervalMap<T, V, Ix>,
51    index: Ix,
52}
53
54impl<'a, T, V, Ix: IndexType> OccupiedEntry<'a, T, V, Ix>
55where T: Copy,
56{
57    pub(super) fn new(tree: &'a mut IntervalMap<T, V, Ix>, index: Ix) -> Self {
58        Self { tree, index }
59    }
60
61    /// Returns the interval that was used for the search.
62    pub fn interval(&self) -> Range<T> {
63        self.tree.nodes[self.index.get()].interval.to_range()
64    }
65
66    /// Gets a reference to the value in the entry.
67    pub fn get(&self) -> &V {
68        &self.tree.nodes[self.index.get()].value
69    }
70
71    /// Gets a mutable reference to the value in the entry.
72    /// If you need a reference to the OccupiedEntry that may outlive the destruction of the Entry value,
73    /// see [into_mut](#method.into_mut).
74    pub fn get_mut(&mut self) -> &mut V {
75        &mut self.tree.nodes[self.index.get()].value
76    }
77
78    /// Converts the entry into a mutable reference to its value.
79    /// If you need multiple references to the OccupiedEntry, see [get_mut](#method.get_mut).
80    pub fn into_mut(self) -> &'a mut V {
81        &mut self.tree.nodes[self.index.get()].value
82    }
83
84    /// Replaces the value of the entry, and returns the entry's old value.
85    pub fn insert(&mut self, value: V) -> V {
86        core::mem::replace(&mut self.tree.nodes[self.index.get()].value, value)
87    }
88}
89
90impl<'a, T, V, Ix: IndexType> fmt::Debug for OccupiedEntry<'a, T, V, Ix>
91where T: PartialOrd + Copy + fmt::Debug,
92      V: fmt::Debug,
93{
94    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95        let node = &self.tree.nodes[self.index.get()];
96        write!(f, "OccupiedEntry({:?} => {:?})", node.interval.to_range(), node.value)
97    }
98}
99
100impl<'a, T, V, Ix: IndexType> OccupiedEntry<'a, T, V, Ix>
101where T: Copy + PartialOrd,
102{
103    /// Removes the entry from the map, and returns the removed value.
104    pub fn remove(self) -> V {
105        self.tree.remove_at(self.index).expect("Cannot be None")
106    }
107}
108
109/// A view into a single entry in an [IntervalMap](../struct.IntervalMap.html), which may either be vacant or occupied.
110/// This enum is constructed from the [entry](../struct.IntervalMap.html#method.entry).
111pub enum Entry<'a, T, V, Ix: IndexType = DefaultIx> {
112    Vacant(VacantEntry<'a, T, V, Ix>),
113    Occupied(OccupiedEntry<'a, T, V, Ix>)
114}
115
116impl<'a, T, V, Ix: IndexType> Entry<'a, T, V, Ix>
117where T: Copy,
118{
119    /// Returns the interval that was used for the search.
120    pub fn interval(&self) -> Range<T> {
121        match self {
122            Entry::Occupied(entry) => entry.interval(),
123            Entry::Vacant(entry) => entry.interval(),
124        }
125    }
126}
127
128impl<'a, T, V, Ix: IndexType> Entry<'a, T, V, Ix>
129where T: Copy + PartialOrd,
130{
131    /// If value is missing, initializes it with the `default` value.
132    /// In any case, returns a mutable reference to the value.
133    pub fn or_insert(self, default: V) -> &'a mut V {
134        match self {
135            Entry::Occupied(entry) => entry.into_mut(),
136            Entry::Vacant(entry) => entry.insert(default),
137        }
138    }
139
140    /// If value is missing, initializes it with a `default()` function call.
141    /// In any case, returns a mutable reference to the value.
142    pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
143        match self {
144            Entry::Occupied(entry) => entry.into_mut(),
145            Entry::Vacant(entry) => entry.insert(default()),
146        }
147    }
148
149    /// If value is missing, initializes it with a `default(interval)`.
150    /// In any case, returns a mutable reference to the value.
151    pub fn or_insert_with_interval<F>(self, default: F) -> &'a mut V
152    where F: FnOnce(Range<T>) -> V,
153    {
154        match self {
155            Entry::Occupied(entry) => entry.into_mut(),
156            Entry::Vacant(entry) => {
157                let val = default(entry.interval());
158                entry.insert(val)
159            }
160        }
161    }
162
163    /// If the entry is occupied, modifies the value with `f` function, and returns a new entry.
164    /// Does nothing if the value was vacant.
165    pub fn and_modify<F>(self, f: F) -> Self
166    where F: FnOnce(&mut V),
167    {
168        match self {
169            Entry::Occupied(mut entry) => {
170                f(entry.get_mut());
171                Entry::Occupied(entry)
172            }
173            Entry::Vacant(entry) => Entry::Vacant(entry),
174        }
175    }
176}
177
178impl<'a, T, V, Ix: IndexType> Entry<'a, T, V, Ix>
179where T: Copy + PartialOrd,
180      V: Default,
181{
182    /// If value is missing, initializes it with `V::default()`.
183    /// In any case, returns a mutable reference to the value.
184    pub fn or_default(self) -> &'a mut V {
185        match self {
186            Entry::Occupied(entry) => entry.into_mut(),
187            Entry::Vacant(entry) => entry.insert(V::default()),
188        }
189    }
190}
191
192impl<'a, T, V, Ix: IndexType> fmt::Debug for Entry<'a, T, V, Ix>
193where T: PartialOrd + Copy + fmt::Debug,
194      V: fmt::Debug,
195{
196    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
197        match self {
198            Entry::Occupied(entry) => entry.fmt(f),
199            Entry::Vacant(entry) => entry.fmt(f),
200        }
201    }
202}