Documentation
¶
Overview ¶
https://go.dev/wiki/SliceTricks
Package vector provides a vector implementation.
Index ¶
- type Vector
- func (v Vector[V]) All() iter.Seq2[int, V]
- func (v Vector[V]) At(i int) V
- func (v Vector[V]) Back() V
- func (v Vector[V]) Backwards() iter.Seq[V]
- func (v *Vector[V]) Clear()
- func (v *Vector[V]) Concat(other Vector[V])
- func (v *Vector[V]) Copy(src Vector[V])
- func (v *Vector[V]) Cut(i, j int)
- func (v Vector[V]) Front() V
- func (v *Vector[V]) Insert(i int, vals ...V)
- func (v Vector[V]) IsEmpty() bool
- func (v Vector[V]) Len() int
- func (v *Vector[V]) Pop() V
- func (v *Vector[V]) PopAt(i int) V
- func (v *Vector[V]) Push(val V)
- func (v Vector[V]) Reverse()
- func (v Vector[V]) Set(i int, val V)
- func (v Vector[V]) String() string
- func (v Vector[V]) Swap(i, j int)
- func (v Vector[V]) Values() iter.Seq[V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Vector ¶
type Vector[V any] []V
Vector is a slice of values.
func New ¶
New returns a new Vector with the given values.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) fmt.Println(v) }
Output: [1 2 3]
func (Vector[V]) All ¶
All returns an iter.Seq2[int, V] of values and their indexes in the vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) for idx, val := range v.All() { fmt.Println(idx, val) } }
Output: 0 1 1 2 2 3
func (Vector[V]) At ¶
At returns the value at index i.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) fmt.Println(v.At(1)) }
Output: 2
func (Vector[V]) Back ¶
func (v Vector[V]) Back() V
Back returns the last value in the vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) fmt.Println(v.Back()) }
Output: 3
func (Vector[V]) Backwards ¶
Backwards returns an iter.Seq[V] of all values in the vector in reverse order.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) for val := range v.Backwards() { fmt.Println(val) } }
Output: 3 2 1
func (*Vector[V]) Clear ¶
func (v *Vector[V]) Clear()
Clear removes all values from this vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) v.Clear() fmt.Println(v) }
Output: [0 0 0]
func (*Vector[V]) Concat ¶
Concat appends the values of other to this vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) v.Concat(vector.New(4, 5, 6)) fmt.Println(v) }
Output: [1 2 3 4 5 6]
func (*Vector[V]) Copy ¶
Copy copies the values of src to this vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) v2 := vector.New(4, 5, 6) v.Copy(v2) fmt.Println(v) }
Output: [4 5 6]
func (*Vector[V]) Cut ¶
Cut removes the values from i to j from this vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3, 4, 5, 6) v.Cut(1, 4) fmt.Println(v) }
Output: [1 5 6]
func (Vector[V]) Front ¶
func (v Vector[V]) Front() V
Front returns the first value in the vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) fmt.Println(v.Front()) }
Output: 1
func (*Vector[V]) Insert ¶
Insert inserts the values vals at index i in this vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) v.Insert(1, 4) fmt.Println(v) }
Output: [1 4 2 3]
func (Vector[V]) IsEmpty ¶
IsEmpty returns true if the vector is empty.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New[int]() fmt.Println(v.IsEmpty()) v.Push(1) fmt.Println(v.IsEmpty()) }
Output: true false
func (Vector[V]) Len ¶
Len returns the length of the vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) fmt.Println(v.Len()) }
Output: 3
func (*Vector[V]) Pop ¶
func (v *Vector[V]) Pop() V
Pop removes the last value from this vector and returns it.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) fmt.Println(v.Pop()) fmt.Println(v) }
Output: 3 [1 2]
func (*Vector[V]) PopAt ¶
PopAt removes the value at i from this vector and returns it.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) fmt.Println(v.PopAt(1)) fmt.Println(v) }
Output: 2 [1 3]
func (*Vector[V]) Push ¶
func (v *Vector[V]) Push(val V)
Push appends the value val to this vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New[int]() v.Push(1) v.Push(2) v.Push(3) fmt.Println(v) }
Output: [1 2 3]
func (Vector[V]) Reverse ¶
func (v Vector[V]) Reverse()
Reverse reverses the values in the vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) v.Reverse() fmt.Println(v) }
Output: [3 2 1]
func (Vector[V]) Set ¶
Set sets the value at index i to val.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) v.Set(1, 4) fmt.Println(v) }
Output: [1 4 3]
func (Vector[V]) String ¶
String returns a string representation of the vector.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3) fmt.Println(v) }
Output: [1 2 3]
func (Vector[V]) Swap ¶
Swap swaps the values at indexes i and j.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/vector" ) func main() { v := vector.New(1, 2, 3, 4, 5, 6) v.Swap(1, 4) fmt.Println(v) }
Output: [1 5 3 4 2 6]