Documentation
¶
Index ¶
- type List
- func New[E any](cmp func(E, E) int, ops ...Option[E]) (*List[E], error)
- func NewFromOrderedSlice[E cmp.Ordered](elements []E, ops ...Option[E]) (*List[E], error)
- func NewFromSlice[E any](cmp func(E, E) int, elements []E, ops ...Option[E]) (*List[E], error)
- func NewOrdered[E cmp.Ordered](ops ...Option[E]) (*List[E], error)
- func (l *List[E]) Append(el ...E)
- func (l *List[E]) Clear()
- func (l *List[E]) Contains(el ...E) bool
- func (l *List[E]) Get(index int) (E, bool)
- func (l *List[E]) IndexOf(e E) int
- func (l *List[E]) Insert(index int, el ...E)
- func (l *List[E]) IsEmpty() bool
- func (l *List[E]) Len() int
- func (l *List[E]) Range(fn func(e E) bool)
- func (l *List[E]) Remove(e E)
- func (l *List[E]) RemoveAt(index int) E
- func (l *List[E]) Set(index int, e E)
- func (l *List[E]) Sort()
- func (l *List[E]) Swap(i, j int)
- func (l *List[E]) Values() []E
- type Option
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 ¶
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 ¶
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 ¶
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 ¶
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]) Contains ¶
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]) Insert ¶
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]) Range ¶
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 ¶
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 ¶
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.