rust/src/libstd/container.rs

114 lines
4.1 KiB
Rust
Raw Normal View History

// 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
use option::Option;
/// A trait to represent the abstract idea of a container. The only concrete
/// knowledge known is the number of elements contained within.
2013-01-21 20:59:19 -06:00
pub trait Container {
/// Return the number of elements in the container
2013-06-15 01:20:06 -05:00
fn len(&self) -> uint;
2013-01-21 20:59:19 -06:00
/// Return true if the container contains no elements
#[inline]
fn is_empty(&self) -> bool {
self.len() == 0
}
2013-01-21 20:59:19 -06:00
}
/// A trait to represent mutable containers
2013-01-21 20:59:19 -06:00
pub trait Mutable: Container {
/// Clear the container, removing all values.
fn clear(&mut self);
}
/// A map is a key-value store where values may be looked up by their keys. This
/// trait provides basic operations to operate on these stores.
pub trait Map<K, V>: Container {
2013-04-10 15:11:35 -05:00
/// Return true if the map contains a value for the specified key
fn contains_key(&self, key: &K) -> bool;
/// Return a reference to the value corresponding to the key
fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
}
2013-04-10 15:11:35 -05:00
/// This trait provides basic operations to modify the contents of a map.
pub trait MutableMap<K, V>: Map<K, V> + Mutable {
2013-04-10 15:11:35 -05: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.
#[inline]
fn insert(&mut self, key: K, value: V) -> bool {
self.swap(key, value).is_none()
}
2013-04-10 15:11:35 -05:00
/// Remove a key-value pair from the map. Return true if the key
/// was present in the map, otherwise false.
#[inline]
fn remove(&mut self, key: &K) -> bool {
self.pop(key).is_some()
}
2013-05-04 08:54:58 -05:00
/// Insert a key-value pair from the map. If the key already had a value
/// present in the map, that value is returned. Otherwise None is returned.
fn swap(&mut self, k: K, v: V) -> Option<V>;
/// Removes a key from the map, returning the value at the key if the key
/// was previously in the map.
fn pop(&mut self, k: &K) -> Option<V>;
/// Return a mutable reference to the value corresponding to the key
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
2013-04-10 15:11:35 -05:00
}
/// A set is a group of objects which are each distinct from one another. This
/// trait represents actions which can be performed on sets to iterate over
/// them.
pub trait Set<T>: Container {
2013-05-02 17:33:18 -05:00
/// Return true if the set contains a value
fn contains(&self, value: &T) -> bool;
/// Return true if the set has no elements in common with `other`.
/// This is equivalent to checking for an empty intersection.
fn is_disjoint(&self, other: &Self) -> bool;
/// Return true if the set is a subset of another
fn is_subset(&self, other: &Self) -> bool;
/// Return true if the set is a superset of another
fn is_superset(&self, other: &Self) -> bool;
/// Visit the values representing the difference
fn difference(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
/// Visit the values representing the symmetric difference
fn symmetric_difference(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
/// Visit the values representing the intersection
fn intersection(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
/// Visit the values representing the union
fn union(&self, other: &Self, f: &fn(&T) -> bool) -> bool;
}
/// This trait represents actions which can be performed on sets to mutate
/// them.
pub trait MutableSet<T>: Set<T> + Mutable {
/// 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;
}