Skip to content

Commit 460b552

Browse files
committed
Reinstate HashMap.mangle().
This was removed when the Robin Hood hash map came along, but it is a useful thing to have. The comment is taken directly from what was there before (e.g. in 0.9) but with appropriate language changes (like `StrBuf` instead of `~str`).
1 parent d990681 commit 460b552

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed

src/libcollections/hashmap.rs

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,31 +1239,14 @@ impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
12391239
/// Return the value corresponding to the key in the map, or insert
12401240
/// and return the value if it doesn't exist.
12411241
pub fn find_or_insert<'a>(&'a mut self, k: K, v: V) -> &'a mut V {
1242-
let hash = self.make_hash(&k);
1243-
match self.search_hashed(&hash, &k) {
1244-
Some(idx) => {
1245-
let (_, v_ref) = self.table.read_mut(&idx);
1246-
v_ref
1247-
},
1248-
None => self.insert_hashed(hash, k, v)
1249-
}
1242+
self.mangle(k, v, |_k, a| a, |_k, _v, _a| ())
12501243
}
12511244

12521245
/// Return the value corresponding to the key in the map, or create,
12531246
/// insert, and return a new value if it doesn't exist.
12541247
pub fn find_or_insert_with<'a>(&'a mut self, k: K, f: |&K| -> V)
12551248
-> &'a mut V {
1256-
let hash = self.make_hash(&k);
1257-
match self.search_hashed(&hash, &k) {
1258-
Some(idx) => {
1259-
let (_, v_ref) = self.table.read_mut(&idx);
1260-
v_ref
1261-
},
1262-
None => {
1263-
let v = f(&k);
1264-
self.insert_hashed(hash, k, v)
1265-
}
1266-
}
1249+
self.mangle(k, (), |k, _a| f(k), |_k, _v, _a| ())
12671250
}
12681251

12691252
/// Insert a key-value pair into the map if the key is not already present.
@@ -1275,12 +1258,65 @@ impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
12751258
v: V,
12761259
f: |&K, &mut V|)
12771260
-> &'a mut V {
1261+
self.mangle(k, v, |_k, a| a, |k, v, _a| f(k, v))
1262+
}
1263+
1264+
/// Modify and return the value corresponding to the key in the map, or
1265+
/// insert and return a new value if it doesn't exist.
1266+
///
1267+
/// This method allows for all insertion behaviours of a hashmap;
1268+
/// see methods like `insert`, `find_or_insert` and
1269+
/// `insert_or_update_with` for less general and more friendly
1270+
/// variations of this.
1271+
///
1272+
/// # Example
1273+
///
1274+
/// ```rust
1275+
/// use collections::HashMap;
1276+
///
1277+
/// // map some strings to vectors of strings
1278+
/// let mut map = HashMap::<StrBuf, Vec<StrBuf>>::new();
1279+
/// map.insert(StrBuf::from_str("a key"), vec![StrBuf::from_str("value")]);
1280+
/// map.insert(StrBuf::from_str("z key"), vec![StrBuf::from_str("value")]);
1281+
///
1282+
/// let new = vec![StrBuf::from_str("a key"),
1283+
/// StrBuf::from_str("b key"),
1284+
/// StrBuf::from_str("z key")];
1285+
/// for k in new.move_iter() {
1286+
/// map.mangle(k, StrBuf::from_str("new value"),
1287+
/// // if the key doesn't exist in the map yet, add it in
1288+
/// // the obvious way.
1289+
/// |_k, v| vec![v],
1290+
/// // if the key does exist either prepend or append this
1291+
/// // new value based on the first letter of the key.
1292+
/// |key, already, new| {
1293+
/// if key.as_slice().starts_with("z") {
1294+
/// already.unshift(new);
1295+
/// } else {
1296+
/// already.push(new);
1297+
/// }
1298+
/// });
1299+
/// }
1300+
///
1301+
/// for (k, v) in map.iter() {
1302+
/// println!("{} -> {:?}", *k, *v);
1303+
/// }
1304+
/// ```
1305+
pub fn mangle<'a, A>(&'a mut self,
1306+
k: K,
1307+
a: A,
1308+
not_found: |&K, A| -> V,
1309+
found: |&K, &mut V, A|)
1310+
-> &'a mut V {
12781311
let hash = self.make_hash(&k);
12791312
match self.search_hashed(&hash, &k) {
1280-
None => self.insert_hashed(hash, k, v),
1313+
None => {
1314+
let v = not_found(&k, a);
1315+
self.insert_hashed(hash, k, v)
1316+
},
12811317
Some(idx) => {
12821318
let (_, v_ref) = self.table.read_mut(&idx);
1283-
f(&k, v_ref);
1319+
found(&k, v_ref, a);
12841320
v_ref
12851321
}
12861322
}

0 commit comments

Comments
 (0)