@@ -61,27 +61,7 @@ enum SearchResult {
61
61
62
62
#[ inline]
63
63
fn resize_at ( capacity : uint ) -> uint {
64
- ( ( capacity as float ) * 3. / 4. ) as uint
65
- }
66
-
67
- /// Creates a new hash map with the specified capacity.
68
- pub fn linear_map_with_capacity < K : Eq + Hash , V > (
69
- initial_capacity : uint ) -> HashMap < K , V > {
70
- let mut r = rand:: task_rng ( ) ;
71
- linear_map_with_capacity_and_keys ( r. gen ( ) , r. gen ( ) ,
72
- initial_capacity)
73
- }
74
-
75
- fn linear_map_with_capacity_and_keys < K : Eq + Hash , V > (
76
- k0 : u64 , k1 : u64 ,
77
- initial_capacity : uint ) -> HashMap < K , V > {
78
- let cap = num:: max ( INITIAL_CAPACITY , initial_capacity) ;
79
- HashMap {
80
- k0 : k0, k1 : k1,
81
- resize_at : resize_at ( cap) ,
82
- size : 0 ,
83
- buckets : vec:: from_fn ( cap, |_| None )
84
- }
64
+ ( capacity * 3 ) / 4
85
65
}
86
66
87
67
impl < K : Hash + Eq , V > HashMap < K , V > {
@@ -352,10 +332,28 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
352
332
HashMap :: with_capacity ( INITIAL_CAPACITY )
353
333
}
354
334
355
- /// Create an empty HashMap with space for at least `n` elements in
356
- /// the hash table.
335
+ /// Create an empty HashMap with space for at least `capacity`
336
+ /// elements in the hash table.
357
337
pub fn with_capacity ( capacity : uint ) -> HashMap < K , V > {
358
- linear_map_with_capacity ( capacity)
338
+ let mut r = rand:: task_rng ( ) ;
339
+ HashMap :: with_capacity_and_keys ( r. gen ( ) , r. gen ( ) , capacity)
340
+ }
341
+
342
+ /// Create an empty HashMap with space for at least `capacity`
343
+ /// elements, using `k0` and `k1` as the keys.
344
+ ///
345
+ /// Warning: `k0` and `k1` are normally randomly generated, and
346
+ /// are designed to allow HashMaps to be resistant to attacks that
347
+ /// cause many collisions and very poor performance. Setting them
348
+ /// manually using this function can expose a DoS attack vector.
349
+ pub fn with_capacity_and_keys ( k0 : u64 , k1 : u64 , capacity : uint ) -> HashMap < K , V > {
350
+ let cap = num:: max ( INITIAL_CAPACITY , capacity) ;
351
+ HashMap {
352
+ k0 : k0, k1 : k1,
353
+ resize_at : resize_at ( cap) ,
354
+ size : 0 ,
355
+ buckets : vec:: from_fn ( cap, |_| None )
356
+ }
359
357
}
360
358
361
359
/// Reserve space for at least `n` elements in the hash table.
@@ -844,7 +842,7 @@ mod test_map {
844
842
845
843
#[ test]
846
844
fn test_insert_conflicts ( ) {
847
- let mut m = linear_map_with_capacity ( 4 ) ;
845
+ let mut m = HashMap :: with_capacity ( 4 ) ;
848
846
assert ! ( m. insert( 1 , 2 ) ) ;
849
847
assert ! ( m. insert( 5 , 3 ) ) ;
850
848
assert ! ( m. insert( 9 , 4 ) ) ;
@@ -855,7 +853,7 @@ mod test_map {
855
853
856
854
#[ test]
857
855
fn test_conflict_remove ( ) {
858
- let mut m = linear_map_with_capacity ( 4 ) ;
856
+ let mut m = HashMap :: with_capacity ( 4 ) ;
859
857
assert ! ( m. insert( 1 , 2 ) ) ;
860
858
assert ! ( m. insert( 5 , 3 ) ) ;
861
859
assert ! ( m. insert( 9 , 4 ) ) ;
@@ -866,7 +864,7 @@ mod test_map {
866
864
867
865
#[ test]
868
866
fn test_is_empty ( ) {
869
- let mut m = linear_map_with_capacity ( 4 ) ;
867
+ let mut m = HashMap :: with_capacity ( 4 ) ;
870
868
assert ! ( m. insert( 1 , 2 ) ) ;
871
869
assert ! ( !m. is_empty( ) ) ;
872
870
assert ! ( m. remove( & 1 ) ) ;
@@ -927,7 +925,7 @@ mod test_map {
927
925
928
926
#[ test]
929
927
fn test_iterate ( ) {
930
- let mut m = linear_map_with_capacity ( 4 ) ;
928
+ let mut m = HashMap :: with_capacity ( 4 ) ;
931
929
foreach i in range( 0 u, 32 ) {
932
930
assert ! ( m. insert( i, i* 2 ) ) ;
933
931
}
0 commit comments