Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 19, 2024
1 parent 95c9d1a commit 43511b4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 20 deletions.
25 changes: 24 additions & 1 deletion src/extensions/collections/linked_list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp::Ordering;
use std::collections::{HashMap, LinkedList};
use std::collections::{BTreeSet, HashMap, LinkedList};
use std::fmt::Display;
use std::hash::Hash;

Expand Down Expand Up @@ -322,6 +322,29 @@ impl<Item> Sequence<Item> for LinkedList<Item> {
combinations_multi(self.iter(), k)
}

#[inline]
fn delete_at(self, index: usize) -> Self
{
let size = self.len();
if index >= size {
panic!(r#"removal index (is {index:?}) should be < len (is {size:?})"#)
}
self.into_iter().enumerate().filter_map(|(i, x)| if i == index { None } else { Some(x) }).collect()
}

#[inline]
fn delete_at_multi(self, indices: impl IntoIterator<Item = usize>) -> Self
{
let size = self.len();
let positions: BTreeSet<usize> = BTreeSet::from_iter(indices.into_iter().map(|index| {
if index >= size {
panic!(r#"removal index (is {index:?}) should be < len (is {size:?})"#)
};
index
}));
self.into_iter().enumerate().filter_map(|(i, x)| if positions.contains(&i) { None } else { Some(x) }).collect()
}

#[inline]
fn init(mut self) -> Self {
let _unused = self.pop_back();
Expand Down
29 changes: 29 additions & 0 deletions src/extensions/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ impl<Item> Collectible<Item> for Vec<Item> {
Item: PartialEq,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
let size = self.len();
if let Some(index) = self.iter().position(|x| x == element) {
if index >= size {
panic!(r#"replacement index (is {index:?}) should be < len (is {size:?})"#)
}
self[index] = replacement;
}
self
Expand Down Expand Up @@ -360,6 +364,31 @@ impl<Item> Sequence<Item> for Vec<Item> {
combinations_multi(self.iter(), k)
}

#[inline]
fn delete_at(mut self, index: usize) -> Self
{
let _unused = self.remove(index);
self
}

fn delete_at_multi(mut self, indices: impl IntoIterator<Item = usize>) -> Self
{
let mut deleted_indices = Vec::<usize>::from_iter(indices);
let mut last = usize::MAX;
let size = self.len();
deleted_indices.sort_unstable();
for index in deleted_indices.into_iter().rev() {
if index >= size {
panic!(r#"removal index (is {index:?}) should be < len (is {size:?})"#)
}
if index != last {
let _unused = self.remove(index);
last = index;
}
}
self
}

#[inline]
fn init(mut self) -> Self {
if !self.is_empty() {
Expand Down
30 changes: 29 additions & 1 deletion src/extensions/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,16 @@ impl<Item> Collectible<Item> for VecDeque<Item> {
powerset(self.iter())
}

#[inline]
fn substitute(mut self, element: &Item, replacement: Item) -> Self
where
Item: PartialEq,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
let size = self.len();
if let Some(index) = self.iter().position(|x| x == element) {
if index >= size {
panic!(r#"replacement index (is {index:?}) should be < len (is {size:?})"#)
}
self[index] = replacement;
}
self
Expand Down Expand Up @@ -376,6 +379,31 @@ impl<Item> Sequence<Item> for VecDeque<Item> {
combinations_multi(self.iter(), k)
}

#[inline]
fn delete_at(mut self, index: usize) -> Self
{
let _unused = self.remove(index);
self
}

fn delete_at_multi(mut self, indices: impl IntoIterator<Item = usize>) -> Self
{
let mut deleted_indices = Vec::<usize>::from_iter(indices);
let mut last = usize::MAX;
let size = self.len();
deleted_indices.sort_unstable();
for index in deleted_indices.into_iter().rev() {
if index >= size {
panic!(r#"removal index (is {index:?}) should be < len (is {size:?})"#)
}
if index != last {
let _unused = self.remove(index);
last = index;
}
}
self
}

#[inline]
fn init(mut self) -> Self {
if !self.is_empty() {
Expand Down
20 changes: 2 additions & 18 deletions src/extensions/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,8 @@ pub trait Sequence<Item> {
/// assert_eq!(a.delete_at(1), vec![1, 3]);
/// # let a = a_source.clone();
/// assert_eq!(a.delete_at(2), vec![1, 2]);
/// assert_eq!(e.delete_at(0), vec![]);
///
/// # let a = a_source.clone();
/// assert_eq!(a.delete_at(3), vec![1, 2, 3]);
/// ```
#[inline]
fn delete_at(self, index: usize) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
self.into_iter().enumerate().filter_map(|(i, x)| if i == index { None } else { Some(x) }).collect()
}
fn delete_at(self, index: usize) -> Self;

/// Creates a new sequence by omitting elements at specified indices
/// in this sequence.
Expand All @@ -403,13 +393,7 @@ pub trait Sequence<Item> {
///
/// assert_eq!(a.delete_at_multi(vec![0, 2]), vec![2]);
/// # let a = a_source.clone();
/// assert_eq!(a.delete_at_multi(vec![1, 3]), vec![1, 3]);
/// # let a = a_source.clone();
/// assert_eq!(a.delete_at_multi(vec![0, 1, 2, 3]), vec![]);
///
/// assert_eq!(e.delete_at_multi(vec![1, 2]), vec![]);
/// # let a = a_source.clone();
/// assert_eq!(a.delete_at_multi(vec![3, 4]), vec![1, 2, 3]);
/// assert_eq!(a.delete_at_multi(vec![0, 1, 2]), vec![]);
/// ```
#[inline]
fn delete_at_multi(self, indices: impl IntoIterator<Item = usize>) -> Self
Expand Down

0 comments on commit 43511b4

Please sign in to comment.