Skip to content

Commit 06685ba

Browse files
committed
add smallintset
1 parent 15e4438 commit 06685ba

File tree

1 file changed

+242
-1
lines changed

1 file changed

+242
-1
lines changed

src/libstd/smallintmap.rs

Lines changed: 242 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515

1616
use core::container::{Container, Mutable, Map, Set};
17-
use core::old_iter::{BaseIter};
17+
use core::old_iter::BaseIter;
18+
use core::old_iter;
1819
use core::option::{Some, None};
1920
use core::util::replace;
2021

@@ -181,6 +182,87 @@ pub impl<V:Copy> SmallIntMap<V> {
181182
}
182183
}
183184

185+
pub struct SmallIntSet {
186+
priv map: SmallIntMap<()>
187+
}
188+
189+
impl Container for SmallIntSet {
190+
/// Return the number of elements in the map
191+
fn len(&const self) -> uint {
192+
self.map.len()
193+
}
194+
195+
/// Return true if the map contains no elements
196+
fn is_empty(&const self) -> bool { self.len() == 0 }
197+
}
198+
199+
impl Mutable for SmallIntSet {
200+
/// Clear the map, removing all key-value pairs.
201+
fn clear(&mut self) { self.map.clear() }
202+
}
203+
204+
impl BaseIter<uint> for SmallIntSet {
205+
/// Visit all values in order
206+
fn each(&self, f: &fn(&uint) -> bool) -> bool { self.map.each_key(f) }
207+
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
208+
}
209+
210+
impl Set<uint> for SmallIntSet {
211+
/// Return true if the set contains a value
212+
fn contains(&self, value: &uint) -> bool { self.map.contains_key(value) }
213+
214+
/// Add a value to the set. Return true if the value was not already
215+
/// present in the set.
216+
fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) }
217+
218+
/// Remove a value from the set. Return true if the value was
219+
/// present in the set.
220+
fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) }
221+
222+
/// Return true if the set has no elements in common with `other`.
223+
/// This is equivalent to checking for an empty uintersection.
224+
fn is_disjoint(&self, other: &SmallIntSet) -> bool {
225+
old_iter::all(self, |v| !other.contains(v))
226+
}
227+
228+
/// Return true if the set is a subset of another
229+
fn is_subset(&self, other: &SmallIntSet) -> bool {
230+
old_iter::all(self, |v| other.contains(v))
231+
}
232+
233+
/// Return true if the set is a superset of another
234+
fn is_superset(&self, other: &SmallIntSet) -> bool {
235+
other.is_subset(self)
236+
}
237+
238+
/// Visit the values representing the difference
239+
fn difference(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool {
240+
self.each(|v| other.contains(v) || f(v))
241+
}
242+
243+
/// Visit the values representing the symmetric difference
244+
fn symmetric_difference(&self,
245+
other: &SmallIntSet,
246+
f: &fn(&uint) -> bool) -> bool {
247+
self.difference(other, f) && other.difference(self, f)
248+
}
249+
250+
/// Visit the values representing the uintersection
251+
fn intersection(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool {
252+
self.each(|v| !other.contains(v) || f(v))
253+
}
254+
255+
/// Visit the values representing the union
256+
fn union(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool {
257+
self.each(f) && other.each(|v| self.contains(v) || f(v))
258+
}
259+
}
260+
261+
pub impl SmallIntSet {
262+
/// Create an empty SmallIntSet
263+
fn new() -> SmallIntSet { SmallIntSet{map: SmallIntMap::new()} }
264+
}
265+
184266
#[cfg(test)]
185267
mod tests {
186268
use super::SmallIntMap;
@@ -273,3 +355,162 @@ mod tests {
273355
assert_eq!(m.pop(&1), None);
274356
}
275357
}
358+
359+
#[cfg(test)]
360+
mod test_set {
361+
use super::SmallIntSet;
362+
363+
#[test]
364+
fn test_disjoint() {
365+
let mut xs = SmallIntSet::new();
366+
let mut ys = SmallIntSet::new();
367+
assert!(xs.is_disjoint(&ys));
368+
assert!(ys.is_disjoint(&xs));
369+
assert!(xs.insert(5));
370+
assert!(ys.insert(11));
371+
assert!(xs.is_disjoint(&ys));
372+
assert!(ys.is_disjoint(&xs));
373+
assert!(xs.insert(7));
374+
assert!(xs.insert(19));
375+
assert!(xs.insert(4));
376+
assert!(ys.insert(2));
377+
assert!(xs.is_disjoint(&ys));
378+
assert!(ys.is_disjoint(&xs));
379+
assert!(ys.insert(7));
380+
assert!(!xs.is_disjoint(&ys));
381+
assert!(!ys.is_disjoint(&xs));
382+
}
383+
384+
#[test]
385+
fn test_subset_and_superset() {
386+
let mut a = SmallIntSet::new();
387+
assert!(a.insert(0));
388+
assert!(a.insert(5));
389+
assert!(a.insert(11));
390+
assert!(a.insert(7));
391+
392+
let mut b = SmallIntSet::new();
393+
assert!(b.insert(0));
394+
assert!(b.insert(7));
395+
assert!(b.insert(19));
396+
assert!(b.insert(250));
397+
assert!(b.insert(11));
398+
assert!(b.insert(200));
399+
400+
assert!(!a.is_subset(&b));
401+
assert!(!a.is_superset(&b));
402+
assert!(!b.is_subset(&a));
403+
assert!(!b.is_superset(&a));
404+
405+
assert!(b.insert(5));
406+
407+
assert!(a.is_subset(&b));
408+
assert!(!a.is_superset(&b));
409+
assert!(!b.is_subset(&a));
410+
assert!(b.is_superset(&a));
411+
}
412+
413+
#[test]
414+
fn test_intersection() {
415+
let mut a = SmallIntSet::new();
416+
let mut b = SmallIntSet::new();
417+
418+
assert!(a.insert(11));
419+
assert!(a.insert(1));
420+
assert!(a.insert(3));
421+
assert!(a.insert(77));
422+
assert!(a.insert(103));
423+
assert!(a.insert(5));
424+
425+
assert!(b.insert(2));
426+
assert!(b.insert(11));
427+
assert!(b.insert(77));
428+
assert!(b.insert(5));
429+
assert!(b.insert(3));
430+
431+
let mut i = 0;
432+
let expected = [3, 5, 11, 77];
433+
for a.intersection(&b) |x| {
434+
assert!(vec::contains(expected, x));
435+
i += 1
436+
}
437+
assert_eq!(i, expected.len());
438+
}
439+
440+
#[test]
441+
fn test_difference() {
442+
let mut a = SmallIntSet::new();
443+
let mut b = SmallIntSet::new();
444+
445+
assert!(a.insert(1));
446+
assert!(a.insert(3));
447+
assert!(a.insert(5));
448+
assert!(a.insert(9));
449+
assert!(a.insert(11));
450+
451+
assert!(b.insert(3));
452+
assert!(b.insert(9));
453+
454+
let mut i = 0;
455+
let expected = [1, 5, 11];
456+
for a.difference(&b) |x| {
457+
assert!(vec::contains(expected, x));
458+
i += 1
459+
}
460+
assert_eq!(i, expected.len());
461+
}
462+
463+
#[test]
464+
fn test_symmetric_difference() {
465+
let mut a = SmallIntSet::new();
466+
let mut b = SmallIntSet::new();
467+
468+
assert!(a.insert(1));
469+
assert!(a.insert(3));
470+
assert!(a.insert(5));
471+
assert!(a.insert(9));
472+
assert!(a.insert(11));
473+
474+
assert!(b.insert(3));
475+
assert!(b.insert(9));
476+
assert!(b.insert(14));
477+
assert!(b.insert(22));
478+
479+
let mut i = 0;
480+
let expected = [1, 5, 11, 14, 22];
481+
for a.symmetric_difference(&b) |x| {
482+
assert!(vec::contains(expected, x));
483+
i += 1
484+
}
485+
assert_eq!(i, expected.len());
486+
}
487+
488+
#[test]
489+
fn test_union() {
490+
let mut a = SmallIntSet::new();
491+
let mut b = SmallIntSet::new();
492+
493+
assert!(a.insert(1));
494+
assert!(a.insert(3));
495+
assert!(a.insert(5));
496+
assert!(a.insert(9));
497+
assert!(a.insert(11));
498+
assert!(a.insert(16));
499+
assert!(a.insert(19));
500+
assert!(a.insert(24));
501+
502+
assert!(b.insert(1));
503+
assert!(b.insert(5));
504+
assert!(b.insert(9));
505+
assert!(b.insert(13));
506+
assert!(b.insert(19));
507+
508+
let mut i = 0;
509+
let expected = [1, 3, 5, 9, 11, 13, 16, 19, 24];
510+
for a.union(&b) |x| {
511+
assert!(vec::contains(expected, x));
512+
i += 1
513+
}
514+
assert_eq!(i, expected.len());
515+
}
516+
}

0 commit comments

Comments
 (0)