Class: Squeeze::HashTree

Inherits:
Hash
  • Object
show all
Includes:
Traversable
Defined in:
lib/squeeze/hash_tree.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Traversable

#count, #filter, #increment, #reduce, #reducer, #retrieve, #search, #set, #sum, #traverse, #unique

Class Method Details

.[](hash) ⇒ Object



21
22
23
24
25
# File 'lib/squeeze/hash_tree.rb', line 21

def self.[](hash)
  ht = self.new
  ht << hash
  ht
end

._load(*args) ⇒ Object



33
34
35
36
37
38
# File 'lib/squeeze/hash_tree.rb', line 33

def self._load(*args)
  h = Marshal.load(*args)
  ht = self.new
  ht.replace(h)
  ht
end

.newObject

Override the constructor to provide a default_proc NOTE: there’s a better way to do this in >=1.9.2, it seems. See Hash#default_proc=



16
17
18
19
# File 'lib/squeeze/hash_tree.rb', line 16

def self.new()
  hash = Hash.new { |h,k| h[k] = HashTree.new }
  super.replace(hash)
end

Instance Method Details

#+(other) ⇒ Object



89
90
91
92
93
# File 'lib/squeeze/hash_tree.rb', line 89

def +(other)
  out = HashTree.new
  _plus(other, out)
  out
end

#<<(other) ⇒ Object

Merge the argument into self, overwriting where necessary.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/squeeze/hash_tree.rb', line 128

def <<(other)
  other.each do |k,v1|
    if self.has_key?(k)
      v2 = self[k]
      if v1.respond_to?(:has_key?) && v2.respond_to?(:has_key?)
        v2 << v1
      elsif v1.is_a?(Numeric) && v2.is_a?(Numeric)
        self[k] = v1 + v2
      else
        raise ArgumentError,
          "Can't merge leaf with non-leaf:\n#{v1.inspect}\n#{v2.inspect}"
      end
    else
      if v1.respond_to?(:has_key?)
        self[k] << v1
      else
        self[k] = v1
      end
    end
  end
end

#_dump(depth) ⇒ Object



27
28
29
30
31
# File 'lib/squeeze/hash_tree.rb', line 27

def _dump(depth)
  h = Hash[self]
  h.delete_if {|k,v| v.is_a? Proc }
  Marshal.dump(h)
end

#_plus(ht2, out) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/squeeze/hash_tree.rb', line 95

def _plus(ht2, out)
  self.each do |k1,v1|
    v1 = v1.respond_to?(:dup) ? v1 : v1.dup
    if ht2.has_key?(k1)
      v2 = ht2[k1]
      if v1.respond_to?(:_plus)
        out[k1] = v1
        v1._plus(v2, out[k1])
      elsif v2.respond_to?(:_plus)
        raise ArgumentError,
          "Can't merge leaf with non-leaf:\n#{v1.inspect}\n#{v2.inspect}"
      else
        if v2.is_a?(Numeric) && v1.is_a?(Numeric)
          out[k1] = v1 + v2
        else
          out[k1] = [v1, ht2[k1]]
        end
      end
    else
      # should anything happen here?
    end
  end
  ht2.each do |k,v|
    if self.has_key?(k)
      # should anything happen here?
    else
      v = v.respond_to?(:dup) ? v : v.dup
      out[k] = v
    end
  end
end

#children(matcher = true) ⇒ Object



82
83
84
85
86
87
# File 'lib/squeeze/hash_tree.rb', line 82

def children(matcher=true)
  next_keys = self.keys.select do |key|
    match?(matcher, key)
  end
  self.values_at(*next_keys)
end

#create_path(sig) {|hash, final_key| ... } ⇒ Object

Follow the path specified, creating new nodes where necessary. Returns the value at the end of the path. If a block is supplied, it will be called with the last node and the last key as parameters, analogous to Hash.new’s default proc. This is necessary to allow setting a value at the end of the path. See the implementation of #insert.

Yields:

  • (hash, final_key)


45
46
47
48
49
50
51
52
53
# File 'lib/squeeze/hash_tree.rb', line 45

def create_path(sig)
  final_key = sig.pop
  hash = self
  sig.each do |a|
    hash = hash[a]
  end
  yield(hash, final_key) if block_given?
  hash[final_key]
end

#each_leaf(stack = [], &block) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/squeeze/hash_tree.rb', line 181

def each_leaf(stack=[], &block)
  self.each do |k,v|
    stack.push(k)
    if v.respond_to?(:each_leaf)
      v.each_leaf(stack, &block)
    else
      yield(v)
      #block.call(v)
    end
    stack.pop
  end
end

#each_path(stack = [], &block) ⇒ Object

Depth-first traversal, yielding the path to each leaf.



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/squeeze/hash_tree.rb', line 168

def each_path(stack=[], &block)
  self.each do |k, v|
    stack.push(k)
    if v.respond_to?(:each_path)
      v.each_path(stack, &block)
    else
      yield(stack, v)
      #block.call(stack, v)
    end
    stack.pop
  end
end

#find(sig) ⇒ Object

Attempt to retrieve the value at the end of the path specified, without creating new nodes. Returns nil on failure.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/squeeze/hash_tree.rb', line 57

def find(sig)
  stage = self
  sig.each do |a|
    if stage.has_key?(a)
      stage = stage[a]
    else
      return nil
    end
  end
  stage
end

#match?(val, key) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/squeeze/hash_tree.rb', line 150

def match?(val, key)
  case val
  when true
    true
  when String, Symbol
    key == val
  when Regexp
    key =~ val
  when Proc
    val.call(key)
  when nil
    false
  else
    raise ArgumentError, "Unexpected matcher type: #{val.inspect}"
  end
end

#remove(sig) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/squeeze/hash_tree.rb', line 69

def remove(sig)
  stage = self
  s2 = sig.slice(0..-2)
  s2.each do |a|
    if stage.has_key?(a)
      stage = stage[a]
    else
      return nil
    end
  end
  stage.delete(sig.last)
end