Module: RMTools::KeyValueTraversal

Included in:
Hash, KeyValueTraversable
Defined in:
lib/rmtools/enumerable/traversal.rb

Overview

Presumptions:

all keys (at least on the same depth) are uniq
values are either enumerable or nothing

It’s included into Hash

Instance Method Summary collapse

Instance Method Details

#depth_first_find(&b) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/rmtools/enumerable/traversal.rb', line 106

def depth_first_find(&b)
  to_traversal.each {|k, v|
    return k if b[k]
    if v.respond_to?(:depth_first_traverse) and res = v.depth_first_traverse(&b)
      return res
    end
  }
  nil
end

#depth_first_selectObject



120
121
122
# File 'lib/rmtools/enumerable/traversal.rb', line 120

def depth_first_select
  res = []; depth_first_traverse {|k, v| res << k if yield(k)}; res
end

#depth_first_traverse(&b) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/rmtools/enumerable/traversal.rb', line 86

def depth_first_traverse(&b)
  to_traversal.each {|k, v|
    b[k]
    if v.respond_to? :depth_first_traverse
      v.depth_first_traverse &b
    end
  }
end

#preorder_find(&b) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/rmtools/enumerable/traversal.rb', line 95

def preorder_find(&b)
  next_level = RMTools::KeyValueTraversable.new
  to_traversal.each {|k, v|
    return k if b[k]
    if v.respond_to? :preorder_find
      next_level += v
    end
  }
  next_level.preorder_find(&b) if next_level.any?
end

#preorder_selectObject



116
117
118
# File 'lib/rmtools/enumerable/traversal.rb', line 116

def preorder_select
  res = []; preorder_traverse {|k, v| res << k if yield(k)}; res
end

#preorder_traverse(&b) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/rmtools/enumerable/traversal.rb', line 75

def preorder_traverse(&b)
  next_level = RMTools::KeyValueTraversable.new
  to_traversal.each {|k, v|
    b[k]
    if v.respond_to? :preorder_traverse
      next_level += v
    end
  }
  next_level.preorder_traverse(&b) if next_level.any?
end