Class: Irc::NetmaskDb::Tree
Overview
helper backend class: generic nested radix tree
Instance Attribute Summary collapse
-
#chi ⇒ Object
readonly
Returns the value of attribute chi.
-
#pre ⇒ Object
readonly
Returns the value of attribute pre.
Instance Method Summary collapse
- #add(val, *prefs) ⇒ Object
- #empty? ⇒ Boolean
- #find(*prefs) ⇒ Object
-
#initialize(pre = '', chi = Hash.new) ⇒ Tree
constructor
A new instance of Tree.
- #remove(*prefs, &block) ⇒ Object
Constructor Details
#initialize(pre = '', chi = Hash.new) ⇒ Tree
Returns a new instance of Tree.
7 8 9 10 |
# File 'lib/rbot/maskdb.rb', line 7 def initialize(pre = '', chi = Hash.new) @pre = pre @chi = chi end |
Instance Attribute Details
#chi ⇒ Object (readonly)
Returns the value of attribute chi.
5 6 7 |
# File 'lib/rbot/maskdb.rb', line 5 def chi @chi end |
#pre ⇒ Object (readonly)
Returns the value of attribute pre.
5 6 7 |
# File 'lib/rbot/maskdb.rb', line 5 def pre @pre end |
Instance Method Details
#add(val, *prefs) ⇒ Object
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 '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 |
#empty? ⇒ Boolean
43 44 45 |
# File 'lib/rbot/maskdb.rb', line 43 def empty? @chi.empty? end |
#find(*prefs) ⇒ Object
71 72 73 74 |
# File '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 |
#remove(*prefs, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File '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 |