2013-01-20 12:46:06 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Container traits
|
|
|
|
|
2013-01-23 11:21:58 -06:00
|
|
|
use option::Option;
|
|
|
|
|
2013-01-21 20:59:19 -06:00
|
|
|
pub trait Container {
|
|
|
|
/// Return the number of elements in the container
|
2013-03-21 23:20:48 -05:00
|
|
|
fn len(&const self) -> uint;
|
2013-01-21 20:59:19 -06:00
|
|
|
|
|
|
|
/// Return true if the container contains no elements
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_empty(&const self) -> bool;
|
2013-01-21 20:59:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Mutable: Container {
|
2013-01-21 16:25:57 -06:00
|
|
|
/// Clear the container, removing all values.
|
|
|
|
fn clear(&mut self);
|
|
|
|
}
|
|
|
|
|
2013-01-21 17:22:03 -06:00
|
|
|
pub trait Map<K, V>: Mutable {
|
|
|
|
/// Return true if the map contains a value for the specified key
|
2013-03-21 23:20:48 -05:00
|
|
|
fn contains_key(&self, key: &K) -> bool;
|
2013-01-21 17:22:03 -06:00
|
|
|
|
2013-04-06 10:22:36 -05:00
|
|
|
// Visits all keys and values
|
|
|
|
fn each(&self, f: &fn(&K, &V) -> bool);
|
|
|
|
|
2013-01-21 17:22:03 -06:00
|
|
|
/// Visit all keys
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_key(&self, f: &fn(&K) -> bool);
|
2013-01-21 17:22:03 -06:00
|
|
|
|
|
|
|
/// Visit all values
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_value(&self, f: &fn(&V) -> bool);
|
2013-01-21 17:22:03 -06:00
|
|
|
|
2013-03-13 16:07:23 -05:00
|
|
|
/// Iterate over the map and mutate the contained values
|
|
|
|
fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool);
|
|
|
|
|
2013-03-24 15:55:51 -05:00
|
|
|
/// Return a reference to the value corresponding to the key
|
2013-03-21 23:20:48 -05:00
|
|
|
fn find(&self, key: &K) -> Option<&'self V>;
|
2013-01-23 11:21:58 -06:00
|
|
|
|
2013-03-24 15:55:51 -05:00
|
|
|
/// Return a mutable reference to the value corresponding to the key
|
2013-03-24 19:40:17 -05:00
|
|
|
fn find_mut(&mut self, key: &K) -> Option<&'self mut V>;
|
2013-03-24 15:55:51 -05:00
|
|
|
|
2013-01-21 17:22:03 -06:00
|
|
|
/// Insert a key-value pair into the map. An existing value for a
|
|
|
|
/// key is replaced by the new value. Return true if the key did
|
|
|
|
/// not already exist in the map.
|
|
|
|
fn insert(&mut self, key: K, value: V) -> bool;
|
|
|
|
|
|
|
|
/// Remove a key-value pair from the map. Return true if the key
|
|
|
|
/// was present in the map, otherwise false.
|
|
|
|
fn remove(&mut self, key: &K) -> bool;
|
|
|
|
}
|
|
|
|
|
2013-01-21 16:25:57 -06:00
|
|
|
pub trait Set<T>: Mutable {
|
2013-01-20 12:46:06 -06:00
|
|
|
/// Return true if the set contains a value
|
2013-03-21 23:20:48 -05:00
|
|
|
fn contains(&self, value: &T) -> bool;
|
2013-01-20 12:46:06 -06:00
|
|
|
|
|
|
|
/// Add a value to the set. Return true if the value was not already
|
|
|
|
/// present in the set.
|
|
|
|
fn insert(&mut self, value: T) -> bool;
|
|
|
|
|
|
|
|
/// Remove a value from the set. Return true if the value was
|
|
|
|
/// present in the set.
|
|
|
|
fn remove(&mut self, value: &T) -> bool;
|
2013-01-29 15:07:11 -06:00
|
|
|
|
2013-01-29 16:04:25 -06:00
|
|
|
/// Return true if the set has no elements in common with `other`.
|
|
|
|
/// This is equivalent to checking for an empty intersection.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_disjoint(&self, other: &Self) -> bool;
|
2013-01-29 16:04:25 -06:00
|
|
|
|
2013-01-29 15:07:11 -06:00
|
|
|
/// Return true if the set is a subset of another
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_subset(&self, other: &Self) -> bool;
|
2013-01-29 15:07:11 -06:00
|
|
|
|
|
|
|
/// Return true if the set is a superset of another
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_superset(&self, other: &Self) -> bool;
|
2013-01-29 18:30:26 -06:00
|
|
|
|
|
|
|
/// Visit the values representing the difference
|
2013-03-21 23:20:48 -05:00
|
|
|
fn difference(&self, other: &Self, f: &fn(&T) -> bool);
|
2013-01-29 18:30:26 -06:00
|
|
|
|
|
|
|
/// Visit the values representing the symmetric difference
|
2013-03-21 23:20:48 -05:00
|
|
|
fn symmetric_difference(&self, other: &Self, f: &fn(&T) -> bool);
|
2013-01-29 20:58:47 -06:00
|
|
|
|
|
|
|
/// Visit the values representing the intersection
|
2013-03-21 23:20:48 -05:00
|
|
|
fn intersection(&self, other: &Self, f: &fn(&T) -> bool);
|
2013-01-29 20:58:47 -06:00
|
|
|
|
|
|
|
/// Visit the values representing the union
|
2013-03-21 23:20:48 -05:00
|
|
|
fn union(&self, other: &Self, f: &fn(&T) -> bool);
|
2013-01-20 12:46:06 -06:00
|
|
|
}
|