ckb_util/linked_hash_set.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
//! A `HashSet` wrapper that holds value in insertion order.
use linked_hash_map::{self, Keys, LinkedHashMap};
use std::collections::hash_map::DefaultHasher;
use std::hash::{BuildHasher, BuildHasherDefault, Hash};
use std::iter::Extend;
type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
/// A HashSet that holds elements in insertion order.
///
/// ## Examples
///
/// ```
/// use ckb_util::LinkedHashSet;
///
/// let mut set = LinkedHashSet::new();
/// set.insert(2);
/// set.insert(1);
/// set.insert(3);
///
/// let items: Vec<i32> = set.iter().copied().collect();
/// assert_eq!(items, [2, 1, 3]);
/// ```
pub struct LinkedHashSet<T, S = DefaultBuildHasher> {
map: LinkedHashMap<T, (), S>,
}
pub struct Iter<'a, K: 'a> {
iter: Keys<'a, K, ()>,
}
impl<K> Clone for Iter<'_, K> {
fn clone(&self) -> Self {
Iter {
iter: self.iter.clone(),
}
}
}
impl<'a, K> Iterator for Iter<'a, K>
where
K: Eq + Hash,
{
type Item = &'a K;
fn next(&mut self) -> Option<&'a K> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
pub struct Difference<'a, T: 'a, S: 'a> {
// iterator of the first set
iter: Iter<'a, T>,
// the second set
other: &'a LinkedHashSet<T, S>,
}
impl<T, S> Clone for Difference<'_, T, S> {
fn clone(&self) -> Self {
Difference {
iter: self.iter.clone(),
..*self
}
}
}
impl<'a, T, S> Iterator for Difference<'a, T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
fn next(&mut self) -> Option<&'a T> {
loop {
let elt = self.iter.next()?;
if !self.other.contains(elt) {
return Some(elt);
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper)
}
}
impl<T: Hash + Eq> LinkedHashSet<T, DefaultBuildHasher> {
/// Creates a linked hash set.
///
/// ## Examples
///
/// ```
/// use ckb_util::LinkedHashSet;
/// let set: LinkedHashSet<i32> = LinkedHashSet::new();
/// ```
pub fn new() -> LinkedHashSet<T, DefaultBuildHasher> {
LinkedHashSet {
map: LinkedHashMap::default(),
}
}
/// Creates an empty linked hash map with the given initial capacity.
///
/// ## Examples
///
/// ```
/// use ckb_util::LinkedHashSet;
/// let set: LinkedHashSet<i32> = LinkedHashSet::with_capacity(42);
/// ```
pub fn with_capacity(capacity: usize) -> LinkedHashSet<T, DefaultBuildHasher> {
LinkedHashSet {
map: LinkedHashMap::with_capacity_and_hasher(capacity, Default::default()),
}
}
}
impl<T, S> LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
/// Returns `true` if the set contains a value.
///
/// ```
/// use ckb_util::LinkedHashSet;
///
/// let mut set: LinkedHashSet<_> = LinkedHashSet::new();
/// set.insert(1);
/// set.insert(2);
/// set.insert(3);
/// assert_eq!(set.contains(&1), true);
/// assert_eq!(set.contains(&4), false);
/// ```
pub fn contains(&self, value: &T) -> bool {
self.map.contains_key(value)
}
/// Returns the number of elements the set can hold without reallocating.
pub fn capacity(&self) -> usize {
self.map.capacity()
}
/// Returns the number of elements in the set.
pub fn len(&self) -> usize {
self.map.len()
}
/// Returns `true` if the set contains no elements.
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
/// Adds a value to the set.
///
/// If the set did not have this value present, true is returned.
///
/// If the set did have this value present, false is returned.
pub fn insert(&mut self, value: T) -> bool {
self.map.insert(value, ()).is_none()
}
/// Gets an iterator visiting all elements in insertion order.
///
/// The iterator element type is `&'a T`.
pub fn iter(&self) -> Iter<T> {
Iter {
iter: self.map.keys(),
}
}
/// Visits the values representing the difference, i.e., the values that are in `self` but not in `other`.
pub fn difference<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Difference<'a, T, S> {
Difference {
iter: self.iter(),
other,
}
}
/// Clears the set of all value.
pub fn clear(&mut self) {
self.map.clear();
}
}
impl<T: Hash + Eq> Default for LinkedHashSet<T, DefaultBuildHasher> {
/// Creates an empty `HashSet<T>` with the `Default` value for the hasher.
fn default() -> LinkedHashSet<T, DefaultBuildHasher> {
LinkedHashSet {
map: LinkedHashMap::default(),
}
}
}
impl<T, S> Extend<T> for LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
self.map.extend(iter.into_iter().map(|k| (k, ())));
}
}
impl<'a, T, S> IntoIterator for &'a LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}
impl<T, S> IntoIterator for LinkedHashSet<T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> IntoIter<T> {
IntoIter {
iter: self.map.into_iter(),
}
}
}
pub struct IntoIter<K> {
iter: linked_hash_map::IntoIter<K, ()>,
}
impl<K> Iterator for IntoIter<K> {
type Item = K;
fn next(&mut self) -> Option<K> {
self.iter.next().map(|(k, _)| k)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<K> ExactSizeIterator for IntoIter<K> {
fn len(&self) -> usize {
self.iter.len()
}
}