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 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
|
#
#
# Nim's Runtime Library
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## Implementation of:
## * `singly linked lists <#SinglyLinkedList>`_
## * `doubly linked lists <#DoublyLinkedList>`_
## * `singly linked rings <#SinglyLinkedRing>`_ (circular lists)
## * `doubly linked rings <#DoublyLinkedRing>`_ (circular lists)
##
## # Basic Usage
## Because it makes no sense to do otherwise, the `next` and `prev` pointers
## are not hidden from you and can be manipulated directly for efficiency.
##
## ## Lists
runnableExamples:
var list = initDoublyLinkedList[int]()
let
a = newDoublyLinkedNode[int](3)
b = newDoublyLinkedNode[int](7)
c = newDoublyLinkedNode[int](9)
list.add(a)
list.add(b)
list.prepend(c)
assert a.next == b
assert a.prev == c
assert c.next == a
assert c.next.next == b
assert c.prev == nil
assert b.next == nil
## ## Rings
runnableExamples:
var ring = initSinglyLinkedRing[int]()
let
a = newSinglyLinkedNode[int](3)
b = newSinglyLinkedNode[int](7)
c = newSinglyLinkedNode[int](9)
ring.add(a)
ring.add(b)
ring.prepend(c)
assert c.next == a
assert a.next == b
assert c.next.next == b
assert b.next == c
assert c.next.next.next == c
## # See also
## * `deques module <deques.html>`_ for double-ended queues
import std/private/since
when defined(nimPreviewSlimSystem):
import std/assertions
type
DoublyLinkedNodeObj*[T] = object
## A node of a doubly linked list.
##
## It consists of a `value` field, and pointers to `next` and `prev`.
next*: DoublyLinkedNode[T]
prev* {.cursor.}: DoublyLinkedNode[T]
value*: T
DoublyLinkedNode*[T] = ref DoublyLinkedNodeObj[T]
SinglyLinkedNodeObj*[T] = object
## A node of a singly linked list.
##
## It consists of a `value` field, and a pointer to `next`.
next*: SinglyLinkedNode[T]
value*: T
SinglyLinkedNode*[T] = ref SinglyLinkedNodeObj[T]
SinglyLinkedList*[T] = object
## A singly linked list.
head*: SinglyLinkedNode[T]
tail* {.cursor.}: SinglyLinkedNode[T]
DoublyLinkedList*[T] = object
## A doubly linked list.
head*: DoublyLinkedNode[T]
tail* {.cursor.}: DoublyLinkedNode[T]
SinglyLinkedRing*[T] = object
## A singly linked ring.
head*: SinglyLinkedNode[T]
tail* {.cursor.}: SinglyLinkedNode[T]
DoublyLinkedRing*[T] = object
## A doubly linked ring.
head*: DoublyLinkedNode[T]
SomeLinkedList*[T] = SinglyLinkedList[T] | DoublyLinkedList[T]
SomeLinkedRing*[T] = SinglyLinkedRing[T] | DoublyLinkedRing[T]
SomeLinkedCollection*[T] = SomeLinkedList[T] | SomeLinkedRing[T]
SomeLinkedNode*[T] = SinglyLinkedNode[T] | DoublyLinkedNode[T]
proc initSinglyLinkedList*[T](): SinglyLinkedList[T] =
## Creates a new singly linked list that is empty.
##
## Singly linked lists are initialized by default, so it is not necessary to
## call this function explicitly.
runnableExamples:
let a = initSinglyLinkedList[int]()
discard
proc initDoublyLinkedList*[T](): DoublyLinkedList[T] =
## Creates a new doubly linked list that is empty.
##
## Doubly linked lists are initialized by default, so it is not necessary to
## call this function explicitly.
runnableExamples:
let a = initDoublyLinkedList[int]()
discard
proc initSinglyLinkedRing*[T](): SinglyLinkedRing[T] =
## Creates a new singly linked ring that is empty.
##
## Singly linked rings are initialized by default, so it is not necessary to
## call this function explicitly.
runnableExamples:
let a = initSinglyLinkedRing[int]()
discard
proc initDoublyLinkedRing*[T](): DoublyLinkedRing[T] =
## Creates a new doubly linked ring that is empty.
##
## Doubly linked rings are initialized by default, so it is not necessary to
## call this function explicitly.
runnableExamples:
let a = initDoublyLinkedRing[int]()
discard
proc newDoublyLinkedNode*[T](value: T): DoublyLinkedNode[T] =
## Creates a new doubly linked node with the given `value`.
runnableExamples:
let n = newDoublyLinkedNode[int](5)
assert n.value == 5
new(result)
result.value = value
proc newSinglyLinkedNode*[T](value: T): SinglyLinkedNode[T] =
## Creates a new singly linked node with the given `value`.
runnableExamples:
let n = newSinglyLinkedNode[int](5)
assert n.value == 5
new(result)
result.value = value
template itemsListImpl() {.dirty.} =
var it {.cursor.} = L.head
while it != nil:
yield it.value
it = it.next
template itemsRingImpl() {.dirty.} =
var it {.cursor.} = L.head
if it != nil:
while true:
yield it.value
it = it.next
if it == L.head: break
iterator items*[T](L: SomeLinkedList[T]): T =
## Yields every value of `L`.
##
## **See also:**
## * `mitems iterator <#mitems.i,SomeLinkedList[T]>`_
## * `nodes iterator <#nodes.i,SomeLinkedList[T]>`_
runnableExamples:
from std/sugar import collect
from std/sequtils import toSeq
let a = collect(initSinglyLinkedList):
for i in 1..3: 10 * i
assert toSeq(items(a)) == toSeq(a)
assert toSeq(a) == @[10, 20, 30]
itemsListImpl()
iterator items*[T](L: SomeLinkedRing[T]): T =
## Yields every value of `L`.
##
## **See also:**
## * `mitems iterator <#mitems.i,SomeLinkedRing[T]>`_
## * `nodes iterator <#nodes.i,SomeLinkedRing[T]>`_
runnableExamples:
from std/sugar import collect
from std/sequtils import toSeq
let a = collect(initSinglyLinkedRing):
for i in 1..3: 10 * i
assert toSeq(items(a)) == toSeq(a)
assert toSeq(a) == @[10, 20, 30]
itemsRingImpl()
iterator mitems*[T](L: var SomeLinkedList[T]): var T =
## Yields every value of `L` so that you can modify it.
##
## **See also:**
## * `items iterator <#items.i,SomeLinkedList[T]>`_
## * `nodes iterator <#nodes.i,SomeLinkedList[T]>`_
runnableExamples:
var a = initSinglyLinkedList[int]()
for i in 1..5:
a.add(10 * i)
assert $a == "[10, 20, 30, 40, 50]"
for x in mitems(a):
x = 5 * x - 1
assert $a == "[49, 99, 149, 199, 249]"
itemsListImpl()
iterator mitems*[T](L: var SomeLinkedRing[T]): var T =
## Yields every value of `L` so that you can modify it.
##
## **See also:**
## * `items iterator <#items.i,SomeLinkedRing[T]>`_
## * `nodes iterator <#nodes.i,SomeLinkedRing[T]>`_
runnableExamples:
var a = initSinglyLinkedRing[int]()
for i in 1..5:
a.add(10 * i)
assert $a == "[10, 20, 30, 40, 50]"
for x in mitems(a):
x = 5 * x - 1
assert $a == "[49, 99, 149, 199, 249]"
itemsRingImpl()
iterator nodes*[T](L: SomeLinkedList[T]): SomeLinkedNode[T] =
## Iterates over every node of `x`. Removing the current node from the
## list during traversal is supported.
##
## **See also:**
## * `items iterator <#items.i,SomeLinkedList[T]>`_
## * `mitems iterator <#mitems.i,SomeLinkedList[T]>`_
runnableExamples:
var a = initDoublyLinkedList[int]()
for i in 1..5:
a.add(10 * i)
assert $a == "[10, 20, 30, 40, 50]"
for x in nodes(a):
if x.value == 30:
a.remove(x)
else:
x.value = 5 * x.value - 1
assert $a == "[49, 99, 199, 249]"
var it {.cursor.} = L.head
while it != nil:
let nxt = it.next
yield it
it = nxt
iterator nodes*[T](L: SomeLinkedRing[T]): SomeLinkedNode[T] =
## Iterates over every node of `x`. Removing the current node from the
## list during traversal is supported.
##
## **See also:**
## * `items iterator <#items.i,SomeLinkedRing[T]>`_
## * `mitems iterator <#mitems.i,SomeLinkedRing[T]>`_
runnableExamples:
var a = initDoublyLinkedRing[int]()
for i in 1..5:
a.add(10 * i)
assert $a == "[10, 20, 30, 40, 50]"
for x in nodes(a):
if x.value == 30:
a.remove(x)
else:
x.value = 5 * x.value - 1
assert $a == "[49, 99, 199, 249]"
var it {.cursor.} = L.head
if it != nil:
while true:
let nxt = it.next
yield it
it = nxt
if it == L.head: break
proc `$`*[T](L: SomeLinkedCollection[T]): string =
## Turns a list into its string representation for logging and printing.
runnableExamples:
let a = [1, 2, 3, 4].toSinglyLinkedList
assert $a == "[1, 2, 3, 4]"
result = "["
for x in nodes(L):
if result.len > 1: result.add(", ")
result.addQuoted(x.value)
result.add("]")
proc find*[T](L: SomeLinkedCollection[T], value: T): SomeLinkedNode[T] =
## Searches in the list for a value. Returns `nil` if the value does not
## exist.
##
## **See also:**
## * `contains proc <#contains,SomeLinkedCollection[T],T>`_
runnableExamples:
let a = [9, 8].toSinglyLinkedList
assert a.find(9).value == 9
assert a.find(1) == nil
for x in nodes(L):
if x.value == value: return x
proc contains*[T](L: SomeLinkedCollection[T], value: T): bool {.inline.} =
## Searches in the list for a value. Returns `false` if the value does not
## exist, `true` otherwise. This allows the usage of the `in` and `notin`
## operators.
##
## **See also:**
## * `find proc <#find,SomeLinkedCollection[T],T>`_
runnableExamples:
let a = [9, 8].toSinglyLinkedList
assert a.contains(9)
assert 8 in a
assert(not a.contains(1))
assert 2 notin a
result = find(L, value) != nil
proc prepend*[T: SomeLinkedList](a: var T, b: T) {.since: (1, 5, 1).} =
## Prepends a shallow copy of `b` to the beginning of `a`.
##
## **See also:**
## * `prependMoved proc <#prependMoved,T,T>`_
## for moving the second list instead of copying
runnableExamples:
from std/sequtils import toSeq
var a = [4, 5].toSinglyLinkedList
let b = [1, 2, 3].toSinglyLinkedList
a.prepend(b)
assert a.toSeq == [1, 2, 3, 4, 5]
assert b.toSeq == [1, 2, 3]
a.prepend(a)
assert a.toSeq == [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
var tmp = b.copy
tmp.addMoved(a)
a = tmp
proc prependMoved*[T: SomeLinkedList](a, b: var T) {.since: (1, 5, 1).} =
## Moves `b` before the head of `a`. Efficiency: O(1).
## Note that `b` becomes empty after the operation unless it has the same address as `a`.
## Self-prepending results in a cycle.
##
## **See also:**
## * `prepend proc <#prepend,T,T>`_
## for prepending a copy of a list
runnableExamples:
import std/[sequtils, enumerate, sugar]
var
a = [4, 5].toSinglyLinkedList
b = [1, 2, 3].toSinglyLinkedList
c = [0, 1].toSinglyLinkedList
a.prependMoved(b)
assert a.toSeq == [1, 2, 3, 4, 5]
assert b.toSeq == []
c.prependMoved(c)
let s = collect:
for i, ci in enumerate(c):
if i == 6: break
ci
assert s == [0, 1, 0, 1, 0, 1]
b.addMoved(a)
swap a, b
proc add*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) {.inline.} =
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
## for prepending a node
## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
runnableExamples:
var a = initSinglyLinkedList[int]()
let n = newSinglyLinkedNode[int](9)
a.add(n)
assert a.contains(9)
n.next = nil
if L.tail != nil:
assert(L.tail.next == nil)
L.tail.next = n
L.tail = n
if L.head == nil: L.head = n
proc add*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
## Appends (adds to the end) a value to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
## for prepending a node
## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
runnableExamples:
var a = initSinglyLinkedList[int]()
a.add(9)
a.add(8)
assert a.contains(9)
add(L, newSinglyLinkedNode(value))
proc prepend*[T](L: var SinglyLinkedList[T],
n: SinglyLinkedNode[T]) {.inline.} =
## Prepends (adds to the beginning) a node to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
## for appending a node
## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
runnableExamples:
var a = initSinglyLinkedList[int]()
let n = newSinglyLinkedNode[int](9)
a.prepend(n)
assert a.contains(9)
n.next = L.head
L.head = n
if L.tail == nil: L.tail = n
proc prepend*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
## Prepends (adds to the beginning) a node to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
## for appending a node
## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
## for prepending a node
runnableExamples:
var a = initSinglyLinkedList[int]()
a.prepend(9)
a.prepend(8)
assert a.contains(9)
prepend(L, newSinglyLinkedNode(value))
func copy*[T](a: SinglyLinkedList[T]): SinglyLinkedList[T] {.since: (1, 5, 1).} =
## Creates a shallow copy of `a`.
runnableExamples:
from std/sequtils import toSeq
type Foo = ref object
x: int
var
f = Foo(x: 1)
a = [f].toSinglyLinkedList
let b = a.copy
a.add([f].toSinglyLinkedList)
assert a.toSeq == [f, f]
assert b.toSeq == [f] # b isn't modified...
f.x = 42
assert a.head.value.x == 42
assert b.head.value.x == 42 # ... but the elements are not deep copied
let c = [1, 2, 3].toSinglyLinkedList
assert $c == $c.copy
result = initSinglyLinkedList[T]()
for x in a.items:
result.add(x)
proc addMoved*[T](a, b: var SinglyLinkedList[T]) {.since: (1, 5, 1).} =
## Moves `b` to the end of `a`. Efficiency: O(1).
## Note that `b` becomes empty after the operation unless it has the same address as `a`.
## Self-adding results in a cycle.
##
## **See also:**
## * `add proc <#add,T,T>`_ for adding a copy of a list
runnableExamples:
import std/[sequtils, enumerate, sugar]
var
a = [1, 2, 3].toSinglyLinkedList
b = [4, 5].toSinglyLinkedList
c = [0, 1].toSinglyLinkedList
a.addMoved(b)
assert a.toSeq == [1, 2, 3, 4, 5]
assert b.toSeq == []
c.addMoved(c)
let s = collect:
for i, ci in enumerate(c):
if i == 6: break
ci
assert s == [0, 1, 0, 1, 0, 1]
if b.head != nil:
if a.head == nil:
a.head = b.head
else:
a.tail.next = b.head
a.tail = b.tail
if a.addr != b.addr:
b.head = nil
b.tail = nil
proc add*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
## * `prepend proc <#prepend,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for prepending a node
## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for removing a node
runnableExamples:
var a = initDoublyLinkedList[int]()
let n = newDoublyLinkedNode[int](9)
a.add(n)
assert a.contains(9)
n.next = nil
n.prev = L.tail
if L.tail != nil:
assert(L.tail.next == nil)
L.tail.next = n
L.tail = n
if L.head == nil: L.head = n
proc add*[T](L: var DoublyLinkedList[T], value: T) =
## Appends (adds to the end) a value to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for appending a node
## * `prepend proc <#prepend,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for prepending a node
## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for removing a node
runnableExamples:
var a = initDoublyLinkedList[int]()
a.add(9)
a.add(8)
assert a.contains(9)
add(L, newDoublyLinkedNode(value))
proc prepend*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for appending a node
## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for removing a node
runnableExamples:
var a = initDoublyLinkedList[int]()
let n = newDoublyLinkedNode[int](9)
a.prepend(n)
assert a.contains(9)
n.prev = nil
n.next = L.head
if L.head != nil:
assert(L.head.prev == nil)
L.head.prev = n
L.head = n
if L.tail == nil: L.tail = n
proc prepend*[T](L: var DoublyLinkedList[T], value: T) =
## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for appending a node
## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
## * `prepend proc <#prepend,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for prepending a node
## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## for removing a node
runnableExamples:
var a = initDoublyLinkedList[int]()
a.prepend(9)
a.prepend(8)
assert a.contains(9)
prepend(L, newDoublyLinkedNode(value))
func copy*[T](a: DoublyLinkedList[T]): DoublyLinkedList[T] {.since: (1, 5, 1).} =
## Creates a shallow copy of `a`.
runnableExamples:
from std/sequtils import toSeq
type Foo = ref object
x: int
var
f = Foo(x: 1)
a = [f].toDoublyLinkedList
let b = a.copy
a.add([f].toDoublyLinkedList)
assert a.toSeq == [f, f]
assert b.toSeq == [f] # b isn't modified...
f.x = 42
assert a.head.value.x == 42
assert b.head.value.x == 42 # ... but the elements are not deep copied
let c = [1, 2, 3].toDoublyLinkedList
assert $c == $c.copy
result = initDoublyLinkedList[T]()
for x in a.items:
result.add(x)
proc addMoved*[T](a, b: var DoublyLinkedList[T]) {.since: (1, 5, 1).} =
## Moves `b` to the end of `a`. Efficiency: O(1).
## Note that `b` becomes empty after the operation unless it has the same address as `a`.
## Self-adding results in a cycle.
##
## **See also:**
## * `add proc <#add,T,T>`_
## for adding a copy of a list
runnableExamples:
import std/[sequtils, enumerate, sugar]
var
a = [1, 2, 3].toDoublyLinkedList
b = [4, 5].toDoublyLinkedList
c = [0, 1].toDoublyLinkedList
a.addMoved(b)
assert a.toSeq == [1, 2, 3, 4, 5]
assert b.toSeq == []
c.addMoved(c)
let s = collect:
for i, ci in enumerate(c):
if i == 6: break
ci
assert s == [0, 1, 0, 1, 0, 1]
if b.head != nil:
if a.head == nil:
a.head = b.head
else:
b.head.prev = a.tail
a.tail.next = b.head
a.tail = b.tail
if a.addr != b.addr:
b.head = nil
b.tail = nil
proc add*[T: SomeLinkedList](a: var T, b: T) {.since: (1, 5, 1).} =
## Appends a shallow copy of `b` to the end of `a`.
##
## **See also:**
## * `addMoved proc <#addMoved,SinglyLinkedList[T],SinglyLinkedList[T]>`_
## * `addMoved proc <#addMoved,DoublyLinkedList[T],DoublyLinkedList[T]>`_
## for moving the second list instead of copying
runnableExamples:
from std/sequtils import toSeq
var a = [1, 2, 3].toSinglyLinkedList
let b = [4, 5].toSinglyLinkedList
a.add(b)
assert a.toSeq == [1, 2, 3, 4, 5]
assert b.toSeq == [4, 5]
a.add(a)
assert a.toSeq == [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
var tmp = b.copy
a.addMoved(tmp)
proc remove*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.discardable.} =
## Removes a node `n` from `L`.
## Returns `true` if `n` was found in `L`.
## Efficiency: O(n); the list is traversed until `n` is found.
## Attempting to remove an element not contained in the list is a no-op.
## When the list is cyclic, the cycle is preserved after removal.
runnableExamples:
import std/[sequtils, enumerate, sugar]
var a = [0, 1, 2].toSinglyLinkedList
let n = a.head.next
assert n.value == 1
assert a.remove(n) == true
assert a.toSeq == [0, 2]
assert a.remove(n) == false
assert a.toSeq == [0, 2]
a.addMoved(a) # cycle: [0, 2, 0, 2, ...]
a.remove(a.head)
let s = collect:
for i, ai in enumerate(a):
if i == 4: break
ai
assert s == [2, 2, 2, 2]
if n == L.head:
L.head = n.next
if L.tail.next == n:
L.tail.next = L.head # restore cycle
else:
var prev {.cursor.} = L.head
while prev.next != n and prev.next != nil:
prev = prev.next
if prev.next == nil:
return false
prev.next = n.next
if L.tail == n:
L.tail = prev # update tail if we removed the last node
true
proc remove*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
## Removes a node `n` from `L`. Efficiency: O(1).
## This function assumes, for the sake of efficiency, that `n` is contained in `L`,
## otherwise the effects are undefined.
## When the list is cyclic, the cycle is preserved after removal.
runnableExamples:
import std/[sequtils, enumerate, sugar]
var a = [0, 1, 2].toSinglyLinkedList
let n = a.head.next
assert n.value == 1
a.remove(n)
assert a.toSeq == [0, 2]
a.remove(n)
assert a.toSeq == [0, 2]
a.addMoved(a) # cycle: [0, 2, 0, 2, ...]
a.remove(a.head)
let s = collect:
for i, ai in enumerate(a):
if i == 4: break
ai
assert s == [2, 2, 2, 2]
if n == L.tail: L.tail = n.prev
if n == L.head: L.head = n.next
if n.next != nil: n.next.prev = n.prev
if n.prev != nil: n.prev.next = n.next
proc add*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
## * `prepend proc <#prepend,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
## for prepending a node
## * `prepend proc <#prepend,SinglyLinkedRing[T],T>`_ for prepending a value
runnableExamples:
var a = initSinglyLinkedRing[int]()
let n = newSinglyLinkedNode[int](9)
a.add(n)
assert a.contains(9)
if L.head != nil:
n.next = L.head
assert(L.tail != nil)
L.tail.next = n
else:
n.next = n
L.head = n
L.tail = n
proc add*[T](L: var SinglyLinkedRing[T], value: T) =
## Appends (adds to the end) a value to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
## for appending a node
## * `prepend proc <#prepend,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
## for prepending a node
## * `prepend proc <#prepend,SinglyLinkedRing[T],T>`_ for prepending a value
runnableExamples:
var a = initSinglyLinkedRing[int]()
a.add(9)
a.add(8)
assert a.contains(9)
add(L, newSinglyLinkedNode(value))
proc prepend*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
## for appending a node
## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
## * `prepend proc <#prepend,SinglyLinkedRing[T],T>`_ for prepending a value
runnableExamples:
var a = initSinglyLinkedRing[int]()
let n = newSinglyLinkedNode[int](9)
a.prepend(n)
assert a.contains(9)
if L.head != nil:
n.next = L.head
assert(L.tail != nil)
L.tail.next = n
else:
n.next = n
L.tail = n
L.head = n
proc prepend*[T](L: var SinglyLinkedRing[T], value: T) =
## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
## for appending a node
## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
## * `prepend proc <#prepend,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
## for prepending a node
runnableExamples:
var a = initSinglyLinkedRing[int]()
a.prepend(9)
a.prepend(8)
assert a.contains(9)
prepend(L, newSinglyLinkedNode(value))
proc add*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for prepending a node
## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for removing a node
runnableExamples:
var a = initDoublyLinkedRing[int]()
let n = newDoublyLinkedNode[int](9)
a.add(n)
assert a.contains(9)
if L.head != nil:
n.next = L.head
n.prev = L.head.prev
L.head.prev.next = n
L.head.prev = n
else:
n.prev = n
n.next = n
L.head = n
proc add*[T](L: var DoublyLinkedRing[T], value: T) =
## Appends (adds to the end) a value to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for appending a node
## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for prepending a node
## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for removing a node
runnableExamples:
var a = initDoublyLinkedRing[int]()
a.add(9)
a.add(8)
assert a.contains(9)
add(L, newDoublyLinkedNode(value))
proc prepend*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for appending a node
## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for removing a node
runnableExamples:
var a = initDoublyLinkedRing[int]()
let n = newDoublyLinkedNode[int](9)
a.prepend(n)
assert a.contains(9)
if L.head != nil:
n.next = L.head
n.prev = L.head.prev
L.head.prev.next = n
L.head.prev = n
else:
n.prev = n
n.next = n
L.head = n
proc prepend*[T](L: var DoublyLinkedRing[T], value: T) =
## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
##
## **See also:**
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for appending a node
## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for prepending a node
## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
## for removing a node
runnableExamples:
var a = initDoublyLinkedRing[int]()
a.prepend(9)
a.prepend(8)
assert a.contains(9)
prepend(L, newDoublyLinkedNode(value))
proc remove*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
## Removes `n` from `L`. Efficiency: O(1).
## This function assumes, for the sake of efficiency, that `n` is contained in `L`,
## otherwise the effects are undefined.
runnableExamples:
var a = initDoublyLinkedRing[int]()
let n = newDoublyLinkedNode[int](5)
a.add(n)
assert 5 in a
a.remove(n)
assert 5 notin a
n.next.prev = n.prev
n.prev.next = n.next
if n == L.head:
let p = L.head.prev
if p == L.head:
# only one element left:
L.head = nil
else:
L.head = p
proc append*[T](a: var (SinglyLinkedList[T] | SinglyLinkedRing[T]),
b: SinglyLinkedList[T] | SinglyLinkedNode[T] | T) =
## Alias for `a.add(b)`.
##
## **See also:**
## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
## * `add proc <#add,SinglyLinkedList[T],T>`_
## * `add proc <#add,T,T>`_
a.add(b)
proc append*[T](a: var (DoublyLinkedList[T] | DoublyLinkedRing[T]),
b: DoublyLinkedList[T] | DoublyLinkedNode[T] | T) =
## Alias for `a.add(b)`.
##
## **See also:**
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
## * `add proc <#add,DoublyLinkedList[T],T>`_
## * `add proc <#add,T,T>`_
a.add(b)
proc appendMoved*[T: SomeLinkedList](a, b: var T) {.since: (1, 5, 1).} =
## Alias for `a.addMoved(b)`.
##
## **See also:**
## * `addMoved proc <#addMoved,SinglyLinkedList[T],SinglyLinkedList[T]>`_
## * `addMoved proc <#addMoved,DoublyLinkedList[T],DoublyLinkedList[T]>`_
a.addMoved(b)
func toSinglyLinkedList*[T](elems: openArray[T]): SinglyLinkedList[T] {.since: (1, 5, 1).} =
## Creates a new `SinglyLinkedList` from the members of `elems`.
runnableExamples:
from std/sequtils import toSeq
let a = [1, 2, 3, 4, 5].toSinglyLinkedList
assert a.toSeq == [1, 2, 3, 4, 5]
result = initSinglyLinkedList[T]()
for elem in elems.items:
result.add(elem)
func toSinglyLinkedRing*[T](elems: openArray[T]): SinglyLinkedRing[T] =
## Creates a new `SinglyLinkedRing` from the members of `elems`.
runnableExamples:
from std/sequtils import toSeq
let a = [1, 2, 3, 4, 5].toSinglyLinkedRing
assert a.toSeq == [1, 2, 3, 4, 5]
result = initSinglyLinkedRing[T]()
for elem in elems.items:
result.add(elem)
func toDoublyLinkedList*[T](elems: openArray[T]): DoublyLinkedList[T] {.since: (1, 5, 1).} =
## Creates a new `DoublyLinkedList` from the members of `elems`.
runnableExamples:
from std/sequtils import toSeq
let a = [1, 2, 3, 4, 5].toDoublyLinkedList
assert a.toSeq == [1, 2, 3, 4, 5]
result = initDoublyLinkedList[T]()
for elem in elems.items:
result.add(elem)
func toDoublyLinkedRing*[T](elems: openArray[T]): DoublyLinkedRing[T] =
## Creates a new `DoublyLinkedRing` from the members of `elems`.
runnableExamples:
from std/sequtils import toSeq
let a = [1, 2, 3, 4, 5].toDoublyLinkedRing
assert a.toSeq == [1, 2, 3, 4, 5]
result = initDoublyLinkedRing[T]()
for elem in elems.items:
result.add(elem)
|