github.com/LdDl/ch@v1.7.8/incident_edge.go (about)

     1  package ch
     2  
     3  // incidentEdge incident edge for certain vertex
     4  type incidentEdge struct {
     5  	vertexID int64
     6  	weight   float64
     7  }
     8  
     9  // addInIncidentEdge Adds incident edge's to pool of "incoming" edges of given vertex.
    10  // Just an alias to append() function
    11  // incomingVertexID - Library defined ID of vertex
    12  // weight - Travel cost of incoming edge
    13  func (vertex *Vertex) addInIncidentEdge(incomingVertexID int64, weight float64) {
    14  	vertex.inIncidentEdges = append(vertex.inIncidentEdges, incidentEdge{incomingVertexID, weight})
    15  }
    16  
    17  // addOutIncidentEdge Adds incident edge's to pool of "outcoming" edges of given vertex.
    18  // Just an alias to append() function
    19  // outcomingVertexID - Library defined ID of vertex
    20  // weight - Travel cost of outcoming edge
    21  func (vertex *Vertex) addOutIncidentEdge(outcomingVertexID int64, weight float64) {
    22  	vertex.outIncidentEdges = append(vertex.outIncidentEdges, incidentEdge{outcomingVertexID, weight})
    23  }
    24  
    25  // findInIncidentEdge Returns index of incoming incident edge by vertex ID
    26  // If incoming incident edge is not found then this function returns -1
    27  func (vertex *Vertex) findInIncidentEdge(vertexID int64) int {
    28  	for i := range vertex.inIncidentEdges {
    29  		if vertex.inIncidentEdges[i].vertexID == vertexID {
    30  			return i
    31  		}
    32  	}
    33  	return -1
    34  }
    35  
    36  // findOutIncidentEdge Returns index of outcoming incident edge by vertex ID on the other side of that edge
    37  // If outcoming incident edge is not found then this function returns -1
    38  func (vertex *Vertex) findOutIncidentEdge(vertexID int64) int {
    39  	for i := range vertex.outIncidentEdges {
    40  		if vertex.outIncidentEdges[i].vertexID == vertexID {
    41  			return i
    42  		}
    43  	}
    44  	return -1
    45  }
    46  
    47  // updateInIncidentEdge Updates incoming incident edge's cost by vertex ID on the other side of that edge
    48  // If operation is not successful then this function returns False
    49  func (vertex *Vertex) updateInIncidentEdge(vertexID int64, weight float64) bool {
    50  	idx := vertex.findInIncidentEdge(vertexID)
    51  	if idx < 0 {
    52  		return false
    53  	}
    54  	vertex.inIncidentEdges[idx].weight = weight
    55  	return true
    56  }
    57  
    58  // updateOutIncidentEdge Updates outcoming incident edge's cost by vertex ID on the other side of that edge
    59  // If operation is not successful then this function returns False
    60  func (vertex *Vertex) updateOutIncidentEdge(vertexID int64, weight float64) bool {
    61  	idx := vertex.findOutIncidentEdge(vertexID)
    62  	if idx < 0 {
    63  		return false
    64  	}
    65  	vertex.outIncidentEdges[idx].weight = weight
    66  	return true
    67  }