//! # rc-dlist-deque - Doubly-linked list based on `std::Rc` //! //! This crate provides a doubly-linked list implementation for //! circumstances where `Rc` is suitable for managing the individual //! nodes. //! // Copyright (C) 2019-2022 Ian Jackson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . //! How to use this crate //! ===================== //! //! See the [module-level documentation](dlist/index.html) //! //! When not to use this crate //! ========================== //! //! Many programmers new to Rust look for a linked list when another //! data structure is better. Consider `Vec` and `VecDeque`. //! A doubly-linked list is good if: //! //! * You need to retain and store, long-term, a reference to the //! middle of your list, and you need quick access to the //! corresponding item, *and* //! //! * You need to be able to quickly add or remove items in the //! middle of your list, based on such references; or your //! references into the middle of the list must remain valid even //! as you add or remove other items elsewhere in the middle, *and* //! //! * Your lists might be long. //! //! In other circumstances, which is most circumstances, you //! probably wanted `Vec` or `VecDeque` instead. They have much lower //! overhead and are simpler to work with. //! //! In particular: //! //! * If you don't need to add/remove items in the middle, then a //! `Vec` or `VecDeque` works well. You can use array indices as //! your references. //! //! * If you want a `VecDeque` with stable indices, this can be //! achieved by maintaining a signed counter of elements //! pushed/popped at the start, as done by //! [`vecdeque-stableix`](https://lib.rs/crates/vecdeque-stableix). //! //! * If you don't need to retain long-term references to the middle //! of the list, then you can use a vector. Adding or removing //! items in the middle does mean copying to shuffle items up or //! down, but if you aren't keeping a reference to the modification //! location you will have to walk along to find the right place //! anyway. //! //! Note that there are a number of deque-ish alternatives to //! std::VecDeque which are not doubly linked lists and //! do not aim to support modification in the middle of the list, //! at least some of which seem like they might be useful. //! Search crates.io for deque, //! and also consider blist. //! //! Other doubly-linked list libraries //! ================================== //! //! If you do need a doubly-linked list, but do not need your items to //! be on more than one list at once, consider //! [`generational_token_list`](https://lib.rs/crates/generational_token_list) //! instead. //! It has a much simpler ownership model and will be faster too. //! //! If you want each item to be able to be on more than one list at //! once (perhaps even selecting the linked list link within each //! node dynamically at runtime), then this crate //! `rc-dlist-deque` may be what you want. //! //! Survey of available Rust doubly linked lists, compared to VecDeque //! ------------------------------------------------------------------ //! //! (This table is rendered rather poorly by Rustdoc, lib.rs, etc. //! Use the //! [correctly formatted version](https://www.chiark.greenend.org.uk/~ianmdlvl/rc-dlist-deque/) //! built standalone with pandoc, instead.) //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //! //!
Crate or moduleRef to element (aka Index or Cursor)Other misuse possible, and consequencesList owns (T is element type)Cost of editing in middleMemory localityElement can be on multiple listsAPI complete­nessSend/​SyncSafetyComments
TypeAfter altering list at front/​backAfter altering list in middleAfter removing ref'd elementGiven ref to elemWithout ref to elem
Choose one of these approaches
std VecDeque
(or another deque library)
usize
position
wrong element if alteration at startwrong element (or None or panic)Rich API can invalidate indicesTO(n)O(n)sequential-++YesGoodUse if you don't need persistent cursors
vecdeque-stableixi64 isize ...validnot supportedIndex can overflow, giving panics.sequential-+YesSafeConsider if you need to modify only at ends
rc-dlist-dequePointer​<L,S>validvalidvalidSpecify wrong list for alteration: debug_assert, "tangling"Rc<T>O(1)scattered to heapYes+-SafeConsider if you need each element on multiple lists
generational_token_listItemToken
slot+gen.
validvalidreliably None ([] panics)Specify wrong list for access or alteration: Maybe detected by luck, giving None, otherwise wrong elementTrandom order in vector-++YesSafe (exc.​IterMut)Otherwise, use one of these
dlv-listIndex​<T>
slot+gen.
++
Use generational_token_list or dlv-list instead of any of the following
chainlinkIndex
slot+gen.
validvalidreliably NoneSpecify wrong list for access or alteration: Maybe detected by luck, giving None, otherwise wrong elementTO(1)O(n)random order in vector--YesSafeNo IterMut; otherwise plausible
indexlistIndex​<T>
slot+gen.
-- -YesSafe
index_listIndex
slot
None (or panic), or (later) wrong element-+YesSafe
slabigatorusize
slot
TO(1)random order in vector--Yesuses unsafeusize slot indices are a poor API choice
array-linked-listusize
slot
TO(1)random order in vector-+Yesneedless unsafe
im/im-rc Vectorusize
position
wrong element if alteration at startwrong element (or None or panic)TO(log(n))chunks in heap-+imuses unsafeusize position indices are a correctness hazard
skip-linked-listTO(log(n))scattered to heap---uses unsafe
cddlCursor, DoubleCursorvalidvalidnot supportedTO(1)scattered to heap-- - --uses unsafeStrange and limited API
Use VecDeque instead of any of the following
intrusive-collections linked_​listCursor​[Mut]Mutation not possible while //! any other cursor exists (prevented by lifetimes). This almost entirely defeats the point of using a doubly //! linked list.VariousO(1)O(n)scattered to heapYes+Yesuses unsafeIf this API is enough for you, use VecDeque instead
cordyceps ListCursor​[Mut] (roughly)unsafe impl Handlescattered to heap-Yesunsafe to use!
tsil_cevCursor​[Mut]Trandom order in vector-YesSafe (exc.​IterMut)
ixlistCursorT-+Yesneedless unsafe
linked-listCursorTscattered to heap-Yesuses unsafe
cyclic_listCursor​[Mut]Tscattered to heapYesuses unsafe
pin-listCursor​[Mut]Complicateduser provides Pin<&mut Node>-Yesuses unsafe
key-node-listCursor​[Mut]TO(log(n))in vector (via HashMap)--Yes
std LinkedListNot supported. This defeats the point of using a doubly //! linked list.Tn/ascattered to heap-Yes
doublyTscattered to heap-Yesuses unsafe
linked_lists_rs / rust_linked_listTscattered to heap-Yesuses unsafe
xor_listTscattered to heap--uses unsafe
index_queueTsequential-- -YesReimplementation of VecDeque
//!

