Class: Irc::NetmaskDb::Tree
- Inherits:
-
Object
- Object
- Irc::NetmaskDb::Tree
- Defined in:
- /home/apoc/projects/ruby/rbot/lib/rbot/maskdb.rb
Overview
helper backend class: generic nested radix tree
Instance Attribute Summary (collapse)
-
- (Object) chi
readonly
Returns the value of attribute chi.
-
- (Object) pre
readonly
Returns the value of attribute pre.
Instance Method Summary (collapse)
- - (Object) add(val, *prefs)
- - (Boolean) empty?
- - (Object) find(*prefs)
-
- (Tree) initialize(pre = '', chi = Hash.new)
constructor
A new instance of Tree.
- - (Object) remove(*prefs, &block)
Constructor Details
- (Tree) initialize(pre = '', chi = Hash.new)
Returns a new instance of Tree
7 8 9 10 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/maskdb.rb', line 7 def initialize(pre = '', chi = Hash.new) @pre = pre @chi = chi end |
Instance Attribute Details
- (Object) chi (readonly)
Returns the value of attribute chi
5 6 7 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/maskdb.rb', line 5 def chi @chi end |
- (Object) pre (readonly)
Returns the value of attribute pre
5 6 7 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/maskdb.rb', line 5 def pre @pre end |
Instance Method Details
- (Object) add(val, *prefs)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/maskdb.rb', line 12 def add(val, *prefs) str = prefs.shift or raise 'empty prefs' @pre = str.dup if @chi.empty? n = 0 @pre.size.times do break if @pre[n] != str[n] n += 1 end rest = str.slice(n .. -1) if n != @pre.size prest = @pre.slice!(n .. -1) pc = prest.slice! 0 @chi = {pc => Tree.new(prest, @chi)} end c = rest.slice!(0) if c (@chi[c] ||= Tree.new).add(val, rest, *prefs) else if prefs.empty? (@chi[''] ||= Array.new).push val else (@chi[''] ||= Tree.new).add(val, *prefs) end end end |
- (Boolean) empty?
43 44 45 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/maskdb.rb', line 43 def empty? @chi.empty? end |
- (Object) find(*prefs)
71 72 73 74 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/maskdb.rb', line 71 def find(*prefs) str = prefs.shift or raise 'empty prefs?' self.find_helper(str, *prefs) + self.find_helper(str.reverse, *prefs) end |
- (Object) remove(*prefs, &block)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/maskdb.rb', line 47 def remove(*prefs, &block) str = prefs.shift or raise 'empty prefs?' return nil unless @pre.empty? or str.index(@pre) == 0 c = str.slice(@pre.size) || '' return nil unless @chi.include? c if c == '' if prefs.empty? @chi[c].reject!(&block) else @chi[c].remove(*prefs, &block) end else @chi[c].remove(str.slice((@pre.size + 1) .. -1), *prefs, &block) end @chi.delete(c) if @chi[c].empty? if @chi.size == 1 k = @chi.keys.shift return nil if k == '' @pre << k << @chi[k].pre @chi = @chi[k].chi end end |