Skip to content

Commit

Permalink
Add Example to SList.insertAfter documentation
Browse files Browse the repository at this point in the history
Along with matching unit test.
  • Loading branch information
brad-anderson committed Jun 13, 2012
1 parent 9d0e3f9 commit 58c82f1
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions std/container.d
Expand Up @@ -1200,8 +1200,8 @@ Inserts $(D stuff) after range $(D r), 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 uses $(D r) as a potentially fast way to reach the last
node in the list. (Ideally $(D r) is positioned near or at the last
element of the list.)
node in the list. Ideally $(D r) is positioned near or at the last
element of the list.
$(D stuff) can be a value convertible to $(D T) or a range of objects
convertible to $(D T). The stable version behaves the same, but
Expand All @@ -1212,6 +1212,15 @@ Returns: The number of values inserted.
Complexity: $(BIGOH k + m), where $(D k) is the number of elements in
$(D r) and $(D m) is the length of $(D stuff).
Examples:
--------------------
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_t insertAfter(Stuff)(Range r, Stuff stuff)
Expand Down Expand Up @@ -1411,6 +1420,16 @@ unittest
assert(s == SList!int(1, 2, 5, 3, 4));
}

unittest
{
// insertAfter documentation 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"]));
}

unittest
{
auto s = SList!int(1, 2, 3, 4, 5);
Expand Down

0 comments on commit 58c82f1

Please sign in to comment.