Struct List
List
is a container that supports constant time insertion and removal of elements from anywhere in the container.
struct List(_Type, _Allocator, bool _supportGC = shouldAddGCRange!_Type, bool _bidirectional = true)
;
Fast random access is not supported. It is usually implemented as a linked list
Template parameters:
_Type
= element type.
_Allocator
= Type of the allocator object used to define the storage allocation model. By default DefaultAllocator
is used.
_supportGC
_bidirectional
Constructors
Name | Description |
---|---|
this
(a)
|
Constructs a empty List with allocator a .
|
this
(nil)
|
Constructs a empty List
|
this
(rhs)
|
Constructs a List object from other list.
|
this
(rhs, )
|
Forward constructor. |
this
(range)
|
Constructs a bidirectional List object from range of elements.
|
Properties
Name | Type | Description |
---|---|---|
allocator [get]
|
CopyTypeQualifiers!(This,AllocatorType) | Returns copy of allocator. |
begin [get]
|
auto | |
cbegin [get]
|
List | |
empty [get]
|
bool | Returns whether the list is empty (i.e. whether its length is 0). |
length [get]
|
size_t | Returns the length of the list, in terms of number of elements. |
Methods
Name | Description |
---|---|
back
()
|
Returns a reference to the last element in the list. |
backCopy
()
|
Returns a copy of the first element in the list. |
build
(allocator, args)
|
Static function which return List construct from arguments args .
|
contains
(elm)
|
Same as operator in
|
emplaceBack
(args)
|
Appends a new element to the end of the bidirectional list. The element is constructed through emplace .
|
emplaceFront
(args)
|
Appends a new element to the begin of the list. The element is constructed through emplace .
|
front
()
|
Returns a reference to the first element in the list. |
frontCopy
()
|
Returns a copy of the first element in the list. |
moveBack
()
|
Move of the first element in the list and return it. |
moveFront
()
|
Move of the first element in the list and return it. |
opAssign
(rhs)
|
Assigns a new value rhs to the list, replacing its current contents.
|
opBinaryRight
(elm)
|
Operator in
|
opCmp
(rhs)
|
Compares the contents of a list with another list or range. |
opDollar
()
|
Returns the length of the list, in terms of number of elements. |
opEquals
(nil)
|
Compares the contents of a list with another list, range or null. |
opIndex
()
|
Return slice of all elements. |
popBack
()
|
Pop the last element of the bidirectional list, effectively reducing its length by 1. |
popFront
()
|
Pop the last element of the bidirectional list, effectively reducing its length by 1. |
proxySwap
(rhs)
|
Swaps the contents of this and rhs .
|
pushBack
(range)
|
Extends the List by appending additional elements at the end of list.
|
pushFront
(range)
|
Extends the List by appending additional elements at the end of list.
|
release
()
|
Erases and deallocate the contents of the List , which becomes an empty list (with a length of 0 elements).
|
Aliases
Name | Description |
---|---|
AllocatorType
|
Type of the allocator object used to define the storage allocation model. By default DefaultAllocator is used.
|
append
|
Alias to pushBack |
bidirectional
|
|
clear
|
Erases and deallocate the contents of the List , which becomes an empty list (with a length of 0 elements).
|
ConstIterator
|
Const iterator for list. |
ElementReferenceType
|
Type of reference to elements. |
ElementType
|
Type of elements. |
hasStatelessAllocator
|
True if allocator doesn't have state. |
Iterator
|
Iterator for list. |
opOpAssign
|
Operator ~= is same as append/pushBack
|
prepend
|
Alias to pushFront |
put
|
Alias to pushBack |
supportGC
|
Example
import std .range : only;
import std .algorithm : map, equal;
static struct Foo{
int i;
string str;
}
List!(Foo) list;
assert(list .empty);
list .append(Foo(1, "A"));
assert(list .length == 1);
list .append(only(Foo(2, "B"), Foo(3, "C")));
assert(list .length == 3);
list .emplaceBack(4, "D");
assert(list .length == 4);
list = List!(Foo) .build(Foo(-1, "X"), Foo(-2, "Y"));
assert(equal(list[] .map!(e => e .str), only("X", "Y")));
list .release();
assert(list .length == 0);