Skip to content

Commit

Permalink
Merge pull request #3 from seekstar/with-capacity
Browse files Browse the repository at this point in the history
with_capacity
  • Loading branch information
Fairglow authored Nov 6, 2023
2 parents 8692fe0 + 709d2e8 commit d8791d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ impl<T> IndexList<T> {
pub fn new() -> Self {
Default::default()
}
/// Creates an empty `IndexList` with at least the specified capacity.
///
/// Example:
/// ```rust
/// use index_list::IndexList;
/// let list = IndexList::<u64>::with_capacity(233);
/// ```
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
IndexList {
elems: Vec::with_capacity(capacity),
nodes: Vec::with_capacity(capacity),
used: ListEnds::new(),
free: ListEnds::new(),
size: 0,
}
}
/// Returns the current capacity of the list.
///
/// This value is always greater than or equal to the length.
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn test_single_element() {
fn insert_remove_variants() {
let count = 256;
let mut rng = rand::thread_rng();
let mut list = IndexList::<u64>::new();
let mut list = IndexList::<u64>::with_capacity(count);
let mut numbers: HashSet<u64> = HashSet::with_capacity(count);
let mut indexes: Vec<u32> = Vec::with_capacity(count);
for _ in 0..8 {
Expand Down

0 comments on commit d8791d0

Please sign in to comment.