vector

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

https://go.dev/wiki/SliceTricks

Package vector provides a vector implementation.

Index

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

func New[V any](vals ...V) Vector[V]

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

func (v Vector[V]) All() iter.Seq2[int, V]

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

func (v Vector[V]) At(i int) V

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

func (v Vector[V]) Backwards() iter.Seq[V]

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

func (v *Vector[V]) Concat(other Vector[V])

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

func (v *Vector[V]) Copy(src Vector[V])

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

func (v *Vector[V]) Cut(i, j int)

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

func (v *Vector[V]) Insert(i int, vals ...V)

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

func (v Vector[V]) IsEmpty() bool

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

func (v Vector[V]) Len() int

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

func (v *Vector[V]) PopAt(i int) V

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

func (v Vector[V]) Set(i int, val V)

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

func (v Vector[V]) String() 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

func (v Vector[V]) Swap(i, j int)

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]

func (Vector[V]) Values

func (v Vector[V]) Values() iter.Seq[V]

Values returns an iter.Seq[V] of all values in the vector.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/vector"
)

func main() {
	v := vector.New(1, 2, 3)
	for val := range v.Values() {
		fmt.Println(val)
	}
}
Output:

1
2
3

Jump to

Keyboard shortcuts

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