list

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: Apache-2.0 Imports: 3 Imported by: 8 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayList

type ArrayList[T any] struct {
	// contains filtered or unexported fields
}

ArrayList 基于切片的简单封装

func NewArrayList

func NewArrayList[T any](cap int) *ArrayList[T]

NewArrayList 初始化一个len为0,cap为cap的ArrayList

func NewArrayListOf

func NewArrayListOf[T any](ts []T) *ArrayList[T]

NewArrayListOf 直接使用 ts,而不会执行复制

func (*ArrayList[T]) Add

func (a *ArrayList[T]) Add(index int, t T) error

Add 在ArrayList下标为index的位置插入一个元素 当index等于ArrayList长度等同于append

func (*ArrayList[T]) Append

func (a *ArrayList[T]) Append(ts ...T) error

Append 往ArrayList里追加数据

func (*ArrayList[T]) AsSlice

func (a *ArrayList[T]) AsSlice() []T

func (*ArrayList[T]) Cap

func (a *ArrayList[T]) Cap() int

func (*ArrayList[T]) Delete

func (a *ArrayList[T]) Delete(index int) (T, error)

Delete 方法会在必要的时候引起缩容,其缩容规则是: - 如果容量 > 2048,并且长度小于容量一半,那么就会缩容为原本的 5/8 - 如果容量 (64, 2048],如果长度是容量的 1/4,那么就会缩容为原本的一半 - 如果此时容量 <= 64,那么我们将不会执行缩容。在容量很小的情况下,浪费的内存很少,所以没必要消耗 CPU去执行缩容

func (*ArrayList[T]) Get

func (a *ArrayList[T]) Get(index int) (t T, e error)

func (*ArrayList[T]) Len

func (a *ArrayList[T]) Len() int

func (*ArrayList[T]) Range

func (a *ArrayList[T]) Range(fn func(index int, t T) error) error

func (*ArrayList[T]) Set

func (a *ArrayList[T]) Set(index int, t T) error

Set 设置ArrayList里index位置的值为t

type ConcurrentList

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

ConcurrentList 用读写锁封装了对 List 的操作 达到线程安全的目标

func (*ConcurrentList[T]) Add

func (c *ConcurrentList[T]) Add(index int, t T) error

func (*ConcurrentList[T]) Append

func (c *ConcurrentList[T]) Append(ts ...T) error

func (*ConcurrentList[T]) AsSlice

func (c *ConcurrentList[T]) AsSlice() []T

func (*ConcurrentList[T]) Cap

func (c *ConcurrentList[T]) Cap() int

func (*ConcurrentList[T]) Delete

func (c *ConcurrentList[T]) Delete(index int) (T, error)

func (*ConcurrentList[T]) Get

func (c *ConcurrentList[T]) Get(index int) (T, error)

func (*ConcurrentList[T]) Len

func (c *ConcurrentList[T]) Len() int

func (*ConcurrentList[T]) Range

func (c *ConcurrentList[T]) Range(fn func(index int, t T) error) error

func (*ConcurrentList[T]) Set

func (c *ConcurrentList[T]) Set(index int, t T) error

type LinkedList

type LinkedList[T any] struct {
	// contains filtered or unexported fields
}

LinkedList 双向循环链表

func NewLinkedList

func NewLinkedList[T any]() *LinkedList[T]

NewLinkedList 创建一个双向循环链表

func NewLinkedListOf

func NewLinkedListOf[T any](ts []T) *LinkedList[T]

NewLinkedListOf 将切片转换为双向循环链表, 直接使用了切片元素的值,而没有进行复制

func (*LinkedList[T]) Add

func (l *LinkedList[T]) Add(index int, t T) error

Add 在 LinkedList 下标为 index 的位置插入一个元素 当 index 等于 LinkedList 长度等同于 Append

func (*LinkedList[T]) Append

func (l *LinkedList[T]) Append(ts ...T) error

Append 往链表最后添加元素

func (*LinkedList[T]) AsSlice

func (l *LinkedList[T]) AsSlice() []T

func (*LinkedList[T]) Cap

func (l *LinkedList[T]) Cap() int

func (*LinkedList[T]) Delete

func (l *LinkedList[T]) Delete(index int) (T, error)

Delete 删除指定位置的元素

func (*LinkedList[T]) Get

func (l *LinkedList[T]) Get(index int) (T, error)

func (*LinkedList[T]) Len

func (l *LinkedList[T]) Len() int

func (*LinkedList[T]) Range

func (l *LinkedList[T]) Range(fn func(index int, t T) error) error

func (*LinkedList[T]) Set

func (l *LinkedList[T]) Set(index int, t T) error

Set 设置链表中index索引处的值为t

type List

type List[T any] interface {
	// Get 返回对应下标的元素,
	// 在下标超出范围的情况下,返回错误
	Get(index int) (T, error)
	// Append 在末尾追加元素
	Append(ts ...T) error
	// Add 在特定下标处增加一个新元素
	// 如果下标超出范围,应该返回错误
	Add(index int, t T) error
	// Set 重置 index 位置的值
	// 如果下标超出范围,应该返回错误
	Set(index int, t T) error
	// Delete 删除目标元素的位置,并且返回该位置的值
	// 如果 index 超出下标,应该返回错误
	Delete(index int) (T, error)
	// Len 返回长度
	Len() int
	// Cap 返回容量
	Cap() int
	// Range 遍历 List 的所有元素
	Range(fn func(index int, t T) error) error
	// AsSlice 将 List 转化为一个切片
	// 不允许返回nil,在没有元素的情况下,
	// 必须返回一个长度和容量都为 0 的切片
	// AsSlice 每次调用都必须返回一个全新的切片
	AsSlice() []T
}

List 接口 该接口只定义清楚各个方法的行为和表现

Jump to

Keyboard shortcuts

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