which range to choose, must be less than the number of ranges
two or more ranges
The indexed range. If rs consists of only one range, the return type is an alias of that range's type.
1 auto test() 2 { 3 import std.algorithm.comparison : equal; 4 5 int[4] sarr1 = [1, 2, 3, 4]; 6 int[2] sarr2 = [5, 6]; 7 int[1] sarr3 = [7]; 8 auto arr1 = sarr1[]; 9 auto arr2 = sarr2[]; 10 auto arr3 = sarr3[]; 11 12 { 13 auto s = chooseAmong(0, arr1, arr2, arr3); 14 auto t = s.save; 15 assert(s.length == 4); 16 assert(s[2] == 3); 17 s.popFront(); 18 assert(equal(t, only(1, 2, 3, 4))); 19 } 20 { 21 auto s = chooseAmong(1, arr1, arr2, arr3); 22 assert(s.length == 2); 23 s.front = 8; 24 assert(equal(s, only(8, 6))); 25 } 26 { 27 auto s = chooseAmong(1, arr1, arr2, arr3); 28 assert(s.length == 2); 29 s[1] = 9; 30 assert(equal(s, only(8, 9))); 31 } 32 { 33 auto s = chooseAmong(1, arr2, arr1, arr3)[1 .. 3]; 34 assert(s.length == 2); 35 assert(equal(s, only(2, 3))); 36 } 37 { 38 auto s = chooseAmong(0, arr1, arr2, arr3); 39 assert(s.length == 4); 40 assert(s.back == 4); 41 s.popBack(); 42 s.back = 5; 43 assert(equal(s, only(1, 2, 5))); 44 s.back = 3; 45 assert(equal(s, only(1, 2, 3))); 46 } 47 { 48 uint[5] foo = [1, 2, 3, 4, 5]; 49 uint[5] bar = [6, 7, 8, 9, 10]; 50 auto c = chooseAmong(1, foo[], bar[]); 51 assert(c[3] == 9); 52 c[3] = 42; 53 assert(c[3] == 42); 54 assert(c.moveFront() == 6); 55 assert(c.moveBack() == 10); 56 assert(c.moveAt(4) == 10); 57 } 58 { 59 import std.range : cycle; 60 auto s = chooseAmong(0, cycle(arr2), cycle(arr3)); 61 assert(isInfinite!(typeof(s))); 62 assert(!s.empty); 63 assert(s[100] == 8); 64 assert(s[101] == 9); 65 assert(s[0 .. 3].equal(only(8, 9, 8))); 66 } 67 return 0; 68 } 69 // works at runtime 70 auto a = test(); 71 // and at compile time 72 static b = test();
Choose one of multiple ranges at runtime.
The ranges may be different, but they must have compatible element types. The result is a range that offers the weakest capabilities of all Ranges.