Collections is a Crystal shard that provides generic data structures like heaps, including:
BinaryHeapMin: A min-heap implementationBinaryHeapMax: A max-heap implementation
Add this to your application's shard.yml:
dependencies:
collections:
github: Lillevang/collections
version: ~> 0.2.5Run shards install
require "collections"
# Min-Heap Example
heap = Collections::BinaryHeapMin(Int32).new
heap.add([10, 20, 5])
puts heap.extract_root! # => 5
# Max-Heap Example
heap = Collections::BinaryHeapMax(Int32).new
heap.add([10, 20, 5])
puts heap.extract_root! # => 20
# Graph Example
graph = Collections::Graph(Int32).new
graph.add_edge(1, 2)
graph.add_edge(1, 3)
puts graph.neighbors(1).map(&.value) # => [2, 3]
# Grid Example
grid = Collections::Grid(Int32).new(3, 3, 0)
grid.set(1, 1, 1) # block a cell
if result = grid.shortest_path({0, 0}, {2, 2})
distance, path = result
puts distance # => 4
grid.print_grid(path)
endWrite code, good code preferred!
Run tests with: crystal spec
- Fork it (https://github.com/Lillevang/collections/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
Released under the MIT License. See LICENSE for details.
- Lillevang - creator and maintainer