Skip to content

Commit a8cfd63

Browse files
committed
optimize clear method and impl Array up to 64 items
1 parent ac39873 commit a8cfd63

6 files changed

Lines changed: 61 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
authors = ["Davide Di Carlo <daddinuz@gmail.com>"]
33
description = "A dynamic container that combines the characteristics of a Vec and a LinkedList"
44
name = "array_list"
5-
version = "0.1.0"
5+
version = "0.2.0"
66
edition = "2021"
77
license = "MIT"
88
keywords = ["collections", "vec", "xor", "linked-list", "unrolled"]

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ or iterations are required.
1111

1212
- **Dynamic:** Combines the benefits of a `Vec` (compact, cache-friendly storage) and a `LinkedList` (efficient insertions and deletions).
1313
- **Reduced pointer overhead:** Implements a **XOR linked list**, requiring only a single pointer per node for bidirectional traversal.
14-
- **Customizable chunk size:** The size of each chunk is determined at compile time via a const generic parameter up to 32 elements.
14+
- **Customizable chunk size:** The size of each chunk is determined at compile time via a const generic parameter up to 64 elements.
1515
- **Efficient memory operations:** Splits and merges nodes dynamically, redistributing elements when necessary.
1616
- **Rich API:** Provides functionality for:
17-
- Insertions and deletions at arbitrary positions.
18-
- Access to front and back elements.
19-
- Iterators for traversal.
17+
- Insertions, deletions and access at arbitrary positions.
2018
- Index-based access with `get` methods.
19+
- Access to front and back elements.
2120

2221
## Strengths
2322

@@ -43,7 +42,7 @@ or edit your Cargo.toml manually by adding:
4342

4443
```toml
4544
[dependencies]
46-
array_list = "0.1"
45+
array_list = "0.2"
4746
```
4847

4948
## Example Usage

src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,25 @@ where
683683
/// assert_eq!(list.back(), None);
684684
/// ```
685685
pub fn clear(&mut self) {
686-
// TODO: optimize dropping nodes instead of items one at time
687-
while !self.is_empty() {
688-
self.pop_back();
686+
let mut right: Option<NonNull<Node<T, N>>> = None;
687+
let mut cursor = self.tail;
688+
689+
while let Some(mut node) = cursor {
690+
let tmp = cursor;
691+
692+
cursor = NonNull::new(
693+
(unsafe { node.as_ref().link() } ^ right.map_or(0, |p| p.as_ptr() as usize))
694+
as *mut Node<T, N>,
695+
);
696+
697+
right = tmp;
698+
699+
drop(unsafe { Box::from_raw(node.as_mut()) })
689700
}
701+
702+
self.tail = None;
703+
self.head = None;
704+
self.len = 0;
690705
}
691706

692707
/// Returns a reference to the first element of the `ArrayList`, if any.

src/node.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,12 @@ where
183183
[T; N]: Array<T, N>,
184184
{
185185
fn drop(&mut self) {
186-
while !self.is_empty() {
187-
self.len -= 1;
188-
unsafe { self.data[self.len].assume_init_drop() };
186+
for i in (0..self.len).rev() {
187+
unsafe { self.data[i].assume_init_drop() };
189188
}
189+
190+
self.link = 0;
191+
self.len = 0;
190192
}
191193
}
192194

src/sailed.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,35 @@ impl<T> Array<T, 29> for [T; 29] {}
3838
impl<T> Array<T, 30> for [T; 30] {}
3939
impl<T> Array<T, 31> for [T; 31] {}
4040
impl<T> Array<T, 32> for [T; 32] {}
41+
impl<T> Array<T, 33> for [T; 33] {}
42+
impl<T> Array<T, 34> for [T; 34] {}
43+
impl<T> Array<T, 35> for [T; 35] {}
44+
impl<T> Array<T, 36> for [T; 36] {}
45+
impl<T> Array<T, 37> for [T; 37] {}
46+
impl<T> Array<T, 38> for [T; 38] {}
47+
impl<T> Array<T, 39> for [T; 39] {}
48+
impl<T> Array<T, 40> for [T; 40] {}
49+
impl<T> Array<T, 41> for [T; 41] {}
50+
impl<T> Array<T, 42> for [T; 42] {}
51+
impl<T> Array<T, 43> for [T; 43] {}
52+
impl<T> Array<T, 44> for [T; 44] {}
53+
impl<T> Array<T, 45> for [T; 45] {}
54+
impl<T> Array<T, 46> for [T; 46] {}
55+
impl<T> Array<T, 47> for [T; 47] {}
56+
impl<T> Array<T, 48> for [T; 48] {}
57+
impl<T> Array<T, 49> for [T; 49] {}
58+
impl<T> Array<T, 50> for [T; 50] {}
59+
impl<T> Array<T, 51> for [T; 51] {}
60+
impl<T> Array<T, 52> for [T; 52] {}
61+
impl<T> Array<T, 53> for [T; 53] {}
62+
impl<T> Array<T, 54> for [T; 54] {}
63+
impl<T> Array<T, 55> for [T; 55] {}
64+
impl<T> Array<T, 56> for [T; 56] {}
65+
impl<T> Array<T, 57> for [T; 57] {}
66+
impl<T> Array<T, 58> for [T; 58] {}
67+
impl<T> Array<T, 59> for [T; 59] {}
68+
impl<T> Array<T, 60> for [T; 60] {}
69+
impl<T> Array<T, 61> for [T; 61] {}
70+
impl<T> Array<T, 62> for [T; 62] {}
71+
impl<T> Array<T, 63> for [T; 63] {}
72+
impl<T> Array<T, 64> for [T; 64] {}

0 commit comments

Comments
 (0)