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
257
258
259
260
261
262
263
264
265
use std::{collections::{hash_map::RandomState}, hash::BuildHasher, hash::Hash, borrow::Borrow};

use crate::ScopeMap;

/// A layered hash set for representing the scopes of variables.

#[derive(Clone)]
pub struct ScopeSet<T, S: BuildHasher = RandomState> {
  map: ScopeMap<T, (), S>
}

impl<T, S: Default + BuildHasher> Default for ScopeSet<T, S> {
  /// Creates a new `ScopeSet` with the default configuration.

  #[inline]
  fn default() -> Self {
    Self {
      map: Default::default()
    }
  }
}

impl<T> ScopeSet<T, RandomState> {
  /// Creates an empty `ScopeSet` with a default hasher and capacity. 

  #[inline]
  pub fn new() -> Self {
    Default::default()
  }

  /// Creates an empty `ScopeSet` with a default hasher and the specified capacity.

  #[inline]
  pub fn with_capacity(capacity: usize) -> Self {
    Self {
      map: ScopeMap::with_capacity(capacity)
    }
  }
}

impl<T, S: BuildHasher> ScopeSet<T, S> {
  /// Creates an empty `ScopeSet` with the specified hasher and a default capacity.

  #[inline]
  pub fn with_hasher(hash_builder: S) -> Self {
    Self {
      map: ScopeMap::with_hasher(hash_builder)
    }
  }

  /// Creates an empty `ScopeSet` with the specified capacity and hasher.

  #[inline]
  pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
    Self {
      map: ScopeMap::with_capacity_and_hasher(capacity, hash_builder)
    }
  }
}

impl<T, S: BuildHasher> ScopeSet<T, S> {

  /// Returns `true` is the set is empty.

  #[inline]
  pub fn is_empty(&self) -> bool {
    self.map.is_empty()
  }

  /// Gets the number of elements the set can hold without reallocating.

  #[inline]
  pub fn capacity(&self) -> usize {
    self.map.capacity()
  }

  /// Gets the number of unique keys in the set.

  #[inline]
  pub fn len(&self) -> usize {
    self.map.len()
  }

  /// Gets the number of layers in the set.

  #[inline]
  pub fn depth(&self) -> usize {
    self.map.depth()
  }

  /// Adds a new, empty layer.

  ///

  /// Computes in **O(1)** time.

  #[inline]
  pub fn push_layer(&mut self) {
    self.map.push_layer()
  }

  /// Removes the topmost layer (if it isn't the bottom layer) and all associated keys.

  /// Returns `true` if the layer was removed.

  ///

  /// Computes in **O(n)** time in relation to the number of keys stored in the removed layer.

  #[inline]
  pub fn pop_layer(&mut self) -> bool {
    self.map.pop_layer()
  }
}

impl<T: Eq + Hash, S: BuildHasher> ScopeSet<T, S> {
  /// Removes all entries and additional layers. 

  #[inline]
  pub fn clear_all(&mut self) {
    self.map.clear_all()
  }

  /// Removes all keys in the topmost layer.

  #[inline]
  pub fn clear_top(&mut self) {
    self.map.clear_top()
  }

  /// Adds the specified key to the topmost layer.

  #[inline]
  pub fn define(&mut self, key: T) {
    self.map.define(key, ());
  }

  /// Removes the specified key from the topmost layer.

  #[inline]
  pub fn delete(&mut self, key: T) -> bool {
    self.map.delete(key)
  }

  /// Returns `true` if any layer contains the specified key.

  ///

  /// Computes in **O(1)** time.

  #[inline]
  pub fn contains<Q: ?Sized>(&self, key: &Q) -> bool
  where
    T: Borrow<Q>,
    Q: Eq + Hash,
  {
    self.map.contains_key(key)
  }

  /// Returns `true` if the topmost layer contains the specified key.

  //

  /// Computes in **O(1)** time.

  #[inline]
  pub fn contains_at_top<Q: ?Sized>(&self, key: &Q) -> bool 
  where
    T: Borrow<Q>,
    Q: Eq + Hash,
  {
    self.map.contains_key_at_top(key)
  }

  /// Gets the depth of the specified key (i.e. how many layers down the key is).

  /// A depth of 0 means that the current layer contains the key.

  ///

  /// Returns `None` if the key does not exist.

  ///

  /// Computes in **O(n)** time (worst-case) in relation to layer count.

  #[inline]
  pub fn depth_of<Q: ?Sized>(&self, key: &Q) -> Option<usize> 
  where
    T: Borrow<Q>,
    Q: Eq + Hash,
  {
    self.map.depth_of(key)
  }
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn set_init() {
    let set: ScopeSet<String> = ScopeSet::new();
    assert_eq!(0, set.len());
    assert_eq!(1, set.depth());
    assert!(set.is_empty());
  }

  #[test]
  fn set_default() {
    let set: ScopeSet<String> = Default::default();
    assert_eq!(0, set.len());
    assert_eq!(1, set.depth());
    assert!(set.is_empty());
  }

  #[test]
  fn set_capacity() {
    let set: ScopeSet<String> = ScopeSet::with_capacity(32);
    assert_eq!(32, set.capacity());
  }

  #[test]
  fn set_define() {
    let mut set = ScopeSet::new();
    set.define("foo");
    assert_eq!(1, set.len());
  }

  #[test]
  fn set_delete() {
    let mut set = ScopeSet::new();
    set.define("foo");
    set.delete("foo");
    assert!(!set.contains("foo"));
  }

  #[test]
  fn set_pop_to_delete() {
    let mut set = ScopeSet::new();
    set.push_layer();
    set.define("foo");
    assert!(set.contains("foo"));
    set.pop_layer();
    assert!(!set.contains("foo"));
  }

  #[test]
  fn set_layer_count() {
    let mut set: ScopeSet<String> = Default::default();
    set.push_layer();
    assert_eq!(2, set.depth());
    set.pop_layer();
    assert_eq!(1, set.depth());
  }

  #[test]
  fn set_try_pop_first_layer() {
    let mut set: ScopeSet<String> = Default::default();
    assert_eq!(false, set.pop_layer());
    assert_eq!(1, set.depth());
  }

  #[test]
  fn set_contains() {
    let mut set = ScopeSet::new();
    set.define("foo");
    assert!(set.contains("foo"));
  }

  #[test]
  fn set_contains_none() {
    let mut set = ScopeSet::new();
    set.define("foo");
    assert!(!set.contains("bar"));
  }

  #[test]
  fn set_contains_multi_layer() {
    let mut set = ScopeSet::new();
    set.define("foo");
    set.push_layer();
    set.define("bar");
    assert!(set.contains("foo"));
    assert!(set.contains("bar"));
  }

  #[test]
  fn set_depth_of() {
    let mut set = ScopeSet::new();
    set.define("foo");
    set.push_layer();
    set.define("bar");
    assert_eq!(Some(1), set.depth_of("foo"));
    assert_eq!(Some(0), set.depth_of("bar"));
    assert_eq!(None, set.depth_of("baz"));
  }
}