Skip to content

Commit

Permalink
add fn items(&self) -> Vec<Element>
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 10, 2023
1 parent 5920d6f commit 8c88b38
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
30 changes: 24 additions & 6 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,16 @@ where
self.elements.get_mut(id)
}

/// A read-only collection view for the elements contained in this array, as a `Vec<Elements>`.
/// A read-only collection of references to the elements contained in this array, as a `Vec<&Elements>`.
///
/// N.B. that this method is not constant time.
///
/// If `Element` implements `Clone` you can use `self.items()` which returns a `Vec<Element>`, of cloned elements.
///
/// - Complexity: O(n)
#[inline]
pub fn elements(&self) -> Vec<&Element> {
let mut elements_ordered = Vec::<&Element>::new();
for id in &self.order {
elements_ordered.push(self.elements.get(id).expect("element"));
}
elements_ordered
self.iter().collect()
}

/// Returns `true` if the `identified_vec` contains the `element.`
Expand Down Expand Up @@ -441,6 +441,24 @@ where
}
}

impl<ID, Element> IdentifiedVec<ID, Element>
where
Element: Clone,
ID: Eq + Hash + Clone + Debug,
{
/// A read-only collection of clones of the elements contained in this array, as a `Vec<Elements>`.
///
/// N.B. that this method is not constant time.
///
/// Use `self.elements()` if you are looking for a collection of references.
///
/// - Complexity: O(n)
#[inline]
pub fn items(&self) -> Vec<Element> {
self.iter().map(|e| e.clone()).collect()
}
}

