@@ -37,6 +37,29 @@ impl std::hash::BuildHasher for FixedState {
37
37
/// AHash is designed for performance and is NOT cryptographically secure.
38
38
pub type HashMap < K , V > = std:: collections:: HashMap < K , V , RandomState > ;
39
39
40
+ pub trait AHashExt {
41
+ fn with_capacity ( capacity : usize ) -> Self ;
42
+ }
43
+
44
+ impl < K , V > AHashExt for HashMap < K , V > {
45
+ /// Creates an empty `HashMap` with the specified capacity with AHash.
46
+ ///
47
+ /// The hash map will be able to hold at least `capacity` elements without
48
+ /// reallocating. If `capacity` is 0, the hash map will not allocate.
49
+ ///
50
+ /// # Examples
51
+ ///
52
+ /// ```
53
+ /// use bevy_utils::{HashMap, AHashExt};
54
+ /// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10);
55
+ /// assert!(map.capacity() >= 10);
56
+ /// ```
57
+ #[ inline]
58
+ fn with_capacity ( capacity : usize ) -> Self {
59
+ HashMap :: with_capacity_and_hasher ( capacity, RandomState :: default ( ) )
60
+ }
61
+ }
62
+
40
63
/// A stable std hash map implementing AHash, a high speed keyed hashing algorithm
41
64
/// intended for use in in-memory hashmaps.
42
65
///
@@ -46,12 +69,50 @@ pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
46
69
/// AHash is designed for performance and is NOT cryptographically secure.
47
70
pub type StableHashMap < K , V > = std:: collections:: HashMap < K , V , FixedState > ;
48
71
72
+ impl < K , V > AHashExt for StableHashMap < K , V > {
73
+ /// Creates an empty `StableHashMap` with the specified capacity with AHash.
74
+ ///
75
+ /// The hash map will be able to hold at least `capacity` elements without
76
+ /// reallocating. If `capacity` is 0, the hash map will not allocate.
77
+ ///
78
+ /// # Examples
79
+ ///
80
+ /// ```
81
+ /// use bevy_utils::{StableHashMap, AHashExt};
82
+ /// let mut map: StableHashMap<&str, i32> = StableHashMap::with_capacity(10);
83
+ /// assert!(map.capacity() >= 10);
84
+ /// ```
85
+ #[ inline]
86
+ fn with_capacity ( capacity : usize ) -> Self {
87
+ StableHashMap :: with_capacity_and_hasher ( capacity, FixedState :: default ( ) )
88
+ }
89
+ }
90
+
49
91
/// A std hash set implementing AHash, a high speed keyed hashing algorithm
50
92
/// intended for use in in-memory hashmaps.
51
93
///
52
94
/// AHash is designed for performance and is NOT cryptographically secure.
53
95
pub type HashSet < K > = std:: collections:: HashSet < K , RandomState > ;
54
96
97
+ impl < K > AHashExt for HashSet < K > {
98
+ /// Creates an empty `HashSet` with the specified capacity with AHash.
99
+ ///
100
+ /// The hash set will be able to hold at least `capacity` elements without
101
+ /// reallocating. If `capacity` is 0, the hash set will not allocate.
102
+ ///
103
+ /// # Examples
104
+ ///
105
+ /// ```
106
+ /// use bevy_utils::{HashSet, AHashExt};
107
+ /// let set: HashSet<i32> = HashSet::with_capacity(10);
108
+ /// assert!(set.capacity() >= 10);
109
+ /// ```
110
+ #[ inline]
111
+ fn with_capacity ( capacity : usize ) -> Self {
112
+ HashSet :: with_capacity_and_hasher ( capacity, RandomState :: default ( ) )
113
+ }
114
+ }
115
+
55
116
/// A stable std hash set implementing AHash, a high speed keyed hashing algorithm
56
117
/// intended for use in in-memory hashmaps.
57
118
///
@@ -60,3 +121,22 @@ pub type HashSet<K> = std::collections::HashSet<K, RandomState>;
60
121
///
61
122
/// AHash is designed for performance and is NOT cryptographically secure.
62
123
pub type StableHashSet < K > = std:: collections:: HashSet < K , FixedState > ;
124
+
125
+ impl < K > AHashExt for StableHashSet < K > {
126
+ /// Creates an empty `StableHashSet` with the specified capacity with AHash.
127
+ ///
128
+ /// The hash set will be able to hold at least `capacity` elements without
129
+ /// reallocating. If `capacity` is 0, the hash set will not allocate.
130
+ ///
131
+ /// # Examples
132
+ ///
133
+ /// ```
134
+ /// use bevy_utils::{StableHashSet, AHashExt};
135
+ /// let set: StableHashSet<i32> = StableHashSet::with_capacity(10);
136
+ /// assert!(set.capacity() >= 10);
137
+ /// ```
138
+ #[ inline]
139
+ fn with_capacity ( capacity : usize ) -> Self {
140
+ StableHashSet :: with_capacity_and_hasher ( capacity, FixedState :: default ( ) )
141
+ }
142
+ }
0 commit comments