On this page
std.container.slist
This module implements a singly-linked list container. It can be used as a stack.
This module is a submodule of std.container.
- Source
- std/container/slist.d
- License:
- Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at boost.org/LICENSE_1_0.txt).
- Authors:
- Andrei Alexandrescu
- Examples:
-
import std.algorithm.comparison : equal; import std.container : SList; auto s = SList!int(1, 2, 3); assert(equal(s[], [1, 2, 3])); s.removeFront(); assert(equal(s[], [2, 3])); s.insertFront([5, 6]); assert(equal(s[], [5, 6, 2, 3])); // If you want to apply range operations, simply slice it. import std.algorithm.searching : countUntil; import std.range : popFrontN, walkLength; auto sl = SList!int(1, 2, 3, 4, 5); writeln(countUntil(sl[], 2)); // 1 auto r = sl[]; popFrontN(r, 2); writeln(walkLength(r)); // 3
- struct SList(T) if (!is(T == shared));
-
Implements a simple and fast singly-linked list. It can be used as a stack.
SListuses reference semantics.-
this(U)(U[] values...)
Constraints: if (isImplicitlyConvertible!(U, T)); -
Constructor taking a number of nodes
-
this(Stuff)(Stuff stuff)
Constraints: if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T) && !is(Stuff == T[])); -
Constructor taking an input range
-
const bool opEquals(const SList rhs);
const bool opEquals(ref const SList rhs); -
Comparison for equality.
- Complexity
- Ο(
min(n, n1)) wheren1is the number of elements inrhs.
- struct Range;
-
Defines the container's primary range, which embodies a forward range.
-
const @property bool empty();
@property ref T front();
void popFront(); -
Input range primitives.
- @property Range save();
-
Forward range primitive.
-
const @property bool empty();
- const @property bool empty();
-
Property returning
trueif and only if the container has no elements.- Complexity
- Ο(
1)
- @property SList dup();
-
Duplicates the container. The elements themselves are not transitively duplicated.
- Complexity
- Ο(
n).
- Range opSlice();
-
Returns a range that iterates over all elements of the container, in forward order.
- Complexity
- Ο(
1)
- @property ref T front();
-
Forward to
opSlice().front.- Complexity
- Ο(
1)
-
SList opBinary(string op, Stuff)(Stuff rhs)
Constraints: if (op == "~" && is(typeof(SList(rhs))));
SList opBinaryRight(string op, Stuff)(Stuff lhs)
Constraints: if (op == "~" && !is(typeof(lhs.opBinary!"~"(this))) && is(typeof(SList(lhs)))); -
Returns a new
SListthat's the concatenation ofthisand its argument.opBinaryRightis only defined ifStuffdoes not defineopBinary. - void clear();
-
Removes all contents from the
SList.- Postcondition
empty
- Complexity
- Ο(
1)
- void reverse();
-
Reverses SList in-place. Performs no memory allocation.
- Complexity
- Ο(
n)
-
size_t insertFront(Stuff)(Stuff stuff)
Constraints: if (isInputRange!Stuff || isImplicitlyConvertible!(Stuff, T));
alias insert = insertFront;
alias stableInsert = insert;
alias stableInsertFront = insertFront; -
Inserts
stuffto the front of the container.stuffcan be a value convertible toTor a range of objects convertible toT. The stable version behaves the same, but guarantees that ranges iterating over the container are never invalidated.- Returns:
- The number of elements inserted
- Complexity
- Ο(
m), wheremis the length ofstuff
-
T removeAny();
alias stableRemoveAny = removeAny; -
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
!empty
- Returns:
- The element removed.
- Complexity
- Ο(
1).
-
void removeFront();
alias stableRemoveFront = removeFront; -
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
!empty
- Complexity
- Ο(
1).
-
size_t removeFront(size_t howMany);
alias stableRemoveFront = removeFront; -
Removes
howManyvalues at the front or back of the container. Unlike the unparameterized versions above, these functions do not throw if they could not removehowManyelements. Instead, ifhowMany > 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
- Ο(
howMany * log(n)).
-
size_t insertAfter(Stuff)(Range r, Stuff stuff)
Constraints: if (isInputRange!Stuff || isImplicitlyConvertible!(Stuff, T)); -
Inserts
stuffafter ranger, 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 usesras a potentially fast way to reach the last node in the list. Ideallyris positioned near or at the last element of the list.stuffcan be a value convertible toTor a range of objects convertible toT. The stable version behaves the same, but guarantees that ranges iterating over the container are never invalidated.- Returns:
- The number of values inserted.
- Complexity
- Ο(
k + m), wherekis the number of elements inrandmis the length ofstuff.
- 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"])); -
size_t insertAfter(Stuff)(Take!Range r, Stuff stuff)
Constraints: if (isInputRange!Stuff || isImplicitlyConvertible!(Stuff, T));
alias stableInsertAfter = insertAfter; -
Similar to
insertAfterabove, 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 positionr, useinsertAfter(take(r, 1), stuff). The complexity of that operation only depends on the number of elements instuff.- Precondition
r.original.empty || r.maxLength > 0
- Returns:
- The number of values inserted.
- Complexity
- Ο(
k + m), wherekis the number of elements inrandmis the length ofstuff.
- Range linearRemove(Range r);
-
Removes a range from the list in linear time.
- Returns:
- An empty range.
- Complexity
- Ο(
n)
-
Range linearRemove(Take!Range r);
alias stableLinearRemove = linearRemove; -
Removes a
Take!Rangefrom the list in linear time.- Returns:
- A range comprehending the elements after the removed range.
- Complexity
- Ο(
n)
- bool linearRemoveElement(T value);
-
Removes the first occurence of an element from the list in linear time.
- Returns:
- True if the element existed and was successfully removed, false otherwise.
- Parameters:
-
T valuevalue of the node to be removed
- Complexity
- Ο(
n)
-
this(U)(U[] values...)
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_container_slist.html