Documentation ¶
Overview ¶
Package paths provides efficient functions for representing and manipulating Segment Routing paths within a network.
Index ¶
- type PathVar
- func (p *PathVar) CanClear() bool
- func (p *PathVar) CanInsert(pos int, node int) bool
- func (p *PathVar) CanRemove(pos int) bool
- func (p *PathVar) CanUpdate(pos int, node int) bool
- func (p *PathVar) Clear() bool
- func (p *PathVar) Insert(pos int, node int) bool
- func (p *PathVar) Length() int
- func (p *PathVar) Node(pos int) int
- func (p *PathVar) Nodes() []int
- func (p *PathVar) Remove(pos int) bool
- func (p *PathVar) String() string
- func (p *PathVar) Update(pos int, node int) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PathVar ¶
type PathVar struct {
// contains filtered or unexported fields
}
PathVar represents a valid path between two nodes in a network.
A PathVar is a slice of node IDs that defines a valid path. It respects the following invariants:
- Minimum length: 2 (source and destination nodes)
- Source node: First element in the slice
- Destination node: Last element in the slice
- Unique nodes: No consecutive nodes can be the same
All operations on PathVar guarantee that these invariants are maintained.
func New ¶
New instantiates and returns a new PathVar.
Example ¶
p := New(0, 3, 2) fmt.Println(p)
Output: 0 -> 3
func (*PathVar) CanInsert ¶
CanInsert returns true if the Insert operation can be performed at the specified position with the given node.
func (*PathVar) CanRemove ¶
CanRemove returns true if the Remove operation can be performed on the node at the specified position.
func (*PathVar) CanUpdate ¶
CanUpdate returns true if the Update operation can be performed on the node at the specified position.
func (*PathVar) Clear ¶
Clear attempts to remove all the intermediate nodes between the path's source and destination. It returns true if the operation succeeded or false if the operation would violate one of the path invariants.
Example ¶
p := New(0, 4, 4) p.Insert(1, 2) p.Insert(1, 1) fmt.Println(p) fmt.Println(p.Clear()) // valid fmt.Println(p) fmt.Println(p.Clear()) // invalid: cannot clear empty path
Output: 0 -> 1 -> 2 -> 4 true 0 -> 4 false
func (*PathVar) Insert ¶
Insert inserts the given node at the position pos in the path. The node originally at position pos (and all subsequent nodes) are shifted one position to the right to make space for the new node. The function returns true if the operation succeeded or false if the operation would violate one of the path invariants.
Example ¶
p := New(0, 4, 4) fmt.Println(p.Insert(1, 2)) // valid fmt.Println(p.Insert(0, 1)) // invalid: cannot replace the source fmt.Println(p.Insert(1, 0)) // invalid: same consecutive fmt.Println(p.Insert(1, 2)) // invalid: same consecutive fmt.Println(p.Insert(1, 1)) // valid fmt.Println(p.Insert(3, 3)) // exceed length fmt.Println(p)
Output: true false false false true false 0 -> 1 -> 2 -> 4
func (*PathVar) Length ¶
Length returns the length of the path in terms of nodes.
Example ¶
p := New(0, 4, 4) fmt.Println(p.Length()) p.Insert(1, 2) fmt.Println(p.Length()) p.Insert(1, 1) fmt.Println(p.Length())
Output: 2 3 4
func (*PathVar) Node ¶
Node returns the node at position pos starting from 0 (the source) and ending at Length()-1 (the destination).
Example ¶
p := New(0, 4, 5) p.Insert(1, 2) p.Insert(1, 1) for i := 0; i < p.Length(); i++ { fmt.Println(p.Node(i)) }
Output: 0 1 2 4
func (*PathVar) Nodes ¶
Nodes returns the sequence of nodes in the path (including the path's source and destination).
Important: the slice is a view on one of the path's internal structure and should only be used in read-only operations. Modifying the slice will most likely results in incorrect behavior.
Example ¶
p := New(0, 4, 5) p.Insert(1, 2) p.Insert(1, 1) fmt.Println(p.Nodes())
Output: [0 1 2 4]
func (*PathVar) Remove ¶
Remove removes the node at the specified position from the path. It returns true if the operation succeeded or false if the operation would violate one of the path invariants.
Note that this function might remove more than one node to guarantee that the PathVar invariants are maintained. For example, removing node 3 in path 1 -> 2 -> 3 -> 2 -> 4 will results in path 1 -> 2 -> 4 to guarantee that no consecutive nodes in the path are the same.
Example ¶
// Build path 0 -> 1 -> 2 -> 3 -> 2 -> 4 p := New(0, 4, 6) p.Insert(1, 2) p.Insert(1, 3) p.Insert(1, 2) p.Insert(1, 1) fmt.Println(p) fmt.Println(p.Remove(0)) // invalid: cannot remove source fmt.Println(p) fmt.Println(p.Remove(5)) // invalid: cannot remove destination fmt.Println(p) fmt.Println(p.Remove(3)) // valid fmt.Println(p) fmt.Println(p.Remove(2)) // valid fmt.Println(p) fmt.Println(p.Remove(1)) // valid fmt.Println(p)
Output: 0 -> 1 -> 2 -> 3 -> 2 -> 4 false 0 -> 1 -> 2 -> 3 -> 2 -> 4 false 0 -> 1 -> 2 -> 3 -> 2 -> 4 true 0 -> 1 -> 2 -> 4 true 0 -> 1 -> 4 true 0 -> 4
func (*PathVar) String ¶
String returns a string representation of the path as a sequence of nodes separated by " -> ". For example: "0 -> 4 -> 3 -> 1".
func (*PathVar) Update ¶
Update updates the node at the specified position with the new node value. It returns true if the operation succeeded or false if the operation would violate one of the path invariants.
Example ¶
// Build path 0 -> 1 -> 2 -> 4 p := New(0, 4, 4) p.Insert(1, 2) p.Insert(1, 1) fmt.Println(p) fmt.Println(p.Update(0, 5)) // invalid: cannot update source fmt.Println(p) fmt.Println(p.Update(3, 5)) // invalid: cannot update destination fmt.Println(p) fmt.Println(p.Update(1, 2)) // invalid: consecutive node fmt.Println(p) fmt.Println(p.Update(1, 0)) // invalid: consecutive node fmt.Println(p) fmt.Println(p.Update(1, 1)) // invalid: no change fmt.Println(p) fmt.Println(p.Update(1, 3)) // valid fmt.Println(p) fmt.Println(p.Update(2, 1)) // valid fmt.Println(p) fmt.Println(p.Update(1, 2)) // valid fmt.Println(p)
Output: 0 -> 1 -> 2 -> 4 false 0 -> 1 -> 2 -> 4 false 0 -> 1 -> 2 -> 4 false 0 -> 1 -> 2 -> 4 false 0 -> 1 -> 2 -> 4 false 0 -> 1 -> 2 -> 4 true 0 -> 3 -> 2 -> 4 true 0 -> 3 -> 1 -> 4 true 0 -> 2 -> 1 -> 4