Sha256: ecf3de00eaf3d30393c5d57cb7021416c8d40ebe216e386244f6cef032e42dd7

Contents?: true

Size: 582 Bytes

Versions: 2

Compression:

Stored size: 582 Bytes

Contents

module Kruskal
  class Forest
    include Enumerable

    def initialize
      @trees = {}
    end

    def index(node, tree)
      @trees[node] = tree
      @cached_to_a = nil
    end

    def new_tree(value = 0, source, target)
      Tree.new(self, [[value, source, target]])
    end

    def to_a
      @cached_to_a ||= @trees.values.uniq
    end

    def find_tree_for(node)
      @trees[node]
    end

    def [](position)
      to_a[position]
    end

    def each
      to_a.each do |tree|
        yield tree
      end
    end

    def size
      to_a.size
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kruskal-0.1.1 lib/kruskal/forest.rb
kruskal-0.1.0 lib/kruskal/forest.rb