1use core::{
2 fmt,
3 ops::Range,
4};
5use super::{
6 IntervalMap, Interval,
7 ix::{IndexType, DefaultIx},
8};
9
10pub 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 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 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
48pub 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 pub fn interval(&self) -> Range<T> {
63 self.tree.nodes[self.index.get()].interval.to_range()
64 }
65
66 pub fn get(&self) -> &V {
68 &self.tree.nodes[self.index.get()].value
69 }
70
71 pub fn get_mut(&mut self) -> &mut V {
75 &mut self.tree.nodes[self.index.get()].value
76 }
77
78 pub fn into_mut(self) -> &'a mut V {
81 &mut self.tree.nodes[self.index.get()].value
82 }
83
84 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 pub fn remove(self) -> V {
105 self.tree.remove_at(self.index).expect("Cannot be None")
106 }
107}
108
109pub 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 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 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 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 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 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 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}