Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Sep 30, 2023
1 parent 9c99475 commit d4099f0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 66 deletions.
2 changes: 1 addition & 1 deletion sugar/lib/crdt.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export 'src/crdt/string_index.dart';
export 'src/crdt/string_index_set.dart';
export 'src/crdt/string_indexed_list.dart';
64 changes: 0 additions & 64 deletions sugar/lib/src/crdt/string_index_set.dart

This file was deleted.

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

import 'package:sugar/sugar.dart';

class Sil<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;

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


bool put(String index, E element) {
if (_inverse.containsKey(element)) {
return false;
}

final old = _map[index];
if (old == null) {
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;

} else {
_map[index] = element;
_inverse.remove(old);
_inverse[element] = index;
_list[_list.indexWhere((e) => _equals(e, old))] = element;
}

return true;
}


int indexOf(E element) => _list.indexOf(element);

String? stringIndexOf(E element) => _inverse[element];


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

final old = _list[index];
final string = _inverse.remove(old)!;

_map[string] = element;
_inverse[element] = string;
_list[index] = element;
}

E operator [] (int index) => _list[index];


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

}
1 change: 0 additions & 1 deletion sugar/test/src/crdt/string_index_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:math';

import 'package:sugar/src/crdt/string_index.dart';
import 'package:sugar/sugar.dart';
import 'package:test/test.dart';

Expand Down

0 comments on commit d4099f0

Please sign in to comment.