Sha256: 10eae48d012df46375dac2229af07e463bddfab740ef628df351c637c22fee4f

Contents?: true

Size: 933 Bytes

Versions: 3

Compression:

Stored size: 933 Bytes

Contents

require 'set'

module Tangle
  # Edge related methods in a graph
  module GraphEdges
    # Get all edges.
    #
    # edges => Array
    #
    def edges(vertex = nil)
      return @edges if vertex.nil?
      @vertices.fetch(vertex)
    end

    # Add a new edge to the graph
    #
    # add_edge(vtx1, vtx2, ...) => Edge
    #
    def add_edge(*vertices, **kvargs)
      insert_edge(self.class::Edge.new(*vertices, mixins: @mixins, **kvargs))
    end

    # Remove an edge from the graph
    def remove_edge(edge)
      edge.each_vertex do |vertex|
        @vertices.fetch(vertex).delete(edge)
      end
      @edges.delete(edge)
    end

    protected

    # Insert a prepared edge into the graph
    #
    def insert_edge(edge)
      @edges << edge
      edge.each_vertex do |vertex|
        @vertices.fetch(vertex) << edge
      end
      edge
    end

    private

    def initialize_edges
      @edges = Set[]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tangle-0.8.2 lib/tangle/graph_edges.rb
tangle-0.8.1 lib/tangle/graph_edges.rb
tangle-0.8.0 lib/tangle/graph_edges.rb