Sha256: 5ad194d86876572c195e212b66d831520ed1e2a75884c9f2a78d6f9821d98e1e

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 KB

Contents

module RGL

  class EdgePropertiesMap

    def initialize(edge_properties_map, directed)
      @edge_properties_map = edge_properties_map
      @directed = directed

      check_properties
    end

    def edge_property(u, v)
      if @directed
        property = @edge_properties_map[[u, v]]
      else
        property = @edge_properties_map[[u, v]] || @edge_properties_map[[v, u]]
      end

      validate_property(property, u, v)

      property
    end

    private

    def check_properties
      @edge_properties_map.each { |(u, v), property| validate_property(property, u, v) } if @edge_properties_map.respond_to?(:each)
    end

    def validate_property(property, u, v)
      report_missing_property(property, u, v)
    end

    def report_missing_property(property, u, v)
      raise ArgumentError.new("property of edge (#{u}, #{v}) is not defined") unless property
    end

  end # EdgePropertiesMap


  class NonNegativeEdgePropertiesMap < EdgePropertiesMap

    private

    def validate_property(property, u, v)
      super
      report_negative_property(property, u, v)
    end

    def report_negative_property(property, u, v)
      raise ArgumentError.new("property of edge (#{u}, #{v}) is negative") if property < 0
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rgl-0.6.6 lib/rgl/edge_properties_map.rb
rgl-0.6.5 lib/rgl/edge_properties_map.rb
rgl-0.6.4 lib/rgl/edge_properties_map.rb
rgl-0.6.3 lib/rgl/edge_properties_map.rb
rgl-0.6.2 lib/rgl/edge_properties_map.rb
rgl-0.6.1 lib/rgl/edge_properties_map.rb
rgl-0.6.0 lib/rgl/edge_properties_map.rb