1 module onsdata.row; 2 3 import std.stdio; 4 import std.conv; 5 import std.string; 6 import onsdata; 7 8 import Cell = onsdata.cell; 9 10 enum RowStatus { 11 invalid, 12 valid, 13 changed 14 } 15 class Row(T) { 16 RowStatus status = RowStatus.invalid; 17 18 this() {} 19 this(size_t aLength) { length = aLength; } 20 this(Row!T source) { 21 this(source.length); 22 foreach(i, c; source.cells) cells[i] = c; 23 } 24 25 T[] cells; 26 @property void length(size_t aLength) { cells.length = aLength; } 27 @property auto length() { return cells.length; } 28 @property void width(size_t aWidth) { length = aWidth; } 29 @property auto width() { return length; } 30 31 void init(T initValue) { cells[] = initValue; } 32 void value(T val) { cells[] = val; } 33 34 void opIndexAssign(T value, size_t i) { 35 if (i < length) cells[i] = value; 36 } 37 auto opIndex(size_t i) { 38 if (i < length) return cells[i]; 39 return T.init; // blää 40 } 41 42 void min(size_t target, size_t[] cols) { if (cols) this[target] = min(cols); } 43 T min(size_t[] cols) { 44 if (cols.length == 0) return 0; 45 46 T result = this[cols[0]]; 47 foreach(c; cols) if (result > this[c]) result = this[c]; 48 return result; 49 } 50 51 /+ avg - returns the average value of selected columns and write the result in the target col +/ 52 void avg(size_t target, size_t[] cols) { this[target] = avg(cols); } 53 /+ avg - returns the average value of selected columns +/ 54 T avg(size_t[] cols) { 55 T[] values; 56 foreach(c; cols) values ~= this[c]; 57 return to!T(Cell.avg!T(values)); 58 } 59 60 /+ +/ 61 void max(size_t target, size_t[] cols) { if (cols) this[target] = max(cols); } 62 T max(size_t[] cols) { 63 if (cols.length == 0) return 0; 64 65 T result = this[cols[0]]; 66 foreach(c; cols) if (result < this[c]) result = this[c]; 67 return result; 68 } 69 70 void sum(size_t target, size_t[] cols) { if (cols) this[target] = sum(cols); } 71 T sum(size_t[] cols) { 72 if (cols.length == 0) return 0; 73 74 T result = 0; 75 foreach(c; cols) result += this[c]; 76 return result; 77 } 78 79 void delta( size_t target, size_t[2] cols) { this[target] = delta(cols); } 80 T delta( size_t[2] cols) { return this[cols[1]] - this[cols[0]]; } 81 82 auto copy() { 83 Row!T result = new Row!T(length); 84 foreach(i, c; cells) result[i] = c; 85 return result; 86 } 87 88 override string toString() { 89 string[] values; 90 foreach(x; cells) values ~= to!string(x); 91 return "["~values.join(",\t")~"]"; 92 } 93 } 94 T[] newRow(T)(size_t length) { 95 T[] result; result.length = length; 96 foreach(ref r; result) r = 0; 97 return result; 98 } 99 100 Row!T[] copy(T)(Row!T[] rows) { 101 Row!T[] result; 102 result.length = rows.length; 103 foreach(i, r; rows) result[i] = copy(r); 104 return result; 105 } 106 107 bool isIn(T)(T value, T[] values) { 108 foreach(v; values) if (v == value) return true; 109 return false; 110 } 111 bool isNotIn(T)(T value, T[] values) { 112 foreach(v; values) if (v == value) return false; 113 return true; 114 } 115