Skip to content

Commit 13d07ad

Browse files
committed
add a Set trait and implement it for TreeSet
1 parent 5d07a70 commit 13d07ad

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

Diff for: 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+
}

Diff for: 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 */

Diff for: 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)