orx-linked-list
An efficient and recursive singly and doubly linked list implementation.
Variants and Time Complexity of Methods
type SinglyLinkedList<'a, T> = List<'a, Singly, T>;
type DoublyLinkedList<'a, T> = List<'a, Doubly, T>;
Time Complexity of Methods
Method | Time Complexity |
---|---|
access to front and back of the list | O(1) |
push to front and back (Doubly only) of the list |
O(1) |
pop from front and back (Doubly only) of the list |
O(1) |
insert at an arbitrary position | O(n) |
remove from an arbitrary position | O(n) |
append another list to the front or back of the list | O(1) |
retain elements by a predicate | O(n) |
retain and collect remove elements | O(n) |
iteration forwards or backwards (only Doubly ) |
O(n) |
Examples
use *;
let _list: = new;
let _list = new;
let _list: = new;
let _list = new;
let mut list = from_iter;
assert_eq!;
assert_eq!;
assert!;
assert!;
assert_eq!;
assert_eq!;
list.push_back;
list.push_front;
assert!;
let other = from_iter;
list.append_back;
assert!;
let other = from_iter;
list.append_front;
assert!;
list.retain;
assert!;
let mut odds = vec!;
let mut collect_odds = ;
list.retain_collect;
assert!;
assert!;
Internal Features
orx_linked_list::List
makes use of the safety guarantees and efficiency features of SelfRefCol.
SelfRefCol
constructs its safety guarantees around the fact that all references will be among elements of the same collection. By preventing bringing in external references or leaking out references, it is safe to build the self referential collection with regular&
references.- With careful encapsulation,
SelfRefCol
prevents passing in external references to the list and leaking within list node references to outside. Once this is established, it provides methods to easily mutate inter list node references. These features allowed a very convenient implementation of the linked list in this crate with almost no use of theunsafe
keyword, no read or writes through pointers and no access by indices. Compared to thestd::collections::LinkedList
implementation, it can be observed thatorx_linked_list::List
is a much higher level implementation. - Furthermore,
orx_linked_list::List
is significantly faster than the standard linked list. One of the main reasons for this is the feature ofSelfRefCol
keeping all close to each other rather than at arbitrary locations in memory which leads to a better cache locality.
Benchmarks
Mutation Ends
You may see the benchmark at benches/mutation_ends.rs.
This benchmark compares time performance of calls to push_front
, push_back
, pop_front
and pop_back
methods.
Iteration
You may see the benchmark at benches/iter.rs.
This benchmark compares time performance of iteration through the iter
method.
License
This library is licensed under MIT license. See LICENSE for details.