paths

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package paths provides efficient functions for representing and manipulating Segment Routing paths within a network.

Index

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

func New(from int, to int, maxNodes int) *PathVar

New instantiates and returns a new PathVar.

Example
p := New(0, 3, 2)

fmt.Println(p)
Output:

0 -> 3

func (*PathVar) CanClear

func (p *PathVar) CanClear() bool

CanClear returns true if the Clear operation can be performed.

func (*PathVar) CanInsert

func (p *PathVar) CanInsert(pos int, node int) bool

CanInsert returns true if the Insert operation can be performed at the specified position with the given node.

func (*PathVar) CanRemove

func (p *PathVar) CanRemove(pos int) bool

CanRemove returns true if the Remove operation can be performed on the node at the specified position.

func (*PathVar) CanUpdate

func (p *PathVar) CanUpdate(pos int, node int) bool

CanUpdate returns true if the Update operation can be performed on the node at the specified position.

func (*PathVar) Clear

func (p *PathVar) Clear() bool

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

func (p *PathVar) Insert(pos int, node int) bool

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

func (p *PathVar) Length() int

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

func (p *PathVar) Node(pos int) int

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

func (p *PathVar) Nodes() []int

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

func (p *PathVar) Remove(pos int) bool

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

func (p *PathVar) String() 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

func (p *PathVar) Update(pos int, node int) bool

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL