@@ -22,40 +22,61 @@ pub struct Deque<T> {
22
22
}
23
23
24
24
impl < T > Container for Deque < T > {
25
+ /// Return the number of elements in the deque
25
26
pure fn len ( & self ) -> uint { self . nelts }
27
+
28
+ /// Return true if the deque contains no elements
26
29
pure fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
27
30
}
28
31
29
32
impl < T > Mutable for Deque < T > {
33
+ /// Clear the deque, removing all values.
30
34
fn clear ( & mut self ) {
31
- for vec :: each_mut ( self . elts) |x| { * x = None }
35
+ for self . elts. each_mut |x| { * x = None }
32
36
self . nelts = 0 ;
33
37
self . lo = 0 ;
34
38
self . hi = 0 ;
35
39
}
36
40
}
37
41
38
42
pub impl < T > Deque < T > {
43
+ /// Create an empty Deque
39
44
static pure fn new( ) -> Deque <T > {
40
45
Deque { nelts: 0 , lo: 0 , hi: 0 ,
41
46
elts: vec:: from_fn( initial_capacity, |_| None ) }
42
47
}
43
48
49
+ /// Return a reference to the first element in the deque
50
+ ///
51
+ /// Fails if the deque is empty
44
52
fn peek_front ( & self ) -> & self /T { get ( self . elts , self . lo ) }
53
+
54
+ /// Return a reference to the last element in the deque
55
+ ///
56
+ /// Fails if the deque is empty
45
57
fn peek_back ( & self ) -> & self /T { get ( self . elts , self . hi - 1 u) }
46
58
59
+ /// Retrieve an element in the deque by index
60
+ ///
61
+ /// Fails if there is no element with the given index
47
62
fn get ( & self , i : int ) -> & self /T {
48
63
let idx = ( self . lo + ( i as uint ) ) % self . elts . len ( ) ;
49
64
get ( self . elts , idx)
50
65
}
51
66
67
+ /// Remove and return the first element in the deque
68
+ ///
69
+ /// Fails if the deque is empty
52
70
fn pop_front ( & mut self ) -> T {
53
71
let mut result = self . elts [ self . lo ] . swap_unwrap ( ) ;
54
72
self . lo = ( self . lo + 1 u) % self . elts . len ( ) ;
55
73
self . nelts -= 1 u;
56
74
result
57
75
}
58
76
77
+ /// Remove and return the last element in the deque
78
+ ///
79
+ /// Fails if the deque is empty
59
80
fn pop_back ( & mut self ) -> T {
60
81
if self . hi == 0 u {
61
82
self . hi = self . elts . len ( ) - 1 u;
@@ -66,6 +87,7 @@ pub impl<T> Deque<T> {
66
87
result
67
88
}
68
89
90
+ /// Prepend an element to the deque
69
91
fn add_front ( & mut self , t : T ) {
70
92
let oldlo = self . lo ;
71
93
if self . lo == 0 u {
@@ -80,6 +102,7 @@ pub impl<T> Deque<T> {
80
102
self . nelts += 1 u;
81
103
}
82
104
105
+ /// Append an element to the deque
83
106
fn add_back ( & mut self , t : T ) {
84
107
if self . lo == self . hi && self . nelts != 0 u {
85
108
self . elts = grow ( self . nelts , self . lo , self . elts ) ;
0 commit comments