Sha256: 393bd7b16b92015f904f01a93d819861fdaf9bd7fd3d363528ca7082186415e2

Contents?: true

Size: 946 Bytes

Versions: 4

Compression:

Stored size: 946 Bytes

Contents

# frozen_string_literal: true

require 'forwardable'
require_relative 'mixin'

module Tangle
  #
  # An edge in an undirected graph, connecting two vertices
  #
  class Edge
    include Tangle::Mixin::Initialize

    attr_reader :name

    # Create a new edge between vertices
    #
    # Edge.new(vtx1) => Edge (loop)
    # Edge.new(vtx1, vtx2) => Edge
    #
    # End users should probably use Graph#add_edge instead.
    #
    def initialize(vertex1, vertex2 = vertex1, name: nil, **kwargs)
      @name = name
      initialize_vertices(vertex1, vertex2)
      initialize_mixins(**kwargs)
    end

    def [](from_vertex)
      @vertices[from_vertex]
    end

    def walk(from_vertex)
      @vertices.fetch(from_vertex)
    end

    def include?(vertex)
      each_vertex.include?(vertex)
    end

    def loop?
      @loop
    end

    private

    def initialize_vertices(vertex1, vertex2)
      @loop = vertex1 == vertex2
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tangle-0.11.0 lib/tangle/edge.rb
tangle-0.10.2 lib/tangle/edge.rb
tangle-0.10.1 lib/tangle/edge.rb
tangle-0.10.0 lib/tangle/edge.rb