|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | + |
| 3 | +import 'utils/list_base.dart'; |
| 4 | + |
| 5 | +class Column<E> extends ListBase<E> { |
| 6 | + Column(List<E> records) : super(records); |
| 7 | + |
| 8 | + /// Get a casted [Column]. |
| 9 | + Column<T> cast<T>() => Column(super.cast<T>()); |
| 10 | + |
| 11 | + // ************* count ***************** |
| 12 | + |
| 13 | + /// Counts occurrences of [element]. |
| 14 | + int count(E object) => where((element) => element == object).length; |
| 15 | + |
| 16 | + /// Counts column-elements equaling any element contained by [pool]. |
| 17 | + int countElementOccurrencesOf(Set<E> pool) => |
| 18 | + where((element) => pool.contains(element)).length; |
| 19 | + |
| 20 | + // ************* null freeing ************* |
| 21 | + |
| 22 | + /// Returns [Column] without nulls. If [replaceWith] is set to null null-records |
| 23 | + /// are removed from the column, whereas they will replaced by its value otherwise. |
| 24 | + Column<E> nullFree({E? replaceWith = null}) => |
| 25 | + Column<E>(nullFreeIterable(replaceWith: replaceWith).toList()); |
| 26 | + |
| 27 | + /// Iterable-returning counterpart of [nullFree]. |
| 28 | + /// Consult [nullFree] for further documentation. |
| 29 | + Iterable<E> nullFreeIterable({E? replaceWith = null}) => replaceWith == null |
| 30 | + ? where((element) => element != null) |
| 31 | + : map((e) => e ?? replaceWith); |
| 32 | + |
| 33 | + // ****************** transformation ****************** |
| 34 | + |
| 35 | + /// Returns List<num> containing the cumulative sum of the column. |
| 36 | + /// [treatNullsAsZeros] being set to true will lead to the the result being of |
| 37 | + /// the same length as the original column. Otherwise, null records won't be |
| 38 | + /// accounted for. |
| 39 | + /// |
| 40 | + /// Note: requires column records type to be a subtype of <num?> |
| 41 | + List<num> cumulativeSum({bool treatNullsAsZeros = true}) => |
| 42 | + _nullFreedNums(treatNullsAsZeros: treatNullsAsZeros).fold( |
| 43 | + [], |
| 44 | + (sums, element) => |
| 45 | + sums..add(sums.isEmpty ? element : sums.last + element)); |
| 46 | + |
| 47 | + // **************** accumulation **************** |
| 48 | + |
| 49 | + /// Returns the column's mean. Set [treatNullsAsZeros] for the method |
| 50 | + /// to not account for null records. |
| 51 | + /// |
| 52 | + /// Note: requires column records type to be a subtype of <num?> |
| 53 | + double mean({bool treatNullsAsZeros = true}) => |
| 54 | + _nullFreedNums(treatNullsAsZeros: treatNullsAsZeros).average; |
| 55 | + |
| 56 | + /// Returns the column's max. Requires column records type to be a subtype of <num?> |
| 57 | + num max() => _nullFreedNums().max; |
| 58 | + |
| 59 | + /// Returns the column's min. Requires column records type to be a subtype of <num?> |
| 60 | + num min() => _nullFreedNums().min; |
| 61 | + |
| 62 | + /// Returns the column's sum. Requires column records type to be a subtype of <num?> |
| 63 | + num sum() => _nullFreedNums().sum; |
| 64 | + |
| 65 | + Iterable<num> _nullFreedNums({bool treatNullsAsZeros = false}) => cast<num?>() |
| 66 | + .nullFreeIterable(replaceWith: treatNullsAsZeros ? 0 : null) |
| 67 | + .cast<num>(); |
| 68 | + |
| 69 | + // ***************** masks ******************* |
| 70 | + |
| 71 | + Mask equals(E reference) => |
| 72 | + map((element) => element == reference).toList().cast<bool>(); |
| 73 | + |
| 74 | + Mask unequals(E reference) => |
| 75 | + map((element) => element != reference).toList().cast<bool>(); |
| 76 | + |
| 77 | + Mask isIn(Set<E> pool) => |
| 78 | + map((element) => pool.contains(element)).toList().cast<bool>(); |
| 79 | + |
| 80 | + Mask isNotIn(Set<E> pool) => |
| 81 | + map((element) => !pool.contains(element)).toList().cast<bool>(); |
| 82 | + |
| 83 | + /// Get a [Mask] by mapping [test] to the column records. |
| 84 | + Mask maskFrom(bool Function(E) test) => map(test).toList().cast<bool>(); |
| 85 | + |
| 86 | + // ****************** numerical column masks ********************* |
| 87 | + |
| 88 | + /// Requires the column records type to be a subtype of num (i.e. non-null!) |
| 89 | + Mask operator <(num reference) => |
| 90 | + cast<num>().map((element) => element < reference).toList().cast<bool>(); |
| 91 | + |
| 92 | + /// Requires the column records type to be a subtype of num (i.e. non-null!) |
| 93 | + Mask operator >(num reference) => |
| 94 | + cast<num>().map((element) => element > reference).toList().cast<bool>(); |
| 95 | + |
| 96 | + /// Requires the column records type to be a subtype of num (i.e. non-null!) |
| 97 | + Mask operator <=(num reference) => |
| 98 | + cast<num>().map((element) => element <= reference).toList().cast<bool>(); |
| 99 | + |
| 100 | + /// Requires the column records type to be a subtype of num (i.e. non-null!) |
| 101 | + Mask operator >=(num reference) => |
| 102 | + cast<num>().map((element) => element >= reference).toList().cast<bool>(); |
| 103 | +} |
| 104 | + |
| 105 | +typedef Mask = List<bool>; |
| 106 | + |
| 107 | +/// Operators for the conjunction of masks. |
| 108 | +extension MaskExtensions on Mask { |
| 109 | + Mask operator &(Mask other) => |
| 110 | + IterableZip([this, other]).map((e) => e.first && e.last).toList(); |
| 111 | + |
| 112 | + Mask operator |(Mask other) => |
| 113 | + IterableZip([this, other]).map((e) => e.first || e.last).toList(); |
| 114 | + |
| 115 | + Mask operator ^(Mask other) => |
| 116 | + IterableZip([this, other]).map((e) => e.first ^ e.last).toList(); |
| 117 | +} |
0 commit comments