https://github.com/pschiffmann/indexed-set.dart
Provides an `IndexedSet` class, an implementation of `Set` that computes an index for each of its elements, and exposes this mapping through the `[]` operator.
https://github.com/pschiffmann/indexed-set.dart
Last synced: 8 months ago
JSON representation
Provides an `IndexedSet` class, an implementation of `Set` that computes an index for each of its elements, and exposes this mapping through the `[]` operator.
- Host: GitHub
- URL: https://github.com/pschiffmann/indexed-set.dart
- Owner: pschiffmann
- License: mit
- Created: 2017-09-20T23:20:01.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-10T19:40:19.000Z (over 7 years ago)
- Last Synced: 2025-10-23T02:59:45.205Z (8 months ago)
- Language: Dart
- Homepage: https://pub.dartlang.org/packages/indexed_set
- Size: 51.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Dart indexed set
================
[](https://travis-ci.com/pschiffmann/indexed-set.dart)
This package provides an `IndexedSet` class, and a pair of `Superset`/`Subset` classes that implement the `IndexedSet` interface.
IndexedSet
----------
An `IndexedSet` adds a mapping mechanism to the `Set` interface.
A user-provided function `I index(E element)` calculates an index for each element.
The elements are then accessible through the `[]` operator.
This allows you to define cleaner APIs than if you used a `Map`, because the data structure can enforce integrity of the key/value mapping.
```dart
enum System { frontend, backend }
class Account {
final String name;
final System system;
Account(this.name, this.system);
String toString() => '$system-account of $name';
}
/// Supports lookup of accounts by username.
final frontendAccounts = new IndexedSet(
(Account acc) => acc.name,
isValidElement: (Account acc) => acc.system == System.frontend);
```
Superset / Subset
-----------------
Both `Superset` and `Subset` are indexed sets that use `int` as the index type.
A Superset is immutable and stores its elements in ascending order -- the first element will have index `0`, the last element index `set.length - 1`.
A Subset has to be taken from a superset, and can only contain elements that are also contained in the superset.
The idea was that subsets could store their elements in a bit vector, which would be very space-efficient, and time-efficient for set operations (difference, intersection, union) on subsets of the same superset. However, `Subset` is outperformed by a regular `HashSet` in most situations.