D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4348 - std.container.SList append
Summary: std.container.SList append
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-19 12:52 UTC by bearophile_hugs
Modified: 2010-06-20 03:31 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-06-19 12:52:03 UTC
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 Andrei Alexandrescu 2010-06-19 14:51:02 UTC
~= 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 bearophile_hugs 2010-06-19 15:20:42 UTC
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 :-)