1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
//! # 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 Ian Jackson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. //! 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. //! //! * 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 //! <code>std::VecDeque</code> 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 <kbd>deque</kbd>, //! and also consider <code>blist</code>. //! //! 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 `dlv-list-rs` 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 //! <code>rc-dlist-deque</code> may be what you want. //! //! Survey of available Rust doubly linked lists, compared to VecDeque //! ------------------------------------------------------------------ //! //! <style> //! table.dlist_survey th, //! table.dlist_survey td { //! border-top-color: black; //! border-bottom-color: black; //! border-left-color: black; //! border-right-color: black; //! border: 1px solid; //! text-align: center; //! vertical-align: middle; //! } //! </style> //! <table rules=all class="dlist_survey"> //! <tr> //! <th rowspan=2>Crate or module</th> //! <th colspan=4>Ref to element (aka Index or Cursor)</th> //! <th rowspan=2>Other misuse possible, and consequences</th> //! <th rowspan=2>List owns (<code>T</code> is element type)</th> //! <th colspan=2>Cost of editing in middle</th> //! <th rowspan=2>Memory locality</th> //! <th rowspan=2>Element can be on multiple lists</th> //! <th rowspan=2>API complete­ness</th> //! <th rowspan=2><code>Send/​Sync</code></th> //! <th rowspan=2>Safety</th> //! <th rowspan=2>Comments</th> //! </tr> //! <tr> //! <th>Type</th> //! <th>After altering list at front/​back</th> //! <th>After altering list in middle</th> //! <th>After removing ref'd element</th> //! <th>Given ref to elem</th> //! <th>Without ref to elem</th> //! </tr> //! <tr> //! <th colspan=14>Choose one of these approaches</th> //! </tr> //! <tr> //! <td><code>std VecDeque</code></td> //! <td><code>usize</code></td> //! <td>wrong element if alteration at start</td> //! <td>wrong element</td> //! <td>wrong element</td> //! <td rowspan=2>Rich API can invalidate indices</td> //! <td><code>T</code></td> //! <td>O(n)</td> //! <td>O(n)</td> //! <td>sequential</td> //! <td>-</td> //! <td>++</td> //! <td>Yes</td> //! <td>Good</td> //! <td>Use if you don't need persistent cursors</td> //! </tr> //! <tr> //! <td><code>std VecDeque</code> with front counter</td> //! <td><code>isize</code></td> //! <td>valid</td> //! <td>wrong element</td> //! <td>wrong element</td> //! <td><code>T</code></td> //! <td>O(n)</td> //! <td>O(n)</td> //! <td>sequential</td> //! <td>-</td> //! <td>DIY</td> //! <td>Yes</td> //! <td>Good</td> //! <td>Consider if you need to modify only at ends</td> //! </tr> //! <tr> //! <td><code>rc-dlist-deque</code></td> //! <td><code>Pointer​<L,S></code></td> //! <td>valid</td> //! <td>valid</td> //! <td>valid</td> //! <td>Specify wrong list for alteration: <code>debug_assert</code>, "tangling"</td> //! <td><code>Rc<T></code></td> //! <td>O(1)</td> //! <td>O(n)</td> //! <td>scattered to heap</td> //! <td>Yes</td> //! <td>+</td> //! <td>-</td> //! <td>Safe</td> //! <td>Consider if you need each element on multiple lists</td> //! </tr> //! <tr> //! <td><code>dlv-list-rs</code></td> //! <td><code>Index​<T></code></td> //! <td rowspan=2>valid</td> //! <td rowspan=2>valid</td> //! <td rowspan=2>gives <code>None</code> (<code>[]</code> panics)</td> //! <td rowspan=2>Specify wrong list for access or alteration: Maybe detected by luck, giving <code>None</code>, otherwise wrong element</td> //! <td><code>T</code></td> //! <td>O(1)</td> //! <td>O(n)</td> //! <td rowspan=2>random order in vector</td> //! <td>-</td> //! <td>+</td> //! <td>Yes</td> //! <td>Safe (exc.​<code>IterMut</code>)</td> //! <td>Otherwise, use this</td> //! </tr> //! <tr> //! <td><code>indexlist</code></td> //! <td><code>Index​<T></code></td> //! <td><code>T</code></td> //! <td>O(1)</td> //! <td>O(n)</td> //! <td>-</td> //! <td>--</td> //! <td>Yes</td> //! <td>Safe</td> //! <td>No <code>IterMut</code></td> //! </tr> //! <tr> //! <th colspan=14>Use VecDeque instead of any of the following</th> //! </tr> //! <tr> //! <td><code>intrusive_​collections linked_​list</code></td> //! <td><code>Cursor</code>​[<code>Mut</code>]</td> //! <td rowspan=3 colspan=4>Mutation not possible while //! any other cursor exists (prevented by lifetimes). This almost entirely defeats the point of using a doubly //! linked list.</td> //! <td>Various</td> //! <td>O(1)</td> //! <td>O(n)</td> //! <td>scattered to heap</td> //! <td>Yes</td> //! <td>+</td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! <td rowspan=5>If this API is enough for you, use <code>VecDeque</code> instead</td> //! </tr> //! <tr> //! <td><code>ixlist</code></td> //! <td><code>Cursor<code/></td> //! <td><code>T</code></td> //! <td>O(1)</td> //! <td>O(n)</td> //! <td>random order in vector</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td>Safe (exc.​<code>IterMut</code>)</td> //! </tr> //! <tr> //! <td><code>linked-list</code></td> //! <td><code>Cursor</code></td> //! <td><code>T</code></td> //! <td>O(1)</td> //! <td>O(n)</td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! <tr> //! <td><code>std LinkedList</code></td> //! <td rowspan=2 colspan=4>Not supported. This defeats the point of using a doubly //! linked list.</td> //! <td></td> //! <td><code>T</code></td> //! <td></td> //! <td>O(n)</td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td></td> //! </tr> //! <tr> //! <td><code>doubly</code></td> //! <td></td> //! <td><code>T</code></td> //! <td></td> //! <td>O(n)</td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! </table> //! <p> //! Not considered here because single-ended, or special purpose): //! <code>index_queue</code>, //! <code>c_linked_list</code>, //! <code>linked_list_allocator</code>, //! <code>linked-tail-list</code>, //! <code>static-linkedlist</code>. //! //! Not considered here because no Rustdocs, does not build with modern Rust: //! <code>dynalist</code>. //! //! It can be hard to implement <code>IterMut</code> without using //! <code>unsafe</code>, so no criticism is intended for those crates //! that use unsafe for this. //! //! Last updated May 2019. pub mod dlist;