github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/graph/graph.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package graph
     8  
     9  // Vertex defines a vertex of a graph
    10  type Vertex struct {
    11  	Id        string
    12  	Data      interface{}
    13  	neighbors map[string]*Vertex
    14  }
    15  
    16  // NewVertex creates a new vertex with given id and data
    17  func NewVertex(id string, data interface{}) *Vertex {
    18  	return &Vertex{
    19  		Id:        id,
    20  		Data:      data,
    21  		neighbors: make(map[string]*Vertex),
    22  	}
    23  }
    24  
    25  // NeighborById returns a neighbor vertex with the given id,
    26  // or nil if no vertex with such an id is a neighbor
    27  func (v *Vertex) NeighborById(id string) *Vertex {
    28  	return v.neighbors[id]
    29  }
    30  
    31  // Neighbors returns the neighbors of the vertex
    32  func (v *Vertex) Neighbors() []*Vertex {
    33  	var res []*Vertex
    34  	for _, u := range v.neighbors {
    35  		res = append(res, u)
    36  	}
    37  	return res
    38  }
    39  
    40  // AddNeighbor adds the given vertex as a neighbor
    41  // of the vertex
    42  func (v *Vertex) AddNeighbor(u *Vertex) {
    43  	v.neighbors[u.Id] = u
    44  	u.neighbors[v.Id] = v
    45  }