types

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const NoneIndex = -1

Variables

View Source
var (
	AlreadyExistsError = errors.New("already exists")
)

Functions

This section is empty.

Types

type HashSlice

type HashSlice[T comparable] struct {
	// contains filtered or unexported fields
}

HashSlice combines an ordered slice with a hash map for O(1) lookups by key. It maintains insertion order while providing fast index retrieval and element uniqueness.

This is useful when you need to:

  • Keep elements in a stable order (like insertion order)
  • Quickly find an element's position (IndexOf)
  • Check element existence (Contains)
  • Ensure uniqueness of elements
  • Iterate in order (All)

Example:

hs, _ := NewHashSlice(StatusPending, StatusActive, StatusClosed)

idx := hs.IndexOf(StatusActive)      // returns 1 (O(1))
item, _ := hs.GetByIndex(2)          // returns StatusClosed

for idx, status := range hs.All() {
    fmt.Printf("%d: %v\n", idx, status)
}

Trying to add duplicate returns AlreadyExistsError

_, err := hs.Add(StatusActive)

func NewHashSlice

func NewHashSlice[T comparable](items ...T) (*HashSlice[T], error)

func (*HashSlice[T]) Add

func (hs *HashSlice[T]) Add(item T) (int, error)

func (*HashSlice[T]) All

func (hs *HashSlice[T]) All() iter.Seq2[int, T]

func (*HashSlice[T]) Contains

func (hs *HashSlice[T]) Contains(item T) bool

func (*HashSlice[T]) GetByIndex

func (hs *HashSlice[T]) GetByIndex(index int) (T, error)

func (*HashSlice[T]) IndexOf

func (hs *HashSlice[T]) IndexOf(item T) int

func (*HashSlice[T]) Items

func (hs *HashSlice[T]) Items() []T

func (*HashSlice[T]) Len

func (hs *HashSlice[T]) Len() int

func (*HashSlice[T]) Remove

func (hs *HashSlice[T]) Remove(item T)

func (*HashSlice[T]) RemoveByIndex

func (hs *HashSlice[T]) RemoveByIndex(index int)

Jump to

Keyboard shortcuts

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