x_unique_list 2.0.0
x_unique_list: ^2.0.0 copied to clipboard
An ordered Dart collection in which no two elements share the same key, where the key is derived by a function you supply rather than by ==. O(1) lookup, upsert and membership. Zero dependencies.
Changelog #
2.0.0 #
A correctness and performance release. See the migration table in the README.
🐛 Fixed #
replaceOneWhere()could corrupt the collection. When the replacement's key already belonged to another element, it overwrote the matched element anyway and removed the old key from the index — leaving two elements sharing one key, alengththat disagreed with the index, and acontains()that returnedfalsefor an element that was present. It now refuses the replacement and returnsfalse. If you use this method, upgrade.itemshanded out the live internal list, solist.items.add(x)bypassed every uniqueness check. It is now an unmodifiable view.clear()replaced the internal list instead of emptying it, silently detaching anyitemsreference a caller was already holding. It now clears in place.- Argument validation used
assert, which is stripped in release builds. It now throwsRangeError/ArgumentErrorconsistently in every mode.
⚡ Performance #
- Rebuilt on a key → position index.
lookup,containsKey,contains,indexOf,indexOfKey,addOrReplace,replaceOneandupdateare now O(1); they were O(n). - The key function is now called once per element, when the element is inserted, instead of O(n) times per operation. Expensive key functions no longer dominate the cost.
sort()is now stable, unlikeList.sort.
✨ Added #
Iterable<T>conformance —for (final x in list),map,where,fold,any,[...list]and the rest of theIterableAPI now work directly on the collection.syncWith(source, {preserveOrder})— reconciles the collection against a source: adds what is new, replaces what changed, removes what disappeared, leaves equal elements untouched. Returns(added:, updated:, removed:).- Key-based API:
lookup,containsKey,indexOfKey,removeKey,removeKeys,retainKeys,putIfAbsent,update,keys,keyOf,toMap. XObservableUniqueList— notifies listeners on change, pure Dart, withbatch()to coalesce a group of mutations into a single notification. Bulk operations already notify once.XUniqueListBase<T, K>— a public interface to depend on when faking the collection in tests.- List essentials:
operator []=,first,last,removeAt,removeLast,removeAll,retainWhere,sublist,reversed,shuffle,firstWhereOrNull,lastWhereOrNull. - Reordering:
reorder(oldIndex, newIndex)andswap(a, b), for drag-and-drop lists. - Set algebra:
union,intersection,difference,operator +. - Constructors and value semantics:
XUniqueList.from,copy(),operator ==,hashCode,toString().
💥 Breaking #
XUniqueList<T>is nowXUniqueList<T, K>; the key type is explicit and bound toObject, so keys can no longer benull.itemsis unmodifiable. UsetoList()for a modifiable copy.firstWherenow throwsStateErrorwhen nothing matches, matchingIterable. The old null-returning behaviour is available asfirstWhereOrNull.removeWherereturns the number removed instead ofvoid.addAllOrReplacereturns(added:, updated:)instead of a singleint.containsacceptsObject?and returnsfalsefor non-Tvalues, matchingIterable.unmodifiableItemsis deprecated in favour ofitems; it will be removed in 3.0.0.
📚 Documentation & tooling #
- Documentation moved onto the public
XUniqueListBaseinterface, so it now renders on pub.dev (previously it lived on a private class and was invisible). - Test suite rewritten: an invariant harness asserted after every mutation, plus a fuzz test that cross-checks 20,000 random operations against a naive reference implementation.
- Added CI (format, analyze with
--fatal-infos, test on the oldest and newest supported SDK, publish dry-run), stricter analysis options, and a.pubignoreso IDE files stay out of the published archive.
1.1.1 #
- 📝 README.md
1.1.0 #
✨ Added #
- Added
addOrReplace():- Adds item if unique key does not exist.
- Replaces existing item if key matches but value differs.
- Returns
falseif identical item already exists (no-op).
- Added
addAllOrReplace():- Batch version of
addOrReplace. - Returns count of items added + replaced.
- Batch version of
🔧 Improved #
remove():- No longer relies on
==for removal. - Now removes based on
uniqueCondition→ more predictable behavior.
- No longer relies on
removeWhere():- Now correctly keeps
_uniqueItemsSetin sync with_itemsList. - Fixes potential data corruption bug.
- Now correctly keeps
firstWhere():- Removed exception-based flow.
- Now uses safe iteration → avoids unnecessary try/catch overhead.
where():- Returns
Iterable<T>instead ofList<T>to avoid unnecessary allocation.
- Returns
addAll():- Now accepts
Iterable<T>instead ofList<T>→ more flexible API.
- Now accepts
insertAll():- Improved documentation and clarified complexity behavior.
unmodifiableItems:- Uses
List<T>.unmodifiableexplicitly.
- Uses
⚡ Performance #
- Reduced unnecessary allocations:
where()no longer creates a new list.
- Improved predictability of operations by aligning all mutations with
_uniqueItemsSet. - Documented precise time complexity for all public methods.
🐛 Fixed #
- Critical bug where
removeWhere()did not update_uniqueItemsSet. - Potential inconsistency between
_itemsListand_uniqueItemsSet. - Edge cases in
remove()where item existed in set but not properly removed from list.
🧠 Behavioral Changes #
remove(T item):- Now removes based on
uniqueConditioninstead of relying on object equality.
- Now removes based on
addOrReplace():- Explicitly distinguishes between:
- add
- replace
- no-op (identical item)
- Explicitly distinguishes between:
📚 Documentation #
- Added detailed time complexity annotations for all methods.
- Improved method-level comments for clarity and maintainability.
1.0.2 #
🔄 Changes: #
- Improved
README.mdto provide clearer documentation and examples.- Added detailed sections for installation, usage, and methods.
- Updated examples and explanations for better clarity.
1.0.1 Support for Dart and Flutter 🛠 #
🔄 Changes: #
- Added support for both Dart and Flutter environments.
- Improved compatibility and removed unnecessary Flutter dependencies.
- Fixed minor bugs in list manipulation functions.
1.0.0 Initial Release 🎉 #
✨ Features: #
XUniqueListwith uniqueness enforcement viauniqueCondition.- Core operations:
add,addAll,insert,remove,replaceOne, etc.
🛠 Utility Functions: #
contains,clear,length,isEmpty,isNotEmpty.
📦 Access: #
items(modifiable)unmodifiableItems
✅ Testing: #
- Unit tests for core functionality.