Documentation
¶
Index ¶
- Constants
- Variables
- type HashSlice
- func (hs *HashSlice[T]) Add(item T) (int, error)
- func (hs *HashSlice[T]) All() iter.Seq2[int, T]
- func (hs *HashSlice[T]) Contains(item T) bool
- func (hs *HashSlice[T]) GetByIndex(index int) (T, error)
- func (hs *HashSlice[T]) IndexOf(item T) int
- func (hs *HashSlice[T]) Items() []T
- func (hs *HashSlice[T]) Len() int
- func (hs *HashSlice[T]) Remove(item T)
- func (hs *HashSlice[T]) RemoveByIndex(index int)
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]) GetByIndex ¶
func (*HashSlice[T]) RemoveByIndex ¶
Click to show internal directories.
Click to hide internal directories.