nautilus_core/
collections.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Abstraction layer over common hash-based containers.
17
18use std::{
19    collections::{HashMap, HashSet},
20    fmt::{Debug, Display},
21    hash::Hash,
22};
23
24/// Represents a generic set-like container with members.
25pub trait SetLike {
26    type Item: Hash + Eq + Display + Clone;
27
28    fn contains(&self, item: &Self::Item) -> bool;
29    fn is_empty(&self) -> bool;
30}
31
32impl<T, S> SetLike for HashSet<T, S>
33where
34    T: Eq + Hash + Display + Clone,
35    S: std::hash::BuildHasher,
36{
37    type Item = T;
38
39    #[inline]
40    fn contains(&self, v: &T) -> bool {
41        Self::contains(self, v)
42    }
43
44    #[inline]
45    fn is_empty(&self) -> bool {
46        Self::is_empty(self)
47    }
48}
49
50impl<T, S> SetLike for indexmap::IndexSet<T, S>
51where
52    T: Eq + Hash + Display + Clone,
53    S: std::hash::BuildHasher,
54{
55    type Item = T;
56
57    #[inline]
58    fn contains(&self, v: &T) -> bool {
59        Self::contains(self, v)
60    }
61
62    #[inline]
63    fn is_empty(&self) -> bool {
64        Self::is_empty(self)
65    }
66}
67
68impl<T, S> SetLike for ahash::AHashSet<T, S>
69where
70    T: Eq + Hash + Display + Clone,
71    S: std::hash::BuildHasher,
72{
73    type Item = T;
74
75    #[inline]
76    fn contains(&self, v: &T) -> bool {
77        self.get(v).is_some()
78    }
79
80    #[inline]
81    fn is_empty(&self) -> bool {
82        self.len() == 0
83    }
84}
85
86/// Represents a generic map-like container with key-value pairs.
87pub trait MapLike {
88    type Key: Hash + Eq + Display + Clone;
89    type Value: Debug;
90
91    fn contains_key(&self, key: &Self::Key) -> bool;
92    fn is_empty(&self) -> bool;
93}
94
95impl<K, V, S> MapLike for HashMap<K, V, S>
96where
97    K: Eq + Hash + Display + Clone,
98    V: Debug,
99    S: std::hash::BuildHasher,
100{
101    type Key = K;
102    type Value = V;
103
104    #[inline]
105    fn contains_key(&self, k: &K) -> bool {
106        self.contains_key(k)
107    }
108
109    #[inline]
110    fn is_empty(&self) -> bool {
111        self.is_empty()
112    }
113}
114
115impl<K, V, S> MapLike for indexmap::IndexMap<K, V, S>
116where
117    K: Eq + Hash + Display + Clone,
118    V: Debug,
119    S: std::hash::BuildHasher,
120{
121    type Key = K;
122    type Value = V;
123
124    #[inline]
125    fn contains_key(&self, k: &K) -> bool {
126        self.get(k).is_some()
127    }
128
129    #[inline]
130    fn is_empty(&self) -> bool {
131        self.is_empty()
132    }
133}
134
135impl<K, V, S> MapLike for ahash::AHashMap<K, V, S>
136where
137    K: Eq + Hash + Display + Clone,
138    V: Debug,
139    S: std::hash::BuildHasher,
140{
141    type Key = K;
142    type Value = V;
143
144    #[inline]
145    fn contains_key(&self, k: &K) -> bool {
146        self.get(k).is_some()
147    }
148
149    #[inline]
150    fn is_empty(&self) -> bool {
151        self.len() == 0
152    }
153}