Giter Club home page Giter Club logo

sandromaglione / itrie Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 2.86 MB

Efficient, immutable and stack safe implementation of a Trie data structure in dart: autocomplete, text search, spell checking, strings and prefixes

Home Page: https://pub.dev/packages/itrie

License: MIT License

Dart 100.00%
data-structure immutable immutable-datastructures trie trie-data-structure trie-implementation trie-structure immutable-dart trie-dart trie-immutable

itrie's Introduction

ITrie

GitHub: SandroMaglione Twitter: SandroMaglione

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

# 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:

/// Empty [ITrie] with [int] values
final empty = ITrie<int>.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:

Mutations (Immutable)

Methods to add/remove/modify key/value inside ITrie:

final itrie = ITrie<int>.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:

final itrie = ITrie<int>.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.

final itrie = ITrie<int>.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.

For more frequent updates you can also follow me on my Twitter.

๐Ÿ‘€ License

MIT License, see the LICENSE.md file for details.

itrie's People

Contributors

sandromaglione avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.