Skip to content

Commit b66bbfe

Browse files
authored
Merge pull request #337 from cuviper/append
Add `append` methods
2 parents 22c0b4e + a288bf3 commit b66bbfe

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "indexmap"
33
edition = "2021"
4-
version = "2.3.0"
4+
version = "2.4.0"
55
documentation = "https://docs.rs/indexmap/"
66
repository = "https://github.com/indexmap-rs/indexmap"
77
license = "Apache-2.0 OR MIT"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ allows lookup of entries by either hash table key or numerical index.
1414

1515
Note: this crate was originally released under the name `ordermap`,
1616
but it was renamed to `indexmap` to better reflect its features.
17+
The [`ordermap`](https://crates.io/crates/ordermap) crate now exists
18+
as a wrapper over `indexmap` with stronger ordering properties.
1719

1820
# Background
1921

RELEASES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Releases
22

3+
## 2.4.0
4+
5+
- Added methods `IndexMap::append` and `IndexSet::append`, moving all items from
6+
one map or set into another, and leaving the original capacity for reuse.
7+
38
## 2.3.0
49

510
- Added trait `MutableEntryKey` for opt-in mutable access to map entry keys.

src/map.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,35 @@ where
520520
{
521521
Splice::new(self, range, replace_with.into_iter())
522522
}
523+
524+
/// Moves all key-value pairs from `other` into `self`, leaving `other` empty.
525+
///
526+
/// This is equivalent to calling [`insert`][Self::insert] for each
527+
/// key-value pair from `other` in order, which means that for keys that
528+
/// already exist in `self`, their value is updated in the current position.
529+
///
530+
/// # Examples
531+
///
532+
/// ```
533+
/// use indexmap::IndexMap;
534+
///
535+
/// // Note: Key (3) is present in both maps.
536+
/// let mut a = IndexMap::from([(3, "c"), (2, "b"), (1, "a")]);
537+
/// let mut b = IndexMap::from([(3, "d"), (4, "e"), (5, "f")]);
538+
/// let old_capacity = b.capacity();
539+
///
540+
/// a.append(&mut b);
541+
///
542+
/// assert_eq!(a.len(), 5);
543+
/// assert_eq!(b.len(), 0);
544+
/// assert_eq!(b.capacity(), old_capacity);
545+
///
546+
/// assert!(a.keys().eq(&[3, 2, 1, 4, 5]));
547+
/// assert_eq!(a[&3], "d"); // "c" was overwritten.
548+
/// ```
549+
pub fn append<S2>(&mut self, other: &mut IndexMap<K, V, S2>) {
550+
self.extend(other.drain(..));
551+
}
523552
}
524553

525554
impl<K, V, S> IndexMap<K, V, S>

src/set.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,36 @@ where
501501
{
502502
Splice::new(self, range, replace_with.into_iter())
503503
}
504+
505+
/// Moves all values from `other` into `self`, leaving `other` empty.
506+
///
507+
/// This is equivalent to calling [`insert`][Self::insert] for each value
508+
/// from `other` in order, which means that values that already exist
509+
/// in `self` are unchanged in their current position.
510+
///
511+
/// See also [`union`][Self::union] to iterate the combined values by
512+
/// reference, without modifying `self` or `other`.
513+
///
514+
/// # Examples
515+
///
516+
/// ```
517+
/// use indexmap::IndexSet;
518+
///
519+
/// let mut a = IndexSet::from([3, 2, 1]);
520+
/// let mut b = IndexSet::from([3, 4, 5]);
521+
/// let old_capacity = b.capacity();
522+
///
523+
/// a.append(&mut b);
524+
///
525+
/// assert_eq!(a.len(), 5);
526+
/// assert_eq!(b.len(), 0);
527+
/// assert_eq!(b.capacity(), old_capacity);
528+
///
529+
/// assert!(a.iter().eq(&[3, 2, 1, 4, 5]));
530+
/// ```
531+
pub fn append<S2>(&mut self, other: &mut IndexSet<T, S2>) {
532+
self.map.append(&mut other.map);
533+
}
504534
}
505535

506536
impl<T, S> IndexSet<T, S>

0 commit comments

Comments
 (0)