From 8c0a1cd4be7de02bee713433c449bcb251dcd994 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 13 Aug 2024 11:52:04 -0700 Subject: [PATCH 1/3] Add `append` methods --- src/map.rs | 29 +++++++++++++++++++++++++++++ src/set.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/map.rs b/src/map.rs index ea7e1b48..85310934 100644 --- a/src/map.rs +++ b/src/map.rs @@ -520,6 +520,35 @@ where { Splice::new(self, range, replace_with.into_iter()) } + + /// Moves all key-value pairs from `other` into `self`, leaving `other` empty. + /// + /// This is equivalent to calling [`insert`][Self::insert] for each + /// key-value pair from `other` in order, which means that for keys that + /// already exist in `self`, their value is updated in the current position. + /// + /// # Examples + /// + /// ``` + /// use indexmap::IndexMap; + /// + /// // Note: Key (3) is present in both maps. + /// let mut a = IndexMap::from([(3, "c"), (2, "b"), (1, "a")]); + /// let mut b = IndexMap::from([(3, "d"), (4, "e"), (5, "f")]); + /// let old_capacity = b.capacity(); + /// + /// a.append(&mut b); + /// + /// assert_eq!(a.len(), 5); + /// assert_eq!(b.len(), 0); + /// assert_eq!(b.capacity(), old_capacity); + /// + /// assert!(a.keys().eq(&[3, 2, 1, 4, 5])); + /// assert_eq!(a[&3], "d"); // "c" was overwritten. + /// ``` + pub fn append(&mut self, other: &mut IndexMap) { + self.extend(other.drain(..)); + } } impl IndexMap diff --git a/src/set.rs b/src/set.rs index b5bd05f1..835ccf02 100644 --- a/src/set.rs +++ b/src/set.rs @@ -501,6 +501,36 @@ where { Splice::new(self, range, replace_with.into_iter()) } + + /// Moves all values from `other` into `self`, leaving `other` empty. + /// + /// This is equivalent to calling [`insert`][Self::insert] for each value + /// from `other` in order, which means that values that already exist + /// in `self` are unchanged in their current position. + /// + /// See also [`union`][Self::union] to iterate the combined values by + /// reference, without modifying `self` or `other`. + /// + /// # Examples + /// + /// ``` + /// use indexmap::IndexSet; + /// + /// let mut a = IndexSet::from([3, 2, 1]); + /// let mut b = IndexSet::from([3, 4, 5]); + /// let old_capacity = b.capacity(); + /// + /// a.append(&mut b); + /// + /// assert_eq!(a.len(), 5); + /// assert_eq!(b.len(), 0); + /// assert_eq!(b.capacity(), old_capacity); + /// + /// assert!(a.iter().eq(&[3, 2, 1, 4, 5])); + /// ``` + pub fn append(&mut self, other: &mut IndexSet) { + self.map.append(&mut other.map); + } } impl IndexSet From 0b2b4b9a7829905292378cb99c392c44a0931850 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 13 Aug 2024 12:40:28 -0700 Subject: [PATCH 2/3] Release 2.4.0 --- Cargo.toml | 2 +- RELEASES.md | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bdd94be5..cdcc77b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "indexmap" edition = "2021" -version = "2.3.0" +version = "2.4.0" documentation = "https://docs.rs/indexmap/" repository = "https://github.com/indexmap-rs/indexmap" license = "Apache-2.0 OR MIT" diff --git a/RELEASES.md b/RELEASES.md index 3b30af76..daf4493e 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,5 +1,10 @@ # Releases +## 2.4.0 + +- Added methods `IndexMap::append` and `IndexSet::append`, moving all items from + one map or set into another, and leaving the original capacity for reuse. + ## 2.3.0 - Added trait `MutableEntryKey` for opt-in mutable access to map entry keys. From a288bf3b3a14ecc5f05006dd15f43759548b1469 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 13 Aug 2024 13:20:56 -0700 Subject: [PATCH 3/3] Add a README note for `ordermap` --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9112d528..e504109d 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ allows lookup of entries by either hash table key or numerical index. Note: this crate was originally released under the name `ordermap`, but it was renamed to `indexmap` to better reflect its features. +The [`ordermap`](https://crates.io/crates/ordermap) crate now exists +as a wrapper over `indexmap` with stronger ordering properties. # Background