Skip to content

Commit 6d5f0f1

Browse files
committed
Make examples runnable
Fix equal/find imports.
1 parent 6a3a8de commit 6d5f0f1

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

std/container/package.d

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

std/container/rbtree.d

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,13 +1531,18 @@ if (is(typeof((ref const T a) => binaryFun!less(a, a))))
15311531
Complexity: $(BIGOH m log(n)) (where m is the number of elements to remove)
15321532
15331533
Example:
1534+
$(RUNNABLE_EXAMPLE
15341535
--------------------
1536+
import std.algorithm, std.container;
1537+
15351538
auto rbt = redBlackTree!true(0, 1, 1, 1, 4, 5, 7);
15361539
rbt.removeKey(1, 4, 7);
15371540
assert(equal(rbt[], [0, 1, 1, 5]));
1541+
15381542
rbt.removeKey(1, 1, 0);
15391543
assert(equal(rbt[], [5]));
15401544
--------------------
1545+
)
15411546
+/
15421547
size_t removeKey(U...)(U elems)
15431548
if (allSatisfy!(isImplicitlyConvertibleToElem, U))

std/container/slist.d

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,18 @@ Complexity: $(BIGOH k + m), where `k` is the number of elements in
506506
`r` and `m` is the length of `stuff`.
507507
508508
Example:
509+
$(RUNNABLE_EXAMPLE
509510
--------------------
511+
import std.algorithm, std.container, std.range;
512+
510513
auto sl = SList!string(["a", "b", "d"]);
511514
sl.insertAfter(sl[], "e"); // insert at the end (slowest)
512-
assert(std.algorithm.equal(sl[], ["a", "b", "d", "e"]));
513-
sl.insertAfter(std.range.take(sl[], 2), "c"); // insert after "b"
514-
assert(std.algorithm.equal(sl[], ["a", "b", "c", "d", "e"]));
515+
assert(equal(sl[], ["a", "b", "d", "e"]));
516+
517+
sl.insertAfter(take(sl[], 2), "c"); // insert after "b"
518+
assert(equal(sl[], ["a", "b", "c", "d", "e"]));
515519
--------------------
520+
)
516521
*/
517522

518523
size_t insertAfter(Stuff)(Range r, Stuff stuff)

0 commit comments

Comments
 (0)