Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sandromaglione/itrie
Efficient, immutable and stack safe implementation of a Trie data structure in dart: autocomplete, text search, spell checking, strings and prefixes
https://github.com/sandromaglione/itrie
data-structure immutable immutable-dart immutable-datastructures trie trie-dart trie-data-structure trie-immutable trie-implementation trie-structure
Last synced: 2 days ago
JSON representation
Efficient, immutable and stack safe implementation of a Trie data structure in dart: autocomplete, text search, spell checking, strings and prefixes
- Host: GitHub
- URL: https://github.com/sandromaglione/itrie
- Owner: SandroMaglione
- License: mit
- Created: 2024-01-11T18:23:33.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-01-22T04:59:48.000Z (10 months ago)
- Last Synced: 2024-02-01T09:17:22.054Z (10 months ago)
- Topics: data-structure, immutable, immutable-dart, immutable-datastructures, trie, trie-dart, trie-data-structure, trie-immutable, trie-implementation, trie-structure
- Language: Dart
- Homepage: https://pub.dev/packages/itrie
- Size: 2.8 MB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
ITrie
**Efficient, immutable and stack safe** implementation of a `Trie` data structure in dart.
`Trie` is ideal for **autocomplete**, **text search**, **spell checking**, and generally when working with string and prefixes.
- [💻 Installation](#-installation)
- [💡 How to use](#-how-to-use)
- [Constructors](#constructors)
- [`Iterable`](#iterable)
- [Mutations (Immutable)](#mutations-immutable)
- [Getters](#getters)
- [🛠️ Properties](#️-properties)
- [Immutable](#immutable)
- [Stack safe](#stack-safe)
- [(Space) Efficient](#space-efficient)
- [📃 Versioning](#-versioning)
- [😀 Support](#-support)
- [👀 License](#-license)## 💻 Installation
```yaml
# pubspec.yaml
dependencies:
itrie: ^0.0.2
```## 💡 How to use
The package export a single `ITrie` class.### Constructors
An `ITrie` can be created from any `Iterable` or using the `empty` constructor:```dart
/// Empty [ITrie] with [int] values
final empty = ITrie.empty();
final copy = empty.insert("key", 10);/// From [Iterable] containing a record of `(String, T)`
final fromIterable = ITrie.fromIterable([("key", 10)]);
```### `Iterable`
`ITrie` extends `Iterable`.This means that you have access to all the methods
available in the [Iterable API](https://api.dart.dev/stable/3.2.5/dart-core/Iterable-class.html#instance-methods):
- [`map`](https://api.dart.dev/stable/3.2.5/dart-core/Iterable/map.html)
- [`fold`](https://api.dart.dev/stable/3.2.5/dart-core/Iterable/fold.html)
- [`every`](https://api.dart.dev/stable/3.2.5/dart-core/Iterable/every.html)
- [`iterator`](https://api.dart.dev/stable/3.2.5/dart-core/Iterable/iterator.html)
- And more 🚀### Mutations (Immutable)
Methods to add/remove/modify key/value inside `ITrie`:```dart
final itrie = ITrie.empty();final insert = itrie.insert("key", 10);
final remove = itrie.remove("key");
final modify = itrie.modify("key", (value) => value + 1);
final insertMany = itrie.insertMany([("key2", 20)]);
final removeMany = itrie.removeMany(["key"]);
```> 🧱 `ITrie` **is immutable**
>
> These methods return a new copy of the original `ITrie`
> **without modifying the original** `ITrie`### Getters
Methods to extract key/value based on `key` and `prefix`:```dart
final itrie = ITrie.empty();final getV = itrie.get("key");
final longestPrefixOf = itrie.longestPrefixOf("keys");
final withPrefix = itrie.withPrefix("ke");
final keysWithPrefix = itrie.keysWithPrefix("ke");
final valuesWithPrefix = itrie.valuesWithPrefix("ke");
final keys = itrie.keys;
final values = itrie.values;
final length = itrie.length;
final has = itrie.has("key");
```## 🛠️ Properties
### Immutable
The main difference between other trie implementations is that `ITrie` is **immutable**.Every mutation (e.g. `insert`, `delete`, `modify`) returns a new instance of `ITrie` instead of modifying the original `ITrie` in-place.
```dart
final itrie = ITrie.empty();
final newITrie = itrie.insert("key", 10); /// 👈 `insert` returns a new [ITrie]expect(itrie.length, 0); /// 👈 Original `itrie` is not modified
expect(newITrie.length, 1);
```> ✅ Immutability in `ITrie` uses a technique called **Path copying**: this makes `ITrie` efficient since it does not require to copy the full data structure with every mutation
### Stack safe
`ITrie` does **not** use recursion. No matter how many operations or elements it contains, `ITrie` will never cause stack overflow issues.### (Space) Efficient
`ITrie` is implemented internally as a **Ternary Search Trie**. This data structure allows for any alphabet size (any `String`) and it is also more space efficient compared to other trie implementations.***
## 📃 Versioning
- v0.0.2 - 22 January 2024
- v0.0.1 - 22 January 2024## 😀 Support
If you are interested in my work you can [**subscribe to my newsletter**](https://www.sandromaglione.com/newsletter).
For more frequent updates you can also follow me on my [**Twitter**](https://twitter.com/SandroMaglione).
## 👀 License
MIT License, see the [LICENSE.md](https://github.com/SandroMaglione/itrie/blob/main/LICENSE) file for details.