1 module dkh.array; 2 3 /// Make static array literal 4 T[N] fixed(T, size_t N)(T[N] a) {return a;} 5 6 /// 7 unittest { 8 auto a = [[1, 2].fixed]; 9 assert(is(typeof(a) == int[2][])); 10 } 11 12 /// Unstable 13 int[2] findFirst2D(T, U)(in T mp, in U c) { 14 import std.conv : to; 15 int R = mp.length.to!int; 16 int C = mp[0].length.to!int; 17 foreach (i; 0..R) { 18 foreach (j; 0..C) { 19 if (mp[i][j] == c) return [i, j]; 20 } 21 } 22 assert(false); 23 } 24 25 /// 26 unittest { 27 string[] mp = [ 28 "s..", 29 "...", 30 "#g#"]; 31 assert(mp.findFirst2D('s') == [0, 0]); 32 assert(mp.findFirst2D('g') == [2, 1]); 33 } 34 35 /// Unstable 36 auto ref at2D(T, U)(ref T mp, in U idx) { 37 return mp[idx[0]][idx[1]]; 38 } 39 40 /// 41 unittest { 42 string[] mp = [ 43 "s..", 44 "...", 45 "#g#"]; 46 assert(mp.at2D([0, 0]) == 's'); 47 assert(mp.at2D([2, 1]) == 'g'); 48 }