-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: guoweikang <[email protected]>
- Loading branch information
1 parent
5d853ff
commit 02e5d46
Showing
3 changed files
with
163 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# LinkedList | ||
|
||
Linked lists that supports arbitrary removal in constant time. | ||
|
||
It is based on the linked list implementation in [Rust-for-Linux][1]. | ||
|
||
[1]: https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/linked_list.rs | ||
|
||
In more general use cases, shoud not use [`RawList`] directly, | ||
suggest use smart pointers of nodes and move ownership of smart | ||
pointers to List. | ||
|
||
## Examples | ||
|
||
```rust | ||
use linked_list::{GetLinks, Links, List}; | ||
|
||
type InnerType = usize; | ||
|
||
pub struct ExampleNode { | ||
pub inner: InnerType, | ||
links: Links<Self>, | ||
} | ||
|
||
impl GetLinks for ExampleNode { | ||
type EntryType = Self; | ||
|
||
fn get_links(t: &Self) -> &Links<Self> { | ||
&t.links | ||
} | ||
} | ||
|
||
impl ExampleNode { | ||
fn new(inner: InnerType) -> Self { | ||
Self { | ||
inner, | ||
links: Links::new() | ||
} | ||
} | ||
|
||
fn inner(&self) -> &InnerType { | ||
&self.inner | ||
} | ||
} | ||
|
||
let node1 = Box::new(ExampleNode::new(0)); | ||
let node2 = Box::new(ExampleNode::new(1)); | ||
let mut list = List::<Box<ExampleNode>>::new(); | ||
|
||
list.push_back(node1); | ||
list.push_back(node2); | ||
|
||
//Support Iter | ||
for (i,e) in list.iter().enumerate() { | ||
assert!(*e.inner() == i); | ||
} | ||
|
||
// Pop drop | ||
assert!(*list.pop_front().unwrap().inner() == 0); | ||
assert!(*list.pop_front().unwrap().inner() == 1); | ||
|
||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters