/usr/lib/gcc/x86_64-linux-gnu/6/include/d/std/container/slist.d is in libgphobos-6-dev 6.4.0-17ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /**
This module implements a singly-linked list container.
This module is a submodule of $(LINK2 std_container.html, std.container).
Source: $(PHOBOSSRC std/container/_slist.d)
Macros:
WIKI = Phobos/StdContainer
TEXTWITHCOMMAS = $0
Copyright: Red-black tree code copyright (C) 2008- by Steven Schveighoffer. Other code
copyright 2010- Andrei Alexandrescu. All rights reserved by the respective holders.
License: Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at $(WEB
boost.org/LICENSE_1_0.txt)).
Authors: Steven Schveighoffer, $(WEB erdani.com, Andrei Alexandrescu)
*/
module std.container.slist;
public import std.container.util;
/**
Implements a simple and fast singly-linked list.
$(D SList) uses reference semantics.
*/
struct SList(T)
{
import std.exception : enforce;
import std.range : Take;
import std.range.primitives;
import std.traits;
private struct Node
{
Node * _next;
T _payload;
}
private struct NodeWithoutPayload
{
Node* _next;
}
static assert(NodeWithoutPayload._next.offsetof == Node._next.offsetof);
private Node * _root;
private void initialize() @trusted nothrow pure
{
if (_root) return;
_root = cast (Node*) new NodeWithoutPayload();
}
private ref inout(Node*) _first() @property @safe nothrow pure inout
{
assert(_root);
return _root._next;
}
private static Node * findLastNode(Node * n)
{
assert(n);
auto ahead = n._next;
while (ahead)
{
n = ahead;
ahead = n._next;
}
return n;
}
private static Node * findLastNode(Node * n, size_t limit)
{
assert(n && limit);
auto ahead = n._next;
while (ahead)
{
if (!--limit) break;
n = ahead;
ahead = n._next;
}
return n;
}
private static Node * findNode(Node * n, Node * findMe)
{
assert(n);
auto ahead = n._next;
while (ahead != findMe)
{
n = ahead;
enforce(n);
ahead = n._next;
}
return n;
}
/**
Constructor taking a number of nodes
*/
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T))
{
insertFront(values);
}
/**
Constructor taking an input range
*/
this(Stuff)(Stuff stuff)
if (isInputRange!Stuff
&& isImplicitlyConvertible!(ElementType!Stuff, T)
&& !is(Stuff == T[]))
{
insertFront(stuff);
}
/**
Comparison for equality.
Complexity: $(BIGOH min(n, n1)) where $(D n1) is the number of
elements in $(D rhs).
*/
bool opEquals(const SList rhs) const
{
return opEquals(rhs);
}
/// ditto
bool opEquals(ref const SList rhs) const
{
if (_root is rhs._root) return true;
if (_root is null) return rhs._root is null || rhs._first is null;
if (rhs._root is null) return _root is null || _first is null;
const(Node) * n1 = _first, n2 = rhs._first;
for (;; n1 = n1._next, n2 = n2._next)
{
if (!n1) return !n2;
if (!n2 || n1._payload != n2._payload) return false;
}
}
/**
Defines the container's primary range, which embodies a forward range.
*/
struct Range
{
private Node * _head;
private this(Node * p) { _head = p; }
/// Input range primitives.
@property bool empty() const { return !_head; }
/// ditto
@property ref T front()
{
assert(!empty, "SList.Range.front: Range is empty");
return _head._payload;
}
/// ditto
void popFront()
{
assert(!empty, "SList.Range.popFront: Range is empty");
_head = _head._next;
}
/// Forward range primitive.
@property Range save() { return this; }
T moveFront()
{
import std.algorithm : move;
assert(!empty, "SList.Range.moveFront: Range is empty");
return move(_head._payload);
}
bool sameHead(Range rhs)
{
return _head && _head == rhs._head;
}
}
unittest
{
static assert(isForwardRange!Range);
}
/**
Property returning $(D true) if and only if the container has no
elements.
Complexity: $(BIGOH 1)
*/
@property bool empty() const
{
return _root is null || _first is null;
}
/**
Duplicates the container. The elements themselves are not transitively
duplicated.
Complexity: $(BIGOH n).
*/
@property SList dup()
{
return SList(this[]);
}
/**
Returns a range that iterates over all elements of the container, in
forward order.
Complexity: $(BIGOH 1)
*/
Range opSlice()
{
if (empty)
return Range(null);
else
return Range(_first);
}
/**
Forward to $(D opSlice().front).
Complexity: $(BIGOH 1)
*/
@property ref T front()
{
assert(!empty, "SList.front: List is empty");
return _first._payload;
}
unittest
{
auto s = SList!int(1, 2, 3);
s.front = 42;
assert(s == SList!int(42, 2, 3));
}
/**
Returns a new $(D SList) that's the concatenation of $(D this) and its
argument. $(D opBinaryRight) is only defined if $(D Stuff) does not
define $(D opBinary).
*/
SList opBinary(string op, Stuff)(Stuff rhs)
if (op == "~" && is(typeof(SList(rhs))))
{
auto toAdd = SList(rhs);
if (empty) return toAdd;
// TODO: optimize
auto result = dup;
auto n = findLastNode(result._first);
n._next = toAdd._first;
return result;
}
/// ditto
SList opBinaryRight(string op, Stuff)(Stuff lhs)
if (op == "~" && !is(typeof(lhs.opBinary!"~"(this))) && is(typeof(SList(lhs))))
{
auto toAdd = SList(lhs);
if (empty) return toAdd;
auto result = dup;
result.insertFront(toAdd[]);
return result;
}
/**
Removes all contents from the $(D SList).
Postcondition: $(D empty)
Complexity: $(BIGOH 1)
*/
void clear()
{
_first = null;
}
/**
Inserts $(D stuff) to the front of the container. $(D stuff) can be a
value convertible to $(D T) or a range of objects convertible to $(D
T). The stable version behaves the same, but guarantees that ranges
iterating over the container are never invalidated.
Returns: The number of elements inserted
Complexity: $(BIGOH m), where $(D m) is the length of $(D stuff)
*/
size_t insertFront(Stuff)(Stuff stuff)
if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T))
{
initialize();
size_t result;
Node * n, newRoot;
foreach (item; stuff)
{
auto newNode = new Node(null, item);
(newRoot ? n._next : newRoot) = newNode;
n = newNode;
++result;
}
if (!n) return 0;
// Last node points to the old root
n._next = _first;
_first = newRoot;
return result;
}
/// ditto
size_t insertFront(Stuff)(Stuff stuff)
if (isImplicitlyConvertible!(Stuff, T))
{
initialize();
auto newRoot = new Node(_first, stuff);
_first = newRoot;
return 1;
}
/// ditto
alias insert = insertFront;
/// ditto
alias stableInsert = insert;
/// ditto
alias stableInsertFront = insertFront;
/**
Picks one value in an unspecified position in the container, removes
it from the container, and returns it. The stable version behaves the same,
but guarantees that ranges iterating over the container are never invalidated.
Precondition: $(D !empty)
Returns: The element removed.
Complexity: $(BIGOH 1).
*/
T removeAny()
{
import std.algorithm : move;
assert(!empty, "SList.removeAny: List is empty");
auto result = move(_first._payload);
_first = _first._next;
return result;
}
/// ditto
alias stableRemoveAny = removeAny;
/**
Removes the value at the front of the container. The stable version
behaves the same, but guarantees that ranges iterating over the
container are never invalidated.
Precondition: $(D !empty)
Complexity: $(BIGOH 1).
*/
void removeFront()
{
assert(!empty, "SList.removeFront: List is empty");
_first = _first._next;
}
/// ditto
alias stableRemoveFront = removeFront;
/**
Removes $(D howMany) values at the front or back of the
container. Unlike the unparameterized versions above, these functions
do not throw if they could not remove $(D howMany) elements. Instead,
if $(D howMany > n), all elements are removed. The returned value is
the effective number of elements removed. The stable version behaves
the same, but guarantees that ranges iterating over the container are
never invalidated.
Returns: The number of elements removed
Complexity: $(BIGOH howMany * log(n)).
*/
size_t removeFront(size_t howMany)
{
size_t result;
while (_first && result < howMany)
{
_first = _first._next;
++result;
}
return result;
}
/// ditto
alias stableRemoveFront = removeFront;
/**
Inserts $(D stuff) after range $(D r), which must be a range
previously extracted from this container. Given that all ranges for a
list end at the end of the list, this function essentially appends to
the list and uses $(D r) as a potentially fast way to reach the last
node in the list. Ideally $(D r) is positioned near or at the last
element of the list.
$(D stuff) can be a value convertible to $(D T) or a range of objects
convertible to $(D T). The stable version behaves the same, but
guarantees that ranges iterating over the container are never
invalidated.
Returns: The number of values inserted.
Complexity: $(BIGOH k + m), where $(D k) is the number of elements in
$(D r) and $(D m) is the length of $(D stuff).
Examples:
--------------------
auto sl = SList!string(["a", "b", "d"]);
sl.insertAfter(sl[], "e"); // insert at the end (slowest)
assert(std.algorithm.equal(sl[], ["a", "b", "d", "e"]));
sl.insertAfter(std.range.take(sl[], 2), "c"); // insert after "b"
assert(std.algorithm.equal(sl[], ["a", "b", "c", "d", "e"]));
--------------------
*/
size_t insertAfter(Stuff)(Range r, Stuff stuff)
{
initialize();
if (!_first)
{
enforce(!r._head);
return insertFront(stuff);
}
enforce(r._head);
auto n = findLastNode(r._head);
SList tmp;
auto result = tmp.insertFront(stuff);
n._next = tmp._first;
return result;
}
/**
Similar to $(D insertAfter) above, but accepts a range bounded in
count. This is important for ensuring fast insertions in the middle of
the list. For fast insertions after a specified position $(D r), use
$(D insertAfter(take(r, 1), stuff)). The complexity of that operation
only depends on the number of elements in $(D stuff).
Precondition: $(D r.original.empty || r.maxLength > 0)
Returns: The number of values inserted.
Complexity: $(BIGOH k + m), where $(D k) is the number of elements in
$(D r) and $(D m) is the length of $(D stuff).
*/
size_t insertAfter(Stuff)(Take!Range r, Stuff stuff)
{
auto orig = r.source;
if (!orig._head)
{
// Inserting after a null range counts as insertion to the
// front
return insertFront(stuff);
}
enforce(!r.empty);
// Find the last valid element in the range
foreach (i; 1 .. r.maxLength)
{
if (!orig._head._next) break;
orig.popFront();
}
// insert here
SList tmp;
tmp.initialize();
tmp._first = orig._head._next;
auto result = tmp.insertFront(stuff);
orig._head._next = tmp._first;
return result;
}
/// ditto
alias stableInsertAfter = insertAfter;
/**
Removes a range from the list in linear time.
Returns: An empty range.
Complexity: $(BIGOH n)
*/
Range linearRemove(Range r)
{
if (!_first)
{
enforce(!r._head);
return this[];
}
auto n = findNode(_root, r._head);
n._next = null;
return Range(null);
}
/**
Removes a $(D Take!Range) from the list in linear time.
Returns: A range comprehending the elements after the removed range.
Complexity: $(BIGOH n)
*/
Range linearRemove(Take!Range r)
{
auto orig = r.source;
// We have something to remove here
if (orig._head == _first)
{
// remove straight from the head of the list
for (; !r.empty; r.popFront())
{
removeFront();
}
return this[];
}
if (!r.maxLength)
{
// Nothing to remove, return the range itself
return orig;
}
// Remove from somewhere in the middle of the list
enforce(_first);
auto n1 = findNode(_root, orig._head);
auto n2 = findLastNode(orig._head, r.maxLength);
n1._next = n2._next;
return Range(n1._next);
}
/// ditto
alias stableLinearRemove = linearRemove;
}
unittest
{
import std.algorithm : equal;
auto a = SList!int(5);
auto b = a;
auto r = a[];
a.insertFront(1);
b.insertFront(2);
assert(equal(a[], [2, 1, 5]));
assert(equal(b[], [2, 1, 5]));
r.front = 9;
assert(equal(a[], [2, 1, 9]));
assert(equal(b[], [2, 1, 9]));
}
unittest
{
auto s = SList!int(1, 2, 3);
auto n = s.findLastNode(s._root);
assert(n && n._payload == 3);
}
unittest
{
import std.range.primitives;
auto s = SList!int(1, 2, 5, 10);
assert(walkLength(s[]) == 4);
}
unittest
{
import std.range : take;
auto src = take([0, 1, 2, 3], 3);
auto s = SList!int(src);
assert(s == SList!int(0, 1, 2));
}
unittest
{
auto a = SList!int();
auto b = SList!int();
auto c = a ~ b[];
assert(c.empty);
}
unittest
{
auto a = SList!int(1, 2, 3);
auto b = SList!int(4, 5, 6);
auto c = a ~ b[];
assert(c == SList!int(1, 2, 3, 4, 5, 6));
}
unittest
{
auto a = SList!int(1, 2, 3);
auto b = [4, 5, 6];
auto c = a ~ b;
assert(c == SList!int(1, 2, 3, 4, 5, 6));
}
unittest
{
auto a = SList!int(1, 2, 3);
auto c = a ~ 4;
assert(c == SList!int(1, 2, 3, 4));
}
unittest
{
auto a = SList!int(2, 3, 4);
auto b = 1 ~ a;
assert(b == SList!int(1, 2, 3, 4));
}
unittest
{
auto a = [1, 2, 3];
auto b = SList!int(4, 5, 6);
auto c = a ~ b;
assert(c == SList!int(1, 2, 3, 4, 5, 6));
}
unittest
{
auto s = SList!int(1, 2, 3, 4);
s.insertFront([ 42, 43 ]);
assert(s == SList!int(42, 43, 1, 2, 3, 4));
}
unittest
{
auto s = SList!int(1, 2, 3);
assert(s.removeAny() == 1);
assert(s == SList!int(2, 3));
assert(s.stableRemoveAny() == 2);
assert(s == SList!int(3));
}
unittest
{
import std.algorithm : equal;
auto s = SList!int(1, 2, 3);
s.removeFront();
assert(equal(s[], [2, 3]));
s.stableRemoveFront();
assert(equal(s[], [3]));
}
unittest
{
auto s = SList!int(1, 2, 3, 4, 5, 6, 7);
assert(s.removeFront(3) == 3);
assert(s == SList!int(4, 5, 6, 7));
}
unittest
{
auto a = SList!int(1, 2, 3);
auto b = SList!int(1, 2, 3);
assert(a.insertAfter(a[], b[]) == 3);
}
unittest
{
import std.range : take;
auto s = SList!int(1, 2, 3, 4);
auto r = take(s[], 2);
assert(s.insertAfter(r, 5) == 1);
assert(s == SList!int(1, 2, 5, 3, 4));
}
unittest
{
import std.algorithm;
// insertAfter documentation example
auto sl = SList!string(["a", "b", "d"]);
sl.insertAfter(sl[], "e"); // insert at the end (slowest)
assert(std.algorithm.equal(sl[], ["a", "b", "d", "e"]));
sl.insertAfter(std.range.take(sl[], 2), "c"); // insert after "b"
assert(std.algorithm.equal(sl[], ["a", "b", "c", "d", "e"]));
}
unittest
{
import std.range.primitives;
auto s = SList!int(1, 2, 3, 4, 5);
auto r = s[];
popFrontN(r, 3);
auto r1 = s.linearRemove(r);
assert(s == SList!int(1, 2, 3));
assert(r1.empty);
}
unittest
{
auto s = SList!int(1, 2, 3, 4, 5);
auto r = s[];
auto r1 = s.linearRemove(r);
assert(s == SList!int());
assert(r1.empty);
}
unittest
{
import std.algorithm : equal;
import std.range;
auto s = SList!int(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
auto r = s[];
popFrontN(r, 3);
auto r1 = take(r, 4);
assert(equal(r1, [4, 5, 6, 7]));
auto r2 = s.linearRemove(r1);
assert(s == SList!int(1, 2, 3, 8, 9, 10));
assert(equal(r2, [8, 9, 10]));
}
unittest
{
import std.range.primitives;
auto lst = SList!int(1, 5, 42, 9);
assert(!lst.empty);
assert(lst.front == 1);
assert(walkLength(lst[]) == 4);
auto lst2 = lst ~ [ 1, 2, 3 ];
assert(walkLength(lst2[]) == 7);
auto lst3 = lst ~ [ 7 ];
assert(walkLength(lst3[]) == 5);
}
unittest
{
auto s = make!(SList!int)(1, 2, 3);
}
unittest
{
// 5193
static struct Data
{
const int val;
}
SList!Data list;
}
unittest
{
auto s = SList!int([1, 2, 3]);
s.front = 5; //test frontAssign
assert(s.front == 5);
auto r = s[];
r.front = 1; //test frontAssign
assert(r.front == 1);
}
unittest
{
// issue 14920
SList!int s;
s.insertAfter(s[], 1);
assert(s.front == 1);
}
|