Skip to content

Commit d4171da

Browse files
committed
---
yaml --- r: 172787 b: refs/heads/try c: e7b397b h: refs/heads/master i: 172785: 3fba679 172783: 38205fc v: v3
1 parent 1c04b50 commit d4171da

File tree

8 files changed

+226
-54
lines changed

8 files changed

+226
-54
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 170c4399e614fe599c3d41306b3429ca8b3b68c6
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 5b3cd3900ceda838f5798c30ab96ceb41f962534
5-
refs/heads/try: 14b6c6d153edec5731230f919eaafcb5c32959d8
5+
refs/heads/try: e7b397b02e49ab6af5bc2a30dd04c19c38e0e266
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
88
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try/src/libcollections/dlist.rs

Lines changed: 161 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ impl<T> DList<T> {
221221
DList{list_head: None, list_tail: Rawlink::none(), length: 0}
222222
}
223223

224-
/// Adds all elements from `other` to the end of the list.
224+
/// Moves all elements from `other` to the end of the list.
225225
///
226-
/// This operation should compute in O(1) time.
226+
/// This reuses all the nodes from `other` and moves them into `self`. After
227+
/// this operation, `other` becomes empty.
228+
///
229+
/// This operation should compute in O(1) time and O(1) memory.
227230
///
228231
/// # Examples
229232
///
@@ -237,16 +240,20 @@ impl<T> DList<T> {
237240
/// b.push_back(3i);
238241
/// b.push_back(4);
239242
///
240-
/// a.append(b);
243+
/// a.append(&mut b);
241244
///
242245
/// for e in a.iter() {
243246
/// println!("{}", e); // prints 1, then 2, then 3, then 4
244247
/// }
248+
/// println!("{}", b.len()); // prints 0
245249
/// ```
246-
#[unstable = "append should be by-mutable-reference"]
247-
pub fn append(&mut self, mut other: DList<T>) {
250+
pub fn append(&mut self, other: &mut DList<T>) {
248251
match self.list_tail.resolve() {
249-
None => *self = other,
252+
None => {
253+
self.length = other.length;
254+
self.list_head = other.list_head.take();
255+
self.list_tail = other.list_tail.take();
256+
},
250257
Some(tail) => {
251258
// Carefully empty `other`.
252259
let o_tail = other.list_tail.take();
@@ -261,6 +268,7 @@ impl<T> DList<T> {
261268
}
262269
}
263270
}
271+
other.length = 0;
264272
}
265273

266274
/// Provides a forward iterator.
@@ -404,6 +412,51 @@ impl<T> DList<T> {
404412
pub fn pop_back(&mut self) -> Option<T> {
405413
self.pop_back_node().map(|box Node{value, ..}| value)
406414
}
415+
416+
/// Splits the list into two at the given index. Returns everything after the given index,
417+
/// including the index.
418+
///
419+
/// This operation should compute in O(n) time.
420+
#[stable]
421+
pub fn split_off(&mut self, at: uint) -> DList<T> {
422+
let len = self.len();
423+
assert!(at < len, "Cannot split off at a nonexistent index");
424+
if at == 0 {
425+
return mem::replace(self, DList::new());
426+
}
427+
428+
// Below, we iterate towards the `i-1`th node, either from the start or the end,
429+
// depending on which would be faster.
430+
let mut split_node = if at - 1 <= len - 1 - (at - 1) {
431+
let mut iter = self.iter_mut();
432+
// instead of skipping using .skip() (which creates a new struct),
433+
// we skip manually so we can access the head field without
434+
// depending on implementation details of Skip
435+
for _ in range(0, at - 1) {
436+
iter.next();
437+
}
438+
iter.head
439+
} else {
440+
// better off starting from the end
441+
let mut iter = self.iter_mut();
442+
for _ in range(0, len - 1 - (at - 1)) {
443+
iter.next_back();
444+
}
445+
iter.tail
446+
};
447+
448+
let mut splitted_list = DList {
449+
list_head: None,
450+
list_tail: self.list_tail,
451+
length: len - at
452+
};
453+
454+
mem::swap(&mut split_node.resolve().unwrap().next, &mut splitted_list.list_head);
455+
self.list_tail = split_node;
456+
self.length = at;
457+
458+
splitted_list
459+
}
407460
}
408461

409462
#[unsafe_destructor]
@@ -777,6 +830,108 @@ mod tests {
777830
v.iter().map(|x| (*x).clone()).collect()
778831
}
779832

833+
#[test]
834+
fn test_append() {
835+
// Empty to empty
836+
{
837+
let mut m: DList<int> = DList::new();
838+
let mut n = DList::new();
839+
m.append(&mut n);
840+
check_links(&m);
841+
assert_eq!(m.len(), 0);
842+
assert_eq!(n.len(), 0);
843+
}
844+
// Non-empty to empty
845+
{
846+
let mut m = DList::new();
847+
let mut n = DList::new();
848+
n.push_back(2i);
849+
m.append(&mut n);
850+
check_links(&m);
851+
assert_eq!(m.len(), 1);
852+
assert_eq!(m.pop_back(), Some(2));
853+
assert_eq!(n.len(), 0);
854+
check_links(&m);
855+
}
856+
// Empty to non-empty
857+
{
858+
let mut m = DList::new();
859+
let mut n = DList::new();
860+
m.push_back(2i);
861+
m.append(&mut n);
862+
check_links(&m);
863+
assert_eq!(m.len(), 1);
864+
assert_eq!(m.pop_back(), Some(2));
865+
check_links(&m);
866+
}
867+
868+
// Non-empty to non-empty
869+
let v = vec![1i,2,3,4,5];
870+
let u = vec![9i,8,1,2,3,4,5];
871+
let mut m = list_from(v.as_slice());
872+
let mut n = list_from(u.as_slice());
873+
m.append(&mut n);
874+
check_links(&m);
875+
let mut sum = v;
876+
sum.push_all(u.as_slice());
877+
assert_eq!(sum.len(), m.len());
878+
for elt in sum.into_iter() {
879+
assert_eq!(m.pop_front(), Some(elt))
880+
}
881+
assert_eq!(n.len(), 0);
882+
// let's make sure it's working properly, since we
883+
// did some direct changes to private members
884+
n.push_back(3);
885+
assert_eq!(n.len(), 1);
886+
assert_eq!(n.pop_front(), Some(3));
887+
check_links(&n);
888+
}
889+
890+
#[test]
891+
fn test_split_off() {
892+
// singleton
893+
{
894+
let mut m = DList::new();
895+
m.push_back(1i);
896+
897+
let p = m.split_off(0);
898+
assert_eq!(m.len(), 0);
899+
assert_eq!(p.len(), 1);
900+
assert_eq!(p.back(), Some(&1));
901+
assert_eq!(p.front(), Some(&1));
902+
}
903+
904+
// not singleton, forwards
905+
{
906+
let u = vec![1i,2,3,4,5];
907+
let mut m = list_from(u.as_slice());
908+
let mut n = m.split_off(2);
909+
assert_eq!(m.len(), 2);
910+
assert_eq!(n.len(), 3);
911+
for elt in range(1i, 3) {
912+
assert_eq!(m.pop_front(), Some(elt));
913+
}
914+
for elt in range(3i, 6) {
915+
assert_eq!(n.pop_front(), Some(elt));
916+
}
917+
}
918+
// not singleton, backwards
919+
{
920+
let u = vec![1i,2,3,4,5];
921+
let mut m = list_from(u.as_slice());
922+
let mut n = m.split_off(4);
923+
assert_eq!(m.len(), 4);
924+
assert_eq!(n.len(), 1);
925+
for elt in range(1i, 5) {
926+
assert_eq!(m.pop_front(), Some(elt));
927+
}
928+
for elt in range(5i, 6) {
929+
assert_eq!(n.pop_front(), Some(elt));
930+
}
931+
}
932+
933+
}
934+
780935
#[test]
781936
fn test_iterator() {
782937
let m = generate_test();
@@ -1065,41 +1220,6 @@ mod tests {
10651220
assert_eq!(i, v.len());
10661221
}
10671222

1068-
#[allow(deprecated)]
1069-
#[test]
1070-
fn test_append() {
1071-
{
1072-
let mut m = DList::new();
1073-
let mut n = DList::new();
1074-
n.push_back(2i);
1075-
m.append(n);
1076-
assert_eq!(m.len(), 1);
1077-
assert_eq!(m.pop_back(), Some(2));
1078-
check_links(&m);
1079-
}
1080-
{
1081-
let mut m = DList::new();
1082-
let n = DList::new();
1083-
m.push_back(2i);
1084-
m.append(n);
1085-
assert_eq!(m.len(), 1);
1086-
assert_eq!(m.pop_back(), Some(2));
1087-
check_links(&m);
1088-
}
1089-
1090-
let v = vec![1i,2,3,4,5];
1091-
let u = vec![9i,8,1,2,3,4,5];
1092-
let mut m = list_from(v.as_slice());
1093-
m.append(list_from(u.as_slice()));
1094-
check_links(&m);
1095-
let mut sum = v;
1096-
sum.push_all(u.as_slice());
1097-
assert_eq!(sum.len(), m.len());
1098-
for elt in sum.into_iter() {
1099-
assert_eq!(m.pop_front(), Some(elt))
1100-
}
1101-
}
1102-
11031223
#[bench]
11041224
fn bench_collect_into(b: &mut test::Bencher) {
11051225
let v = &[0i; 64];

branches/try/src/libcollections/string.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ impl String {
302302
/// assert_eq!(String::from_utf16_lossy(v),
303303
/// "𝄞mus\u{FFFD}ic\u{FFFD}".to_string());
304304
/// ```
305+
#[inline]
305306
#[stable]
306307
pub fn from_utf16_lossy(v: &[u16]) -> String {
307308
unicode_str::utf16_items(v).map(|c| c.to_char_lossy()).collect()
@@ -556,6 +557,7 @@ impl String {
556557
/// assert_eq!(s.remove(1), 'o');
557558
/// assert_eq!(s.remove(0), 'o');
558559
/// ```
560+
#[inline]
559561
#[stable]
560562
pub fn remove(&mut self, idx: uint) -> char {
561563
let len = self.len();
@@ -582,6 +584,7 @@ impl String {
582584
///
583585
/// If `idx` does not lie on a character boundary or is out of bounds, then
584586
/// this function will panic.
587+
#[inline]
585588
#[stable]
586589
pub fn insert(&mut self, idx: uint, ch: char) {
587590
let len = self.len();
@@ -618,6 +621,7 @@ impl String {
618621
/// }
619622
/// assert_eq!(s.as_slice(), "olleh");
620623
/// ```
624+
#[inline]
621625
#[stable]
622626
pub unsafe fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec<u8> {
623627
&mut self.vec
@@ -645,6 +649,7 @@ impl String {
645649
/// v.push('a');
646650
/// assert!(!v.is_empty());
647651
/// ```
652+
#[inline]
648653
#[stable]
649654
pub fn is_empty(&self) -> bool { self.len() == 0 }
650655

@@ -801,6 +806,7 @@ impl Str for String {
801806

802807
#[stable]
803808
impl Default for String {
809+
#[inline]
804810
#[stable]
805811
fn default() -> String {
806812
String::new()
@@ -809,13 +815,15 @@ impl Default for String {
809815

810816
#[stable]
811817
impl fmt::String for String {
818+
#[inline]
812819
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
813820
fmt::String::fmt(&**self, f)
814821
}
815822
}
816823

817824
#[unstable = "waiting on fmt stabilization"]
818825
impl fmt::Show for String {
826+
#[inline]
819827
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
820828
fmt::Show::fmt(&**self, f)
821829
}
@@ -842,6 +850,7 @@ impl<H: hash::Writer + hash::Hasher> hash::Hash<H> for String {
842850
impl<'a> Add<&'a str> for String {
843851
type Output = String;
844852

853+
#[inline]
845854
fn add(mut self, other: &str) -> String {
846855
self.push_str(other);
847856
self
@@ -881,6 +890,7 @@ impl ops::Index<ops::FullRange> for String {
881890
impl ops::Deref for String {
882891
type Target = str;
883892

893+
#[inline]
884894
fn deref<'a>(&'a self) -> &'a str {
885895
unsafe { mem::transmute(&self.vec[]) }
886896
}
@@ -895,6 +905,7 @@ pub struct DerefString<'a> {
895905
impl<'a> Deref for DerefString<'a> {
896906
type Target = String;
897907

908+
#[inline]
898909
fn deref<'b>(&'b self) -> &'b String {
899910
unsafe { mem::transmute(&*self.x) }
900911
}
@@ -933,6 +944,7 @@ pub trait ToString {
933944
}
934945

935946
impl<T: fmt::String + ?Sized> ToString for T {
947+
#[inline]
936948
fn to_string(&self) -> String {
937949
use core::fmt::Writer;
938950
let mut buf = String::new();
@@ -943,12 +955,14 @@ impl<T: fmt::String + ?Sized> ToString for T {
943955
}
944956

945957
impl IntoCow<'static, String, str> for String {
958+
#[inline]
946959
fn into_cow(self) -> CowString<'static> {
947960
Cow::Owned(self)
948961
}
949962
}
950963

951964
impl<'a> IntoCow<'a, String, str> for &'a str {
965+
#[inline]
952966
fn into_cow(self) -> CowString<'a> {
953967
Cow::Borrowed(self)
954968
}
@@ -966,6 +980,7 @@ impl<'a> Str for CowString<'a> {
966980
}
967981

968982
impl fmt::Writer for String {
983+
#[inline]
969984
fn write_str(&mut self, s: &str) -> fmt::Result {
970985
self.push_str(s);
971986
Ok(())

branches/try/src/librustdoc/html/static/playpen.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
(function() {
1515
if (window.playgroundUrl) {
1616
$('pre.rust').hover(function() {
17+
if (!$(this).attr('id')) { return; }
1718
var id = '#' + $(this).attr('id').replace('rendered', 'raw');
1819
var a = $('<a>').text('⇱').attr('class', 'test-arrow');
1920
var code = $(id).text();

0 commit comments

Comments
 (0)