Class: Puppet::Graph::RbTreeMap::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/graph/rb_tree_map.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value) ⇒ Node

Returns a new instance of Node.



206
207
208
209
210
211
212
# File 'lib/puppet/graph/rb_tree_map.rb', line 206

def initialize(key, value)
  @key = key
  @value = value
  @color = :red
  @left = nil
  @right = nil
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



204
205
206
# File 'lib/puppet/graph/rb_tree_map.rb', line 204

def color
  @color
end

#keyObject

Returns the value of attribute key.



204
205
206
# File 'lib/puppet/graph/rb_tree_map.rb', line 204

def key
  @key
end

#leftObject

Returns the value of attribute left.



204
205
206
# File 'lib/puppet/graph/rb_tree_map.rb', line 204

def left
  @left
end

#rightObject

Returns the value of attribute right.



204
205
206
# File 'lib/puppet/graph/rb_tree_map.rb', line 204

def right
  @right
end

#valueObject

Returns the value of attribute value.



204
205
206
# File 'lib/puppet/graph/rb_tree_map.rb', line 204

def value
  @value
end

Instance Method Details

#colorflipObject



231
232
233
234
235
# File 'lib/puppet/graph/rb_tree_map.rb', line 231

def colorflip
  @color       = @color == :red       ? :black : :red
  @left.color  = @left.color == :red  ? :black : :red
  @right.color = @right.color == :red ? :black : :red
end

#fixupObject



290
291
292
293
294
295
296
# File 'lib/puppet/graph/rb_tree_map.rb', line 290

def fixup
  rotate_left if @right && @right.red?
  rotate_right if (@left && @left.red?) && (@left.left && @left.left.red?)
  colorflip if (@left && @left.red?) && (@right && @right.red?)

  self
end

#move_red_leftObject



271
272
273
274
275
276
277
278
279
# File 'lib/puppet/graph/rb_tree_map.rb', line 271

def move_red_left
  colorflip
  if @right.left && @right.left.red?
    @right.rotate_right
    rotate_left
    colorflip
  end
  self
end

#move_red_rightObject



281
282
283
284
285
286
287
288
# File 'lib/puppet/graph/rb_tree_map.rb', line 281

def move_red_right
  colorflip
  if @left.left && @left.left.red?
    rotate_right
    colorflip
  end
  self
end

#red?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/puppet/graph/rb_tree_map.rb', line 227

def red?
  @color == :red
end

#rotate_leftObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/puppet/graph/rb_tree_map.rb', line 237

def rotate_left
  r = @right
  r_key = r.key
  r_value = r.value
  b = r.left
  r.left = @left
  @left = r
  @right = r.right
  r.right = b
  r.color = :red
  r.key = @key
  r.value = @value
  @key = r_key
  @value = r_value
  self
end

#rotate_rightObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/puppet/graph/rb_tree_map.rb', line 254

def rotate_right
  l = @left
  l_key = l.key
  l_value = l.value
  b = l.right
  l.right = @right
  @right = l
  @left = l.left
  l.left = b
  l.color = :red
  l.key = @key
  l.value = @value
  @key = l_key
  @value = l_value
  self
end

#to_hashObject



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/puppet/graph/rb_tree_map.rb', line 214

def to_hash
  h = {
    :node => {
      :key => @key,
      :value => @value,
      :color => @color,
    }
  }
  h[:left] = left.to_hash if @left
  h[:right] = right.to_hash if @right
  h
end