Thread overview
[Issue 3384] New: toArray
Oct 09, 2009
David Simcha
Nov 20, 2009
David Simcha
October 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3384

           Summary: toArray
           Product: D
           Version: 2.033
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: patch
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: dsimcha@yahoo.com


--- Comment #0 from David Simcha <dsimcha@yahoo.com> 2009-10-09 10:16:21 PDT ---
Phobos needs an easy, efficient way of converting an arbitrary finite range into an array, for example to allow for eager evaluation.  Below is a proposed solution.  It relies on the existence of newVoid().  (See Bugzilla 3383.)

/**Converts any range to an array on the GC heap by the most efficient means
 * available.  If it is already an array, duplicates the range.*/
Unqual!(ElementType!(T))[] toArray(T)(T range) if(isInputRange!(T)) {
    static if(isArray!(T)) {
        // Allow fast copying by assuming that the input is an array.
        return range.dup;
    } else static if(hasLength!(T)) {
        // Preallocate array, then copy.
        auto ret = newVoid!(Unqual!(ElementType!(T)))(range.length);
        static if(is(typeof(ret[] = range[]))) {
            ret[] = range[];
        } else {
            size_t pos = 0;
            foreach(elem; range) {
                ret[pos++] = elem;
            }
        }
        return ret;
    } else {
        // Don't have length, have to use appending.
        Unqual!(ElementType!(T))[] ret;
        auto app = appender(&ret);
        foreach(elem; range) {
            app.put(elem);
        }
        return ret;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 11, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3384


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 20, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3384


David Simcha <dsimcha@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


--- Comment #1 from David Simcha <dsimcha@yahoo.com> 2009-11-19 19:43:42 PST ---
I was looking through std.array for unrelated reasons and noticed that there is an array() function already in there.  I have no idea when that was added, but it solves this bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 20, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3384



--- Comment #2 from Andrei Alexandrescu <andrei@metalanguage.com> 2009-11-19 20:07:28 PST ---
Thanks!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------