arraylist

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List[E any] struct {
	// contains filtered or unexported fields
}

List represents a resizable array-backend list. Call New or NewFromSlice default creates a list witout concurrent safety. Call New or NewFromSlice with `WithSafe` option to make the List safe for concurrent use.

Example
list, _ := arraylist.New(cmp)

list.Append(1, 2, 3)
list.Remove(2)
list.Sort()

values := list.Values()
for _, v := range values {
	fmt.Println(v)
}
Output:

1
3

func New

func New[E any](cmp func(E, E) int, ops ...Option[E]) (*List[E], error)

New creates and returns a new array-backed list. The provided equal function is used to compare elements for equality. Optional options can be passed to modify the list's behavior, such as enabling concurrent safety.

func NewFromOrderedSlice

func NewFromOrderedSlice[E cmp.Ordered](elements []E, ops ...Option[E]) (*List[E], error)

NewFromOrderedSlice creates a new array-backed list from the given slice. It use cmp.Compare[E] as the default comparison function for elements. Optional options can be passed to modify the list's behavior, such as enabling concurrent safety.

func NewFromSlice

func NewFromSlice[E any](cmp func(E, E) int, elements []E, ops ...Option[E]) (*List[E], error)

NewFromSlice creates a new array-backed list from the given slice. The provided equal function is used to compare elements for equality. Optional options can be passed to modify the list's behavior, such as enabling concurrent safety.

func NewOrdered

func NewOrdered[E cmp.Ordered](ops ...Option[E]) (*List[E], error)

NewOrdered creates and returns a new array-backed list for ordered elements. It use cmp.Compare[E] as the default comparison function for elements. Optional options can be passed to modify the list's behavior, such as enabling concurrent safety.

func (*List[E]) Append

func (l *List[E]) Append(el ...E)

Append appends specified elements to the end of the list.

func (*List[E]) Clear

func (l *List[E]) Clear()

Clear removes all elements from the list.

func (*List[E]) Contains

func (l *List[E]) Contains(el ...E) bool

Contains reports whether the list contains all the given elements. Returns true if all elements are present in the list, false otherwise.

func (*List[E]) Get

func (l *List[E]) Get(index int) (E, bool)

Get returns the element at the given index.

func (*List[E]) IndexOf

func (l *List[E]) IndexOf(e E) int

IndexOf returns the index of the first occurrence of element in the list.

func (*List[E]) Insert

func (l *List[E]) Insert(index int, el ...E)

Insert inserts elements at the given index. If the index is the length of the list, the elements will be appended. If the index out of range, this function is no-op.

func (*List[E]) IsEmpty

func (l *List[E]) IsEmpty() bool

IsEmpty reports whether the list has no elements.

func (*List[E]) Len

func (l *List[E]) Len() int

Len returns the number of elements in the list.

func (*List[E]) Range

func (l *List[E]) Range(fn func(e E) bool)

Range call function fn on each element in the list. if `fn` returns false, the iteration stops. if `fn` is nil, the method does nothing.

func (*List[E]) Remove

func (l *List[E]) Remove(e E)

Remove removes all the elements from the list.

func (*List[E]) RemoveAt

func (l *List[E]) RemoveAt(index int) E

RemoveAt removes the element at the given index. If the index out of range, this function is no-op and returns zero value of E.

func (*List[E]) Set

func (l *List[E]) Set(index int, e E)

Set sets the element at the given index. If the index is the length of the list, the element will be appended. If the index out of range, this function is no-op.

func (*List[E]) Sort

func (l *List[E]) Sort()

Sort sorts the list using the given comparator if cmp is nil, the function is no-op. cmp should return: - A negative value if first argument is less than second. - Zero if the arguments are equal. - A positive value if first argument is greater than second.

func (*List[E]) Swap

func (l *List[E]) Swap(i, j int)

Swap swaps the elements at the given indexes.

func (*List[E]) Values

func (l *List[E]) Values() []E

Values returns a slice containing all elements in the list. The returned slice is a copy of the internal slice, and modifications to it will not affect the list.

type Option

type Option[E any] func(*List[E]) error

func WithSafe

func WithSafe[E any]() Option[E]

WithSafe creates a Option that make the array-backed list safe for concurrent use.

Jump to

Keyboard shortcuts

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