diff --git a/lib/models/documents/attribute.dart b/lib/models/documents/attribute.dart index dcef7cfa..ef02b2b7 100644 --- a/lib/models/documents/attribute.dart +++ b/lib/models/documents/attribute.dart @@ -1,4 +1,5 @@ -import 'package:quiver_hashcode/hashcode.dart'; + +import 'package:flutter_quill/utils/hashcode.dart' as hashcode; enum AttributeScope { INLINE, // refer to https://quilljs.com/docs/formats/#inline @@ -181,7 +182,7 @@ class Attribute { } @override - int get hashCode => hash3(key, scope, value); + int get hashCode => hashcode.hash3(key, scope, value); @override String toString() { diff --git a/lib/models/documents/style.dart b/lib/models/documents/style.dart index 70d6201e..1e43ce2f 100644 --- a/lib/models/documents/style.dart +++ b/lib/models/documents/style.dart @@ -1,6 +1,6 @@ import 'package:collection/collection.dart'; import 'package:flutter_quill/models/documents/attribute.dart'; -import 'package:quiver_hashcode/hashcode.dart'; +import 'package:flutter_quill/utils/hashcode.dart' as hashcode; /* Collection of style attributes */ class Style { @@ -101,8 +101,8 @@ class Style { @override int get hashCode { final hashes = - _attributes.entries.map((entry) => hash2(entry.key, entry.value)); - return hashObjects(hashes); + _attributes.entries.map((entry) => hashcode.hash2(entry.key, entry.value)); + return hashcode.hashObjects(hashes); } @override diff --git a/lib/models/quill_delta.dart b/lib/models/quill_delta.dart index 42b9b6b3..643605cc 100644 --- a/lib/models/quill_delta.dart +++ b/lib/models/quill_delta.dart @@ -7,7 +7,7 @@ library quill_delta; import 'dart:math' as math; import 'package:collection/collection.dart'; -import 'package:quiver_hashcode/hashcode.dart'; +import 'package:flutter_quill/utils/hashcode.dart' as hashcode; const _attributeEquality = DeepCollectionEquality(); const _valueEquality = DeepCollectionEquality(); @@ -155,10 +155,10 @@ class Operation { int get hashCode { if (_attributes != null && _attributes!.isNotEmpty) { final attrsHash = - hashObjects(_attributes!.entries.map((e) => hash2(e.key, e.value))); - return hash3(key, value, attrsHash); + hashcode.hashObjects(_attributes!.entries.map((e) => hashcode.hash2(e.key, e.value))); + return hashcode.hash3(key, value, attrsHash); } - return hash2(key, value); + return hashcode.hash2(key, value); } @override @@ -301,7 +301,7 @@ class Delta { } @override - int get hashCode => hashObjects(_operations); + int get hashCode => hashcode.hashObjects(_operations); /// Retain [count] of characters from current position. void retain(int count, [Map? attributes]) { diff --git a/lib/utils/hashcode.dart b/lib/utils/hashcode.dart new file mode 100644 index 00000000..fe1e7985 --- /dev/null +++ b/lib/utils/hashcode.dart @@ -0,0 +1,31 @@ +library quiver.hashcode; + +/// Generates a hash code for multiple [objects]. +int hashObjects(Iterable objects) => + _finish(objects.fold(0, (h, i) => _combine(h, i.hashCode))); + +/// Generates a hash code for two objects. +int hash2(a, b) => _finish(_combine(_combine(0, a.hashCode), b.hashCode)); + +/// Generates a hash code for three objects. +int hash3(a, b, c) => _finish( + _combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode)); + +/// Generates a hash code for four objects. +int hash4(a, b, c, d) => _finish(_combine( + _combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode), + d.hashCode)); + +// Jenkins hash functions + +int _combine(int hash, int value) { + hash = 0x1fffffff & (hash + value); + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); + return hash ^ (hash >> 6); +} + +int _finish(int hash) { + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); + hash = hash ^ (hash >> 11); + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 63157ea9..90b373f5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -212,13 +212,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.0" - quiver_hashcode: - dependency: "direct main" - description: - name: quiver_hashcode - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.0" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index fa9a7e91..2e04e777 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,6 @@ environment: dependencies: flutter: sdk: flutter - quiver_hashcode: ^2.0.0 collection: ^1.15.0 tuple: ^2.0.0 url_launcher: ^6.0.2