Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Sep 29, 2023
1 parent 35170e1 commit 9c99475
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 35 deletions.
1 change: 1 addition & 0 deletions sugar/lib/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export 'src/collection/lists.dart';
export 'src/collection/maps.dart';
export 'src/collection/move.dart';
export 'src/collection/sets.dart';
export 'src/collection/splay_tree_maps.dart';
2 changes: 2 additions & 0 deletions sugar/lib/crdt.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'src/crdt/string_index.dart';
export 'src/crdt/string_index_set.dart';
15 changes: 15 additions & 0 deletions sugar/lib/src/collection/splay_tree_maps.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'dart:collection';

extension SplayTreeMaps<K, V> on SplayTreeMap<K, V> {

V? firstValueAfter(K key) => switch (firstKeyAfter(key)) {
null => null,
final key => this[key],
};

V? lastValueBefore(K key) => switch (lastKeyBefore(key)) {
null => null,
final key => this[key],
};

}
35 changes: 0 additions & 35 deletions sugar/lib/src/crdt/sios.dart

This file was deleted.

64 changes: 64 additions & 0 deletions sugar/lib/src/crdt/string_index_set.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'dart:collection';

import 'package:sugar/sugar.dart';

class StringIndexSet<E> extends Iterable<E> {

/// Whether to strictly validate a string index's format. True by default.
final bool strict;

final SplayTreeMap<String, E> _map;
final HashMap<E, String> _inverse;
final List<E> _list;
final bool Function(E, E) _equals;
final int Function(E) _hash;

StringIndexSet._(
this._map,
this._inverse,
this._list,
this._equals,
this._hash, {
required this.strict,
});


void operator []= (Object index, E element) {
if (_inverse.containsKey(element)) {
return;
}

switch (index) {
case final String index:
if (_map.firstValueAfter(index) case final after?) {
_list.insert(_list.indexWhere((e) => _equals(e, after)), element);

} else {
_list.add(element);
}

_map[index] = element;
_inverse[element] = index;

// Different semantics between map and list
case final int index when index < _list.length:
final min = _inverse[_list.elementAtOrNull(index - 1)] ?? StringIndex.min;
final max = _inverse[_list.elementAtOrNull(index + 1)] ?? StringIndex.max;
final string = StringIndex.between(min: min, max: max, strict: strict);
_list.insert(index, element);

}
}


@override
Iterator<E> get iterator => _list.iterator;

@override
E get first => _list.first;

set first(E first) {

}

}
1 change: 1 addition & 0 deletions sugar/lib/sugar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export 'collection_aggregate.dart';
export 'core.dart';
export 'core_range.dart';
export 'core_system.dart';
export 'crdt.dart';
export 'math.dart';
export 'time.dart';
export 'time_interop.dart';
Expand Down

0 comments on commit 9c99475

Please sign in to comment.