Skip to content

Commit

Permalink
Add methods new(), with_capacity(), len() and shrink_to_fit()
Browse files Browse the repository at this point in the history
… to `List` trait

Also implement the methods for `UniqueList` and `RemovableList`
  • Loading branch information
simu committed Aug 30, 2023
1 parent d442559 commit 1796f03
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ mod unique;
/// Defines the shared interface between the unique list (which is effectively an insert-ordered
/// Set) and the unique list which supports removals.
pub trait List {
fn new() -> Self;
fn with_capacity(capacity: usize) -> Self;
fn len(&self) -> usize;
fn shrink_to_fit(&mut self);
fn append_if_new(&mut self, item: String);
fn merge(&mut self, other: Self);
fn merge_from(&mut self, other: &Self);
Expand Down
24 changes: 24 additions & 0 deletions src/list/removable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ impl From<RemovableList> for Vec<String> {
}

impl List for RemovableList {
#[inline]
fn new() -> Self {
Self::default()
}

#[inline]
fn with_capacity(capacity: usize) -> Self {
Self {
items: Vec::with_capacity(capacity),
negations: vec![],
}
}

#[inline]
fn len(&self) -> usize {
self.items.len()
}

#[inline]
fn shrink_to_fit(&mut self) {
self.items.shrink_to_fit();
self.negations.shrink_to_fit();
}

/// Appends or removes item from list
///
/// Regular strings are inserted in the list if they're not present yet. When `item` is
Expand Down
22 changes: 22 additions & 0 deletions src/list/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ impl From<UniqueList> for Vec<String> {
}

impl List for UniqueList {
#[inline]
fn new() -> Self {
Self::default()
}

#[inline]
fn with_capacity(capacity: usize) -> Self {
Self {
items: Vec::with_capacity(capacity),
}
}

#[inline]
fn len(&self) -> usize {
self.items.len()
}

#[inline]
fn shrink_to_fit(&mut self) {
self.items.shrink_to_fit();
}

/// Appends item to list if it's not present yet
fn append_if_new(&mut self, item: String) {
if item_pos(&self.items, &item).is_none() {
Expand Down

0 comments on commit 1796f03

Please sign in to comment.