diff --git a/dart/lib/binary_table.dart b/dart/lib/binary_table.dart index 115c085..6fc1e2a 100644 --- a/dart/lib/binary_table.dart +++ b/dart/lib/binary_table.dart @@ -17,6 +17,7 @@ This file is part of the Binary Table package for Dart. */ +import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; @@ -453,6 +454,22 @@ extension FreeList on List { } +extension fnv1a on String { + + int get bt_hash { + List bytes = utf8.encode(this); + int hash = 0xcbf29ce484222325; // FNV offset basis + + for (int byte in bytes) { + hash ^= byte; + hash *= 0x100000001b3; // FNV prime + } + + return hash; + } + +} + class BinaryTable { RandomAccessFile _file; @@ -732,7 +749,7 @@ class BinaryTable { antiFreeListScope(() { Map addressTable = _addressTable; - int keyHash = key.hashCode; + int keyHash = key.bt_hash; if (addressTable.containsKey(keyHash)) { throw Exception('Key already exists'); @@ -757,7 +774,7 @@ class BinaryTable { operator [](String key) { Map addressTable = _addressTable; - int keyHash = key.hashCode; + int keyHash = key.bt_hash; if (!addressTable.containsKey(keyHash)) { throw Exception('Key does not exist'); @@ -774,7 +791,7 @@ class BinaryTable { antiFreeListScope(() { Map addressTable = _addressTable; - int keyHash = key.hashCode; + int keyHash = key.bt_hash; if (!addressTable.containsKey(keyHash)) { throw Exception('Key does not exist');