-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
83 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}; | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters