vectorgo

package module
v0.0.0-...-ca736ff Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: MIT Imports: 2 Imported by: 0

README

VectorGo

go-ci license latest-version Go Reference code-size

Dynamic array container module for Go

Overview

VectorGo is a go package contains dynamic array data structure called 'vector'. Dynamic array is an array that is resized automatically.

package main

import (
	"github.com/entimer/vectorgo"
)

func main() {
	vec := vectorgo.New()
	vec.Add(1)
	vec.Add(2)
	vec.Add(3)
	vec.RemoveAt(2)
	vec.RemoveAt(1)
	vec.RemoveAt(0)
}

Installation

$ go get github.com/entimer/vectorgo

Documentation

Check pkg.go.dev.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Vector

type Vector struct {
	Capacity int
	Size     int
	// contains filtered or unexported fields
}

Vector is an dynamic array implementation structure. It is basically array, but can resized. Size is changed automatically.

func New

func New() *Vector

New function creates a new dynamic array.

func (*Vector) Add

func (v *Vector) Add(value interface{})

Add method adds a new value at the last of the dynamic array.

func (*Vector) Clear

func (v *Vector) Clear()

Clear method removes all values in the dynamic array.

func (*Vector) Empty

func (v *Vector) Empty() bool

Empty method returns true if dynamic array is empty, false otherwise.

func (*Vector) Get

func (v *Vector) Get(index int) (interface{}, error)

Get method returns a value at the given index. If index is out of range, throws error.

func (*Vector) GetOrDefault

func (v *Vector) GetOrDefault(index int, def interface{}) interface{}

GetOrDefault method return a value at the given index. If index is out of bounds, it returns given default value.

func (*Vector) GetOrNil

func (v *Vector) GetOrNil(index int) interface{}

GetOrNil method return a value at the given index. If index is out of range, it returns nil.

func (*Vector) Insert

func (v *Vector) Insert(index int, value interface{}) error

Insert method add a new value at the given index. If index equals to size, adds at the last. If index is out of range, returns error.

func (*Vector) Remove

func (v *Vector) Remove(value interface{}) interface{}

Remove method removes a first value that is matched to given value. And also it returns removed value. If there is no matched value, returns nil.

func (*Vector) RemoveAt

func (v *Vector) RemoveAt(index int) (interface{}, error)

RemoveAt method removes a value at the given index. And also it returns removed value. If given index is out of range, returns error.

func (*Vector) Set

func (v *Vector) Set(index int, value interface{}) (interface{}, error)

Set method replaces a value at the given index. And also it returns old value. If index is out of range, returns error.

func (*Vector) SetOrDefault

func (v *Vector) SetOrDefault(index int, value interface{}, def interface{}) interface{}

SetOrDefault method replaces a value at the given index. And also it returns old value. If index is out of range, returns given default value.

func (*Vector) SetOrNil

func (v *Vector) SetOrNil(index int, value interface{}) interface{}

SetOrNil method replaces a value at the given index. And also it returns old value. If index is out of range, returns nil.

func (*Vector) String

func (v *Vector) String() string

String method returns a string expression of the dynamic array. e.g. [1, 2, 3, 4, 5]

Jump to

Keyboard shortcuts

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