[Issue 4114] New: Support static arrays in some algorithms
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Apr 21 15:53:49 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4114
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
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 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
(equals).
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:
isSorted(stat_arr[])
sort(stat_arr[])
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[]);
else
return isSorted_impl!(less)(data);
}
void main() {
int[3] b = [1, 2, 3];
assert(isSorted(b));
}
Better, the original isSorted() can become a function statically nested in this
wrapper.
std.range.array, equal() and sort() can be augmented in similar ways.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list