radix
Version, currently 0.4.117 versions
- 0.4.1latestMar 23, 2021
- 0.4.0not indexedJun 2, 2024
- 0.3.9not indexedJun 2, 2024
- 0.3.8not indexedJun 2, 2024
- 0.3.7not indexedJun 2, 2024
- 0.3.6not indexedJun 2, 2024
- 0.3.5not indexedJun 2, 2024
- 0.3.4not indexedJun 2, 2024
- 0.3.3not indexedJun 2, 2024
- 0.3.2not indexedJun 2, 2024
- 0.3.1not indexedJun 2, 2024
- 0.3.0not indexedJun 2, 2024
- 0.2.1not indexedJun 2, 2024
- 0.2.0not indexedJun 2, 2024
- 0.1.2not indexedJun 2, 2024
- 0.1.1not indexedJun 2, 2024
- 0.1.0not indexedJun 2, 2024
github.com/luislavena/radix
Radix Tree implementation for Crystal
Installation
# Add this to your shard.yml
dependencies:
radix:
github: luislavena/radix
version: ~> 0.4.1Then run:
shards installshard.yml
- Crystal
>= 0.35.0- License
- MIT
- Author
- Luis Lavena
Dependencies
This version declares no dependencies.
README
Radix Tree
Radix tree implementation for Crystal language
Installation
Add this to your application's shard.yml:
dependencies:
radix:
github: luislavena/radix
Usage
Building Trees
You can associate a payload with each path added to the tree:
require "radix"
tree = Radix::Tree(Symbol).new
tree.add "/products", :products
tree.add "/products/featured", :featured
result = tree.find "/products/featured"
if result.found?
puts result.payload # => :featured
end
The types allowed for payload are defined on Tree definition:
tree = Radix::Tree(Symbol).new
# Good, since Symbol is allowed as payload
tree.add "/", :root
# Compilation error, Int32 is not allowed
tree.add "/meaning-of-life", 42
Can combine multiple types if needed:
tree = Radix::Tree(Int32 | String | Symbol).new
tree.add "/", :root
tree.add "/meaning-of-life", 42
tree.add "/hello", "world"
Lookup and placeholders
You can also extract values from placeholders (as named segments or globbing):
tree.add "/products/:id", :product
result = tree.find "/products/1234"
if result.found?
puts result.params["id"]? # => "1234"
end
Please see Radix::Tree#add documentation for more usage examples.
Caveats
Pretty much all Radix implementations have their limitations and this project is no exception.
When designing and adding paths to a Tree, please consider that two different named parameters cannot share the same level:
tree.add "/", :root
tree.add "/:post", :post
tree.add "/:category/:post", :category_post # => Radix::Tree::SharedKeyError
This is because different named parameters at the same level will result in
incorrect params when lookup is performed, and sometimes the value for
post or category parameters will not be stored as expected.
To avoid this issue, usage of explicit keys that differentiate each path is recommended.
For example, following a good SEO practice will be consider /:post as
absolute permalink for the post and have a list of categories which links to
the permalinks of the posts under that category:
tree.add "/", :root
tree.add "/:post", :post # this is post permalink
tree.add "/categories", :categories # list of categories
tree.add "/categories/:category", :category # listing of posts under each category
Implementation
This project has been inspired and adapted from julienschmidt/httprouter and spriet2000/vertx-http-router Go and Java implementations, respectively.
Changes to logic and optimizations have been made to take advantage of Crystal's features.
Contributing
- Fork it ( https://github.com/luislavena/radix/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Luis Lavena - creator, maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.4.1- Tagged
- Mar 23, 2021
- Commit
e2e402fcaf2d- Crystal
>= 0.35.0- Indexed
- yes
Dependents
and 15 more
Repository
github.com/luislavena/radix
Metadata
- Created
- Aug 12, 2026
- Updated
- Sep 3, 2026
- Synced
- Sep 3, 2026
- Versions
- 17