De-export std::{fun_treemap, list, map}. Part of #3583.
This commit is contained in:
parent
092de78fab
commit
201513e859
@ -15,13 +15,7 @@ use core::cmp::{Eq, Ord};
|
||||
use option::{Some, None};
|
||||
use option = option;
|
||||
|
||||
export Treemap;
|
||||
export init;
|
||||
export insert;
|
||||
export find;
|
||||
export traverse;
|
||||
|
||||
type Treemap<K, V> = @TreeNode<K, V>;
|
||||
pub type Treemap<K, V> = @TreeNode<K, V>;
|
||||
|
||||
enum TreeNode<K, V> {
|
||||
Empty,
|
||||
@ -29,10 +23,10 @@ enum TreeNode<K, V> {
|
||||
}
|
||||
|
||||
/// Create a treemap
|
||||
fn init<K, V>() -> Treemap<K, V> { @Empty }
|
||||
pub fn init<K, V>() -> Treemap<K, V> { @Empty }
|
||||
|
||||
/// Insert a value into the map
|
||||
fn insert<K: Copy Eq Ord, V: Copy>(m: Treemap<K, V>, +k: K, +v: V)
|
||||
pub fn insert<K: Copy Eq Ord, V: Copy>(m: Treemap<K, V>, +k: K, +v: V)
|
||||
-> Treemap<K, V> {
|
||||
@match m {
|
||||
@Empty => Node(@k, @v, @Empty, @Empty),
|
||||
@ -47,7 +41,7 @@ fn insert<K: Copy Eq Ord, V: Copy>(m: Treemap<K, V>, +k: K, +v: V)
|
||||
}
|
||||
|
||||
/// Find a value based on the key
|
||||
fn find<K: Eq Ord, V: Copy>(m: Treemap<K, V>, +k: K) -> Option<V> {
|
||||
pub fn find<K: Eq Ord, V: Copy>(m: Treemap<K, V>, +k: K) -> Option<V> {
|
||||
match *m {
|
||||
Empty => None,
|
||||
Node(@ref kk, @copy v, left, right) => {
|
||||
@ -59,7 +53,7 @@ fn find<K: Eq Ord, V: Copy>(m: Treemap<K, V>, +k: K) -> Option<V> {
|
||||
}
|
||||
|
||||
/// Visit all pairs in the map in order.
|
||||
fn traverse<K, V: Copy>(m: Treemap<K, V>, f: fn((&K), (&V))) {
|
||||
pub fn traverse<K, V: Copy>(m: Treemap<K, V>, f: fn((&K), (&V))) {
|
||||
match *m {
|
||||
Empty => (),
|
||||
/*
|
||||
|
@ -6,13 +6,13 @@ use core::option;
|
||||
use option::*;
|
||||
use option::{Some, None};
|
||||
|
||||
enum List<T> {
|
||||
pub enum List<T> {
|
||||
Cons(T, @List<T>),
|
||||
Nil,
|
||||
}
|
||||
|
||||
/// Cregate a list from a vector
|
||||
fn from_vec<T: Copy>(v: &[T]) -> @List<T> {
|
||||
pub fn from_vec<T: Copy>(v: &[T]) -> @List<T> {
|
||||
vec::foldr(v, @Nil::<T>, |h, t| @Cons(*h, t))
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ fn from_vec<T: Copy>(v: &[T]) -> @List<T> {
|
||||
* * z - The initial value
|
||||
* * f - The function to apply
|
||||
*/
|
||||
fn foldl<T: Copy, U>(+z: T, ls: @List<U>, f: fn((&T), (&U)) -> T) -> T {
|
||||
pub fn foldl<T: Copy, U>(+z: T, ls: @List<U>, f: fn((&T), (&U)) -> T) -> T {
|
||||
let mut accum: T = z;
|
||||
do iter(ls) |elt| { accum = f(&accum, elt);}
|
||||
accum
|
||||
@ -42,7 +42,7 @@ fn foldl<T: Copy, U>(+z: T, ls: @List<U>, f: fn((&T), (&U)) -> T) -> T {
|
||||
* When function `f` returns true then an option containing the element
|
||||
* is returned. If `f` matches no elements then none is returned.
|
||||
*/
|
||||
fn find<T: Copy>(ls: @List<T>, f: fn((&T)) -> bool) -> Option<T> {
|
||||
pub fn find<T: Copy>(ls: @List<T>, f: fn((&T)) -> bool) -> Option<T> {
|
||||
let mut ls = ls;
|
||||
loop {
|
||||
ls = match *ls {
|
||||
@ -56,7 +56,7 @@ fn find<T: Copy>(ls: @List<T>, f: fn((&T)) -> bool) -> Option<T> {
|
||||
}
|
||||
|
||||
/// Returns true if a list contains an element with the given value
|
||||
fn has<T: Copy Eq>(ls: @List<T>, +elt: T) -> bool {
|
||||
pub fn has<T: Copy Eq>(ls: @List<T>, +elt: T) -> bool {
|
||||
for each(ls) |e| {
|
||||
if *e == elt { return true; }
|
||||
}
|
||||
@ -64,7 +64,7 @@ fn has<T: Copy Eq>(ls: @List<T>, +elt: T) -> bool {
|
||||
}
|
||||
|
||||
/// Returns true if the list is empty
|
||||
pure fn is_empty<T: Copy>(ls: @List<T>) -> bool {
|
||||
pub pure fn is_empty<T: Copy>(ls: @List<T>) -> bool {
|
||||
match *ls {
|
||||
Nil => true,
|
||||
_ => false
|
||||
@ -72,19 +72,19 @@ pure fn is_empty<T: Copy>(ls: @List<T>) -> bool {
|
||||
}
|
||||
|
||||
/// Returns true if the list is not empty
|
||||
pure fn is_not_empty<T: Copy>(ls: @List<T>) -> bool {
|
||||
pub pure fn is_not_empty<T: Copy>(ls: @List<T>) -> bool {
|
||||
return !is_empty(ls);
|
||||
}
|
||||
|
||||
/// Returns the length of a list
|
||||
fn len<T>(ls: @List<T>) -> uint {
|
||||
pub fn len<T>(ls: @List<T>) -> uint {
|
||||
let mut count = 0u;
|
||||
iter(ls, |_e| count += 1u);
|
||||
count
|
||||
}
|
||||
|
||||
/// Returns all but the first element of a list
|
||||
pure fn tail<T: Copy>(ls: @List<T>) -> @List<T> {
|
||||
pub pure fn tail<T: Copy>(ls: @List<T>) -> @List<T> {
|
||||
match *ls {
|
||||
Cons(_, tl) => return tl,
|
||||
Nil => fail ~"list empty"
|
||||
@ -92,7 +92,7 @@ pure fn tail<T: Copy>(ls: @List<T>) -> @List<T> {
|
||||
}
|
||||
|
||||
/// Returns the first element of a list
|
||||
pure fn head<T: Copy>(ls: @List<T>) -> T {
|
||||
pub pure fn head<T: Copy>(ls: @List<T>) -> T {
|
||||
match *ls {
|
||||
Cons(copy hd, _) => hd,
|
||||
// makes me sad
|
||||
@ -101,7 +101,7 @@ pure fn head<T: Copy>(ls: @List<T>) -> T {
|
||||
}
|
||||
|
||||
/// Appends one list to another
|
||||
pure fn append<T: Copy>(l: @List<T>, m: @List<T>) -> @List<T> {
|
||||
pub pure fn append<T: Copy>(l: @List<T>, m: @List<T>) -> @List<T> {
|
||||
match *l {
|
||||
Nil => return m,
|
||||
Cons(copy x, xs) => {
|
||||
@ -120,7 +120,7 @@ pure fn push<T: Copy>(ll: &mut @list<T>, +vv: T) {
|
||||
*/
|
||||
|
||||
/// Iterate over a list
|
||||
fn iter<T>(l: @List<T>, f: fn((&T))) {
|
||||
pub fn iter<T>(l: @List<T>, f: fn((&T))) {
|
||||
let mut cur = l;
|
||||
loop {
|
||||
cur = match *cur {
|
||||
@ -134,7 +134,7 @@ fn iter<T>(l: @List<T>, f: fn((&T))) {
|
||||
}
|
||||
|
||||
/// Iterate over a list
|
||||
fn each<T>(l: @List<T>, f: fn((&T)) -> bool) {
|
||||
pub fn each<T>(l: @List<T>, f: fn((&T)) -> bool) {
|
||||
let mut cur = l;
|
||||
loop {
|
||||
cur = match *cur {
|
||||
|
@ -11,16 +11,12 @@ use core::cmp::Eq;
|
||||
use hash::Hash;
|
||||
use to_bytes::IterBytes;
|
||||
|
||||
export HashMap, hashfn, eqfn, Set, Map, chained, set_add;
|
||||
export hash_from_vec;
|
||||
export vec_from_set;
|
||||
|
||||
/// A convenience type to treat a hashmap as a set
|
||||
type Set<K:Eq IterBytes Hash> = HashMap<K, ()>;
|
||||
pub type Set<K:Eq IterBytes Hash> = HashMap<K, ()>;
|
||||
|
||||
type HashMap<K:Eq IterBytes Hash, V> = chained::T<K, V>;
|
||||
pub type HashMap<K:Eq IterBytes Hash, V> = chained::T<K, V>;
|
||||
|
||||
trait Map<K:Eq IterBytes Hash Copy, V: Copy> {
|
||||
pub trait Map<K:Eq IterBytes Hash Copy, V: Copy> {
|
||||
/// Return the number of elements in the map
|
||||
pure fn size() -> uint;
|
||||
|
||||
@ -82,10 +78,9 @@ trait Map<K:Eq IterBytes Hash Copy, V: Copy> {
|
||||
}
|
||||
|
||||
mod util {
|
||||
#[legacy_exports];
|
||||
type Rational = {num: int, den: int}; // : int::positive(*.den);
|
||||
pub type Rational = {num: int, den: int}; // : int::positive(*.den);
|
||||
|
||||
pure fn rational_leq(x: Rational, y: Rational) -> bool {
|
||||
pub pure fn rational_leq(x: Rational, y: Rational) -> bool {
|
||||
// NB: Uses the fact that rationals have positive denominators WLOG:
|
||||
|
||||
x.num * y.den <= y.num * x.den
|
||||
@ -95,9 +90,7 @@ mod util {
|
||||
|
||||
// FIXME (#2344): package this up and export it as a datatype usable for
|
||||
// external code that doesn't want to pay the cost of a box.
|
||||
mod chained {
|
||||
#[legacy_exports];
|
||||
export T, mk, HashMap;
|
||||
pub mod chained {
|
||||
|
||||
const initial_capacity: uint = 32u; // 2^5
|
||||
|
||||
@ -113,7 +106,7 @@ mod chained {
|
||||
mut chains: ~[mut Option<@Entry<K,V>>]
|
||||
}
|
||||
|
||||
type T<K:Eq IterBytes Hash, V> = @HashMap_<K, V>;
|
||||
pub type T<K:Eq IterBytes Hash, V> = @HashMap_<K, V>;
|
||||
|
||||
enum SearchResult<K, V> {
|
||||
NotFound,
|
||||
@ -366,7 +359,7 @@ mod chained {
|
||||
vec::to_mut(vec::from_elem(nchains, None))
|
||||
}
|
||||
|
||||
fn mk<K:Eq IterBytes Hash, V: Copy>() -> T<K,V> {
|
||||
pub fn mk<K:Eq IterBytes Hash, V: Copy>() -> T<K,V> {
|
||||
let slf: T<K, V> = @HashMap_ {count: 0u,
|
||||
chains: chains(initial_capacity)};
|
||||
slf
|
||||
@ -378,18 +371,18 @@ Function: hashmap
|
||||
|
||||
Construct a hashmap.
|
||||
*/
|
||||
fn HashMap<K:Eq IterBytes Hash Const, V: Copy>()
|
||||
pub fn HashMap<K:Eq IterBytes Hash Const, V: Copy>()
|
||||
-> HashMap<K, V> {
|
||||
chained::mk()
|
||||
}
|
||||
|
||||
/// Convenience function for adding keys to a hashmap with nil type keys
|
||||
fn set_add<K:Eq IterBytes Hash Const Copy>(set: Set<K>, +key: K) -> bool {
|
||||
pub fn set_add<K:Eq IterBytes Hash Const Copy>(set: Set<K>, +key: K) -> bool {
|
||||
set.insert(key, ())
|
||||
}
|
||||
|
||||
/// Convert a set into a vector.
|
||||
fn vec_from_set<T:Eq IterBytes Hash Copy>(s: Set<T>) -> ~[T] {
|
||||
pub fn vec_from_set<T:Eq IterBytes Hash Copy>(s: Set<T>) -> ~[T] {
|
||||
do vec::build_sized(s.size()) |push| {
|
||||
for s.each_key() |k| {
|
||||
push(k);
|
||||
@ -398,7 +391,7 @@ fn vec_from_set<T:Eq IterBytes Hash Copy>(s: Set<T>) -> ~[T] {
|
||||
}
|
||||
|
||||
/// Construct a hashmap from a vector
|
||||
fn hash_from_vec<K: Eq IterBytes Hash Const Copy, V: Copy>(
|
||||
pub fn hash_from_vec<K: Eq IterBytes Hash Const Copy, V: Copy>(
|
||||
items: &[(K, V)]) -> HashMap<K, V> {
|
||||
let map = HashMap();
|
||||
for vec::each(items) |item| {
|
||||
@ -517,7 +510,6 @@ impl<K: Eq IterBytes Hash Copy, V: Copy> @Mut<LinearMap<K, V>>:
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[legacy_exports];
|
||||
|
||||
#[test]
|
||||
fn test_simple() {
|
||||
|
@ -77,11 +77,8 @@ mod comm;
|
||||
|
||||
mod bitv;
|
||||
mod deque;
|
||||
#[legacy_exports]
|
||||
mod fun_treemap;
|
||||
#[legacy_exports]
|
||||
mod list;
|
||||
#[legacy_exports]
|
||||
mod map;
|
||||
mod rope;
|
||||
mod smallintmap;
|
||||
|
Loading…
Reference in New Issue
Block a user