Report a bug
		
				If you spot a problem with this page, click here to create a Bugzilla issue.
		
			Improve this page
		
			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.
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: 
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
- structSList(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...)
 if (isImplicitlyConvertible!(U, T));
- Constructor taking a number of nodes
- this(Stuff)(Stuffstuff)
 if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T) && !is(Stuff == T[]));
- Constructor taking an input range
- boolopEquals(const SListrhs) const;
 boolopEquals(ref const SListrhs) const;
- Comparison for equality.Complexity Ο(min(n, n1)) where n1 is the number of elements in rhs.
- structRange;
- Defines the container's primary range, which embodies a forward range.- @property boolempty() const;
 @property ref Tfront();
 voidpopFront();
- Input range primitives.
- @property Rangesave();
- Forward range primitive.
 
- @property boolempty() const;
- Property returning true if and only if the container has no elements.Complexity Ο(1) 
- @property SListdup();
- Duplicates the container. The elements themselves are not transitively duplicated.Complexity Ο(n). 
- RangeopSlice();
- Returns a range that iterates over all elements of the container, in forward order.Complexity Ο(1) 
- @property ref Tfront();
- Forward to opSlice().front.Complexity Ο(1) 
- SListopBinary(string op, Stuff)(Stuffrhs)
 if (op == "~" && is(typeof(SList(rhs))));
 SListopBinaryRight(string op, Stuff)(Stufflhs)
 if (op == "~" && !is(typeof(lhs.opBinary!"~"(this))) && is(typeof(SList(lhs))));
- Returns a new SList that's the concatenation of this and its argument.opBinaryRightis only defined if Stuff does not defineopBinary.
- voidclear();
- Removes all contents from the SList.Postcondition empty Complexity Ο(1) 
- voidreverse();
- Reverses SList in-place. Performs no memory allocation.Complexity Ο(n) 
- size_tinsertFront(Stuff)(Stuffstuff)
 if (isInputRange!Stuff || isImplicitlyConvertible!(Stuff, T));
 aliasinsert= insertFront;
 aliasstableInsert= insert;
 aliasstableInsertFront= insertFront;
- Insertsstuffto the front of the container.stuffcan 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 insertedComplexity Ο(m), where m is the length of stuff
- TremoveAny();
 aliasstableRemoveAny= 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). 
- voidremoveFront();
 aliasstableRemoveFront= 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_tremoveFront(size_thowMany);
 aliasstableRemoveFront= removeFront;
- RemoveshowManyvalues at the front or back of the container. Unlike the unparameterized versions above, these functions do not throw if they could not removehowManyelements. 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 removedComplexity Ο(howMany * log(n)). 
- size_tinsertAfter(Stuff)(Ranger, Stuffstuff)
 if (isInputRange!Stuff || isImplicitlyConvertible!(Stuff, T));
- Insertsstuffafter 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 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 rand m is 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_tinsertAfter(Stuff)(Take!Ranger, Stuffstuff)
 if (isInputRange!Stuff || isImplicitlyConvertible!(Stuff, T));
 aliasstableInsertAfter= insertAfter;
- Similar toinsertAfterabove, 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, use insertAfter(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), where k is the number of elements in rand m is the length ofstuff.
- RangelinearRemove(Ranger);
- Removes a range from the list in linear time.Returns:An empty range.Complexity Ο(n) 
- RangelinearRemove(Take!Ranger);
 aliasstableLinearRemove= linearRemove;
- Removes a Take!Range from the list in linear time.Returns:A range comprehending the elements after the removed range.Complexity Ο(n) 
- boollinearRemoveElement(Tvalue);
- 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) 
 
Copyright © 1999-2025 by the D Language Foundation | Page generated by
Ddoc on Mon Mar 31 10:28:17 2025