1use crate::{
2 map::{
3 ExtKeyMap, Map, MapEntry, MapWithEntry, OccupiedError, OccupiedMapEntry, OrderedKeyMap,
4 VacantMapEntry,
5 },
6 util::{self, KeyMap, KeyMapMut},
7 KeySet,
8};
9use alloc::collections::{
10 btree_map::{self, Iter, IterMut, Range, RangeMut},
11 BTreeMap, BTreeSet,
12};
13use core::{fmt::Debug, ops::RangeBounds};
14
15impl<K: Copy + Eq + Ord + Debug + 'static, T> Map<T> for BTreeMap<K, T> {
16 type Key = K;
17 type Iter<'a> = KeyMap<'a, Iter<'a, K, T>, K, T> where Self: 'a, T: 'a;
18 type IterMut<'a> = KeyMapMut<'a, IterMut<'a, K, T>, K, T> where Self: 'a, T: 'a;
19
20 fn len(&self) -> usize {
21 BTreeMap::len(self)
22 }
23
24 fn is_empty(&self) -> bool {
25 BTreeMap::is_empty(self)
26 }
27
28 fn contains_key(&self, index: Self::Key) -> bool {
29 BTreeMap::contains_key(self, &index)
30 }
31
32 fn get(&self, index: Self::Key) -> Option<&T> {
33 BTreeMap::get(self, &index)
34 }
35
36 fn get_mut(&mut self, index: Self::Key) -> Option<&mut T> {
37 BTreeMap::get_mut(self, &index)
38 }
39
40 fn remove(&mut self, index: Self::Key) -> Option<T> {
41 BTreeMap::remove(self, &index)
42 }
43
44 fn clear(&mut self) {
45 BTreeMap::clear(self)
46 }
47
48 fn iter(&self) -> Self::Iter<'_> {
49 BTreeMap::iter(self).map(util::map_deref_key)
50 }
51
52 fn iter_mut(&mut self) -> Self::IterMut<'_> {
53 BTreeMap::iter_mut(self).map(util::map_deref_key)
54 }
55}
56
57impl<K: Copy + Eq + Ord + Debug + 'static, T> ExtKeyMap<T> for BTreeMap<K, T> {
58 fn try_insert(&mut self, key: Self::Key, value: T) -> Result<(), OccupiedError> {
59 match BTreeMap::entry(self, key) {
60 btree_map::Entry::Vacant(entry) => {
61 entry.insert(value);
62 Ok(())
63 }
64 btree_map::Entry::Occupied(_) => Err(OccupiedError),
65 }
66 }
67
68 fn replace(&mut self, key: Self::Key, value: T) -> Option<T> {
69 BTreeMap::insert(self, key, value)
70 }
71}
72
73impl<K: Copy + Eq + Ord + Debug + 'static, T> OrderedKeyMap<T> for BTreeMap<K, T> {
74 type Range<'a> = KeyMap<'a, Range<'a, K, T>, K, T> where Self: 'a;
75 type RangeMut<'a> = KeyMapMut<'a, RangeMut<'a, K, T>, K, T> where Self: 'a;
76
77 fn range<R: RangeBounds<Self::Key>>(&self, range: R) -> Self::Range<'_> {
78 BTreeMap::range(self, range).map(util::map_deref_key)
79 }
80
81 fn range_mut<R: RangeBounds<Self::Key>>(&mut self, range: R) -> Self::RangeMut<'_> {
82 BTreeMap::range_mut(self, range).map(util::map_deref_key)
83 }
84
85 fn first(&self) -> Option<(K, &T)> {
86 BTreeMap::first_key_value(self).map(|(k, v)| (*k, v))
87 }
88
89 fn first_mut(&mut self) -> Option<(K, &mut T)> {
90 BTreeMap::first_entry(self).map(|entry| {
91 let key = *entry.key();
92 let value = entry.into_mut();
93 (key, value)
94 })
95 }
96
97 fn last(&self) -> Option<(K, &T)> {
98 BTreeMap::last_key_value(self).map(|(k, v)| (*k, v))
99 }
100
101 fn last_mut(&mut self) -> Option<(K, &mut T)> {
102 BTreeMap::last_entry(self).map(|entry| {
103 let key = *entry.key();
104 let value = entry.into_mut();
105 (key, value)
106 })
107 }
108}
109
110impl<'a, K: Copy + Eq + Ord + Debug + 'static, T> VacantMapEntry
111 for btree_map::VacantEntry<'a, K, T>
112{
113 type Value = T;
114
115 fn insert<'b>(self, value: Self::Value) -> &'b mut Self::Value
116 where
117 Self: 'b,
118 {
119 btree_map::VacantEntry::insert(self, value)
120 }
121}
122
123impl<'a, K: Copy + Eq + Ord + Debug + 'static, T> OccupiedMapEntry
124 for btree_map::OccupiedEntry<'a, K, T>
125{
126 type Value = T;
127
128 fn get(&self) -> &Self::Value {
129 btree_map::OccupiedEntry::get(self)
130 }
131
132 fn get_mut(&mut self) -> &mut Self::Value {
133 btree_map::OccupiedEntry::get_mut(self)
134 }
135
136 fn into_mut<'b>(self) -> &'b mut Self::Value
137 where
138 Self: 'b,
139 {
140 btree_map::OccupiedEntry::into_mut(self)
141 }
142
143 fn replace(&mut self, value: Self::Value) -> Self::Value {
144 btree_map::OccupiedEntry::insert(self, value)
145 }
146
147 fn remove(self) -> Self::Value {
148 btree_map::OccupiedEntry::remove(self)
149 }
150}
151
152impl<K: Copy + Eq + Ord + Debug + 'static, T> MapWithEntry<T> for BTreeMap<K, T> {
153 type VacantEntry<'a> = btree_map::VacantEntry<'a, K, T> where T: 'a;
154 type OccupiedEntry<'a> = btree_map::OccupiedEntry<'a, K, T> where T: 'a;
155
156 fn entry(
157 &mut self,
158 key: Self::Key,
159 ) -> MapEntry<Self::OccupiedEntry<'_>, Self::VacantEntry<'_>> {
160 match BTreeMap::entry(self, key) {
161 btree_map::Entry::Occupied(entry) => MapEntry::Occupied(entry),
162 btree_map::Entry::Vacant(entry) => MapEntry::Vacant(entry),
163 }
164 }
165}
166
167impl<K: Copy + Eq + Ord + Debug + 'static> KeySet<K> for BTreeSet<K> {
168 fn len(&self) -> usize {
169 BTreeSet::len(self)
170 }
171
172 fn is_empty(&self) -> bool {
173 BTreeSet::is_empty(self)
174 }
175
176 fn contains(&self, index: K) -> bool {
177 BTreeSet::contains(self, &index)
178 }
179
180 fn insert(&mut self, index: K) -> bool {
181 BTreeSet::insert(self, index)
182 }
183
184 fn remove(&mut self, index: K) -> bool {
185 BTreeSet::remove(self, &index)
186 }
187
188 fn clear(&mut self) {
189 BTreeSet::clear(self)
190 }
191}