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>(self, default: F) -> &'a mut V
143 where F: FnOnce() -> V,
144 {
145 match self {
146 Entry::Occupied(entry) => entry.into_mut(),
147 Entry::Vacant(entry) => entry.insert(default()),
148 }
149 }
150
151 pub fn or_insert_with_interval<F>(self, default: F) -> &'a mut V
154 where F: FnOnce(Range<T>) -> V,
155 {
156 match self {
157 Entry::Occupied(entry) => entry.into_mut(),
158 Entry::Vacant(entry) => {
159 let val = default(entry.interval());
160 entry.insert(val)
161 }
162 }
163 }
164
165 pub fn and_modify<F>(self, f: F) -> Self
168 where F: FnOnce(&mut V),
169 {
170 match self {
171 Entry::Occupied(mut entry) => {
172 f(entry.get_mut());
173 Entry::Occupied(entry)
174 }
175 Entry::Vacant(entry) => Entry::Vacant(entry),
176 }
177 }
178}
179
180impl<'a, T, V, Ix: IndexType> Entry<'a, T, V, Ix>
181where T: Copy + PartialOrd,
182 V: Default,
183{
184 pub fn or_default(self) -> &'a mut V {
187 match self {
188 Entry::Occupied(entry) => entry.into_mut(),
189 Entry::Vacant(entry) => entry.insert(V::default()),
190 }
191 }
192}
193
194impl<'a, T, V, Ix: IndexType> fmt::Debug for Entry<'a, T, V, Ix>
195where T: PartialOrd + Copy + fmt::Debug,
196 V: fmt::Debug,
197{
198 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199 match self {
200 Entry::Occupied(entry) => entry.fmt(f),
201 Entry::Vacant(entry) => entry.fmt(f),
202 }
203 }
204}