Skip to main content

collections/
btrees.rs

1// SPDX-License-Identifier: Apache-2.0
2//
3// Written in 2022-2026 by Dr. Maxim Orlovsky <orlovsky@ubideco.org>
4//
5// Copyright 2022-2026 Laboratories for Ubiquitous and Deterministic Computing,
6// Institute for Distributed and Cognitive Computing (InDCS), Switzerland.
7// All rights reserved.
8//
9// Copyright (C) 2022-2026 Dr Maxim Orlovsky.
10// All rights reserved.
11//
12// Licensed under the Apache License, Version 2.0 (the "License"); you may not
13// use this file except in compliance with the License. You may obtain a copy of
14// the License at <http://www.apache.org/licenses/LICENSE-2.0>
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19// License for the specific language governing permissions and limitations under
20// the License.
21
22use alloc::collections::{BTreeMap, BTreeSet, btree_map};
23
24use super::{Collection, CollectionBase, CollectionEntry, CollectionMap, CollectionSet, MapEntry};
25
26impl<T: Ord> CollectionBase for BTreeSet<T> {
27    type Item = T;
28
29    fn item_count(&self) -> usize {
30        BTreeSet::len(self)
31    }
32
33    fn push(&mut self, elem: Self::Item) {
34        BTreeSet::insert(self, elem);
35    }
36
37    fn clear(&mut self) {
38        BTreeSet::clear(self)
39    }
40}
41
42impl<T: Ord> Collection for BTreeSet<T> {
43    /// B-tree collections cannot reserve capacity, so this argument is ignored.
44    fn with_capacity(_capacity: usize) -> Self {
45        BTreeSet::new()
46    }
47}
48
49impl<T: Ord> CollectionSet for BTreeSet<T> {
50    fn contains(&self, item: &Self::Item) -> bool {
51        BTreeSet::contains(self, item)
52    }
53
54    fn get(&self, item: &Self::Item) -> Option<&Self::Item> {
55        BTreeSet::get(self, item)
56    }
57
58    fn iter(&self) -> impl Iterator<Item = &Self::Item> {
59        BTreeSet::iter(self)
60    }
61
62    fn insert(&mut self, item: Self::Item) -> bool {
63        BTreeSet::insert(self, item)
64    }
65
66    fn replace(&mut self, item: Self::Item) -> Option<Self::Item> {
67        BTreeSet::replace(self, item)
68    }
69
70    fn remove(&mut self, item: &Self::Item) -> bool {
71        BTreeSet::remove(self, item)
72    }
73
74    fn take(&mut self, item: &Self::Item) -> Option<Self::Item> {
75        BTreeSet::take(self, item)
76    }
77
78    fn retain(&mut self, f: impl FnMut(&Self::Item) -> bool) {
79        BTreeSet::retain(self, f)
80    }
81}
82
83impl<K: Ord, V> CollectionBase for BTreeMap<K, V> {
84    type Item = (K, V);
85
86    fn item_count(&self) -> usize {
87        BTreeMap::len(self)
88    }
89
90    fn push(&mut self, elem: Self::Item) {
91        BTreeMap::insert(self, elem.0, elem.1);
92    }
93
94    fn clear(&mut self) {
95        BTreeMap::clear(self)
96    }
97}
98
99impl<K: Ord, V> Collection for BTreeMap<K, V> {
100    /// B-tree collections cannot reserve capacity, so this argument is ignored.
101    fn with_capacity(_capacity: usize) -> Self {
102        BTreeMap::new()
103    }
104}
105
106impl<K: Ord, V> CollectionMap for BTreeMap<K, V> {
107    type Key = K;
108    type Value = V;
109
110    fn contains_key(&self, key: &Self::Key) -> bool {
111        BTreeMap::contains_key(self, key)
112    }
113
114    fn get(&self, key: &Self::Key) -> Option<&Self::Value> {
115        BTreeMap::get(self, key)
116    }
117
118    fn get_mut(&mut self, key: &Self::Key) -> Option<&mut Self::Value> {
119        BTreeMap::get_mut(self, key)
120    }
121
122    fn iter(&self) -> impl Iterator<Item = (&Self::Key, &Self::Value)> {
123        BTreeMap::iter(self)
124    }
125
126    fn keys(&self) -> impl Iterator<Item = &Self::Key> {
127        BTreeMap::keys(self)
128    }
129
130    fn values(&self) -> impl Iterator<Item = &Self::Value> {
131        BTreeMap::values(self)
132    }
133
134    fn iter_mut(&mut self) -> impl Iterator<Item = (&Self::Key, &mut Self::Value)> {
135        BTreeMap::iter_mut(self)
136    }
137
138    fn values_mut(&mut self) -> impl Iterator<Item = &mut Self::Value> {
139        BTreeMap::values_mut(self)
140    }
141
142    fn insert(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value> {
143        BTreeMap::insert(self, key, value)
144    }
145
146    fn remove(&mut self, key: &Self::Key) -> Option<Self::Value> {
147        BTreeMap::remove(self, key)
148    }
149
150    fn retain(&mut self, f: impl FnMut(&K, &mut V) -> bool) {
151        BTreeMap::retain(self, f)
152    }
153}
154
155impl<K: Ord, V> CollectionEntry for BTreeMap<K, V> {
156    type Entry<'a>
157        = btree_map::Entry<'a, K, V>
158    where
159        K: 'a,
160        V: 'a;
161
162    fn entry(&mut self, key: Self::Key) -> Self::Entry<'_> {
163        BTreeMap::entry(self, key)
164    }
165}
166
167impl<'a, K: Ord + 'a, V: 'a> MapEntry<'a> for btree_map::Entry<'a, K, V> {
168    type Key = K;
169    type Value = V;
170
171    fn key(&self) -> &Self::Key {
172        btree_map::Entry::key(self)
173    }
174
175    fn or_insert(self, default: Self::Value) -> &'a mut Self::Value {
176        btree_map::Entry::or_insert(self, default)
177    }
178
179    fn or_insert_with<F: FnOnce() -> Self::Value>(self, default: F) -> &'a mut Self::Value {
180        btree_map::Entry::or_insert_with(self, default)
181    }
182
183    fn or_default(self) -> &'a mut Self::Value
184    where
185        Self::Value: Default,
186    {
187        btree_map::Entry::or_default(self)
188    }
189
190    fn and_modify<F: FnOnce(&mut Self::Value)>(self, f: F) -> Self {
191        btree_map::Entry::and_modify(self, f)
192    }
193}