Comment #0 by bearophile_hugs — 2010-06-19T12:52:03Z
This shows how you can append to a std.container.SList:
import std.container: SList;
void main() {
auto l = SList!int(1, 2);
l.insertAfter(l[], 3);
}
But the standard D syntax too can be supported, despite it's O(n):
import std.container: SList;
void main() {
auto l = SList!int(1, 2);
l ~= 3;
}
(The member function "insertFront" might be named "prepend", that is shorter , equally readable and contains no upper case letters).
Comment #1 by andrei — 2010-06-19T14:51:02Z
~= is only for containers that can implement it in time independent of the size of the container. Writing s.insertAfter(s[], value) hints the user that the cost is higher (i.e. proportional to the length of s[]).
Comment #2 by bearophile_hugs — 2010-06-19T15:20:42Z
Answer to Comment 1: thank you for your answer, I didn't know about this rule.
In arrays the append can require a full array copy, so it can be O(n), but it's (hopefully) O(1) on amortized time.
If this rule is present and well established then you can close this bug report (the suggestion about the "prepend" name is for you, but you can ignore it if you don't like it).
Another possibility is to find a compromise: instead of writing something hairy like:
s.insertAfter(s[], value)
You can use:
s.linearAppend(value)
That is less noisy and equally clear in its complexity :-)