Skip to content

Commit 1796f03

Browse files
committed
Add methods new(), with_capacity(), len() and shrink_to_fit() to List trait
Also implement the methods for `UniqueList` and `RemovableList`
1 parent d442559 commit 1796f03

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/list/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ mod unique;
44
/// Defines the shared interface between the unique list (which is effectively an insert-ordered
55
/// Set) and the unique list which supports removals.
66
pub trait List {
7+
fn new() -> Self;
8+
fn with_capacity(capacity: usize) -> Self;
9+
fn len(&self) -> usize;
10+
fn shrink_to_fit(&mut self);
711
fn append_if_new(&mut self, item: String);
812
fn merge(&mut self, other: Self);
913
fn merge_from(&mut self, other: &Self);

src/list/removable.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ impl From<RemovableList> for Vec<String> {
6767
}
6868

6969
impl List for RemovableList {
70+
#[inline]
71+
fn new() -> Self {
72+
Self::default()
73+
}
74+
75+
#[inline]
76+
fn with_capacity(capacity: usize) -> Self {
77+
Self {
78+
items: Vec::with_capacity(capacity),
79+
negations: vec![],
80+
}
81+
}
82+
83+
#[inline]
84+
fn len(&self) -> usize {
85+
self.items.len()
86+
}
87+
88+
#[inline]
89+
fn shrink_to_fit(&mut self) {
90+
self.items.shrink_to_fit();
91+
self.negations.shrink_to_fit();
92+
}
93+
7094
/// Appends or removes item from list
7195
///
7296
/// Regular strings are inserted in the list if they're not present yet. When `item` is

src/list/unique.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ impl From<UniqueList> for Vec<String> {
4444
}
4545

4646
impl List for UniqueList {
47+
#[inline]
48+
fn new() -> Self {
49+
Self::default()
50+
}
51+
52+
#[inline]
53+
fn with_capacity(capacity: usize) -> Self {
54+
Self {
55+
items: Vec::with_capacity(capacity),
56+
}
57+
}
58+
59+
#[inline]
60+
fn len(&self) -> usize {
61+
self.items.len()
62+
}
63+
64+
#[inline]
65+
fn shrink_to_fit(&mut self) {
66+
self.items.shrink_to_fit();
67+
}
68+
4769
/// Appends item to list if it's not present yet
4870
fn append_if_new(&mut self, item: String) {
4971
if item_pos(&self.items, &item).is_none() {

0 commit comments

Comments
 (0)