@@ -9,22 +9,29 @@ To implement the different containers, both struct and class based
99approaches have been used. $(REF make, std,container,util) allows for
1010uniform construction with either approach.
1111
12+ $(RUNNABLE_EXAMPLE
1213---
1314import std.container;
1415// Construct a red-black tree and an array both containing the values 1, 2, 3.
1516// RedBlackTree should typically be allocated using `new`
1617RedBlackTree!int rbTree = new RedBlackTree!int(1, 2, 3);
1718// But `new` should not be used with Array
1819Array!int array = Array!int(1, 2, 3);
20+
1921// `make` hides the differences
2022RedBlackTree!int rbTree2 = make!(RedBlackTree!int)(1, 2, 3);
2123Array!int array2 = make!(Array!int)(1, 2, 3);
24+
25+ assert(rbTree == rbTree2);
26+ assert(array == array2);
2227---
28+ )
2329
2430Note that `make` can infer the element type from the given arguments.
2531
2632---
2733import std.container;
34+
2835auto rbTree = make!RedBlackTree(1, 2, 3); // RedBlackTree!int
2936auto array = make!Array("1", "2", "3"); // Array!string
3037---
@@ -35,8 +42,11 @@ All containers have reference semantics, which means that after
3542assignment both variables refer to the same underlying data.
3643
3744To make a copy of a container, use the `c.dup` container primitive.
45+
46+ $(RUNNABLE_EXAMPLE
3847---
39- import std.container, std.range;
48+ import std.algorithm, std.container, std.range;
49+
4050Array!int originalArray = make!(Array!int)(1, 2, 3);
4151Array!int secondArray = originalArray;
4252assert(equal(originalArray[], secondArray[]));
@@ -51,6 +61,7 @@ secondArray[0] = 1;
5161// assert that originalArray has not been affected
5262assert(originalArray[0] == 12);
5363---
64+ )
5465
5566$(B Attention:) If the container is implemented as a class, using an
5667uninitialized instance can cause a null pointer dereference.
@@ -67,6 +78,7 @@ intializes itself upon use; however, up to this point the container will not
6778have an identity and assignment does not create two references to the same
6879data.
6980
81+ $(RUNNABLE_EXAMPLE
7082---
7183import std.container;
7284
@@ -84,6 +96,8 @@ array1 = array2;
8496array1.removeBack();
8597assert(array2.empty);
8698---
99+ )
100+
87101It is therefore recommended to always construct containers using
88102$(REF make, std,container,util).
89103
@@ -147,9 +161,10 @@ these parameters $(B must) be obtained from the same container instance
147161as the one being worked with. It is important to note that many generic range
148162algorithms return the same range type as their input range.
149163
164+ $(RUNNABLE_EXAMPLE
150165---
151166import std.algorithm.comparison : equal;
152- import std.algorithm.iteration : find;
167+ import std.algorithm.searching : find;
153168import std.container;
154169import std.range : take;
155170
@@ -168,11 +183,13 @@ array.linearRemove(array[].find(2).take(1));
168183
169184assert(array[].equal([1, 3]));
170185---
186+ )
171187
172188When any $(MREF_ALTTEXT range, std, range) can be passed as an argument to
173189a member function, the documention usually refers to the parameter's templated
174190type as `Stuff`.
175191
192+ $(RUNNABLE_EXAMPLE
176193---
177194import std.algorithm.comparison : equal;
178195import std.container;
@@ -186,6 +203,7 @@ array.insertBack(iota(3, 10));
186203
187204assert(array[].equal([1, 2, 3, 4, 5, 6, 7, 8, 9]));
188205---
206+ )
189207
190208$(H3 $(LNAME2 primitives, Container Primitives))
191209
0 commit comments