Skip to content

Commit b86c90b

Browse files
committed
Merge pull request #4554 from thestinger/set
add a Set trait
2 parents da55521 + 5320e13 commit b86c90b

File tree

4 files changed

+86
-15
lines changed

4 files changed

+86
-15
lines changed

src/libcore/container.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Container traits
12+
13+
pub trait Set<T> {
14+
/// Return true if the set contains a value
15+
pure fn contains(&self, value: &T) -> bool;
16+
17+
/// Add a value to the set. Return true if the value was not already
18+
/// present in the set.
19+
fn insert(&mut self, value: T) -> bool;
20+
21+
/// Remove a value from the set. Return true if the value was
22+
/// present in the set.
23+
fn remove(&mut self, value: &T) -> bool;
24+
}

src/libcore/core.rc

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub mod to_bytes;
122122
pub mod clone;
123123
pub mod io;
124124
pub mod hash;
125+
pub mod container;
125126

126127

127128
/* Common data structures */

src/libcore/send_map.rs

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -46,6 +46,8 @@ pub trait SendMap<K:Eq Hash, V: Copy> {
4646

4747
/// Open addressing with linear probing.
4848
pub mod linear {
49+
use iter::BaseIter;
50+
use container::Set;
4951
use cmp::Eq;
5052
use cmp;
5153
use hash::Hash;
@@ -442,7 +444,7 @@ pub mod linear {
442444
}
443445
}
444446

445-
impl<K:Hash IterBytes Eq, V: Eq> LinearMap<K, V>: cmp::Eq {
447+
impl<K:Hash IterBytes Eq, V: Eq> LinearMap<K, V>: Eq {
446448
pure fn eq(&self, other: &LinearMap<K, V>) -> bool {
447449
if self.len() != other.len() { return false; }
448450

@@ -460,6 +462,47 @@ pub mod linear {
460462
!self.eq(other)
461463
}
462464
}
465+
466+
pub struct LinearSet<T: Hash IterBytes Eq> {
467+
priv map: LinearMap<T, ()>
468+
}
469+
470+
impl <T: Hash IterBytes Eq> LinearSet<T>: BaseIter<T> {
471+
/// Visit all values in order
472+
pure fn each(&self, f: fn(&T) -> bool) { self.map.each_key(f) }
473+
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
474+
}
475+
476+
impl <T: Hash IterBytes Eq> LinearSet<T>: Eq {
477+
pure fn eq(&self, other: &LinearSet<T>) -> bool { self.map == other.map }
478+
pure fn ne(&self, other: &LinearSet<T>) -> bool { self.map != other.map }
479+
}
480+
481+
impl <T: Hash IterBytes Eq> LinearSet<T>: Set<T> {
482+
/// Return true if the set contains a value
483+
pure fn contains(&self, value: &T) -> bool {
484+
self.map.contains_key(value)
485+
}
486+
487+
/// Add a value to the set. Return true if the value was not already
488+
/// present in the set.
489+
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
490+
491+
/// Remove a value from the set. Return true if the value was
492+
/// present in the set.
493+
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
494+
}
495+
496+
impl <T: Hash IterBytes Eq> LinearSet<T> {
497+
/// Create an empty LinearSet
498+
static fn new() -> LinearSet<T> { LinearSet{map: LinearMap()} }
499+
500+
/// Return the number of elements in the set
501+
pure fn len(&self) -> uint { self.map.len() }
502+
503+
/// Return true if the set contains no elements
504+
pure fn is_empty(&self) -> bool { self.map.is_empty() }
505+
}
463506
}
464507

465508
#[test]

src/libstd/treemap.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
#[forbid(deprecated_mode)];
1616

17+
use core::container::Set;
1718
use core::cmp::{Eq, Ord};
1819
use core::option::{Option, Some, None};
1920
use core::prelude::*;
@@ -197,6 +198,21 @@ impl <T: Eq Ord> TreeSet<T>: Eq {
197198
pure fn ne(&self, other: &TreeSet<T>) -> bool { self.map != other.map }
198199
}
199200

201+
impl <T: Ord> TreeSet<T>: Set<T> {
202+
/// Return true if the set contains a value
203+
pure fn contains(&self, value: &T) -> bool {
204+
self.map.contains_key(value)
205+
}
206+
207+
/// Add a value to the set. Return true if the value was not already
208+
/// present in the set.
209+
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
210+
211+
/// Remove a value from the set. Return true if the value was
212+
/// present in the set.
213+
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
214+
}
215+
200216
impl <T: Ord> TreeSet<T> {
201217
/// Create an empty TreeSet
202218
static pure fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
@@ -215,19 +231,6 @@ impl <T: Ord> TreeSet<T> {
215231
self.map.each_key_reverse(f)
216232
}
217233

218-
/// Return true if the set contains a value
219-
pure fn contains(&self, value: &T) -> bool {
220-
self.map.contains_key(value)
221-
}
222-
223-
/// Add a value to the set. Return true if the value was not
224-
/// already present in the set.
225-
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
226-
227-
/// Remove a value from the set. Return true if the value was
228-
/// present in the set.
229-
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
230-
231234
/// Get a lazy iterator over the values in the set.
232235
/// Requires that it be frozen (immutable).
233236
pure fn iter(&self) -> TreeSetIterator/&self<T> {

0 commit comments

Comments
 (0)