Skip to content

Commit

Permalink
Rename mask getters
Browse files Browse the repository at this point in the history
  • Loading branch information
w2sv committed Sep 15, 2022
1 parent 44a79c3 commit 70cf4f2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
14 changes: 7 additions & 7 deletions lib/src/column.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class Column<E> extends ListBase<E> {

// ***************** masks *******************

Mask equals(E reference) =>
Mask eq(E reference) =>
map((element) => element == reference).toList().cast<bool>();

Mask unequals(E reference) =>
Mask neq(E reference) =>
map((element) => element != reference).toList().cast<bool>();

Mask isIn(Set<E> pool) =>
Expand All @@ -81,24 +81,24 @@ class Column<E> extends ListBase<E> {
map((element) => !pool.contains(element)).toList().cast<bool>();

/// Get a [Mask] by mapping [test] to the column records.
Mask maskFrom(bool Function(E) test) => map(test).toList().cast<bool>();
Mask getMask(bool Function(E) test) => map(test).toList().cast<bool>();

// ****************** numerical column masks *********************

/// Requires the column records type to be a subtype of num (i.e. non-null!)
Mask operator <(num reference) =>
Mask st(num reference) =>
cast<num>().map((element) => element < reference).toList().cast<bool>();

/// Requires the column records type to be a subtype of num (i.e. non-null!)
Mask operator >(num reference) =>
Mask lt(num reference) =>
cast<num>().map((element) => element > reference).toList().cast<bool>();

/// Requires the column records type to be a subtype of num (i.e. non-null!)
Mask operator <=(num reference) =>
Mask seq(num reference) =>
cast<num>().map((element) => element <= reference).toList().cast<bool>();

/// Requires the column records type to be a subtype of num (i.e. non-null!)
Mask operator >=(num reference) =>
Mask leq(num reference) =>
cast<num>().map((element) => element >= reference).toList().cast<bool>();
}

Expand Down
17 changes: 9 additions & 8 deletions lib/src/utils/element_position_tracking_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import 'extensions/list.dart';

/// List keeping track of element indices by storing them
/// in a map and updating them upon list mutation
class ElementPositionTrackingList<T> extends ListBase<T> {
Map<T, int> _object2Index;
class ElementPositionTrackingList<E> extends ListBase<E> {
Map<E, int> _object2Index;

ElementPositionTrackingList(List<T> elements)
ElementPositionTrackingList(List<E> elements)
: _object2Index = elements.asInvertedMap(),
super(elements);

ElementPositionTrackingList<T> copy() =>
ElementPositionTrackingList<E> copy() =>
ElementPositionTrackingList(ListExtensions(this).copy());

// *************** overrides *******************
Expand All @@ -20,24 +20,25 @@ class ElementPositionTrackingList<T> extends ListBase<T> {
_object2Index.containsKey(element);

@override
void add(T element) {
void add(E element) {
super.add(element);
_object2Index[element] = length - 1;
}

@override
void addAll(Iterable<T> iterable) {
void addAll(Iterable<E> iterable) {
super.addAll(iterable);
_object2Index = asInvertedMap();
}

@override
T removeAt(int index) {
E removeAt(int index) {
final removedElement = super.removeAt(index);
_object2Index = asInvertedMap();
return removedElement;
}

@override
int indexOf(Object? element, [int? start]) => _object2Index[element]!;
int indexOf(Object? element, [int? start]) =>
_object2Index[element]!;
}
14 changes: 7 additions & 7 deletions test/src/column_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ void main(){
});

test('masks', (){
expect(column.equals(3), [false, true, false, false, false, false, false, false, true, false]);
expect(column.unequals(3), [true, false, true, true, true, true, true, true, false, true]);
expect(column.eq(3), [false, true, false, false, false, false, false, false, true, false]);
expect(column.neq(3), [true, false, true, true, true, true, true, true, false, true]);
expect(column.isIn({2, 7, 87}), [false, false, true, false, true, false, true, false, false, false]);
expect(column.isNotIn({2, 7, 87}), [true, true, false, true, false, true, false, true, true, true]);
expect(column.maskFrom((p0) => p0 == null ? true : p0.isEven), [false, false, false, true, true, true, false, true, false, true]);
expect(column.getMask((p0) => p0 == null ? true : p0.isEven), [false, false, false, true, true, true, false, true, false, true]);

final nullFreed = column.nullFree(replaceWith: 97);
expect(nullFreed > 20, [false, false, false, true, false, false, true, true, false, true]);
expect(nullFreed < 20, [true, true, true, false, true, true, false, false, true, false]);
expect(nullFreed >= 20, [false, false, false, true, false, false, true, true, false, true]);
expect(nullFreed <= 20, [true, true, true, false, true, true, false, false, true, false]);
expect(nullFreed.lt(20), [false, false, false, true, false, false, true, true, false, true]);
expect(nullFreed.st(20), [true, true, true, false, true, true, false, false, true, false]);
expect(nullFreed.leq(20), [false, false, false, true, false, false, true, true, false, true]);
expect(nullFreed.seq(20), [true, true, true, false, true, true, false, false, true, false]);
});

test('mask concatenations', (){
Expand Down
2 changes: 1 addition & 1 deletion test/src/dataframe_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void main() {
' a b c \n'
'0 | 43 omg null\n'
'1 | -32 noob -0.7');
expect(df.rowsWhere((df('a') < 0) | df<String>('b').maskFrom((el) => el.length > 0)).toString(),
expect(df.rowsWhere(df('a').st(0) | df<String>('b').getMask((el) => el.length > 0)).toString(),
' a b c \n'
'0 | 43 omg null \n'
'1 | -9 ubiquitous 101.8\n'
Expand Down

0 comments on commit 70cf4f2

Please sign in to comment.