Summary: Support static arrays in some algorithms
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: patch
          Severity: normal
          Priority: P2
         Component: Phobos

--- Comment #0 from 2010-04-21 15:53:45 PDT ---
Fixed-sized stack-allocated static arrays can't support all features of a
dynamic array, so they are not a fully flexible range. So it's not possible to
perform some operations/algorithms on static arrays.

But there are several basic operations that I might desire to perform on static
arrays too, like to test if their items are sorted (isSorted), to sort them
(sort()), or to compare the items of a sequence to the items of a static array

Currently std.algorithm.isSorted doesn't work with static arrays, but I see no
point in forbidding isSorted() or sort() on a static array. A simple workaround
is to use:
to give isSorted a dynamic array view of the same contents of the static array.
This is not too much bad looking, but it gratuitously breaks some generic code.

This shows a possible simple way to fix it: to rename the current isSorted as
isSorted_impl, and add this function template that uses isSorted_impl:

import std.algorithm: isSorted_impl = isSorted;

bool isSorted(alias less="a < b", Range)(Range data)
    if (__traits(isStaticArray, Range) || isForwardRange!Range) {
        static if (__traits(isStaticArray, Range))
            return isSorted_impl!(less)(data[]);
            return isSorted_impl!(less)(data);

void main() {
    int[3] b = [1, 2, 3];

Better, the original isSorted() can become a function statically nested in this

std.range.array, equal() and sort() can be augmented in similar ways.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to