nahi / avl_tree Goto Github PK
View Code? Open in Web Editor NEWAVL tree and Red-black tree in Ruby
License: BSD 2-Clause "Simplified" License
AVL tree and Red-black tree in Ruby
License: BSD 2-Clause "Simplified" License
I am looking for a key-value data structure where the keys (floats) are naturally sorted for my metriks gem.
I am currently using RBTree but would like to use something without native code (to allow it to be used in JRuby, etc).
The class I am using it in is here: https://github.com/eric/metriks/blob/master/lib/metriks/exponentially_decaying_sample.rb
Would you take a pull request that removed the instances of calling .to_s
on the keys?
I just got a report of this: eric/metriks#13
It looks like the problem is that if you try to delete a key that wasn't found, it still tries to call #value
on the result.
A block can be passed to Hash::new. The block will be run to produce default values.
See Hash::new docs for how this works with Hashes. Note that the block expects 2 arguments.
For example
$ cat test_hash.rb
require 'pp'
t = Hash.new {|h, k| h[k] = []}
t[12] << "boo"
pp t.keys
pp t.values
$ ruby test_hash.rb
[12]
[["boo"]]
But with AVLTree:
$ cat test_avl_tree.rb
require 'avl_tree'
require 'pp'
t = AVLTree.new {|h, k| h[k] = []}
p "OK so far"
t[12] << "boo"
pp t.keys
pp t.values
$ ruby test_avl_tree.rb
"OK so far"
test_avl_tree.rb:3:in `block in <main>': undefined method `[]=' for nil:NilClass (NoMethodError)
from /Users/matt/.gem/ruby/2.4.0/gems/avl_tree-1.2.1/lib/avl_tree.rb:409:in `default_value'
from /Users/matt/.gem/ruby/2.4.0/gems/avl_tree-1.2.1/lib/avl_tree.rb:378:in `[]'
from test_avl_tree.rb:5:in `<main>'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.