Sha256: 0bf03af15423025ac44b072b212cecaa6094ffdd178db33b34ab681db793b1bf

Contents?: true

Size: 1.57 KB

Versions: 14

Compression:

Stored size: 1.57 KB

Contents

# Copyright: Copyright (c) 2004  Nicolas Despres. All rights reserved.
# Author: Nicolas Despres  <polrop@lrde.epita.fr>.
# License: Gnu General Public License.

# $LastChangedBy: polrop $
# $Id: node.rb 171 2005-03-29 09:12:47Z polrop $


class Node

  def initialize(data=nil, *sub_nodes)
    @data = data
    sub_nodes.each { |sub_node| check_sub_node_type(sub_node) }
    @sub_nodes = sub_nodes
  end

  attr_reader :data, :sub_nodes

  def [](sub_node_index)
    @sub_nodes[sub_node_index]
  end

  def []=(sub_node_index, sub_node)
    check_sub_node_type(sub_node)
    @sub_nodes[sub_node_index] = sub_node
  end

  def <<(sub_node)
    check_sub_node_type(sub_node)
    @sub_nodes << sub_node
  end

  def each_pair(&block)
    @sub_nodes.each_with_index { |sub_node, i| block[i, sub_node] }
  end

  def each_node(&block)
    @sub_nodes.each_with_index { |sub_node, index| block[sub_node] }
  end

  alias :each :each_node

  def each_index(&block)
    @sub_nodes.each_with_index { |sub_node, index| block[index] }
  end

  def delete(sub_node)
    @sub_nodes.delete(sub_node)
  end

  def nb_sub_nodes
    @sub_nodes.size
  end

  alias size nb_sub_nodes
  alias length nb_sub_nodes

  def leaf?
    @sub_nodes.empty?
  end

  def pre_depth_first(&block)
    block[self]
    @sub_nodes.each { |sub_node| sub_node.pre_depth_first(&block) }
    nil
  end

#   FIXME: implement me
#   def breadth_first(&block)
#   end

  protected
  def check_sub_node_type(sub_node)
    unless sub_node.is_a?(self.class)
      raise(TypeError, "`#{sub_node}' - must be a #{self.class}")
    end
  end

end # class Node

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
ruby_ex-0.4.5.0 lib/node.rb
ruby_ex-0.2.0 lib/node.rb
ruby_ex-0.4.6.1 lib/node.rb
ruby_ex-0.4.6.2 lib/node.rb
ruby_ex-0.5.6.1 lib/node.rb
ruby_ex-0.4.1.2 lib/node.rb
ruby_ex-0.3.0 lib/node.rb
ruby_ex-0.4.1.3 lib/node.rb
ruby_ex-0.5.6.2 lib/node.rb
ruby_ex-0.5.5.0 lib/node.rb
ttk-0.1.576 ruby_ex/node.rb
ttk-0.1.580 ruby_ex/node.rb
ttk-0.1.579 ruby_ex/node.rb
vcs-0.2.148 ruby_ex/node.rb