Class: Immutable::Trie

Inherits:
Object
  • Object
show all
Defined in:
lib/immutable/trie.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bitshift, size = 0, entries = [], children = []) ⇒ Trie

Returns a new instance of Trie.



13
14
15
16
17
18
# File 'lib/immutable/trie.rb', line 13

def initialize(bitshift, size = 0, entries = [], children = [])
  @bitshift = bitshift
  @entries = entries
  @children = children
  @size = size
end

Instance Attribute Details

#sizeObject (readonly)

Returns the number of key-value pairs in the trie.



11
12
13
# File 'lib/immutable/trie.rb', line 11

def size
  @size
end

Class Method Details

.[](pairs) ⇒ Object



4
5
6
7
8
# File 'lib/immutable/trie.rb', line 4

def self.[](pairs)
  result = new(0)
  pairs.each { |key, val| result.put!(key, val) }
  result
end

Instance Method Details

#at(index) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/immutable/trie.rb', line 244

def at(index)
  @entries.each do |entry|
    if entry
      return entry if index == 0
      index -= 1
    end
  end
  @children.each do |child|
    if child
      if child.size >= index+1
        return child.at(index)
      else
        index -= child.size
      end
    end
  end
  nil
end

#bulk_delete(keys) ⇒ Trie

Delete multiple elements from a Trie. This is more efficient than several calls to ‘#delete`.

Parameters:

Returns:



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/immutable/trie.rb', line 196

def bulk_delete(keys)
  new_entries = nil
  new_children = nil
  new_size = @size

  keys.each do |key|
    index = index_for(key)
    entry = (new_entries || @entries)[index]
    if !entry
      next
    elsif entry[0].eql?(key)
      new_entries ||= @entries.dup
      child = (new_children || @children)[index]
      if child
        # Bring up the first entry from the child into entries
        new_children ||= @children.dup
        new_children[index] = child.delete_at do |entry|
          new_entries[index] = entry
        end
      else
        new_entries[index] = nil
      end
      new_size -= 1
    else
      child = (new_children || @children)[index]
      if child
        copy = child.find_and_delete(key)
        unless copy.equal?(child)
          new_children ||= @children.dup
          new_children[index] = copy
          new_size -= (child.size - copy_size(copy))
        end
      end
    end
  end

  if new_entries || new_children
    Trie.new(@bitshift, new_size, new_entries || @entries, new_children || @children)
  else
    self
  end
end

#bulk_put(key_value_pairs) ⇒ Trie

Put multiple elements into a Trie. This is more efficient than several calls to ‘#put`.

