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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
//! # 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 <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, 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 //! <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 <a href="https://lib.rs/crates/blist"><code>blist</code></a>. //! //! 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.) //! //! <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><a href="https://doc.rust-lang.org/std/collections/struct.VecDeque.html"><code>std VecDeque</code></a><br>(or another deque library)</td> //! <td><code>usize</code><br>position</td> //! <td>wrong element if alteration at start</td> //! <td colspan=2>wrong element (or <code>None</code> or panic)</td> //! <td>Rich API can invalidate indices</td> //! <td rowspan=2><code>T</code></td> //! <td rowspan=2>O(n)</td> //! <td rowspan=5>O(n)</td> //! <td>sequential</td> //! <td>-</td> //! <td>++</td> //! <td>Yes</td> //! <td>Good</td> //! <td><strong>Use if you don't need persistent cursors</strong></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/vecdeque-stableix"><code>vecdeque-stableix</code></a></td> //! <td><code>i64 isize</code> ...</td> //! <td>valid</td> //! <td colspan=2>not supported</td> //! <td>Index can overflow, giving panics.</td> //! <td>sequential</td> //! <td>-</td> //! <td>+</td> //! <td>Yes</td> //! <td>Safe</td> //! <td>Consider if you need to modify only at ends</td> //! </tr> //! <tr> //! <td><a href="dlist/index.html"><code>rc-dlist-deque</code></a></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 rowspan=3>O(1)</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><a href="https://lib.rs/crates/generational_token_list"><code>generational_token_list</code></a></td> //! <td><code>ItemToken</code><br>slot+gen.</td> //! <td rowspan=2>valid</td> //! <td rowspan=2>valid</td> //! <td rowspan=2>reliably <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 rowspan=2><code>T</code></td> //! <td rowspan=2>random order in vector</td> //! <td rowspan=2>-</td> //! <td>++</td> //! <td rowspan=2>Yes</td> //! <td rowspan=2>Safe (exc.​<code>IterMut</code>)</td> //! <td rowspan=2><strong>Otherwise, use one of these</strong></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/dlv-list"><code>dlv-list</code></a></td> //! <td><code>Index​<T></code><br>slot+gen.</td> //! <td>++</td> //! </tr> //! <tr> //! <th colspan=14>Use <code>generational_token_list</code> or <code>dlv-list</code> instead of any of the following</th> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/chainlink"><code>chainlink</code></a></td> //! <td><code>Index</code><br>slot+gen.</td> //! <td rowspan=5>valid</td> //! <td rowspan=5>valid</td> //! <td rowspan=2>reliably <code>None</code></td> //! <td rowspan=7>Specify wrong list for access or alteration: Maybe detected by luck, giving <code>None</code>, otherwise wrong element</td> //! <td rowspan=3><code>T</code></td> //! <td rowspan=3>O(1)</td> //! <td rowspan=8>O(n)</td> //! <td rowspan=3>random order in vector</td> //! <td>-</td> //! <td>-</td> //! <td>Yes</td> //! <td>Safe</td> //! <td rowspan=2>No <code>IterMut</code>; otherwise plausible</td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/indexlist"><code>indexlist</code></a></td> //! <td><code>Index​<T></code><br>slot+gen.</td> //! <td>-</td> //! <td>- -</td> //! <td>Yes</td> //! <td>Safe</td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/index_list"><code>index_list</code></a></td> //! <td><code>Index</code><br>slot</td> //! <td rowspan=3><code>None</code> (or panic), or (later) wrong element</td> //! <td>-</td> //! <td>+</td> //! <td>Yes</td> //! <td>Safe</td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/slabigator"><code>slabigator</code></a></td> //! <td><code>usize</code><br>slot</td> //! <td><code>T</code></td> //! <td>O(1)</td> //! <td>random order in vector</td> //! <td>-</td> //! <td>-</td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! <td rowspan=2><code>usize</code> slot indices are a poor API choice</td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/array-linked-list"><code>array-linked-list</code></a></td> //! <td><code>usize</code><br>slot</td> //! <td><code>T</code></td> //! <td>O(1)</td> //! <td>random order in vector</td> //! <td>-</td> //! <td>+</td> //! <td>Yes</td> //! <td>needless <code>unsafe</code></td> //! </tr> //! <tr> //! <td><code><a href="https://lib.rs/crates/im">im</a>/<a href="https://lib.rs/crates/im-rc">im-rc</a> <a href="https://docs.rs/im/latest/im/struct.Vector.html">Vector</a></code></td> //! <td rowspan=2><code>usize</code><br>position</td> //! <td rowspan=2>wrong element if alteration at start</td> //! <td rowspan=2 colspan=2>wrong element (or <code>None</code> or panic)</td> //! <td><code>T</code></td> //! <td>O(log(n))</td> //! <td>chunks in heap</td> //! <td>-</td> //! <td>+</td> //! <td><code>im</code></td> //! <td>uses <code>unsafe</code></td> //! <td rowspan=2><code>usize</code> position indices are a correctness hazard</td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/skip-linked-list"><code>skip-linked-list</code></a></td> //! <td><code>T</code></td> //! <td>O(log(n))</td> //! <td>scattered to heap</td> //! <td>-</td> //! <td>-</td> //! <td>-</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/cddl"><code>cddl</code></a></td> //! <td><code>Cursor</code>, <code>DoubleCursor</code></td> //! <td>valid</td> //! <td>valid</td> //! <td>not supported</td> //! <td></td> //! <td><code>T</code></td> //! <td>O(1)</td> //! <td>scattered to heap</td> //! <td>-</td> //! <td>- - -</td> //! <td>-</td> //! <td>uses <code>unsafe</code></td> //! <td>Strange and limited API</td> //! </tr> //! <tr> //! <th colspan=14>Use VecDeque instead of any of the following</th> //! </tr> //! <tr> //! <td><code><a href="https://lib.rs/crates/intrusive-collections"><code>intrusive-collections</a> <a href="https://docs.rs/intrusive-collections/latest/intrusive_collections/linked_list/index.html">linked_​list</a></code></td> //! <td><code>Cursor</code>​[<code>Mut</code>]</td> //! <td rowspan=8 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 rowspan=7>O(1)</td> //! <td rowspan=13>O(n)</td> //! <td>scattered to heap</td> //! <td>Yes</td> //! <td>+</td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! <td rowspan=12>If this API is enough for you, use <code>VecDeque</code> instead</td> //! </tr> //! <tr> //! <td><code><a href="https://lib.rs/crates/cordyceps">cordyceps</a> <a href="https://docs.rs/cordyceps/latest/cordyceps/struct.List.html">List</a></code></td> //! <td><code>Cursor</code>​[<code>Mut</code>] (roughly)</td> //! <td><code>unsafe impl Handle</code></td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td><code>unsafe</code> to use!</td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/tsil_cev"><code>tsil_cev</code></a></td> //! <td><code>Cursor</code>​[<code>Mut</code>]</td> //! <td><code>T</code></td> //! <td rowspan=2>random order in vector</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td>Safe (exc.​<code>IterMut</code>)</td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/ixlist"><code>ixlist</code></a></td> //! <td><code>Cursor<code/></td> //! <td><code>T</code></td> //! <td>-</td> //! <td>+</td> //! <td>Yes</td> //! <td>needless <code>unsafe</code></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/linked-list"><code>linked-list</code></a></td> //! <td><code>Cursor</code></td> //! <td><code>T</code></td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/cyclic_list"><code>cyclic_list</code></a></td> //! <td><code>Cursor</code>​[<code>Mut</code>]</td> //! <td><code>T</code></td> //! <td>scattered to heap</td> //! <td></td> //! <td></td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/pin-list"><code>pin-list</code></a></td> //! <td><code>Cursor</code>​[<code>Mut</code>]</td> //! <td>Complicated</td> //! <td>user provides <code>Pin<&mut Node></code></td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/key-node-list"><code>key-node-list</code></a></td> //! <td><code>Cursor</code>​[<code>Mut</code>]</td> //! <td><code>T</code></td> //! <td>O(log(n))</td> //! <td>in vector (via <code>HashMap</code>)</td> //! <td>-</td> //! <td>-</td> //! <td>Yes</td> //! <td></td> //! </tr> //! <tr> //! <td><a href="https://doc.rust-lang.org/std/collections/struct.LinkedList.html"><code>std LinkedList</code></a></td> //! <td rowspan=5 colspan=4>Not supported. This defeats the point of using a doubly //! linked list.</td> //! <td></td> //! <td><code>T</code></td> //! <td rowspan=5>n/a</td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/doubly"><code>doubly</code></a></td> //! <td></td> //! <td><code>T</code></td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/linked_lists_rs"><code>linked_lists_rs</code></a> / <a href="https://lib.rs/crates/rust_linked_list"><code>rust_linked_list</code></a></td> //! <td></td> //! <td><code>T</code></td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>Yes</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/xor_list"><code>xor_list</code></a></td> //! <td></td> //! <td><code>T</code></td> //! <td>scattered to heap</td> //! <td>-</td> //! <td></td> //! <td>-</td> //! <td>uses <code>unsafe</code></td> //! </tr> //! <tr> //! <td><a href="https://lib.rs/crates/index_queue"><code>index_queue</code></a></td> //! <td></td> //! <td><code>T</code></td> //! <td>sequential</td> //! <td>-</td> //! <td>- -</td> //! <td>Yes</td> //! <td></td> //! <td>Reimplementation of <code>VecDeque</code></td> //! </tr> //! </table> //! <p> //! //! 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;