//! //! It can be hard to implement `IterMut` without using //! `unsafe` , so no criticism is intended for those crates //! that use unsafe for this. //! //! ### Not considered //! //! #### Single-ended, or special purpose) //! //! Not considered here because they are a single-ended list //! (so there is little reason to use anything but a `Vec`); //! or because they are special purpose: //! //! `c_linked_list`, //! `char-list`, //! `concurrent_list`, //! `cons-list`, //! `doubly_linked_list`, //! `fplist`, //! `functional_list`, //! `fwdlist`, //! `im-list`, //! `linked_hash_map`, //! `linked_hash_set`, //! `linked_list_allocator`, //! `linked-tail-list`, //! `list`, //! `moonlight_collections`, //! `nerdondon-hopscotch`, //! `nt_list`, //! `persistent-list`, //! `rose_bloom`, //! `secured_linked_list`, //! `simple-stack`, //! `stack_list`, //! `static-linkedlist`, //! `weak-list2`, //! `uell`, //! `unrolled_linked_list`. //! //! #### Not intended (or not suitable) for general use //! //! Not considered here because of poor or missing docs, very poor API, //! Nightly-only, won't build, //! or other indications that //! they're not intended (or suitable) for use by the world in general: //! //! `cll`, //! `clist`, //! `double_linkedlist`, //! `dynalist`, //! `ilist`, //! `linkedlist`, //! `rs_trie`, //! `static-linkedlist`, //! `matecito-dll`. //! //! ### Survey colophon //! //! Last updated, and last resurveyed, November 2022. //! If I do a future survey, I may look at only the most popular crates. //! Please let me know if you think your library ought to be //! listed in the section "Choose one of these approaches". //! //! Changelog and colophon for `rc-dlist-deque` //! =========================================== //! //! Minimum Supported Rust Version is 1.31.0. //! MSRV policy: //! We do not anticipate changing the MSRV in the foreseeable future. //! If we do, it will be at least a minor version bump. //! We will not increase the MSRV beyond that available in Debian oldstable. //! //! #### 1.1.2 //! //! * Minor updates to doubly linked list survey (crate level docs). //! * No API or implementation changes. //! //! #### 1.1.1 //! //! * Minor updates to doubly linked list survey (crate level docs). //! * No API or implementation changes. //! //! #### 1.1.0 //! //! * **MSRV 1.31.0** (was 1.30.0) //! * Change to 2018 edition. //! * Add this Changelog. //! * Update doubly linked list survey (crate level docs). //! * Changes to documentation generation and release arrangements. //! //! #### 1.0.0 //! //! * Documentation. No API or implemnetation changes. //! //! #### 0.1.0 //! //! * First public release. //! //! `rc-dlist-deque` is Copyright 2019-2022 Ian Jackson. //! GNU GPLv3+; NO WARRANTY. pub mod dlist;