Parameters:

  • key_value_pairs

    Enumerable of pairs (‘[key, value]`)

Returns:

  • (Trie)

    A copy of ‘self` after associated the given keys and values (or `self` if no modifications where needed).



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/immutable/trie.rb', line 105

def bulk_put(key_value_pairs)
  new_entries = nil
  new_children = nil
  new_size = @size

  key_value_pairs.each do |key, value|
    index = index_for(key)
    entry = (new_entries || @entries)[index]

    if !entry
      new_entries ||= @entries.dup
      key = key.dup.freeze if key.is_a?(String) && !key.frozen?
      new_entries[index] = [key, value].freeze
      new_size += 1
    elsif entry[0].eql?(key)
      if !entry[1].equal?(value)
        new_entries ||= @entries.dup
        key = key.dup.freeze if key.is_a?(String) && !key.frozen?
        new_entries[index] = [key, value].freeze
      end
    else
      child = (new_children || @children)[index]
      if child
        new_child = child.put(key, value)
        if !new_child.equal?(child)
          new_children ||= @children.dup
          new_children[index] = new_child
          new_size += new_child.size - child.size
        end
      else
        new_children ||= @children.dup
        new_children[index] = Trie.new(@bitshift + 5).put!(key, value)
        new_size += 1
      end
    end
  end

  if new_entries || new_children
    Trie.new(@bitshift, new_size, new_entries || @entries, new_children || @children)
  else
    self
  end
end

#delete(key) ⇒ Object

Returns a copy of self with the given key (and associated value) deleted. If not found, returns self.



187
188
189
# File 'lib/immutable/trie.rb', line 187

def delete(key)
  find_and_delete(key) || Trie.new(@bitshift)
end

#each(&block) ⇒ Object

Calls block once for each entry in the trie, passing the key-value pair as parameters.



31
32
33
34
35
36
37
# File 'lib/immutable/trie.rb', line 31

def each(&block)
  @entries.each { |entry| yield entry if entry }
  @children.each do |child|
    child.each(&block) if child
  end
  nil
end

#empty?Boolean

Returns true if the trie contains no key-value pairs.

Returns:

  • (Boolean)


21
22
23
# File 'lib/immutable/trie.rb', line 21

def empty?
  @size == 0
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns true if . eql? is synonymous with ==

Returns:

  • (Boolean)


264
265
266
267
268
269
270
271
# File 'lib/immutable/trie.rb', line 264

def eql?(other)
  return true if equal?(other)
  return false unless instance_of?(other.class) && size == other.size
  each do |entry|
    return false unless other.include?(entry[0], entry[1])
  end
  true
end

#get(key) ⇒ Object

Retrieves the entry corresponding to the given key. If not found, returns nil.



175
176
177
178
179
180
181
182
183
184
# File 'lib/immutable/trie.rb', line 175

def get(key)
  index = index_for(key)
  entry = @entries[index]
  if entry && entry[0].eql?(key)
    entry
  else
    child = @children[index]
    child.get(key) if child
  end
end

#include?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
242
# File 'lib/immutable/trie.rb', line 239

def include?(key, value)
  entry = get(key)
  entry && value.eql?(entry[1])
end

#key?(key) ⇒ Boolean

Returns true if the given key is present in the trie.

Returns:

  • (Boolean)


26
27
28
# File 'lib/immutable/trie.rb', line 26

def key?(key)
  !!get(key)
end

#put(key, value) ⇒ Trie

Returns A copy of ‘self` with the given value associated with the key (or `self` if no modification was needed because an identical key-value pair was already stored.

Returns:

  • (Trie)

    A copy of ‘self` with the given value associated with the key (or `self` if no modification was needed because an identical key-value pair was already stored



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/immutable/trie.rb', line 61

def put(key, value)
  index = index_for(key)
  entry = @entries[index]

  if !entry
    entries = @entries.dup
    key = key.dup.freeze if key.is_a?(String) && !key.frozen?
    entries[index] = [key, value].freeze
    Trie.new(@bitshift, @size + 1, entries, @children)
  elsif entry[0].eql?(key)
    if entry[1].equal?(value)
      self
    else
      entries = @entries.dup
      key = key.dup.freeze if key.is_a?(String) && !key.frozen?
      entries[index] = [key, value].freeze
      Trie.new(@bitshift, @size, entries, @children)
    end
  else
    child = @children[index]
    if child
      new_child = child.put(key, value)
      if new_child.equal?(child)
        self
      else
        children = @children.dup
        children[index] = new_child
        new_self_size = @size + (new_child.size - child.size)
        Trie.new(@bitshift, new_self_size, @entries, children)
      end
    else
      children = @children.dup
      children[index] = Trie.new(@bitshift + 5).put!(key, value)
      Trie.new(@bitshift, @size + 1, @entries, children)
    end
  end
end

#put!(key, value) ⇒ Object

Returns self after overwriting the element associated with the specified key.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/immutable/trie.rb', line 150

def put!(key, value)
  index = index_for(key)
  entry = @entries[index]
  if !entry
    @size += 1
    key = key.dup.freeze if key.is_a?(String) && !key.frozen?
    @entries[index] = [key, value].freeze
  elsif entry[0].eql?(key)
    key = key.dup.freeze if key.is_a?(String) && !key.frozen?
    @entries[index] = [key, value].freeze
  else
    child = @children[index]
    if child
      old_child_size = child.size
      @children[index] = child.put!(key, value)
      @size += child.size - old_child_size
    else
      @children[index] = Trie.new(@bitshift + 5).put!(key, value)
      @size += 1
    end
  end
  self
end

#reduce(memo) ⇒ Object



47
48
49
50
# File 'lib/immutable/trie.rb', line 47

def reduce(memo)
  each { |entry| memo = yield(memo, entry) }
  memo
end

#reverse_each(&block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/immutable/trie.rb', line 39

def reverse_each(&block)
  @children.reverse_each do |child|
    child.reverse_each(&block) if child
  end
  @entries.reverse_each { |entry| yield(entry) if entry }
  nil
end

#selectObject



52
53
54
55
56
# File 'lib/immutable/trie.rb', line 52

def select
  keys_to_delete = []
  each { |entry| keys_to_delete << entry[0] unless yield(entry) }
  bulk_delete(keys_to_delete)
end