Class Irc::NetmaskDb::Tree
In: lib/rbot/maskdb.rb
Parent: Object
BasicUserMessage JoinMessage NamesMessage WhoisMessage ModeChangeMessage KickMessage MotdMessage QuitMessage BanlistMessage UserMessage NoSuchTargetMessage TopicMessage NickMessage WelcomeMessage UnknownMessage InviteMessage PartMessage NetmaskList UserList ArrayOf ChannelList Netmask User\n[lib/rbot/botuser.rb\nlib/rbot/irc.rb] Channel Singleton RfcCasemap StrictRfcCasemap AsciiCasemap Casemap PrivMessage NoticeMessage TokyoCabinet::BDB CIBDB Btree CIBtree Socket MessageQueue QueueRing Client DBHash\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] DBTree\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] Server NetmaskDb Bot\n[lib/rbot/botuser.rb\nlib/rbot/config.rb\nlib/rbot/ircbot.rb\nlib/rbot/language.rb\nlib/rbot/message.rb\nlib/rbot/messagemapper.rb\nlib/rbot/plugins.rb\nlib/rbot/rbotconfig.rb\nlib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] lib/rbot/ircsocket.rb lib/rbot/rfc2812.rb lib/rbot/registry/tc.rb lib/rbot/irc.rb lib/rbot/maskdb.rb lib/rbot/message.rb lib/rbot/messagemapper.rb lib/rbot/botuser.rb lib/rbot/registry/tc.rb (null) BotConfig PKGConfig ServerOrCasemap Irc dot/m_35_0.png

helper backend class: generic nested radix tree

Methods

add   empty?   find   find_helper   new   remove  

Attributes

chi  [R] 
pre  [R] 

Public Class methods

[Source]

    # File lib/rbot/maskdb.rb, line 7
 7:       def initialize(pre = '', chi = Hash.new)
 8:         @pre = pre
 9:         @chi = chi
10:       end

Public Instance methods

[Source]

    # File lib/rbot/maskdb.rb, line 12
12:       def add(val, *prefs)
13:         str = prefs.shift or raise 'empty prefs'
14:         @pre = str.dup if @chi.empty?
15: 
16:         n = 0
17:         @pre.size.times do
18:           break if @pre[n] != str[n]
19:           n += 1
20:         end
21: 
22:         rest = str.slice(n .. -1)
23: 
24:         if n != @pre.size
25:           prest = @pre.slice!(n .. -1)
26:           pc = prest.slice! 0
27:           @chi = {pc => Tree.new(prest, @chi)}
28:         end
29: 
30:         c = rest.slice!(0)
31: 
32:         if c
33:           (@chi[c] ||= Tree.new).add(val, rest, *prefs)
34:         else
35:           if prefs.empty?
36:             (@chi[''] ||= Array.new).push val
37:           else
38:             (@chi[''] ||= Tree.new).add(val, *prefs)
39:           end
40:         end
41:       end

[Source]

    # File lib/rbot/maskdb.rb, line 43
43:       def empty?
44:         @chi.empty?
45:       end

[Source]

    # File lib/rbot/maskdb.rb, line 71
71:       def find(*prefs)
72:         str = prefs.shift or raise 'empty prefs?'
73:         self.find_helper(str, *prefs) + self.find_helper(str.reverse, *prefs)
74:       end

[Source]

    # File lib/rbot/maskdb.rb, line 47
47:       def remove(*prefs, &block)
48:         str = prefs.shift or raise 'empty prefs?'
49:         return nil unless @pre.empty? or str.index(@pre) == 0
50:         c = str.slice(@pre.size) || ''
51:         return nil unless @chi.include? c
52:         if c == ''
53:           if prefs.empty?
54:             @chi[c].reject!(&block)
55:           else
56:             @chi[c].remove(*prefs, &block)
57:           end
58:         else
59:           @chi[c].remove(str.slice((@pre.size + 1) .. -1), *prefs, &block)
60:         end
61:         @chi.delete(c) if @chi[c].empty?
62: 
63:         if @chi.size == 1
64:           k = @chi.keys.shift
65:           return nil if k == ''
66:           @pre << k << @chi[k].pre
67:           @chi = @chi[k].chi
68:         end
69:       end

Protected Instance methods

[Source]

    # File lib/rbot/maskdb.rb, line 77
77:       def find_helper(*prefs)
78:         str = prefs.shift or raise 'empty prefs?'
79:         return [] unless @pre.empty? or str.index(@pre) == 0
80:         # puts "#{self.inspect}: #{str} == #{@pre} pfx matched"
81:         if !@chi.include? ''
82:           matches = []
83:         elsif Array === @chi['']
84:           matches = @chi['']
85:         else
86:           matches = @chi[''].find(*prefs)
87:         end
88: 
89:         c = str.slice(@pre.size)
90: 
91:         more = []
92:         if c and @chi.include?(c)
93:           more = @chi[c].find_helper(str.slice((@pre.size + 1) .. -1), *prefs)
94:         end
95:         return more + matches
96:       end

[Validate]