2014-02-02 23:56:49 -06:00
|
|
|
// Copyright 2013-2014 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.
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
//! Collection types.
|
2014-02-02 23:56:49 -06:00
|
|
|
|
2014-07-01 09:12:04 -05:00
|
|
|
#![crate_name = "collections"]
|
2014-06-18 00:13:36 -05:00
|
|
|
#![experimental]
|
2014-03-21 20:05:05 -05:00
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![license = "MIT/ASL2"]
|
|
|
|
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
|
2014-07-11 13:21:57 -05:00
|
|
|
html_root_url = "http://doc.rust-lang.org/master/",
|
2014-06-06 11:12:18 -05:00
|
|
|
html_playground_url = "http://play.rust-lang.org/")]
|
2014-02-02 23:56:49 -06:00
|
|
|
|
2014-09-26 00:48:16 -05:00
|
|
|
#![allow(unknown_features)]
|
2014-10-01 16:19:55 -05:00
|
|
|
#![feature(macro_rules, default_type_params, phase, globs)]
|
2014-09-26 00:48:16 -05:00
|
|
|
#![feature(unsafe_destructor, import_shadowing, slicing_syntax)]
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
#![no_std]
|
2014-02-02 23:56:49 -06:00
|
|
|
|
2014-06-11 20:47:09 -05:00
|
|
|
#[phase(plugin, link)] extern crate core;
|
Add libunicode; move unicode functions from core
- created new crate, libunicode, below libstd
- split Char trait into Char (libcore) and UnicodeChar (libunicode)
- Unicode-aware functions now live in libunicode
- is_alphabetic, is_XID_start, is_XID_continue, is_lowercase,
is_uppercase, is_whitespace, is_alphanumeric, is_control,
is_digit, to_uppercase, to_lowercase
- added width method in UnicodeChar trait
- determines printed width of character in columns, or None if it is
a non-NULL control character
- takes a boolean argument indicating whether the present context is
CJK or not (characters with 'A'mbiguous widths are double-wide in
CJK contexts, single-wide otherwise)
- split StrSlice into StrSlice (libcore) and UnicodeStrSlice
(libunicode)
- functionality formerly in StrSlice that relied upon Unicode
functionality from Char is now in UnicodeStrSlice
- words, is_whitespace, is_alphanumeric, trim, trim_left, trim_right
- also moved Words type alias into libunicode because words method is
in UnicodeStrSlice
- unified Unicode tables from libcollections, libcore, and libregex into
libunicode
- updated unicode.py in src/etc to generate aforementioned tables
- generated new tables based on latest Unicode data
- added UnicodeChar and UnicodeStrSlice traits to prelude
- libunicode is now the collection point for the std::char module,
combining the libunicode functionality with the Char functionality
from libcore
- thus, moved doc comment for char from core::char to unicode::char
- libcollections remains the collection point for std::str
The Unicode-aware functions that previously lived in the Char and
StrSlice traits are no longer available to programs that only use
libcore. To regain use of these methods, include the libunicode crate
and use the UnicodeChar and/or UnicodeStrSlice traits:
extern crate unicode;
use unicode::UnicodeChar;
use unicode::UnicodeStrSlice;
use unicode::Words; // if you want to use the words() method
NOTE: this does *not* impact programs that use libstd, since UnicodeChar
and UnicodeStrSlice have been added to the prelude.
closes #15224
[breaking-change]
2014-06-30 16:04:10 -05:00
|
|
|
extern crate unicode;
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
extern crate alloc;
|
2014-03-01 18:33:24 -06:00
|
|
|
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
#[cfg(test)] extern crate native;
|
2014-02-13 19:49:11 -06:00
|
|
|
#[cfg(test)] extern crate test;
|
2014-05-29 21:03:06 -05:00
|
|
|
#[cfg(test)] extern crate debug;
|
2014-05-24 23:15:16 -05:00
|
|
|
|
2014-06-11 20:47:09 -05:00
|
|
|
#[cfg(test)] #[phase(plugin, link)] extern crate std;
|
|
|
|
#[cfg(test)] #[phase(plugin, link)] extern crate log;
|
2014-02-02 23:56:49 -06:00
|
|
|
|
2014-08-12 22:31:30 -05:00
|
|
|
use core::prelude::Option;
|
2014-06-06 18:33:44 -05:00
|
|
|
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
pub use bitv::{Bitv, BitvSet};
|
2014-09-16 09:49:26 -05:00
|
|
|
pub use btree::{BTreeMap, BTreeSet};
|
2014-08-12 22:31:30 -05:00
|
|
|
pub use core::prelude::Collection;
|
2014-02-02 23:56:49 -06:00
|
|
|
pub use dlist::DList;
|
2014-02-20 00:10:41 -06:00
|
|
|
pub use enum_set::EnumSet;
|
2014-02-02 23:56:49 -06:00
|
|
|
pub use priority_queue::PriorityQueue;
|
|
|
|
pub use ringbuf::RingBuf;
|
|
|
|
pub use smallintmap::SmallIntMap;
|
2014-06-07 13:13:26 -05:00
|
|
|
pub use string::String;
|
2014-02-02 23:56:49 -06:00
|
|
|
pub use treemap::{TreeMap, TreeSet};
|
2014-02-19 21:29:58 -06:00
|
|
|
pub use trie::{TrieMap, TrieSet};
|
2014-06-07 13:13:26 -05:00
|
|
|
pub use vec::Vec;
|
2014-02-02 23:56:49 -06:00
|
|
|
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
mod macros;
|
|
|
|
|
2014-02-02 23:56:49 -06:00
|
|
|
pub mod bitv;
|
|
|
|
pub mod btree;
|
|
|
|
pub mod dlist;
|
2014-02-20 00:10:41 -06:00
|
|
|
pub mod enum_set;
|
2014-02-02 23:56:49 -06:00
|
|
|
pub mod priority_queue;
|
|
|
|
pub mod ringbuf;
|
|
|
|
pub mod smallintmap;
|
2014-02-14 12:10:06 -06:00
|
|
|
pub mod treemap;
|
2014-02-19 21:29:58 -06:00
|
|
|
pub mod trie;
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
pub mod slice;
|
|
|
|
pub mod str;
|
|
|
|
pub mod string;
|
|
|
|
pub mod vec;
|
|
|
|
pub mod hash;
|
|
|
|
|
2014-06-06 18:33:44 -05:00
|
|
|
mod deque;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// A mutable container type.
|
2014-06-06 18:33:44 -05:00
|
|
|
pub trait Mutable: Collection {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Clears the container, removing all values.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut v = vec![1i, 2, 3];
|
|
|
|
/// v.clear();
|
|
|
|
/// assert!(v.is_empty());
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn clear(&mut self);
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// A key-value store where values may be looked up by their keys. This trait
|
|
|
|
/// provides basic operations to operate on these stores.
|
2014-06-06 18:33:44 -05:00
|
|
|
pub trait Map<K, V>: Collection {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns a reference to the value corresponding to the key.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map = HashMap::new();
|
|
|
|
/// map.insert("a", 1i);
|
|
|
|
/// assert_eq!(map.find(&"a"), Some(&1i));
|
|
|
|
/// assert_eq!(map.find(&"b"), None);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns true if the map contains a value for the specified key.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map = HashMap::new();
|
|
|
|
/// map.insert("a", 1i);
|
|
|
|
/// assert_eq!(map.contains_key(&"a"), true);
|
|
|
|
/// assert_eq!(map.contains_key(&"b"), false);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
#[inline]
|
|
|
|
fn contains_key(&self, key: &K) -> bool {
|
|
|
|
self.find(key).is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// A key-value store (map) where the values can be modified.
|
2014-06-06 18:33:44 -05:00
|
|
|
pub trait MutableMap<K, V>: Map<K, V> + Mutable {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Inserts a key-value pair into the map. An existing value for a
|
|
|
|
/// key is replaced by the new value. Returns `true` if the key did
|
2014-06-06 18:33:44 -05:00
|
|
|
/// not already exist in the map.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map = HashMap::new();
|
|
|
|
/// assert_eq!(map.insert("key", 2i), true);
|
|
|
|
/// assert_eq!(map.insert("key", 9i), false);
|
2014-09-22 12:31:31 -05:00
|
|
|
/// assert_eq!(map["key"], 9i);
|
2014-07-19 05:10:09 -05:00
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
#[inline]
|
|
|
|
fn insert(&mut self, key: K, value: V) -> bool {
|
|
|
|
self.swap(key, value).is_none()
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Removes a key-value pair from the map. Returns `true` if the key
|
|
|
|
/// was present in the map.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map = HashMap::new();
|
|
|
|
/// assert_eq!(map.remove(&"key"), false);
|
|
|
|
/// map.insert("key", 2i);
|
|
|
|
/// assert_eq!(map.remove(&"key"), true);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
#[inline]
|
|
|
|
fn remove(&mut self, key: &K) -> bool {
|
|
|
|
self.pop(key).is_some()
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Inserts a key-value pair into the map. If the key already had a value
|
|
|
|
/// present in the map, that value is returned. Otherwise, `None` is
|
|
|
|
/// returned.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map = HashMap::new();
|
|
|
|
/// assert_eq!(map.swap("a", 37i), None);
|
|
|
|
/// assert_eq!(map.is_empty(), false);
|
|
|
|
///
|
|
|
|
/// map.insert("a", 1i);
|
|
|
|
/// assert_eq!(map.swap("a", 37i), Some(1i));
|
2014-09-22 12:31:31 -05:00
|
|
|
/// assert_eq!(map["a"], 37i);
|
2014-07-19 05:10:09 -05:00
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
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.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map: HashMap<&str, int> = HashMap::new();
|
|
|
|
/// map.insert("a", 1i);
|
|
|
|
/// assert_eq!(map.pop(&"a"), Some(1i));
|
|
|
|
/// assert_eq!(map.pop(&"a"), None);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn pop(&mut self, k: &K) -> Option<V>;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns a mutable reference to the value corresponding to the key.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashMap;
|
|
|
|
///
|
|
|
|
/// let mut map = HashMap::new();
|
|
|
|
/// map.insert("a", 1i);
|
|
|
|
/// match map.find_mut(&"a") {
|
|
|
|
/// Some(x) => *x = 7i,
|
|
|
|
/// None => (),
|
|
|
|
/// }
|
2014-09-22 12:31:31 -05:00
|
|
|
/// assert_eq!(map["a"], 7i);
|
2014-07-19 05:10:09 -05:00
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// A group of objects which are each distinct from one another. This
|
2014-06-06 18:33:44 -05:00
|
|
|
/// trait represents actions which can be performed on sets to iterate over
|
|
|
|
/// them.
|
|
|
|
pub trait Set<T>: Collection {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns `true` if the set contains a value.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashSet;
|
|
|
|
///
|
|
|
|
/// let set: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
|
|
|
|
/// assert_eq!(set.contains(&1), true);
|
|
|
|
/// assert_eq!(set.contains(&4), false);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn contains(&self, value: &T) -> bool;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns `true` if the set has no elements in common with `other`.
|
2014-06-06 18:33:44 -05:00
|
|
|
/// This is equivalent to checking for an empty intersection.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashSet;
|
|
|
|
///
|
|
|
|
/// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
|
|
|
|
/// let mut b: HashSet<int> = HashSet::new();
|
|
|
|
///
|
|
|
|
/// assert_eq!(a.is_disjoint(&b), true);
|
|
|
|
/// b.insert(4);
|
|
|
|
/// assert_eq!(a.is_disjoint(&b), true);
|
|
|
|
/// b.insert(1);
|
|
|
|
/// assert_eq!(a.is_disjoint(&b), false);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn is_disjoint(&self, other: &Self) -> bool;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns `true` if the set is a subset of another.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashSet;
|
|
|
|
///
|
|
|
|
/// let sup: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
|
|
|
|
/// let mut set: HashSet<int> = HashSet::new();
|
|
|
|
///
|
|
|
|
/// assert_eq!(set.is_subset(&sup), true);
|
|
|
|
/// set.insert(2);
|
|
|
|
/// assert_eq!(set.is_subset(&sup), true);
|
|
|
|
/// set.insert(4);
|
|
|
|
/// assert_eq!(set.is_subset(&sup), false);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn is_subset(&self, other: &Self) -> bool;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns `true` if the set is a superset of another.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashSet;
|
|
|
|
///
|
|
|
|
/// let sub: HashSet<int> = [1i, 2].iter().map(|&x| x).collect();
|
|
|
|
/// let mut set: HashSet<int> = HashSet::new();
|
|
|
|
///
|
|
|
|
/// assert_eq!(set.is_superset(&sub), false);
|
|
|
|
///
|
|
|
|
/// set.insert(0);
|
|
|
|
/// set.insert(1);
|
|
|
|
/// assert_eq!(set.is_superset(&sub), false);
|
|
|
|
///
|
|
|
|
/// set.insert(2);
|
|
|
|
/// assert_eq!(set.is_superset(&sub), true);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn is_superset(&self, other: &Self) -> bool {
|
|
|
|
other.is_subset(self)
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
}
|
2014-06-06 18:33:44 -05:00
|
|
|
|
|
|
|
// FIXME #8154: Add difference, sym. difference, intersection and union iterators
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// A mutable collection of values which are distinct from one another that
|
2014-09-02 00:35:58 -05:00
|
|
|
/// can be mutated.
|
2014-06-06 18:33:44 -05:00
|
|
|
pub trait MutableSet<T>: Set<T> + Mutable {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Adds a value to the set. Returns `true` if the value was not already
|
2014-06-06 18:33:44 -05:00
|
|
|
/// present in the set.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashSet;
|
|
|
|
///
|
|
|
|
/// let mut set = HashSet::new();
|
|
|
|
///
|
|
|
|
/// assert_eq!(set.insert(2i), true);
|
|
|
|
/// assert_eq!(set.insert(2i), false);
|
|
|
|
/// assert_eq!(set.len(), 1);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn insert(&mut self, value: T) -> bool;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Removes a value from the set. Returns `true` if the value was
|
2014-06-06 18:33:44 -05:00
|
|
|
/// present in the set.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::HashSet;
|
|
|
|
///
|
|
|
|
/// let mut set = HashSet::new();
|
|
|
|
///
|
|
|
|
/// set.insert(2i);
|
|
|
|
/// assert_eq!(set.remove(&2), true);
|
|
|
|
/// assert_eq!(set.remove(&2), false);
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn remove(&mut self, value: &T) -> bool;
|
|
|
|
}
|
|
|
|
|
2014-07-11 12:12:38 -05:00
|
|
|
pub trait MutableSeq<T>: Mutable {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Appends an element to the back of a collection.
|
2014-07-11 14:01:06 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let mut vec = vec!(1i, 2);
|
|
|
|
/// vec.push(3);
|
|
|
|
/// assert_eq!(vec, vec!(1, 2, 3));
|
|
|
|
/// ```
|
2014-07-11 12:12:38 -05:00
|
|
|
fn push(&mut self, t: T);
|
2014-08-04 05:48:39 -05:00
|
|
|
|
|
|
|
/// Removes the last element from a collection and returns it, or `None` if
|
|
|
|
/// it is empty.
|
2014-07-11 14:01:06 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let mut vec = vec!(1i, 2, 3);
|
|
|
|
/// assert_eq!(vec.pop(), Some(3));
|
|
|
|
/// assert_eq!(vec, vec!(1, 2));
|
|
|
|
/// ```
|
2014-07-11 12:12:38 -05:00
|
|
|
fn pop(&mut self) -> Option<T>;
|
|
|
|
}
|
|
|
|
|
2014-06-06 18:33:44 -05:00
|
|
|
/// A double-ended sequence that allows querying, insertion and deletion at both
|
|
|
|
/// ends.
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-07-20 07:59:13 -05:00
|
|
|
/// With a `Deque` we can simulate a queue efficiently:
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{RingBuf, Deque};
|
|
|
|
///
|
2014-07-20 07:59:13 -05:00
|
|
|
/// let mut queue = RingBuf::new();
|
2014-07-20 19:57:29 -05:00
|
|
|
/// queue.push(1i);
|
|
|
|
/// queue.push(2i);
|
|
|
|
/// queue.push(3i);
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
2014-07-20 07:59:13 -05:00
|
|
|
/// // Will print 1, 2, 3
|
|
|
|
/// while !queue.is_empty() {
|
|
|
|
/// let x = queue.pop_front().unwrap();
|
2014-07-20 07:12:26 -05:00
|
|
|
/// println!("{}", x);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2014-07-20 07:59:13 -05:00
|
|
|
/// We can also simulate a stack:
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{RingBuf, Deque};
|
|
|
|
///
|
2014-07-20 07:59:13 -05:00
|
|
|
/// let mut stack = RingBuf::new();
|
|
|
|
/// stack.push_front(1i);
|
|
|
|
/// stack.push_front(2i);
|
|
|
|
/// stack.push_front(3i);
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
2014-07-20 07:59:13 -05:00
|
|
|
/// // Will print 3, 2, 1
|
|
|
|
/// while !stack.is_empty() {
|
|
|
|
/// let x = stack.pop_front().unwrap();
|
2014-07-20 07:12:26 -05:00
|
|
|
/// println!("{}", x);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// And of course we can mix and match:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{DList, Deque};
|
|
|
|
///
|
|
|
|
/// let mut deque = DList::new();
|
|
|
|
///
|
|
|
|
/// // Init deque with 1, 2, 3, 4
|
|
|
|
/// deque.push_front(2i);
|
|
|
|
/// deque.push_front(1i);
|
2014-07-20 19:57:29 -05:00
|
|
|
/// deque.push(3i);
|
|
|
|
/// deque.push(4i);
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// // Will print (1, 4) and (2, 3)
|
|
|
|
/// while !deque.is_empty() {
|
|
|
|
/// let f = deque.pop_front().unwrap();
|
2014-07-20 19:57:29 -05:00
|
|
|
/// let b = deque.pop().unwrap();
|
2014-07-20 07:12:26 -05:00
|
|
|
/// println!("{}", (f, b));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-07-11 12:12:38 -05:00
|
|
|
pub trait Deque<T> : MutableSeq<T> {
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Provides a reference to the front element, or `None` if the sequence is
|
2014-07-20 07:12:26 -05:00
|
|
|
/// empty.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{RingBuf, Deque};
|
|
|
|
///
|
|
|
|
/// let mut d = RingBuf::new();
|
|
|
|
/// assert_eq!(d.front(), None);
|
|
|
|
///
|
2014-07-20 19:57:29 -05:00
|
|
|
/// d.push(1i);
|
|
|
|
/// d.push(2i);
|
2014-07-20 07:12:26 -05:00
|
|
|
/// assert_eq!(d.front(), Some(&1i));
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn front<'a>(&'a self) -> Option<&'a T>;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Provides a mutable reference to the front element, or `None` if the
|
2014-07-20 07:12:26 -05:00
|
|
|
/// sequence is empty.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{RingBuf, Deque};
|
|
|
|
///
|
|
|
|
/// let mut d = RingBuf::new();
|
|
|
|
/// assert_eq!(d.front_mut(), None);
|
|
|
|
///
|
2014-07-20 19:57:29 -05:00
|
|
|
/// d.push(1i);
|
|
|
|
/// d.push(2i);
|
2014-07-20 07:12:26 -05:00
|
|
|
/// match d.front_mut() {
|
|
|
|
/// Some(x) => *x = 9i,
|
|
|
|
/// None => (),
|
|
|
|
/// }
|
|
|
|
/// assert_eq!(d.front(), Some(&9i));
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T>;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Provides a reference to the back element, or `None` if the sequence is
|
2014-07-20 07:12:26 -05:00
|
|
|
/// empty.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{DList, Deque};
|
|
|
|
///
|
|
|
|
/// let mut d = DList::new();
|
|
|
|
/// assert_eq!(d.back(), None);
|
|
|
|
///
|
2014-07-20 19:57:29 -05:00
|
|
|
/// d.push(1i);
|
|
|
|
/// d.push(2i);
|
2014-07-20 07:12:26 -05:00
|
|
|
/// assert_eq!(d.back(), Some(&2i));
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn back<'a>(&'a self) -> Option<&'a T>;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Provides a mutable reference to the back element, or `None` if the
|
|
|
|
/// sequence is empty.
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{DList, Deque};
|
|
|
|
///
|
|
|
|
/// let mut d = DList::new();
|
|
|
|
/// assert_eq!(d.back(), None);
|
|
|
|
///
|
2014-07-20 19:57:29 -05:00
|
|
|
/// d.push(1i);
|
|
|
|
/// d.push(2i);
|
2014-07-20 07:12:26 -05:00
|
|
|
/// match d.back_mut() {
|
|
|
|
/// Some(x) => *x = 9i,
|
|
|
|
/// None => (),
|
|
|
|
/// }
|
|
|
|
/// assert_eq!(d.back(), Some(&9i));
|
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T>;
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Inserts an element first in the sequence.
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{DList, Deque};
|
|
|
|
///
|
|
|
|
/// let mut d = DList::new();
|
|
|
|
/// d.push_front(1i);
|
|
|
|
/// d.push_front(2i);
|
|
|
|
/// assert_eq!(d.front(), Some(&2i));
|
2014-07-20 07:59:13 -05:00
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn push_front(&mut self, elt: T);
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Inserts an element last in the sequence.
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-07-20 19:57:29 -05:00
|
|
|
/// ```ignore
|
2014-07-20 07:12:26 -05:00
|
|
|
/// use std::collections::{DList, Deque};
|
|
|
|
///
|
|
|
|
/// let mut d = DList::new();
|
|
|
|
/// d.push_back(1i);
|
|
|
|
/// d.push_back(2i);
|
|
|
|
/// assert_eq!(d.front(), Some(&1i));
|
2014-07-20 07:59:13 -05:00
|
|
|
/// ```
|
2014-07-11 12:19:27 -05:00
|
|
|
#[deprecated = "use the `push` method"]
|
2014-07-11 20:08:46 -05:00
|
|
|
fn push_back(&mut self, elt: T) { self.push(elt) }
|
2014-06-06 18:33:44 -05:00
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Removes the last element and returns it, or `None` if the sequence is
|
|
|
|
/// empty.
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-07-20 19:57:29 -05:00
|
|
|
/// ```ignore
|
2014-07-20 07:12:26 -05:00
|
|
|
/// use std::collections::{RingBuf, Deque};
|
|
|
|
///
|
|
|
|
/// let mut d = RingBuf::new();
|
|
|
|
/// d.push_back(1i);
|
|
|
|
/// d.push_back(2i);
|
|
|
|
///
|
|
|
|
/// assert_eq!(d.pop_back(), Some(2i));
|
|
|
|
/// assert_eq!(d.pop_back(), Some(1i));
|
|
|
|
/// assert_eq!(d.pop_back(), None);
|
2014-07-20 07:59:13 -05:00
|
|
|
/// ```
|
2014-07-11 12:19:27 -05:00
|
|
|
#[deprecated = "use the `pop` method"]
|
2014-07-11 20:08:46 -05:00
|
|
|
fn pop_back(&mut self) -> Option<T> { self.pop() }
|
2014-06-06 18:33:44 -05:00
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Removes the first element and returns it, or `None` if the sequence is
|
|
|
|
/// empty.
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::{RingBuf, Deque};
|
|
|
|
///
|
|
|
|
/// let mut d = RingBuf::new();
|
2014-07-20 19:57:29 -05:00
|
|
|
/// d.push(1i);
|
|
|
|
/// d.push(2i);
|
2014-07-20 07:12:26 -05:00
|
|
|
///
|
|
|
|
/// assert_eq!(d.pop_front(), Some(1i));
|
|
|
|
/// assert_eq!(d.pop_front(), Some(2i));
|
|
|
|
/// assert_eq!(d.pop_front(), None);
|
2014-07-20 07:59:13 -05:00
|
|
|
/// ```
|
2014-06-06 18:33:44 -05:00
|
|
|
fn pop_front(&mut self) -> Option<T>;
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:01:40 -05:00
|
|
|
// FIXME(#14344) this shouldn't be necessary
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn fixme_14344_be_sure_to_link_to_collections() {}
|
|
|
|
|
2014-05-29 21:03:06 -05:00
|
|
|
#[cfg(not(test))]
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
mod std {
|
|
|
|
pub use core::fmt; // necessary for fail!()
|
|
|
|
pub use core::option; // necessary for fail!()
|
|
|
|
pub use core::clone; // deriving(Clone)
|
|
|
|
pub use core::cmp; // deriving(Eq, Ord, etc.)
|
|
|
|
pub use hash; // deriving(Hash)
|
2014-07-11 12:12:38 -05:00
|
|
|
|
|
|
|
pub mod collections {
|
|
|
|
pub use MutableSeq;
|
|
|
|
}
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
}
|