Skip to content

feat(trie): add trie package with Map and Set interfaces - #71

Merged
lock14 merged 5 commits into
mainfrom
feat/trie-package
Jun 29, 2026
Merged

feat(trie): add trie package with Map and Set interfaces#71
lock14 merged 5 commits into
mainfrom
feat/trie-package

Conversation

@lock14

@lock14 lock14 commented Jun 29, 2026

Copy link
Copy Markdown
Owner

PR Description: feat(trie): Add generic Trie package with Map and Set interfaces

Description

This PR introduces a highly optimized trie (prefix tree) package to lock14/collections, providing Map and Set implementations specifically tailored for efficient string and slice prefix matching.

These implementations are ideal for use-cases requiring fast auto-completion, IP/subnet routing, URL path matching, and hierarchical data queries.

Key Features

  • Map & Set Interfaces: Exposes simple Map[K, V] and Set[K] interfaces with both standard collection methods and prefix-specific routing operations:
    • HasPrefix(K)
    • KeysWithPrefix(K) / ValuesWithPrefix(K) / EntriesWithPrefix(K) (returning standard iter.Seq)
    • LongestPrefixOf(K) / ShortestPrefixOf(K)
    • PrefixesOf(K)
    • RemovePrefix(K)
  • StringMap & StringSet: Zero-allocation hot paths for string keys. Under the hood, these avoid allocation overhead by iterating directly over the underlying byte representation of Go strings without making copies.
  • SliceMap & SliceSet: Fully generic implementations for []E where E is any comparable type (e.g. []byte, []int, etc).

Implementation Details

  • The underlying structure uses maps for child nodes (map[byte]*stringNode and map[E]*sliceNode), ensuring $O(1)$ child lookups at each depth of the trie.
  • Comprehensive table-driven tests have been included, spanning both basic operations and prefix operations.
  • Iterator-based accessors use iter.Seq (Go 1.23+ conventions) and natively support early-exits.

Performance

Benchmarks demonstrate that the hot-path read operations are zero-allocation:

BenchmarkStringMap_Get-16               2651206               446.6 ns/op             0 B/op          0 allocs/op
BenchmarkSliceMap_Get-16                2854777               421.6 ns/op             0 B/op          0 allocs/op
BenchmarkStringMap_Put-16               2592894               472.5 ns/op             0 B/op          0 allocs/op

Note: Put benchmark results denote rewriting existing keys, which appropriately skips node allocation.

Testing

  • Basic Map and Set CRUD operations.
  • Prefix queries (Longest, Shortest, KeysWithPrefix, RemovePrefix).
  • Edge-cases (Empty tries, missing keys, nil children branching, etc).
  • Early-exit (break) iterator logic handling.
  • Overall Code Coverage: ~89.1% statements.

Related Issues

(Link any related issues here if applicable)

@codecov-commenter

codecov-commenter commented Jun 29, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 92.12454% with 43 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
trie/slice.go 90.40% 16 Missing and 10 partials ⚠️
trie/string.go 93.63% 9 Missing and 8 partials ⚠️

📢 Thoughts on this report? Let us know!

@lock14
lock14 merged commit 27cc7aa into main Jun 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants