Skip to main content

collections/
indexed.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 core::hash::{BuildHasher, Hash};
23
24use indexmap::{IndexMap, IndexSet, map};
25
26use super::{
27    Collection, CollectionBase, CollectionCapacity, CollectionEntry, CollectionMap, CollectionSet,
28    MapEntry,
29};
30
31impl<T: Eq + Hash, S: BuildHasher> CollectionBase for IndexSet<T, S> {
32    type Item = T;
33
34    fn item_count(&self) -> usize {
35        IndexSet::len(self)
36    }
37
38    fn push(&mut self, elem: Self::Item) {
39        IndexSet::insert(self, elem);
40    }
41
42    fn clear(&mut self) {
43        IndexSet::clear(self)
44    }
45}
46
47impl<T: Eq + Hash, S: BuildHasher + Default> Collection for IndexSet<T, S> {
48    fn with_capacity(capacity: usize) -> Self {
49        IndexSet::with_capacity_and_hasher(capacity, S::default())
50    }
51}
52
53impl<T: Eq + Hash, S: BuildHasher> CollectionSet for IndexSet<T, S> {
54    fn contains(&self, item: &Self::Item) -> bool {
55        IndexSet::contains(self, item)
56    }
57
58    fn get(&self, item: &Self::Item) -> Option<&Self::Item> {
59        IndexSet::get(self, item)
60    }
61
62    fn iter(&self) -> impl Iterator<Item = &Self::Item> {
63        IndexSet::iter(self)
64    }
65
66    fn insert(&mut self, item: Self::Item) -> bool {
67        IndexSet::insert(self, item)
68    }
69
70    fn replace(&mut self, item: Self::Item) -> Option<Self::Item> {
71        IndexSet::replace(self, item)
72    }
73
74    fn remove(&mut self, item: &Self::Item) -> bool {
75        IndexSet::shift_remove(self, item)
76    }
77
78    fn take(&mut self, item: &Self::Item) -> Option<Self::Item> {
79        IndexSet::shift_take(self, item)
80    }
81
82    fn retain(&mut self, f: impl FnMut(&Self::Item) -> bool) {
83        IndexSet::retain(self, f)
84    }
85}
86
87impl<T: Eq + Hash, S: BuildHasher> CollectionCapacity for IndexSet<T, S> {
88    type ReserveError = indexmap::TryReserveError;
89
90    fn capacity(&self) -> usize {
91        IndexSet::capacity(self)
92    }
93
94    fn reserve(&mut self, additional: usize) {
95        IndexSet::reserve(self, additional)
96    }
97
98    fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError> {
99        IndexSet::try_reserve(self, additional)
100    }
101}
102
103impl<K: Eq + Hash, V, S: BuildHasher> CollectionBase for IndexMap<K, V, S> {
104    type Item = (K, V);
105
106    fn item_count(&self) -> usize {
107        IndexMap::len(self)
108    }
109
110    fn push(&mut self, elem: Self::Item) {
111        IndexMap::insert(self, elem.0, elem.1);
112    }
113
114    fn clear(&mut self) {
115        IndexMap::clear(self)
116    }
117}
118
119impl<K: Eq + Hash, V, S: BuildHasher + Default> Collection for IndexMap<K, V, S> {
120    fn with_capacity(capacity: usize) -> Self {
121        IndexMap::with_capacity_and_hasher(capacity, S::default())
122    }
123}
124
125impl<K: Eq + Hash, V, S: BuildHasher> CollectionMap for IndexMap<K, V, S> {
126    type Key = K;
127    type Value = V;
128
129    fn contains_key(&self, key: &Self::Key) -> bool {
130        IndexMap::contains_key(self, key)
131    }
132
133    fn get(&self, key: &Self::Key) -> Option<&Self::Value> {
134        IndexMap::get(self, key)
135    }
136
137    fn get_mut(&mut self, key: &Self::Key) -> Option<&mut Self::Value> {
138        IndexMap::get_mut(self, key)
139    }
140
141    fn iter(&self) -> impl Iterator<Item = (&Self::Key, &Self::Value)> {
142        IndexMap::iter(self)
143    }
144
145    fn keys(&self) -> impl Iterator<Item = &Self::Key> {
146        IndexMap::keys(self)
147    }
148
149    fn values(&self) -> impl Iterator<Item = &Self::Value> {
150        IndexMap::values(self)
151    }
152
153    fn iter_mut(&mut self) -> impl Iterator<Item = (&Self::Key, &mut Self::Value)> {
154        IndexMap::iter_mut(self)
155    }
156
157    fn values_mut(&mut self) -> impl Iterator<Item = &mut Self::Value> {
158        IndexMap::values_mut(self)
159    }
160
161    fn insert(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value> {
162        IndexMap::insert(self, key, value)
163    }
164
165    fn remove(&mut self, key: &Self::Key) -> Option<Self::Value> {
166        IndexMap::shift_remove(self, key)
167    }
168
169    fn retain(&mut self, f: impl FnMut(&K, &mut V) -> bool) {
170        IndexMap::retain(self, f)
171    }
172}
173
174impl<K: Eq + Hash, V, S: BuildHasher> CollectionCapacity for IndexMap<K, V, S> {
175    type ReserveError = indexmap::TryReserveError;
176
177    fn capacity(&self) -> usize {
178        IndexMap::capacity(self)
179    }
180
181    fn reserve(&mut self, additional: usize) {
182        IndexMap::reserve(self, additional)
183    }
184
185    fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError> {
186        IndexMap::try_reserve(self, additional)
187    }
188}
189
190impl<K: Eq + Hash, V, S: BuildHasher> CollectionEntry for IndexMap<K, V, S> {
191    type Entry<'a>
192        = map::Entry<'a, K, V>
193    where
194        K: 'a,
195        V: 'a,
196        S: 'a;
197
198    fn entry(&mut self, key: Self::Key) -> Self::Entry<'_> {
199        IndexMap::entry(self, key)
200    }
201}
202
203impl<'a, K: 'a, V: 'a> MapEntry<'a> for map::Entry<'a, K, V> {
204    type Key = K;
205    type Value = V;
206
207    fn key(&self) -> &Self::Key {
208        map::Entry::key(self)
209    }
210
211    fn or_insert(self, default: Self::Value) -> &'a mut Self::Value {
212        map::Entry::or_insert(self, default)
213    }
214
215    fn or_insert_with<F: FnOnce() -> Self::Value>(self, default: F) -> &'a mut Self::Value {
216        map::Entry::or_insert_with(self, default)
217    }
218
219    fn or_default(self) -> &'a mut Self::Value
220    where
221        Self::Value: Default,
222    {
223        map::Entry::or_default(self)
224    }
225
226    fn and_modify<F: FnOnce(&mut Self::Value)>(self, f: F) -> Self {
227        map::Entry::and_modify(self, f)
228    }
229}