On Mon, 13 Jun 2011 12:15:40 -0400, nrgyzer <nrgy...@gmail.com> wrote:
Hi there,
is there any possibility to get a sliced array from another array
between two ranges like:
int[uint] myArray;
myArray[10] = 1000;
myArray[20] = 2000;
myArray[30] = 3000;
myArray[40] = 4000;
myArray[50] = 5000;
int[] newArray = myArray[>= 20 .. <= 40]; // not able to do this
writeln(newArray); // should print [2000, 3000, 4000]
Is there any way to do this?
import dcollections.TreeMap;
auto myArray = new TreeMap!(uint, int);
myArray[10] = 1000;
myArray[20] = 2000;
myArray[30] = 3000;
myArray[40] = 4000;
myArray[50] = 5000;
// this is a little kludgy, but necessary since you require <= 40
auto c = myArray.elemAt(40);
c.popFront();
int newArray = array(myArray[20..c]);
Note two things:
1. int[uint] is a hash, and so has no particular order. Therefore, there
is no guarantee of iteration order, or that a range of such a container
(if one existed) would be properly constructed with two keys. A TreeMap,
or RedBlackTree, is sorted, and so the order is guaranteed.
2. dcollections.TreeMap is implemented with the same collection as
std.container.RedBlackTree, so you could potentially do the same thing
with it. But the dcollections.TreeMap API is more polished.
-Steve