2015-01-11 16:02:51 -06:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2013-07-10 09:06:09 -05:00
|
|
|
// 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.
|
2013-07-09 09:55:04 -05:00
|
|
|
|
|
|
|
//! A doubly-linked list with owned nodes.
|
|
|
|
//!
|
2015-02-18 01:44:55 -06:00
|
|
|
//! The `LinkedList` allows pushing and popping elements at either end and is thus
|
2014-12-09 16:05:16 -06:00
|
|
|
//! efficiently usable as a double-ended queue.
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
// LinkedList is constructed like a singly-linked list over the field `next`.
|
2013-07-09 09:55:04 -05:00
|
|
|
// including the last link being None; each Node owns its `next` field.
|
2012-12-03 18:48:01 -06:00
|
|
|
//
|
2015-02-18 01:44:55 -06:00
|
|
|
// Backlinks over LinkedList::prev are raw pointers that form a full chain in
|
2013-07-09 09:55:04 -05:00
|
|
|
// the reverse direction.
|
2012-12-03 18:48:01 -06:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2015-01-04 18:35:20 -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
|
|
|
use core::prelude::*;
|
|
|
|
|
2014-07-10 16:19:17 -05:00
|
|
|
use alloc::boxed::Box;
|
2014-12-22 11:04:23 -06:00
|
|
|
use core::cmp::Ordering;
|
2014-06-09 02:30:04 -05:00
|
|
|
use core::default::Default;
|
2014-06-07 08:01:44 -05:00
|
|
|
use core::fmt;
|
2015-02-17 22:48:07 -06:00
|
|
|
use core::hash::{Hasher, Hash};
|
2015-01-07 21:01:05 -06:00
|
|
|
use core::iter::{self, FromIterator, IntoIterator};
|
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
|
|
|
use core::mem;
|
|
|
|
use core::ptr;
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
#[deprecated(since = "1.0.0", reason = "renamed to LinkedList")]
|
|
|
|
#[unstable(feature = "collections")]
|
|
|
|
pub use LinkedList as DList;
|
|
|
|
|
2013-07-11 09:17:51 -05:00
|
|
|
/// A doubly-linked list.
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
pub struct LinkedList<T> {
|
2015-02-04 20:17:19 -06:00
|
|
|
length: usize,
|
2014-03-27 17:10:04 -05:00
|
|
|
list_head: Link<T>,
|
|
|
|
list_tail: Rawlink<Node<T>>,
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2014-05-05 20:56:44 -05:00
|
|
|
type Link<T> = Option<Box<Node<T>>>;
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
|
|
|
|
struct Rawlink<T> {
|
|
|
|
p: *mut T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Copy for Rawlink<T> {}
|
2014-12-23 14:42:54 -06:00
|
|
|
unsafe impl<T:'static+Send> Send for Rawlink<T> {}
|
|
|
|
unsafe impl<T:Send+Sync> Sync for Rawlink<T> {}
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
struct Node<T> {
|
2014-01-24 13:02:03 -06:00
|
|
|
next: Link<T>,
|
|
|
|
prev: Rawlink<Node<T>>,
|
|
|
|
value: T,
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
/// An iterator over references to the items of a `LinkedList`.
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-19 14:52:10 -06:00
|
|
|
pub struct Iter<'a, T:'a> {
|
2014-08-27 20:46:52 -05:00
|
|
|
head: &'a Link<T>,
|
|
|
|
tail: Rawlink<Node<T>>,
|
2015-02-04 20:17:19 -06:00
|
|
|
nelem: usize,
|
2014-08-27 20:46:52 -05:00
|
|
|
}
|
|
|
|
|
2014-12-30 18:07:53 -06:00
|
|
|
// FIXME #19839: deriving is too aggressive on the bounds (T doesn't need to be Clone).
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-19 14:52:10 -06:00
|
|
|
impl<'a, T> Clone for Iter<'a, T> {
|
2014-12-30 18:07:53 -06:00
|
|
|
fn clone(&self) -> Iter<'a, T> {
|
|
|
|
Iter {
|
|
|
|
head: self.head.clone(),
|
|
|
|
tail: self.tail,
|
|
|
|
nelem: self.nelem,
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 07:20:50 -06:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
/// An iterator over mutable references to the items of a `LinkedList`.
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-19 14:52:10 -06:00
|
|
|
pub struct IterMut<'a, T:'a> {
|
2015-02-18 01:44:55 -06:00
|
|
|
list: &'a mut LinkedList<T>,
|
2014-08-27 20:46:52 -05:00
|
|
|
head: Rawlink<Node<T>>,
|
|
|
|
tail: Rawlink<Node<T>>,
|
2015-02-04 20:17:19 -06:00
|
|
|
nelem: usize,
|
2014-08-27 20:46:52 -05:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
/// An iterator over mutable references to the items of a `LinkedList`.
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Clone)]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-19 14:52:10 -06:00
|
|
|
pub struct IntoIter<T> {
|
2015-02-18 01:44:55 -06:00
|
|
|
list: LinkedList<T>
|
2012-08-27 16:22:25 -05:00
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
/// Rawlink is a type like Option<T> but for holding a raw pointer
|
|
|
|
impl<T> Rawlink<T> {
|
|
|
|
/// Like Option::None for Rawlink
|
|
|
|
fn none() -> Rawlink<T> {
|
2014-09-14 22:27:36 -05:00
|
|
|
Rawlink{p: ptr::null_mut()}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Like Option::Some for Rawlink
|
|
|
|
fn some(n: &mut T) -> Rawlink<T> {
|
2014-02-14 17:42:01 -06:00
|
|
|
Rawlink{p: n}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert the `Rawlink` into an Option value
|
2014-07-17 23:44:59 -05:00
|
|
|
fn resolve_immut<'a>(&self) -> Option<&'a T> {
|
|
|
|
unsafe {
|
2014-12-19 10:57:12 -06:00
|
|
|
mem::transmute(self.p.as_ref())
|
2014-07-17 23:44:59 -05:00
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert the `Rawlink` into an Option value
|
2014-07-17 23:44:59 -05:00
|
|
|
fn resolve<'a>(&mut self) -> Option<&'a mut T> {
|
2013-07-09 20:49:32 -05:00
|
|
|
if self.p.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
Some(unsafe { mem::transmute(self.p) })
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
}
|
2013-08-04 20:38:06 -05:00
|
|
|
|
|
|
|
/// Return the `Rawlink` and replace with `Rawlink::none()`
|
|
|
|
fn take(&mut self) -> Rawlink<T> {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
mem::replace(self, Rawlink::none())
|
2013-08-04 20:38:06 -05:00
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 11:46:37 -05:00
|
|
|
impl<T> Clone for Rawlink<T> {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Rawlink<T> {
|
|
|
|
Rawlink{p: self.p}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
impl<T> Node<T> {
|
|
|
|
fn new(v: T) -> Node<T> {
|
|
|
|
Node{value: v, next: None, prev: Rawlink::none()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
/// Set the .prev field on `next`, then return `Some(next)`
|
2014-05-05 20:56:44 -05:00
|
|
|
fn link_with_prev<T>(mut next: Box<Node<T>>, prev: Rawlink<Node<T>>)
|
|
|
|
-> Link<T> {
|
2013-07-09 20:49:32 -05:00
|
|
|
next.prev = prev;
|
|
|
|
Some(next)
|
|
|
|
}
|
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
// private methods
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<T> LinkedList<T> {
|
2013-07-21 12:31:40 -05:00
|
|
|
/// Add a Node first in the list
|
|
|
|
#[inline]
|
2014-05-05 20:56:44 -05:00
|
|
|
fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
|
2013-07-21 12:31:40 -05:00
|
|
|
match self.list_head {
|
|
|
|
None => {
|
2014-06-25 01:11:57 -05:00
|
|
|
self.list_tail = Rawlink::some(&mut *new_head);
|
2013-07-21 12:31:40 -05:00
|
|
|
self.list_head = link_with_prev(new_head, Rawlink::none());
|
|
|
|
}
|
|
|
|
Some(ref mut head) => {
|
|
|
|
new_head.prev = Rawlink::none();
|
2014-06-25 01:11:57 -05:00
|
|
|
head.prev = Rawlink::some(&mut *new_head);
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
mem::swap(head, &mut new_head);
|
2013-07-21 12:31:40 -05:00
|
|
|
head.next = Some(new_head);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.length += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the first Node and return it, or None if the list is empty
|
|
|
|
#[inline]
|
2014-05-05 20:56:44 -05:00
|
|
|
fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
|
2013-11-20 17:46:49 -06:00
|
|
|
self.list_head.take().map(|mut front_node| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.length -= 1;
|
|
|
|
match front_node.next.take() {
|
|
|
|
Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
|
|
|
|
None => self.list_tail = Rawlink::none()
|
|
|
|
}
|
|
|
|
front_node
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a Node last in the list
|
|
|
|
#[inline]
|
2014-05-05 20:56:44 -05:00
|
|
|
fn push_back_node(&mut self, mut new_tail: Box<Node<T>>) {
|
2013-07-21 12:31:40 -05:00
|
|
|
match self.list_tail.resolve() {
|
|
|
|
None => return self.push_front_node(new_tail),
|
|
|
|
Some(tail) => {
|
2014-06-25 01:11:57 -05:00
|
|
|
self.list_tail = Rawlink::some(&mut *new_tail);
|
2013-07-21 12:31:40 -05:00
|
|
|
tail.next = link_with_prev(new_tail, Rawlink::some(tail));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.length += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the last Node and return it, or None if the list is empty
|
|
|
|
#[inline]
|
2014-05-05 20:56:44 -05:00
|
|
|
fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
|
2013-12-06 12:51:10 -06:00
|
|
|
self.list_tail.resolve().map_or(None, |tail| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.length -= 1;
|
|
|
|
self.list_tail = tail.prev;
|
|
|
|
match tail.prev.resolve() {
|
2013-07-21 15:37:14 -05:00
|
|
|
None => self.list_head.take(),
|
|
|
|
Some(tail_prev) => tail_prev.next.take()
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<T> Default for LinkedList<T> {
|
2014-06-09 02:30:04 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
fn default() -> LinkedList<T> { LinkedList::new() }
|
2014-06-09 02:30:04 -05:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<T> LinkedList<T> {
|
|
|
|
/// Creates an empty `LinkedList`.
|
2013-07-10 08:27:15 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
pub fn new() -> LinkedList<T> {
|
|
|
|
LinkedList{list_head: None, list_tail: Rawlink::none(), length: 0}
|
2013-07-10 08:27:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2015-01-01 02:30:11 -06:00
|
|
|
/// Moves all elements from `other` to the end of the list.
|
2013-07-09 09:55:04 -05:00
|
|
|
///
|
2015-01-01 02:30:11 -06:00
|
|
|
/// This reuses all the nodes from `other` and moves them into `self`. After
|
|
|
|
/// this operation, `other` becomes empty.
|
|
|
|
///
|
|
|
|
/// This operation should compute in O(1) time and O(1) memory.
|
2014-07-17 15:46:27 -05:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-07-17 15:46:27 -05:00
|
|
|
///
|
2015-01-11 16:02:51 -06:00
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2014-07-17 15:46:27 -05:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut a = LinkedList::new();
|
|
|
|
/// let mut b = LinkedList::new();
|
2015-01-25 15:05:03 -06:00
|
|
|
/// a.push_back(1);
|
2014-11-06 11:24:47 -06:00
|
|
|
/// a.push_back(2);
|
2015-01-25 15:05:03 -06:00
|
|
|
/// b.push_back(3);
|
2014-11-06 11:24:47 -06:00
|
|
|
/// b.push_back(4);
|
2014-07-17 15:46:27 -05:00
|
|
|
///
|
2015-01-01 02:30:11 -06:00
|
|
|
/// a.append(&mut b);
|
2014-07-17 15:46:27 -05:00
|
|
|
///
|
|
|
|
/// for e in a.iter() {
|
|
|
|
/// println!("{}", e); // prints 1, then 2, then 3, then 4
|
|
|
|
/// }
|
2015-01-01 02:30:11 -06:00
|
|
|
/// println!("{}", b.len()); // prints 0
|
2014-07-17 15:46:27 -05:00
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
pub fn append(&mut self, other: &mut LinkedList<T>) {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.list_tail.resolve() {
|
2015-01-01 02:30:11 -06:00
|
|
|
None => {
|
|
|
|
self.length = other.length;
|
|
|
|
self.list_head = other.list_head.take();
|
|
|
|
self.list_tail = other.list_tail.take();
|
|
|
|
},
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(tail) => {
|
2013-08-04 20:38:06 -05:00
|
|
|
// Carefully empty `other`.
|
|
|
|
let o_tail = other.list_tail.take();
|
|
|
|
let o_length = other.length;
|
|
|
|
match other.list_head.take() {
|
|
|
|
None => return,
|
|
|
|
Some(node) => {
|
2013-07-09 20:49:32 -05:00
|
|
|
tail.next = link_with_prev(node, self.list_tail);
|
2013-07-09 09:55:04 -05:00
|
|
|
self.list_tail = o_tail;
|
|
|
|
self.length += o_length;
|
|
|
|
}
|
|
|
|
}
|
2012-09-21 21:37:57 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2015-01-01 02:30:11 -06:00
|
|
|
other.length = 0;
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Provides a forward iterator.
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-30 18:07:53 -06:00
|
|
|
pub fn iter(&self) -> Iter<T> {
|
2014-12-19 14:52:10 -06:00
|
|
|
Iter{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Provides a forward iterator with mutable references.
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-30 18:07:53 -06:00
|
|
|
pub fn iter_mut(&mut self) -> IterMut<T> {
|
2013-07-11 21:23:15 -05:00
|
|
|
let head_raw = match self.list_head {
|
2014-06-25 01:11:57 -05:00
|
|
|
Some(ref mut h) => Rawlink::some(&mut **h),
|
2013-07-11 21:23:15 -05:00
|
|
|
None => Rawlink::none(),
|
|
|
|
};
|
2014-12-19 14:52:10 -06:00
|
|
|
IterMut{
|
2013-07-11 21:23:15 -05:00
|
|
|
nelem: self.len(),
|
|
|
|
head: head_raw,
|
|
|
|
tail: self.list_tail,
|
|
|
|
list: self
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Consumes the list into an iterator yielding elements by value.
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-19 14:52:10 -06:00
|
|
|
pub fn into_iter(self) -> IntoIter<T> {
|
|
|
|
IntoIter{list: self}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2014-10-30 15:43:24 -05:00
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
/// Returns `true` if the `LinkedList` is empty.
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
|
|
|
/// This operation should compute in O(1) time.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut dl = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
/// assert!(dl.is_empty());
|
|
|
|
///
|
|
|
|
/// dl.push_front("foo");
|
|
|
|
/// assert!(!dl.is_empty());
|
|
|
|
/// ```
|
2014-10-30 15:43:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 15:43:24 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.list_head.is_none()
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
/// Returns the length of the `LinkedList`.
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
|
|
|
/// This operation should compute in O(1) time.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut dl = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-01-31 10:23:42 -06:00
|
|
|
/// dl.push_front(2);
|
2015-01-11 16:02:51 -06:00
|
|
|
/// assert_eq!(dl.len(), 1);
|
|
|
|
///
|
|
|
|
/// dl.push_front(1);
|
|
|
|
/// assert_eq!(dl.len(), 2);
|
|
|
|
///
|
|
|
|
/// dl.push_back(3);
|
|
|
|
/// assert_eq!(dl.len(), 3);
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 15:43:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-04 20:17:19 -06:00
|
|
|
pub fn len(&self) -> usize {
|
2014-10-30 15:43:24 -05:00
|
|
|
self.length
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
/// Removes all elements from the `LinkedList`.
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
|
|
|
/// This operation should compute in O(n) time.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut dl = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-01-31 10:23:42 -06:00
|
|
|
/// dl.push_front(2);
|
2015-01-11 16:02:51 -06:00
|
|
|
/// dl.push_front(1);
|
|
|
|
/// assert_eq!(dl.len(), 2);
|
2015-01-31 10:23:42 -06:00
|
|
|
/// assert_eq!(dl.front(), Some(&1));
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// dl.clear();
|
|
|
|
/// assert_eq!(dl.len(), 0);
|
|
|
|
/// assert_eq!(dl.front(), None);
|
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 15:43:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 15:43:24 -05:00
|
|
|
pub fn clear(&mut self) {
|
2015-02-18 01:44:55 -06:00
|
|
|
*self = LinkedList::new()
|
2014-10-30 15:43:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provides a reference to the front element, or `None` if the list is
|
|
|
|
/// empty.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut dl = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
/// assert_eq!(dl.front(), None);
|
|
|
|
///
|
|
|
|
/// dl.push_front(1);
|
2015-01-31 10:23:42 -06:00
|
|
|
/// assert_eq!(dl.front(), Some(&1));
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 15:43:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-06 11:24:47 -06:00
|
|
|
pub fn front(&self) -> Option<&T> {
|
2014-10-30 15:43:24 -05:00
|
|
|
self.list_head.as_ref().map(|head| &head.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provides a mutable reference to the front element, or `None` if the list
|
|
|
|
/// is empty.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut dl = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
/// assert_eq!(dl.front(), None);
|
|
|
|
///
|
|
|
|
/// dl.push_front(1);
|
2015-01-31 10:23:42 -06:00
|
|
|
/// assert_eq!(dl.front(), Some(&1));
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// match dl.front_mut() {
|
|
|
|
/// None => {},
|
2015-01-31 10:23:42 -06:00
|
|
|
/// Some(x) => *x = 5,
|
2015-01-11 16:02:51 -06:00
|
|
|
/// }
|
2015-01-31 10:23:42 -06:00
|
|
|
/// assert_eq!(dl.front(), Some(&5));
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 15:43:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-06 11:24:47 -06:00
|
|
|
pub fn front_mut(&mut self) -> Option<&mut T> {
|
2014-10-30 15:43:24 -05:00
|
|
|
self.list_head.as_mut().map(|head| &mut head.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provides a reference to the back element, or `None` if the list is
|
|
|
|
/// empty.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut dl = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
/// assert_eq!(dl.back(), None);
|
|
|
|
///
|
|
|
|
/// dl.push_back(1);
|
2015-01-31 10:23:42 -06:00
|
|
|
/// assert_eq!(dl.back(), Some(&1));
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 15:43:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-06 11:24:47 -06:00
|
|
|
pub fn back(&self) -> Option<&T> {
|
2014-10-30 15:43:24 -05:00
|
|
|
self.list_tail.resolve_immut().as_ref().map(|tail| &tail.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provides a mutable reference to the back element, or `None` if the list
|
|
|
|
/// is empty.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut dl = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
/// assert_eq!(dl.back(), None);
|
|
|
|
///
|
|
|
|
/// dl.push_back(1);
|
2015-01-31 10:23:42 -06:00
|
|
|
/// assert_eq!(dl.back(), Some(&1));
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// match dl.back_mut() {
|
|
|
|
/// None => {},
|
2015-01-31 10:23:42 -06:00
|
|
|
/// Some(x) => *x = 5,
|
2015-01-11 16:02:51 -06:00
|
|
|
/// }
|
2015-01-31 10:23:42 -06:00
|
|
|
/// assert_eq!(dl.back(), Some(&5));
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// ```
|
2014-10-30 15:43:24 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-06 11:24:47 -06:00
|
|
|
pub fn back_mut(&mut self) -> Option<&mut T> {
|
2014-10-30 15:43:24 -05:00
|
|
|
self.list_tail.resolve().map(|tail| &mut tail.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds an element first in the list.
|
|
|
|
///
|
|
|
|
/// This operation should compute in O(1) time.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut dl = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-01-31 10:23:42 -06:00
|
|
|
/// dl.push_front(2);
|
|
|
|
/// assert_eq!(dl.front().unwrap(), &2);
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// dl.push_front(1);
|
|
|
|
/// assert_eq!(dl.front().unwrap(), &1);
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 15:43:24 -05:00
|
|
|
pub fn push_front(&mut self, elt: T) {
|
|
|
|
self.push_front_node(box Node::new(elt))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the first element and returns it, or `None` if the list is
|
|
|
|
/// empty.
|
|
|
|
///
|
|
|
|
/// This operation should compute in O(1) time.
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut d = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
/// assert_eq!(d.pop_front(), None);
|
|
|
|
///
|
2015-01-31 10:23:42 -06:00
|
|
|
/// d.push_front(1);
|
2015-01-11 16:02:51 -06:00
|
|
|
/// d.push_front(3);
|
|
|
|
/// assert_eq!(d.pop_front(), Some(3));
|
|
|
|
/// assert_eq!(d.pop_front(), Some(1));
|
|
|
|
/// assert_eq!(d.pop_front(), None);
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
///
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 15:43:24 -05:00
|
|
|
pub fn pop_front(&mut self) -> Option<T> {
|
|
|
|
self.pop_front_node().map(|box Node{value, ..}| value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends an element to the back of a list
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2015-01-11 16:02:51 -06:00
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut d = LinkedList::new();
|
2015-01-25 15:05:03 -06:00
|
|
|
/// d.push_back(1);
|
2014-11-06 11:24:47 -06:00
|
|
|
/// d.push_back(3);
|
2014-10-30 15:43:24 -05:00
|
|
|
/// assert_eq!(3, *d.back().unwrap());
|
|
|
|
/// ```
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-06 11:24:47 -06:00
|
|
|
pub fn push_back(&mut self, elt: T) {
|
2014-10-30 15:43:24 -05:00
|
|
|
self.push_back_node(box Node::new(elt))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the last element from a list and returns it, or `None` if
|
|
|
|
/// it is empty.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2015-01-11 16:02:51 -06:00
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2014-10-30 15:43:24 -05:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut d = LinkedList::new();
|
2014-11-06 11:24:47 -06:00
|
|
|
/// assert_eq!(d.pop_back(), None);
|
2015-01-25 15:05:03 -06:00
|
|
|
/// d.push_back(1);
|
2014-11-06 11:24:47 -06:00
|
|
|
/// d.push_back(3);
|
|
|
|
/// assert_eq!(d.pop_back(), Some(3));
|
2014-10-30 15:43:24 -05:00
|
|
|
/// ```
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-06 11:24:47 -06:00
|
|
|
pub fn pop_back(&mut self) -> Option<T> {
|
2014-10-30 15:43:24 -05:00
|
|
|
self.pop_back_node().map(|box Node{value, ..}| value)
|
|
|
|
}
|
2015-01-01 02:30:11 -06:00
|
|
|
|
|
|
|
/// Splits the list into two at the given index. Returns everything after the given index,
|
|
|
|
/// including the index.
|
|
|
|
///
|
2015-02-12 16:11:08 -06:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `at > len`.
|
|
|
|
///
|
2015-01-01 02:30:11 -06:00
|
|
|
/// This operation should compute in O(n) time.
|
2015-02-12 16:11:08 -06:00
|
|
|
///
|
2015-01-11 16:02:51 -06:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut d = LinkedList::new();
|
2015-01-11 16:02:51 -06:00
|
|
|
///
|
2015-01-31 10:23:42 -06:00
|
|
|
/// d.push_front(1);
|
2015-01-11 16:02:51 -06:00
|
|
|
/// d.push_front(2);
|
|
|
|
/// d.push_front(3);
|
|
|
|
///
|
|
|
|
/// let mut splitted = d.split_off(2);
|
|
|
|
///
|
|
|
|
/// assert_eq!(splitted.pop_front(), Some(1));
|
|
|
|
/// assert_eq!(splitted.pop_front(), None);
|
|
|
|
/// ```
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
pub fn split_off(&mut self, at: usize) -> LinkedList<T> {
|
2015-01-01 02:30:11 -06:00
|
|
|
let len = self.len();
|
2015-02-12 16:11:08 -06:00
|
|
|
assert!(at <= len, "Cannot split off at a nonexistent index");
|
2015-01-01 02:30:11 -06:00
|
|
|
if at == 0 {
|
2015-02-18 01:44:55 -06:00
|
|
|
return mem::replace(self, LinkedList::new());
|
2015-02-12 16:11:08 -06:00
|
|
|
} else if at == len {
|
2015-02-18 01:44:55 -06:00
|
|
|
return LinkedList::new();
|
2015-01-01 02:30:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Below, we iterate towards the `i-1`th node, either from the start or the end,
|
|
|
|
// depending on which would be faster.
|
|
|
|
let mut split_node = if at - 1 <= len - 1 - (at - 1) {
|
|
|
|
let mut iter = self.iter_mut();
|
|
|
|
// instead of skipping using .skip() (which creates a new struct),
|
|
|
|
// we skip manually so we can access the head field without
|
|
|
|
// depending on implementation details of Skip
|
2015-01-26 14:46:12 -06:00
|
|
|
for _ in 0..at - 1 {
|
2015-01-01 02:30:11 -06:00
|
|
|
iter.next();
|
|
|
|
}
|
|
|
|
iter.head
|
|
|
|
} else {
|
|
|
|
// better off starting from the end
|
|
|
|
let mut iter = self.iter_mut();
|
2015-01-26 15:05:07 -06:00
|
|
|
for _ in 0..len - 1 - (at - 1) {
|
2015-01-01 02:30:11 -06:00
|
|
|
iter.next_back();
|
|
|
|
}
|
|
|
|
iter.tail
|
|
|
|
};
|
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut splitted_list = LinkedList {
|
2015-01-01 02:30:11 -06:00
|
|
|
list_head: None,
|
|
|
|
list_tail: self.list_tail,
|
|
|
|
length: len - at
|
|
|
|
};
|
|
|
|
|
|
|
|
mem::swap(&mut split_node.resolve().unwrap().next, &mut splitted_list.list_head);
|
|
|
|
self.list_tail = split_node;
|
|
|
|
self.length = at;
|
|
|
|
|
|
|
|
splitted_list
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-08-04 20:38:06 -05:00
|
|
|
#[unsafe_destructor]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<T> Drop for LinkedList<T> {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2015-02-18 01:44:55 -06:00
|
|
|
// Dissolve the linked_list in backwards direction
|
2013-08-04 20:38:06 -05:00
|
|
|
// Just dropping the list_head can lead to stack exhaustion
|
|
|
|
// when length is >> 1_000_000
|
2013-09-16 20:18:07 -05:00
|
|
|
let mut tail = self.list_tail;
|
2013-08-04 20:38:06 -05:00
|
|
|
loop {
|
|
|
|
match tail.resolve() {
|
|
|
|
None => break,
|
|
|
|
Some(prev) => {
|
2014-05-05 20:56:44 -05:00
|
|
|
prev.next.take(); // release Box<Node<T>>
|
2013-08-04 20:38:06 -05:00
|
|
|
tail = prev.prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 20:18:07 -05:00
|
|
|
self.length = 0;
|
|
|
|
self.list_head = None;
|
|
|
|
self.list_tail = Rawlink::none();
|
2013-08-04 20:38:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 22:15:35 -06:00
|
|
|
impl<'a, A> Iterator for Iter<'a, A> {
|
|
|
|
type Item = &'a A;
|
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
fn next(&mut self) -> Option<&'a A> {
|
2013-07-11 20:24:59 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
self.head.as_ref().map(|head| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.nelem -= 1;
|
|
|
|
self.head = &head.next;
|
|
|
|
&head.value
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2015-02-04 20:17:19 -06:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2013-07-09 09:55:04 -05:00
|
|
|
(self.nelem, Some(self.nelem))
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 22:15:35 -06:00
|
|
|
impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
fn next_back(&mut self) -> Option<&'a A> {
|
2013-07-11 20:24:59 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2014-05-25 04:41:20 -05:00
|
|
|
self.tail.resolve_immut().as_ref().map(|prev| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.nelem -= 1;
|
|
|
|
self.tail = prev.prev;
|
|
|
|
&prev.value
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-11 20:24:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 22:15:35 -06:00
|
|
|
impl<'a, A> ExactSizeIterator for Iter<'a, A> {}
|
2013-09-01 11:20:24 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 22:15:35 -06:00
|
|
|
impl<'a, A> Iterator for IterMut<'a, A> {
|
|
|
|
type Item = &'a mut A;
|
2013-07-09 09:55:04 -05:00
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
fn next(&mut self) -> Option<&'a mut A> {
|
2013-07-11 21:23:15 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
self.head.resolve().map(|next| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.nelem -= 1;
|
|
|
|
self.head = match next.next {
|
|
|
|
Some(ref mut node) => Rawlink::some(&mut **node),
|
|
|
|
None => Rawlink::none(),
|
|
|
|
};
|
|
|
|
&mut next.value
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2015-02-04 20:17:19 -06:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2013-07-09 09:55:04 -05:00
|
|
|
(self.nelem, Some(self.nelem))
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 22:15:35 -06:00
|
|
|
impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
|
2013-07-09 09:55:04 -05:00
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
fn next_back(&mut self) -> Option<&'a mut A> {
|
2013-07-11 21:23:15 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
self.tail.resolve().map(|prev| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.nelem -= 1;
|
|
|
|
self.tail = prev.prev;
|
|
|
|
&mut prev.value
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-01-24 17:40:46 -06:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 22:15:35 -06:00
|
|
|
impl<'a, A> ExactSizeIterator for IterMut<'a, A> {}
|
2013-07-11 21:23:15 -05:00
|
|
|
|
2014-12-19 14:52:10 -06:00
|
|
|
// private methods for IterMut
|
|
|
|
impl<'a, A> IterMut<'a, A> {
|
2014-05-05 20:56:44 -05:00
|
|
|
fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
|
2013-07-21 12:31:40 -05:00
|
|
|
// Insert before `self.head` so that it is between the
|
2013-07-11 21:23:15 -05:00
|
|
|
// previously yielded element and self.head.
|
2013-07-21 12:31:40 -05:00
|
|
|
//
|
|
|
|
// The inserted node will not appear in further iteration.
|
2013-07-11 21:23:15 -05:00
|
|
|
match self.head.resolve() {
|
2013-07-21 12:31:40 -05:00
|
|
|
None => { self.list.push_back_node(ins_node); }
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(node) => {
|
|
|
|
let prev_node = match node.prev.resolve() {
|
2013-07-21 12:31:40 -05:00
|
|
|
None => return self.list.push_front_node(ins_node),
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(prev) => prev,
|
2013-07-09 09:55:04 -05:00
|
|
|
};
|
2014-08-18 19:52:38 -05:00
|
|
|
let node_own = prev_node.next.take().unwrap();
|
2014-06-25 01:11:57 -05:00
|
|
|
ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
|
2013-07-09 20:49:32 -05:00
|
|
|
prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
|
2013-07-09 09:55:04 -05:00
|
|
|
self.list.length += 1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
}
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
2014-12-23 16:27:27 -06:00
|
|
|
impl<'a, A> IterMut<'a, A> {
|
|
|
|
/// Inserts `elt` just after the element most recently returned by `.next()`.
|
|
|
|
/// The inserted element does not appear in the iteration.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2015-01-11 16:02:51 -06:00
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2014-12-23 16:27:27 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut list: LinkedList<_> = vec![1, 3, 4].into_iter().collect();
|
2014-12-23 16:27:27 -06:00
|
|
|
///
|
|
|
|
/// {
|
|
|
|
/// let mut it = list.iter_mut();
|
|
|
|
/// assert_eq!(it.next().unwrap(), &1);
|
|
|
|
/// // insert `2` after `1`
|
|
|
|
/// it.insert_next(2);
|
|
|
|
/// }
|
|
|
|
/// {
|
2015-02-12 15:45:07 -06:00
|
|
|
/// let vec: Vec<_> = list.into_iter().collect();
|
2015-02-24 12:15:45 -06:00
|
|
|
/// assert_eq!(vec, [1, 2, 3, 4]);
|
2014-12-23 16:27:27 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2013-07-21 12:31:40 -05:00
|
|
|
#[inline]
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "collections",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "this is probably better handled by a cursor type -- we'll see")]
|
2014-12-23 16:27:27 -06:00
|
|
|
pub fn insert_next(&mut self, elt: A) {
|
2014-04-25 03:08:02 -05:00
|
|
|
self.insert_next_node(box Node::new(elt))
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
|
2014-12-23 16:27:27 -06:00
|
|
|
/// Provides a reference to the next element, without changing the iterator.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2015-01-11 16:02:51 -06:00
|
|
|
/// ```
|
2015-02-18 01:44:55 -06:00
|
|
|
/// use std::collections::LinkedList;
|
2014-12-23 16:27:27 -06:00
|
|
|
///
|
2015-02-18 01:44:55 -06:00
|
|
|
/// let mut list: LinkedList<_> = vec![1, 2, 3].into_iter().collect();
|
2014-12-23 16:27:27 -06:00
|
|
|
///
|
|
|
|
/// let mut it = list.iter_mut();
|
|
|
|
/// assert_eq!(it.next().unwrap(), &1);
|
|
|
|
/// assert_eq!(it.peek_next().unwrap(), &2);
|
|
|
|
/// // We just peeked at 2, so it was not consumed from the iterator.
|
|
|
|
/// assert_eq!(it.next().unwrap(), &2);
|
|
|
|
/// ```
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "collections",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "this is probably better handled by a cursor type -- we'll see")]
|
2014-12-23 16:27:27 -06:00
|
|
|
pub fn peek_next(&mut self) -> Option<&mut A> {
|
2013-07-22 10:51:11 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
self.head.resolve().map(|head| &mut head.value)
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 22:15:35 -06:00
|
|
|
impl<A> Iterator for IntoIter<A> {
|
|
|
|
type Item = A;
|
|
|
|
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-09 09:55:04 -05:00
|
|
|
fn next(&mut self) -> Option<A> { self.list.pop_front() }
|
2013-07-14 16:03:54 -05:00
|
|
|
|
|
|
|
#[inline]
|
2015-02-04 20:17:19 -06:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2013-07-09 09:55:04 -05:00
|
|
|
(self.list.length, Some(self.list.length))
|
2012-07-17 18:03:54 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 22:15:35 -06:00
|
|
|
impl<A> DoubleEndedIterator for IntoIter<A> {
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2014-11-06 11:24:47 -06:00
|
|
|
fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<A> FromIterator<A> for LinkedList<A> {
|
2015-02-18 12:06:21 -06:00
|
|
|
fn from_iter<T: IntoIterator<Item=A>>(iter: T) -> LinkedList<A> {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut ret = DList::new();
|
2015-02-18 12:06:21 -06:00
|
|
|
ret.extend(iter);
|
2013-07-09 09:55:04 -05:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:06:24 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<T> IntoIterator for LinkedList<T> {
|
2015-02-13 16:55:10 -06:00
|
|
|
type Item = T;
|
|
|
|
type IntoIter = IntoIter<T>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> IntoIter<T> {
|
|
|
|
self.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:06:24 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<'a, T> IntoIterator for &'a LinkedList<T> {
|
2015-02-13 16:55:10 -06:00
|
|
|
type Item = &'a T;
|
2015-02-06 16:47:55 -06:00
|
|
|
type IntoIter = Iter<'a, T>;
|
2015-01-07 21:01:05 -06:00
|
|
|
|
|
|
|
fn into_iter(self) -> Iter<'a, T> {
|
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<'a, T> IntoIterator for &'a mut LinkedList<T> {
|
2015-02-13 16:55:10 -06:00
|
|
|
type Item = &'a mut T;
|
2015-02-06 16:47:55 -06:00
|
|
|
type IntoIter = IterMut<'a, T>;
|
2015-01-07 21:01:05 -06:00
|
|
|
|
|
|
|
fn into_iter(mut self) -> IterMut<'a, T> {
|
|
|
|
self.iter_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<A> Extend<A> for LinkedList<A> {
|
2015-02-18 09:04:30 -06:00
|
|
|
fn extend<T: IntoIterator<Item=A>>(&mut self, iter: T) {
|
|
|
|
for elt in iter { self.push_back(elt); }
|
2013-07-29 19:06:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<A: PartialEq> PartialEq for LinkedList<A> {
|
|
|
|
fn eq(&self, other: &LinkedList<A>) -> bool {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.len() == other.len() &&
|
2013-09-08 10:01:16 -05:00
|
|
|
iter::order::eq(self.iter(), other.iter())
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-14 16:03:54 -05:00
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
fn ne(&self, other: &LinkedList<A>) -> bool {
|
2013-08-29 10:11:11 -05:00
|
|
|
self.len() != other.len() ||
|
2013-09-08 10:01:16 -05:00
|
|
|
iter::order::ne(self.iter(), other.iter())
|
2013-08-08 15:07:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<A: Eq> Eq for LinkedList<A> {}
|
2014-08-01 15:05:03 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<A: PartialOrd> PartialOrd for LinkedList<A> {
|
|
|
|
fn partial_cmp(&self, other: &LinkedList<A>) -> Option<Ordering> {
|
2014-06-18 01:25:51 -05:00
|
|
|
iter::order::partial_cmp(self.iter(), other.iter())
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<A: Ord> Ord for LinkedList<A> {
|
2014-08-01 15:22:48 -05:00
|
|
|
#[inline]
|
2015-02-18 01:44:55 -06:00
|
|
|
fn cmp(&self, other: &LinkedList<A>) -> Ordering {
|
2014-08-01 15:22:48 -05:00
|
|
|
iter::order::cmp(self.iter(), other.iter())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<A: Clone> Clone for LinkedList<A> {
|
|
|
|
fn clone(&self) -> LinkedList<A> {
|
2015-02-13 01:33:44 -06:00
|
|
|
self.iter().cloned().collect()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2014-02-05 10:52:54 -06:00
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 01:44:55 -06:00
|
|
|
impl<A: fmt::Debug> fmt::Debug for LinkedList<A> {
|
2014-06-07 08:01:44 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-02-10 15:12:13 -06:00
|
|
|
try!(write!(f, "["));
|
2014-06-07 08:01:44 -05:00
|
|
|
|
|
|
|
for (i, e) in self.iter().enumerate() {
|
|
|
|
if i != 0 { try!(write!(f, ", ")); }
|
2014-12-20 02:09:35 -06:00
|
|
|
try!(write!(f, "{:?}", *e));
|
2014-06-07 08:01:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-18 16:34:08 -06:00
|
|
|
impl<A: Hash> Hash for LinkedList<A> {
|
2015-02-17 22:48:07 -06:00
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.len().hash(state);
|
|
|
|
for elt in self {
|
|
|
|
elt.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 15:24:06 -05:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
#[cfg(test)]
|
2014-01-07 00:33:50 -06:00
|
|
|
mod tests {
|
2014-12-19 06:02:22 -06:00
|
|
|
use prelude::*;
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 03:39:37 -05:00
|
|
|
use std::rand;
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 14:37:23 -06:00
|
|
|
use std::hash::{self, SipHasher};
|
2015-02-17 17:10:25 -06:00
|
|
|
use std::thread;
|
2014-05-29 21:03:06 -05:00
|
|
|
use test::Bencher;
|
|
|
|
use test;
|
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
use super::{LinkedList, Node};
|
2014-01-07 00:33:50 -06:00
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
pub fn check_links<T>(list: &LinkedList<T>) {
|
2015-02-04 20:17:19 -06:00
|
|
|
let mut len = 0;
|
2014-01-07 00:33:50 -06:00
|
|
|
let mut last_ptr: Option<&Node<T>> = None;
|
|
|
|
let mut node_ptr: &Node<T>;
|
|
|
|
match list.list_head {
|
2015-02-04 20:17:19 -06:00
|
|
|
None => { assert_eq!(0, list.length); return }
|
2014-01-07 00:33:50 -06:00
|
|
|
Some(ref node) => node_ptr = &**node,
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2014-01-07 00:33:50 -06:00
|
|
|
loop {
|
|
|
|
match (last_ptr, node_ptr.prev.resolve_immut()) {
|
|
|
|
(None , None ) => {}
|
2014-10-09 14:17:22 -05:00
|
|
|
(None , _ ) => panic!("prev link for list_head"),
|
2014-01-07 00:33:50 -06:00
|
|
|
(Some(p), Some(pptr)) => {
|
2014-06-25 14:47:34 -05:00
|
|
|
assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
|
2014-01-07 00:33:50 -06:00
|
|
|
}
|
2014-10-09 14:17:22 -05:00
|
|
|
_ => panic!("prev link is none, not good"),
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2014-01-07 00:33:50 -06:00
|
|
|
match node_ptr.next {
|
|
|
|
Some(ref next) => {
|
|
|
|
last_ptr = Some(node_ptr);
|
|
|
|
node_ptr = &**next;
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
len += 1;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-07 00:33:50 -06:00
|
|
|
assert_eq!(len, list.length);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-03-08 17:19:30 -06:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_basic() {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m = LinkedList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.pop_front(), None);
|
2014-11-06 11:24:47 -06:00
|
|
|
assert_eq!(m.pop_back(), None);
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.pop_front(), None);
|
2014-04-25 03:08:02 -05:00
|
|
|
m.push_front(box 1);
|
2014-05-05 20:56:44 -05:00
|
|
|
assert_eq!(m.pop_front(), Some(box 1));
|
2014-11-06 11:24:47 -06:00
|
|
|
m.push_back(box 2);
|
|
|
|
m.push_back(box 3);
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.len(), 2);
|
2014-05-05 20:56:44 -05:00
|
|
|
assert_eq!(m.pop_front(), Some(box 2));
|
|
|
|
assert_eq!(m.pop_front(), Some(box 3));
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.len(), 0);
|
|
|
|
assert_eq!(m.pop_front(), None);
|
2014-11-06 11:24:47 -06:00
|
|
|
m.push_back(box 1);
|
|
|
|
m.push_back(box 3);
|
|
|
|
m.push_back(box 5);
|
|
|
|
m.push_back(box 7);
|
2014-05-05 20:56:44 -05:00
|
|
|
assert_eq!(m.pop_front(), Some(box 1));
|
2013-05-21 19:24:31 -05:00
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut n = LinkedList::new();
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_front(2);
|
2013-07-09 20:49:32 -05:00
|
|
|
n.push_front(3);
|
|
|
|
{
|
2013-07-10 08:27:15 -05:00
|
|
|
assert_eq!(n.front().unwrap(), &3);
|
|
|
|
let x = n.front_mut().unwrap();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(*x, 3);
|
|
|
|
*x = 0;
|
|
|
|
}
|
|
|
|
{
|
2013-07-10 08:27:15 -05:00
|
|
|
assert_eq!(n.back().unwrap(), &2);
|
|
|
|
let y = n.back_mut().unwrap();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(*y, 2);
|
|
|
|
*y = 1;
|
|
|
|
}
|
|
|
|
assert_eq!(n.pop_front(), Some(0));
|
|
|
|
assert_eq!(n.pop_front(), Some(1));
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[cfg(test)]
|
2015-02-18 01:44:55 -06:00
|
|
|
fn generate_test() -> LinkedList<i32> {
|
2015-01-25 15:05:03 -06:00
|
|
|
list_from(&[0,1,2,3,4,5,6])
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[cfg(test)]
|
2015-02-18 18:39:32 -06:00
|
|
|
fn list_from<T: Clone>(v: &[T]) -> LinkedList<T> {
|
2015-02-13 01:33:44 -06:00
|
|
|
v.iter().cloned().collect()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 02:30:11 -06:00
|
|
|
#[test]
|
|
|
|
fn test_append() {
|
|
|
|
// Empty to empty
|
|
|
|
{
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m = LinkedList::<i32>::new();
|
|
|
|
let mut n = LinkedList::new();
|
2015-01-01 02:30:11 -06:00
|
|
|
m.append(&mut n);
|
|
|
|
check_links(&m);
|
|
|
|
assert_eq!(m.len(), 0);
|
|
|
|
assert_eq!(n.len(), 0);
|
|
|
|
}
|
|
|
|
// Non-empty to empty
|
|
|
|
{
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m = LinkedList::new();
|
|
|
|
let mut n = LinkedList::new();
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_back(2);
|
2015-01-01 02:30:11 -06:00
|
|
|
m.append(&mut n);
|
|
|
|
check_links(&m);
|
|
|
|
assert_eq!(m.len(), 1);
|
|
|
|
assert_eq!(m.pop_back(), Some(2));
|
|
|
|
assert_eq!(n.len(), 0);
|
|
|
|
check_links(&m);
|
|
|
|
}
|
|
|
|
// Empty to non-empty
|
|
|
|
{
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m = LinkedList::new();
|
|
|
|
let mut n = LinkedList::new();
|
2015-01-25 15:05:03 -06:00
|
|
|
m.push_back(2);
|
2015-01-01 02:30:11 -06:00
|
|
|
m.append(&mut n);
|
|
|
|
check_links(&m);
|
|
|
|
assert_eq!(m.len(), 1);
|
|
|
|
assert_eq!(m.pop_back(), Some(2));
|
|
|
|
check_links(&m);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-empty to non-empty
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = vec![1,2,3,4,5];
|
|
|
|
let u = vec![9,8,1,2,3,4,5];
|
2015-02-01 20:53:25 -06:00
|
|
|
let mut m = list_from(&v);
|
|
|
|
let mut n = list_from(&u);
|
2015-01-01 02:30:11 -06:00
|
|
|
m.append(&mut n);
|
|
|
|
check_links(&m);
|
|
|
|
let mut sum = v;
|
2015-02-01 20:53:25 -06:00
|
|
|
sum.push_all(&u);
|
2015-01-01 02:30:11 -06:00
|
|
|
assert_eq!(sum.len(), m.len());
|
2015-01-31 19:03:04 -06:00
|
|
|
for elt in sum {
|
2015-01-01 02:30:11 -06:00
|
|
|
assert_eq!(m.pop_front(), Some(elt))
|
|
|
|
}
|
|
|
|
assert_eq!(n.len(), 0);
|
|
|
|
// let's make sure it's working properly, since we
|
|
|
|
// did some direct changes to private members
|
|
|
|
n.push_back(3);
|
|
|
|
assert_eq!(n.len(), 1);
|
|
|
|
assert_eq!(n.pop_front(), Some(3));
|
|
|
|
check_links(&n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_off() {
|
|
|
|
// singleton
|
|
|
|
{
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m = LinkedList::new();
|
2015-01-25 15:05:03 -06:00
|
|
|
m.push_back(1);
|
2015-01-01 02:30:11 -06:00
|
|
|
|
|
|
|
let p = m.split_off(0);
|
|
|
|
assert_eq!(m.len(), 0);
|
|
|
|
assert_eq!(p.len(), 1);
|
|
|
|
assert_eq!(p.back(), Some(&1));
|
|
|
|
assert_eq!(p.front(), Some(&1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// not singleton, forwards
|
|
|
|
{
|
2015-01-25 15:05:03 -06:00
|
|
|
let u = vec![1,2,3,4,5];
|
2015-02-01 20:53:25 -06:00
|
|
|
let mut m = list_from(&u);
|
2015-01-01 02:30:11 -06:00
|
|
|
let mut n = m.split_off(2);
|
|
|
|
assert_eq!(m.len(), 2);
|
|
|
|
assert_eq!(n.len(), 3);
|
2015-01-25 15:05:03 -06:00
|
|
|
for elt in 1..3 {
|
2015-01-01 02:30:11 -06:00
|
|
|
assert_eq!(m.pop_front(), Some(elt));
|
|
|
|
}
|
2015-01-25 15:05:03 -06:00
|
|
|
for elt in 3..6 {
|
2015-01-01 02:30:11 -06:00
|
|
|
assert_eq!(n.pop_front(), Some(elt));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// not singleton, backwards
|
|
|
|
{
|
2015-01-25 15:05:03 -06:00
|
|
|
let u = vec![1,2,3,4,5];
|
2015-02-01 20:53:25 -06:00
|
|
|
let mut m = list_from(&u);
|
2015-01-01 02:30:11 -06:00
|
|
|
let mut n = m.split_off(4);
|
|
|
|
assert_eq!(m.len(), 4);
|
|
|
|
assert_eq!(n.len(), 1);
|
2015-01-25 15:05:03 -06:00
|
|
|
for elt in 1..5 {
|
2015-01-01 02:30:11 -06:00
|
|
|
assert_eq!(m.pop_front(), Some(elt));
|
|
|
|
}
|
2015-01-25 15:05:03 -06:00
|
|
|
for elt in 5..6 {
|
2015-01-01 02:30:11 -06:00
|
|
|
assert_eq!(n.pop_front(), Some(elt));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:11:08 -06:00
|
|
|
// no-op on the last index
|
|
|
|
{
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m = LinkedList::new();
|
2015-02-12 16:11:08 -06:00
|
|
|
m.push_back(1);
|
|
|
|
|
|
|
|
let p = m.split_off(1);
|
|
|
|
assert_eq!(m.len(), 1);
|
|
|
|
assert_eq!(p.len(), 0);
|
|
|
|
assert_eq!(m.back(), Some(&1));
|
|
|
|
assert_eq!(m.front(), Some(&1));
|
|
|
|
}
|
|
|
|
|
2015-01-01 02:30:11 -06:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator() {
|
|
|
|
let m = generate_test();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, elt) in m.iter().enumerate() {
|
2015-02-04 20:17:19 -06:00
|
|
|
assert_eq!(i as i32, *elt);
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut n = LinkedList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(n.iter().next(), None);
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_front(4);
|
2013-07-09 20:49:32 -05:00
|
|
|
let mut it = n.iter();
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(it.next().unwrap(), &4);
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:46:37 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_clone() {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut n = LinkedList::new();
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_back(2);
|
2014-11-06 11:24:47 -06:00
|
|
|
n.push_back(3);
|
|
|
|
n.push_back(4);
|
2013-07-18 11:46:37 -05:00
|
|
|
let mut it = n.iter();
|
|
|
|
it.next();
|
|
|
|
let mut jt = it.clone();
|
|
|
|
assert_eq!(it.next(), jt.next());
|
|
|
|
assert_eq!(it.next_back(), jt.next_back());
|
|
|
|
assert_eq!(it.next(), jt.next());
|
|
|
|
}
|
|
|
|
|
2013-07-11 21:23:15 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_double_end() {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut n = LinkedList::new();
|
2013-07-11 21:23:15 -05:00
|
|
|
assert_eq!(n.iter().next(), None);
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_front(4);
|
2013-07-11 21:23:15 -05:00
|
|
|
n.push_front(5);
|
|
|
|
n.push_front(6);
|
|
|
|
let mut it = n.iter();
|
|
|
|
assert_eq!(it.size_hint(), (3, Some(3)));
|
|
|
|
assert_eq!(it.next().unwrap(), &6);
|
|
|
|
assert_eq!(it.size_hint(), (2, Some(2)));
|
|
|
|
assert_eq!(it.next_back().unwrap(), &4);
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(it.next_back().unwrap(), &5);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_rev_iter() {
|
|
|
|
let m = generate_test();
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
for (i, elt) in m.iter().rev().enumerate() {
|
2015-02-04 20:17:19 -06:00
|
|
|
assert_eq!((6 - i) as i32, *elt);
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut n = LinkedList::new();
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
assert_eq!(n.iter().rev().next(), None);
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_front(4);
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
let mut it = n.iter().rev();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(it.next().unwrap(), &4);
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mut_iter() {
|
|
|
|
let mut m = generate_test();
|
|
|
|
let mut len = m.len();
|
2014-09-14 22:27:36 -05:00
|
|
|
for (i, elt) in m.iter_mut().enumerate() {
|
2015-02-04 20:17:19 -06:00
|
|
|
assert_eq!(i as i32, *elt);
|
2013-07-09 20:49:32 -05:00
|
|
|
len -= 1;
|
|
|
|
}
|
|
|
|
assert_eq!(len, 0);
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut n = LinkedList::new();
|
2014-09-14 22:27:36 -05:00
|
|
|
assert!(n.iter_mut().next().is_none());
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_front(4);
|
2014-11-06 11:24:47 -06:00
|
|
|
n.push_back(5);
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut it = n.iter_mut();
|
2013-07-11 21:23:15 -05:00
|
|
|
assert_eq!(it.size_hint(), (2, Some(2)));
|
|
|
|
assert!(it.next().is_some());
|
2013-07-09 20:49:32 -05:00
|
|
|
assert!(it.next().is_some());
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
2013-07-11 21:23:15 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_mut_double_end() {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut n = LinkedList::new();
|
2014-09-14 22:27:36 -05:00
|
|
|
assert!(n.iter_mut().next_back().is_none());
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_front(4);
|
2013-07-11 21:23:15 -05:00
|
|
|
n.push_front(5);
|
|
|
|
n.push_front(6);
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut it = n.iter_mut();
|
2013-07-11 21:23:15 -05:00
|
|
|
assert_eq!(it.size_hint(), (3, Some(3)));
|
|
|
|
assert_eq!(*it.next().unwrap(), 6);
|
|
|
|
assert_eq!(it.size_hint(), (2, Some(2)));
|
|
|
|
assert_eq!(*it.next_back().unwrap(), 4);
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(*it.next_back().unwrap(), 5);
|
|
|
|
assert!(it.next_back().is_none());
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
2013-07-09 20:49:32 -05:00
|
|
|
fn test_insert_prev() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut m = list_from(&[0,2,4,6,8]);
|
2013-07-09 20:49:32 -05:00
|
|
|
let len = m.len();
|
|
|
|
{
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut it = m.iter_mut();
|
2013-07-11 21:23:15 -05:00
|
|
|
it.insert_next(-2);
|
2013-07-09 20:49:32 -05:00
|
|
|
loop {
|
|
|
|
match it.next() {
|
|
|
|
None => break,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(elt) => {
|
2013-07-11 21:23:15 -05:00
|
|
|
it.insert_next(*elt + 1);
|
2013-07-09 20:49:32 -05:00
|
|
|
match it.peek_next() {
|
|
|
|
Some(x) => assert_eq!(*x, *elt + 2),
|
|
|
|
None => assert_eq!(8, *elt),
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-11 21:23:15 -05:00
|
|
|
it.insert_next(0);
|
|
|
|
it.insert_next(1);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
check_links(&m);
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.len(), 3 + len * 2);
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!(m.into_iter().collect::<Vec<_>>(), [-2,0,1,2,3,4,5,6,7,8,9,0,1]);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mut_rev_iter() {
|
|
|
|
let mut m = generate_test();
|
2014-09-14 22:27:36 -05:00
|
|
|
for (i, elt) in m.iter_mut().rev().enumerate() {
|
2015-02-04 20:17:19 -06:00
|
|
|
assert_eq!((6 - i) as i32, *elt);
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut n = LinkedList::new();
|
2014-09-14 22:27:36 -05:00
|
|
|
assert!(n.iter_mut().rev().next().is_none());
|
2015-01-25 15:05:03 -06:00
|
|
|
n.push_front(4);
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut it = n.iter_mut().rev();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert!(it.next().is_some());
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let n = list_from(&[1,2,3]);
|
2015-02-17 17:10:25 -06:00
|
|
|
thread::spawn(move || {
|
2013-07-09 20:49:32 -05:00
|
|
|
check_links(&n);
|
2014-08-04 07:19:02 -05:00
|
|
|
let a: &[_] = &[&1,&2,&3];
|
2015-02-04 20:17:19 -06:00
|
|
|
assert_eq!(a, n.iter().collect::<Vec<_>>());
|
2015-01-02 01:53:35 -06:00
|
|
|
}).join().ok().unwrap();
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_eq() {
|
2015-02-04 20:17:19 -06:00
|
|
|
let mut n = list_from(&[]);
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut m = list_from(&[]);
|
2014-02-28 03:23:06 -06:00
|
|
|
assert!(n == m);
|
2013-07-09 20:49:32 -05:00
|
|
|
n.push_front(1);
|
|
|
|
assert!(n != m);
|
2014-11-06 11:24:47 -06:00
|
|
|
m.push_back(1);
|
2014-02-28 03:23:06 -06:00
|
|
|
assert!(n == m);
|
2013-08-29 10:11:11 -05:00
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let n = list_from(&[2,3,4]);
|
|
|
|
let m = list_from(&[1,2,3]);
|
2013-08-29 10:11:11 -05:00
|
|
|
assert!(n != m);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-12-27 19:53:04 -06:00
|
|
|
|
2014-07-29 15:24:06 -05:00
|
|
|
#[test]
|
|
|
|
fn test_hash() {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut x = LinkedList::new();
|
|
|
|
let mut y = LinkedList::new();
|
2014-07-29 15:24:06 -05:00
|
|
|
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 14:37:23 -06:00
|
|
|
assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
|
2014-07-29 15:24:06 -05:00
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
x.push_back(1);
|
2014-11-06 11:24:47 -06:00
|
|
|
x.push_back(2);
|
|
|
|
x.push_back(3);
|
2014-07-29 15:24:06 -05:00
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
y.push_front(3);
|
2014-07-29 15:24:06 -05:00
|
|
|
y.push_front(2);
|
|
|
|
y.push_front(1);
|
|
|
|
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 14:37:23 -06:00
|
|
|
assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
|
2014-07-29 15:24:06 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:07:21 -05:00
|
|
|
#[test]
|
|
|
|
fn test_ord() {
|
2015-02-04 20:17:19 -06:00
|
|
|
let n = list_from(&[]);
|
2015-01-25 15:05:03 -06:00
|
|
|
let m = list_from(&[1,2,3]);
|
2013-08-08 15:07:21 -05:00
|
|
|
assert!(n < m);
|
|
|
|
assert!(m > n);
|
|
|
|
assert!(n <= n);
|
|
|
|
assert!(n >= n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ord_nan() {
|
2014-04-21 16:58:52 -05:00
|
|
|
let nan = 0.0f64/0.0;
|
2014-11-17 02:39:01 -06:00
|
|
|
let n = list_from(&[nan]);
|
|
|
|
let m = list_from(&[nan]);
|
2013-08-08 15:07:21 -05:00
|
|
|
assert!(!(n < m));
|
|
|
|
assert!(!(n > m));
|
|
|
|
assert!(!(n <= m));
|
|
|
|
assert!(!(n >= m));
|
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
let n = list_from(&[nan]);
|
|
|
|
let one = list_from(&[1.0f64]);
|
2013-08-08 15:07:21 -05:00
|
|
|
assert!(!(n < one));
|
|
|
|
assert!(!(n > one));
|
|
|
|
assert!(!(n <= one));
|
|
|
|
assert!(!(n >= one));
|
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
let u = list_from(&[1.0f64,2.0,nan]);
|
|
|
|
let v = list_from(&[1.0f64,2.0,3.0]);
|
2013-08-08 15:07:21 -05:00
|
|
|
assert!(!(u < v));
|
|
|
|
assert!(!(u > v));
|
|
|
|
assert!(!(u <= v));
|
|
|
|
assert!(!(u >= v));
|
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
let s = list_from(&[1.0f64,2.0,4.0,2.0]);
|
|
|
|
let t = list_from(&[1.0f64,2.0,3.0,2.0]);
|
2013-08-08 15:07:21 -05:00
|
|
|
assert!(!(s < t));
|
|
|
|
assert!(s > one);
|
|
|
|
assert!(!(s <= one));
|
|
|
|
assert!(s >= one);
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_fuzz() {
|
2015-02-04 20:17:19 -06:00
|
|
|
for _ in 0..25 {
|
2013-07-09 20:49:32 -05:00
|
|
|
fuzz_test(3);
|
|
|
|
fuzz_test(16);
|
|
|
|
fuzz_test(189);
|
2014-01-29 18:20:34 -06:00
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2014-06-07 08:01:44 -05:00
|
|
|
#[test]
|
|
|
|
fn test_show() {
|
2015-02-18 01:44:55 -06:00
|
|
|
let list: LinkedList<_> = (0..10).collect();
|
2015-02-10 15:12:13 -06:00
|
|
|
assert_eq!(format!("{:?}", list), "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
|
2014-06-07 08:01:44 -05:00
|
|
|
|
2015-02-18 01:44:55 -06:00
|
|
|
let list: LinkedList<_> = vec!["just", "one", "test", "more"].iter().cloned().collect();
|
2015-02-10 15:12:13 -06:00
|
|
|
assert_eq!(format!("{:?}", list), "[\"just\", \"one\", \"test\", \"more\"]");
|
2014-06-07 08:01:44 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[cfg(test)]
|
2015-02-04 20:17:19 -06:00
|
|
|
fn fuzz_test(sz: i32) {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m: LinkedList<_> = LinkedList::new();
|
2014-04-05 00:45:42 -05:00
|
|
|
let mut v = vec![];
|
2015-01-26 14:46:12 -06:00
|
|
|
for i in 0..sz {
|
2013-07-09 20:49:32 -05:00
|
|
|
check_links(&m);
|
|
|
|
let r: u8 = rand::random();
|
|
|
|
match r % 6 {
|
|
|
|
0 => {
|
2014-11-06 11:24:47 -06:00
|
|
|
m.pop_back();
|
2013-12-23 09:20:52 -06:00
|
|
|
v.pop();
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
1 => {
|
2014-12-30 12:51:18 -06:00
|
|
|
if !v.is_empty() {
|
|
|
|
m.pop_front();
|
|
|
|
v.remove(0);
|
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
2 | 4 => {
|
|
|
|
m.push_front(-i);
|
2014-08-26 14:45:55 -05:00
|
|
|
v.insert(0, -i);
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
3 | 5 | _ => {
|
2014-11-06 11:24:47 -06:00
|
|
|
m.push_back(i);
|
2013-07-09 20:49:32 -05:00
|
|
|
v.push(i);
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
check_links(&m);
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2015-02-04 20:17:19 -06:00
|
|
|
let mut i = 0;
|
2014-09-14 22:27:36 -05:00
|
|
|
for (a, &b) in m.into_iter().zip(v.iter()) {
|
2013-07-09 20:49:32 -05:00
|
|
|
i += 1;
|
|
|
|
assert_eq!(a, b);
|
|
|
|
}
|
|
|
|
assert_eq!(i, v.len());
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_collect_into(b: &mut test::Bencher) {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = &[0; 64];
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2015-02-18 01:44:55 -06:00
|
|
|
let _: LinkedList<_> = v.iter().cloned().collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_push_front(b: &mut test::Bencher) {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m: LinkedList<_> = LinkedList::new();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-07-09 09:55:04 -05:00
|
|
|
m.push_front(0);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_push_back(b: &mut test::Bencher) {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m: LinkedList<_> = LinkedList::new();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2014-11-06 11:24:47 -06:00
|
|
|
m.push_back(0);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-21 14:05:48 -05:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_push_back_pop_back(b: &mut test::Bencher) {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m: LinkedList<_> = LinkedList::new();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2014-11-06 11:24:47 -06:00
|
|
|
m.push_back(0);
|
|
|
|
m.pop_back();
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_push_front_pop_front(b: &mut test::Bencher) {
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m: LinkedList<_> = LinkedList::new();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-07-21 12:31:40 -05:00
|
|
|
m.push_front(0);
|
|
|
|
m.pop_front();
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_iter(b: &mut test::Bencher) {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = &[0; 128];
|
2015-02-18 01:44:55 -06:00
|
|
|
let m: LinkedList<_> = v.iter().cloned().collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2014-06-06 01:18:51 -05:00
|
|
|
assert!(m.iter().count() == 128);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_iter_mut(b: &mut test::Bencher) {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = &[0; 128];
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m: LinkedList<_> = v.iter().cloned().collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2014-09-14 22:27:36 -05:00
|
|
|
assert!(m.iter_mut().count() == 128);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_iter_rev(b: &mut test::Bencher) {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = &[0; 128];
|
2015-02-18 01:44:55 -06:00
|
|
|
let m: LinkedList<_> = v.iter().cloned().collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2014-06-06 01:18:51 -05:00
|
|
|
assert!(m.iter().rev().count() == 128);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_iter_mut_rev(b: &mut test::Bencher) {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = &[0; 128];
|
2015-02-18 01:44:55 -06:00
|
|
|
let mut m: LinkedList<_> = v.iter().cloned().collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2014-09-14 22:27:36 -05:00
|
|
|
assert!(m.iter_mut().rev().count() == 128);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|