Module std.container
This module defines generic containers.
Construction
To implement the different containers both struct and class based
approaches have been used. make allows for
uniform construction with either approach.
import stdNote that make can infer the element type from the given arguments.
import stdReference semantics
All containers have reference semantics, which means that after assignment both variables refer to the same underlying data.
To make a copy of a container, use the c container primitive.
import stdAttention: If the container is implemented as a class, using an uninitialized instance can cause a null pointer dereference.
import stdUsing an uninitialized struct-based container will work, because the struct intializes itself upon use; however, up to this point the container will not have an identity and assignment does not create two references to the same data.
import stdIt is therefore recommended to always construct containers using
make.
This is in fact necessary to put containers into another container.
For example, to construct an Array of ten empty Arrays, use
the following that calls make ten times.
import stdSubmodules
This module consists of the following submodules:
-         The stdmodule provides an array type with deterministic control of memory, not reliant on the GC unlike built-in arrays..container .array 
-         The stdmodule provides a binary heap implementation that can be applied to any user-provided random-access range..container .binaryheap 
-         The stdmodule provides a doubly-linked list implementation..container .dlist 
-         The stdmodule implements red-black trees..container .rbtree 
-         The stdmodule implements singly-linked lists..container .slist 
-         The stdmodule contains some generic tools commonly used by container implementations..container .util 
The primary range of a container
While some containers offer direct access to their elements e.g. via
opIndex, c or c, access
and modification of a container's contents is generally done through
its primary range type,
which is aliased as C. For example, the primary range type of
Array!int is Array!int.
If the documentation of a member function of a container takes
a parameter of type Range, then it refers to the primary range type of
this container. Oftentimes Take!Range will be used, in which case
the range refers to a span of the elements in the container. Arguments to
these parameters must be obtained from the same container instance
as the one being worked with. It is important to note that many generic range
algorithms return the same range type as their input range.
import stdWhen any range can be passed as an argument to
a member function, the documention usually refers to the parameter's templated
type as Stuff.
import stdContainer primitives
Containers do not form a class hierarchy, instead they implement a common set of primitives (see table below). These primitives each guarantee a specific worst case complexity and thus allow generic code to be written independently of the container implementation.
For example the primitives c and c both
remove the sequence of elements in range r from the container c.
The primitive c guarantees
Ο(nr log nc) complexity in the worst case and
c relaxes this guarantee to Ο(nc).
Since a sequence of elements can be removed from a doubly linked list
in constant time, DList provides the primitive c
as well as c. On the other hand
Array only offers c.
The following table describes the common set of primitives that containers
implement.  A container need not implement all primitives, but if a
primitive is implemented, it must support the syntax described in the syntax column with the semantics described in the description column, and
it must not have a worst-case complexity worse than denoted in big-O notation in
the Ο(·) column.  Below, C means a container type, c is
a value of container type, nx represents the effective length of
value x, which could be a single element (in which case nx is
1), a container, or a range.
| Syntax | Ο( ·) | Description | 
|---|---|---|
| C(x) | nx | Creates a container of type Cfrom either another container or a range.
    The created container must not be a null reference even if x is empty. | 
| c | nc | Returns a duplicate of the container. | 
| c ~ x | nc + nx | Returns the concatenation of candr.xmay be a single
        element or an input range. | 
| x ~ c | nc + nx | Returns the concatenation of xandc.xmay be a
        single element or an input range type. | 
| Iteration | ||
| c | The primary range type associated with the container. | |
| c[] | log nc | Returns a range iterating over the entire container, in a container-defined order. | 
| c[a .. b] | log nc | Fetches a portion of the container from key ato keyb. | 
| Capacity | ||
| c | 1 | Returns trueif the container has no elements,falseotherwise. | 
| c | log nc | Returns the number of elements in the container. | 
| c | nc + n | Forces the number of elements in the container to n.
        If the container ends up growing, the added elements are initialized
        in a container-dependent manner (usually withT). | 
| c | log nc | Returns the maximum number of elements that can be stored in the container without triggering a reallocation. | 
| c | nc | Forces capacityto at leastxwithout reducing it. | 
| Access | ||
| c | log nc | Returns the first element of the container, in a container-defined order. | 
| c | log nc | Destructively reads and returns the first element of the
         container. The slot is not removed from the container; it is left
         initialized with T. This routine need not be defined if         frontreturns aref. | 
| c | log nc | Assigns vto the first element of the container. | 
| c | log nc | Returns the last element of the container, in a container-defined order. | 
| c | log nc | Destructively reads and returns the last element of the
         container. The slot is not removed from the container; it is left
         initialized with T. This routine need not be defined if         frontreturns aref. | 
| c | log nc | Assigns vto the last element of the container. | 
| c[x] | log nc | Provides indexed access into the container. The index type is container-defined. A container may define several index types (and consequently overloaded indexing). | 
| c | log nc | Destructively reads and returns the value at position x. The slot
         is not removed from the container; it is left initialized with         T. | 
| c[x] = v | log nc | Sets element at specified index into the container. | 
| c[x] op= v | log nc | Performs read-modify-write operation at specified index into the container. | 
| Operations | ||
| e in c | log nc | Returns nonzero if e is found in c. | 
| c | log nc | Returns a range of all elements strictly less than v. | 
| c | log nc | Returns a range of all elements strictly greater than v. | 
| c | log nc | Returns a range of all elements in cthat are equal tov. | 
| Modifiers | ||
| c ~= x | nc + nx | Appends xtoc.xmay be a single element or an input range type. | 
| c | nc | Removes all elements in c. | 
| c | nx * log nc | Inserts xincat a position (or positions) chosen byc. | 
| c | nx * log nc | Same as c, but is guaranteed to not invalidate any ranges. | 
| c | nc | Same as cbut relaxes complexity to linear. | 
| c | nc | Same as cbut relaxes complexity to linear. | 
| c | log nc | Removes some element from cand returns it. | 
| c | log nc | Same as c, but is guaranteed to not invalidate any
         iterators. | 
| c | log nc | Inserts vat the front ofc. | 
| c | log nc | Same as c, but guarantees no ranges will be
         invalidated. | 
| c | log nc | Inserts vat the back ofc. | 
| c | log nc | Same as c, but guarantees no ranges will be
         invalidated. | 
| c | log nc | Removes the element at the front of c. | 
| c | log nc | Same as c, but guarantees no ranges will be
         invalidated. | 
| c | log nc | Removes the value at the back of c. | 
| c | log nc | Same as c, but guarantees no ranges will be
         invalidated. | 
| c | nr * log nc | Removes range rfromc. | 
| c | nr * log nc | Same as c, but guarantees iterators are not
         invalidated. | 
| c | nc | Removes range rfromc. | 
| c | nc | Same as c, but guarantees iterators are not
         invalidated. | 
| c | log nc | Removes an element from cby using its keyk.
         The key's type is defined by the container. | 
Authors
Steven Schveighoffer, Andrei Alexandrescu
License
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at ).