/// An iterator over the items of an `IdentifiedVec`.
pub struct IdentifiedVecIterator<'a, ID, Element>
where
Expand Down
4 changes: 2 additions & 2 deletions src/vec_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
/// - Complexity: Expected O(*n*) on average, where *n* is the count of elements, if `ID`
/// implements high-quality hashing.
#[inline]
pub fn tryfrom_iter_select_unique_with<E, I>(
pub fn try_from_iter_select_unique_with<E, I>(
elements: I,
combine: fn((usize, &Element, &Element)) -> Result<ConflictResolutionChoice, E>,
) -> Result<Self, E>
Expand Down Expand Up @@ -146,7 +146,7 @@ where
deserializer: D,
) -> Result<IdentifiedVecOf<Element>, D::Error> {
let elements = Vec::<Element>::deserialize(deserializer)?;
IdentifiedVecOf::<Element>::tryfrom_iter_select_unique_with(elements, |(idx, _, _)| {
IdentifiedVecOf::<Element>::try_from_iter_select_unique_with(elements, |(idx, _, _)| {
Err(IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(idx))
})
.map_err(de::Error::custom)
Expand Down
47 changes: 26 additions & 21 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,18 @@ fn index_id() {
fn remove_element() {
let mut identified_vec = SUT::from_iter([1, 2, 3]);
assert_eq!(identified_vec.remove(&2), Some(2));
assert_eq!(identified_vec.elements(), [&1, &3]);
assert_eq!(identified_vec.items(), [1, 3]);
}

#[test]
fn remove_by_id() {
let mut identified_vec = SUT::from_iter([1, 2, 3]);
assert_eq!(identified_vec.remove_by_id(&2), Some(2));
assert_eq!(identified_vec.elements(), [&1, &3]);
assert_eq!(identified_vec.items(), [1, 3]);
}

#[test]
fn constructor_id_uniquing_elements() {
fn constructor_from_iter_select_unique_ids_with() {
#[derive(Eq, PartialEq, Clone, Hash, Debug)]
struct Model {
id: i32,
Expand All @@ -227,8 +227,8 @@ fn constructor_id_uniquing_elements() {
);

assert_eq!(
conservative.elements(),
[&Model::new(1, "A"), &Model::new(2, "B")]
conservative.items(),
[Model::new(1, "A"), Model::new(2, "B")]
);

let progressive = IdentifiedVec::<i32, Model>::from_iter_select_unique_ids_with(
Expand All @@ -242,8 +242,8 @@ fn constructor_id_uniquing_elements() {
);

assert_eq!(
progressive.elements(),
[&Model::new(1, "AAAA"), &Model::new(2, "B")]
progressive.items(),
[Model::new(1, "AAAA"), Model::new(2, "B")]
)
}

Expand Down Expand Up @@ -277,8 +277,13 @@ fn constructor_from_iter_select_unique_with() {
);

assert_eq!(
conservative.elements(),
[&Model::new(1, "A"), &Model::new(2, "B")]
conservative.items(),
[Model::new(1, "A"), Model::new(2, "B")]
);

assert_eq!(
conservative.items(),
[Model::new(1, "A"), Model::new(2, "B")]
);

let progressive = IdentifiedVecOf::<Model>::from_iter_select_unique_with(
Expand All @@ -291,8 +296,8 @@ fn constructor_from_iter_select_unique_with() {
);

assert_eq!(
progressive.elements(),
[&Model::new(1, "AAAA"), &Model::new(2, "B")]
progressive.items(),
[Model::new(1, "AAAA"), Model::new(2, "B")]
)
}

Expand All @@ -302,18 +307,18 @@ fn append() {
let (mut inserted, mut index) = identified_vec.append(4);
assert!(inserted);
assert_eq!(index, 3);
assert_eq!(identified_vec.elements(), [&1, &2, &3, &4]);
assert_eq!(identified_vec.items(), [1, 2, 3, 4]);
(inserted, index) = identified_vec.append(2);
assert_eq!(inserted, false);
assert_eq!(index, 1);
assert_eq!(identified_vec.elements(), [&1, &2, &3, &4]);
assert_eq!(identified_vec.items(), [1, 2, 3, 4]);
}

#[test]
fn append_other() {
let mut identified_vec = SUT::from_iter([1, 2, 3]);
identified_vec.append_other([1, 4, 3, 5]);
assert_eq!(identified_vec.elements(), [&1, &2, &3, &4, &5])
assert_eq!(identified_vec.items(), [1, 2, 3, 4, 5])
}

#[test]
Expand All @@ -322,11 +327,11 @@ fn insert() {
let (mut inserted, mut index) = identified_vec.insert(0, 0);
assert!(inserted);
assert_eq!(index, 0);
assert_eq!(identified_vec.elements(), [&0, &1, &2, &3]);
assert_eq!(identified_vec.items(), [0, 1, 2, 3]);
(inserted, index) = identified_vec.insert(2, 0);
assert_eq!(inserted, false);
assert_eq!(index, 2);
assert_eq!(identified_vec.elements(), [&0, &1, &2, &3]);
assert_eq!(identified_vec.items(), [0, 1, 2, 3]);
}

#[test]
Expand Down Expand Up @@ -363,7 +368,7 @@ fn update_at_expect_panic_other_id() {
fn update_or_append() {
let mut identified_vec = SUT::from_iter([1, 2, 3]);
assert_eq!(identified_vec.update_or_append(4), None);
assert_eq!(identified_vec.elements(), [&1, &2, &3, &4]);
assert_eq!(identified_vec.items(), [1, 2, 3, 4]);
assert_eq!(identified_vec.update_or_append(2), Some(2));
}

Expand All @@ -373,18 +378,18 @@ fn update_or_insert() {
let (mut original_member, mut index) = identified_vec.update_or_insert(0, 0);
assert_eq!(original_member, None);
assert_eq!(index, 0);
assert_eq!(identified_vec.elements(), [&0, &1, &2, &3]);
assert_eq!(identified_vec.items(), [0, 1, 2, 3]);
(original_member, index) = identified_vec.update_or_insert(2, 0);
assert_eq!(original_member, Some(2));
assert_eq!(index, 2);
assert_eq!(identified_vec.elements(), [&0, &1, &2, &3])
assert_eq!(identified_vec.items(), [0, 1, 2, 3])
}

#[test]
fn remove_at_offsets() {
let mut identified_vec = SUT::from_iter([1, 2, 3]);
identified_vec.remove_at_offsets([0, 2]);
assert_eq!(identified_vec.elements(), [&2])
assert_eq!(identified_vec.items(), [2])
}

#[test]
Expand Down Expand Up @@ -480,7 +485,7 @@ fn eq() {
);

assert_eq!(
IdentifiedVecOf::tryfrom_iter_select_unique_with([Foo::new(), Foo::new()], |_| Err(
IdentifiedVecOf::try_from_iter_select_unique_with([Foo::new(), Foo::new()], |_| Err(
IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1)
),),
Err(IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1))
Expand Down

0 comments on commit 8c88b38

Please sign in to comment.