Report a bug
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# 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.
Examples:
```import std.container : SList;
import std.algorithm.comparison : equal;

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);
assert(countUntil(sl[], 2) == 1);

auto r = sl[];
popFrontN(r, 2);
assert(walkLength(r) == 3);
```
struct SList(T);
Implements a simple and fast singly-linked list. It can be used as a stack.
SList uses reference semantics.
this(U)(U[] values...)
if (isImplicitlyConvertible!(U, T));
Constructor taking a number of nodes
this(Stuff)(Stuff stuff)
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)) where n1 is the number of elements in rhs.

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();
Property returning true if 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)
if (op == "~" && is(typeof(SList(rhs))));

SList opBinaryRight(string op, Stuff)(Stuff lhs)
if (op == "~" && !is(typeof(lhs.opBinary!"~"(this))) && is(typeof(SList(lhs))));
Returns a new SList that's the concatenation of this and its argument. opBinaryRight is only defined if Stuff does not define opBinary.
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)
if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T));

size_t insertFront(Stuff)(Stuff stuff)
if (isImplicitlyConvertible!(Stuff, T));

alias insert = insertFront;

alias stableInsert = insert;

alias stableInsertFront = insertFront;
Inserts stuff to the front of the container. stuff can be a value convertible to T or a range of objects convertible to 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: Ο(m), where m is the length of stuff

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 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 howMany elements. Instead, if 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: Ο(howMany * log(n)).

size_t insertAfter(Stuff)(Range r, Stuff stuff);
Inserts stuff after range 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 r as a potentially fast way to reach the last node in the list. Ideally r is positioned near or at the last element of the list.
stuff can be a value convertible to T or a range of objects convertible to 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: Ο(k + m), where k is the number of elements in r and m is the length of stuff.

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);

alias stableInsertAfter = insertAfter;
Similar to 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 r, use insertAfter(take(r, 1), stuff). The complexity of that operation only depends on the number of elements in stuff.

Precondition: r.original.empty || r.maxLength > 0

Returns:
The number of values inserted.

Complexity: Ο(k + m), where k is the number of elements in r and m is the length of stuff.

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!Range from the list in linear time.
Returns:
A range comprehending the elements after the removed range.

Complexity: Ο(n)
{"ft_lang_label":"__label__en","ft_lang_prob":0.67743844,"math_prob":0.95298564,"size":6052,"snap":"2020-34-2020-40","text_gpt3_token_len":1492,"char_repetition_ratio":0.14434524,"word_repetition_ratio":0.18488371,"special_character_ratio":0.2523133,"punctuation_ratio":0.21222411,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.971778,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-30T00:14:53Z\",\"WARC-Record-ID\":\"<urn:uuid:cb018716-c7b3-475e-991b-1b749667d0c5>\",\"Content-Length\":\"58001\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:509af3b5-0562-47f6-9d91-9f88f764f530>\",\"WARC-Concurrent-To\":\"<urn:uuid:c82da8c3-aff3-4a95-8890-78f18d323ca0>\",\"WARC-IP-Address\":\"162.243.166.170\",\"WARC-Target-URI\":\"https://docarchives.dlang.io/v2.072.0/phobos/std_container_slist.html\",\"WARC-Payload-Digest\":\"sha1:C4BSGFHE5HEPXAS4CFS7A2KJMHDLNWPD\",\"WARC-Block-Digest\":\"sha1:PHGY74ZTZBXGOQKS2PHISI65ZYDNFJ4G\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600402093104.90_warc_CC-MAIN-20200929221433-20200930011433-00102.warc.gz\"}"}