diff --git a/lib/src/iterable/built_iterable.dart b/lib/src/iterable/built_iterable.dart index 7011f7d..db132f5 100644 --- a/lib/src/iterable/built_iterable.dart +++ b/lib/src/iterable/built_iterable.dart @@ -11,4 +11,6 @@ abstract class BuiltIterable implements Iterable { /// Converts to a [BuiltSet]. BuiltSet toBuiltSet(); + + const BuiltIterable(); } diff --git a/lib/src/list/built_list.dart b/lib/src/list/built_list.dart index 089cfbd..aec174e 100644 --- a/lib/src/list/built_list.dart +++ b/lib/src/list/built_list.dart @@ -4,6 +4,8 @@ part of '../list.dart'; +final _hashCodeExpando = Expando(); + /// The Built Collection [List]. /// /// It implements [Iterable] and the non-mutating part of the [List] interface. @@ -15,7 +17,6 @@ part of '../list.dart'; /// for the general properties of Built Collections. abstract class BuiltList implements Iterable, BuiltIterable { final List _list; - int? _hashCode; /// Instantiates with elements from an [Iterable]. factory BuiltList([Iterable iterable = const []]) { @@ -26,6 +27,8 @@ abstract class BuiltList implements Iterable, BuiltIterable { } } + const factory BuiltList.fromList([List list]) = _BuiltList.withSafeList; + /// Instantiates with elements from an [Iterable]. /// /// `E` must not be `dynamic`. @@ -62,8 +65,7 @@ abstract class BuiltList implements Iterable, BuiltIterable { /// the same order. Then, the `hashCode` is guaranteed to be the same. @override int get hashCode { - _hashCode ??= hashObjects(_list); - return _hashCode!; + return _hashCodeExpando[this] ??= hashObjects(_list); } /// Deep equality. @@ -234,12 +236,12 @@ abstract class BuiltList implements Iterable, BuiltIterable { // Internal. - BuiltList._(this._list); + const BuiltList._(this._list); } /// Default implementation of the public [BuiltList] interface. class _BuiltList extends BuiltList { - _BuiltList.withSafeList(List list) : super._(list); + const _BuiltList.withSafeList([List list = const []]) : super._(list); _BuiltList.from([Iterable iterable = const []]) : super._(List.from(iterable, growable: false)) { diff --git a/lib/src/list/list_builder.dart b/lib/src/list/list_builder.dart index a9a1413..4bc895f 100644 --- a/lib/src/list/list_builder.dart +++ b/lib/src/list/list_builder.dart @@ -13,7 +13,7 @@ part of '../list.dart'; /// for the general properties of Built Collections. class ListBuilder { late List _list; - _BuiltList? _listOwner; + BuiltList? _listOwner; /// Instantiates with elements from an [Iterable]. factory ListBuilder([Iterable iterable = const []]) { @@ -38,7 +38,7 @@ class ListBuilder { /// Replaces all elements with elements from an [Iterable]. void replace(Iterable iterable) { - if (iterable is _BuiltList) { + if (iterable is BuiltList) { _setOwner(iterable); } else { _setSafeList(List.from(iterable)); @@ -258,7 +258,7 @@ class ListBuilder { ListBuilder._uninitialized(); - void _setOwner(_BuiltList listOwner) { + void _setOwner(BuiltList listOwner) { _list = listOwner._list; _listOwner = listOwner; }