File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
5
5
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
6
6
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
7
7
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8
- refs/heads/try2: 8a3267672c43e7cc116e588dd21998d14fc21ba4
8
+ refs/heads/try2: 0ff5c17cbbef0aedfa2eb1142cbf7a87abfd1d05
9
9
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
10
10
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
11
11
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
Original file line number Diff line number Diff line change @@ -14,7 +14,8 @@ use std::uint;
14
14
use std:: vec;
15
15
use std:: iterator:: FromIterator ;
16
16
17
- static INITIAL_CAPACITY : uint = 32 u; // 2^5
17
+ static INITIAL_CAPACITY : uint = 8 u; // 2^3
18
+ static MINIMUM_CAPACITY : uint = 2 u;
18
19
19
20
#[ allow( missing_doc) ]
20
21
pub struct Deque < T > {
@@ -43,8 +44,13 @@ impl<T> Mutable for Deque<T> {
43
44
impl <T > Deque < T > {
44
45
/// Create an empty Deque
45
46
pub fn new( ) -> Deque < T > {
47
+ Deque :: with_capacity( INITIAL_CAPACITY )
48
+ }
49
+
50
+ /// Create an empty Deque with space for at least `n` elements.
51
+ pub fn with_capacity( n: uint) -> Deque < T > {
46
52
Deque { nelts : 0 , lo : 0 ,
47
- elts : vec:: from_fn( INITIAL_CAPACITY , |_| None ) }
53
+ elts : vec:: from_fn( uint :: max ( MINIMUM_CAPACITY , n ) , |_| None ) }
48
54
}
49
55
50
56
/// Return a reference to the first element in the deque
@@ -527,6 +533,16 @@ mod tests {
527
533
528
534
}
529
535
536
+ #[ test]
537
+ fn test_with_capacity ( ) {
538
+ let mut d = Deque :: with_capacity ( 0 ) ;
539
+ d. add_back ( 1 ) ;
540
+ assert_eq ! ( d. len( ) , 1 ) ;
541
+ let mut d = Deque :: with_capacity ( 50 ) ;
542
+ d. add_back ( 1 ) ;
543
+ assert_eq ! ( d. len( ) , 1 ) ;
544
+ }
545
+
530
546
#[ test]
531
547
fn test_reserve ( ) {
532
548
let mut d = Deque :: new ( ) ;
You can’t perform that action at this time.
0 commit comments