list

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: May 17, 2025 License: Apache-2.0 Imports: 5 Imported by: 4

Documentation

Overview

Copyright 2021 ecodeclub

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Examples

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) (err 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 CopyOnWriteArrayList added in v0.0.10

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

CopyOnWriteArrayList 基于切片的简单封装 写时加锁,读不加锁,适合于读多写少场景

func NewCopyOnWriteArrayList added in v0.0.10

func NewCopyOnWriteArrayList[T any]() *CopyOnWriteArrayList[T]

NewCopyOnWriteArrayList

func NewCopyOnWriteArrayListOf added in v0.0.10

func NewCopyOnWriteArrayListOf[T any](ts []T) *CopyOnWriteArrayList[T]

NewCopyOnWriteArrayListOf 直接使用 ts,会执行复制

func (*CopyOnWriteArrayList[T]) Add added in v0.0.10

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

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

func (*CopyOnWriteArrayList[T]) Append added in v0.0.10

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

Append 往CopyOnWriteArrayList里追加数据

func (*CopyOnWriteArrayList[T]) AsSlice added in v0.0.10

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

func (*CopyOnWriteArrayList[T]) Cap added in v0.0.10

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

func (*CopyOnWriteArrayList[T]) Delete added in v0.0.10

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

这里不涉及缩容,每次都是当前内容长度申请的数组容量

func (*CopyOnWriteArrayList[T]) Get added in v0.0.10

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

func (*CopyOnWriteArrayList[T]) Len added in v0.0.10

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

func (*CopyOnWriteArrayList[T]) Range added in v0.0.10

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

func (*CopyOnWriteArrayList[T]) Set added in v0.0.10

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

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

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 在特定下标处增加一个新元素
	// 如果下标不在[0, Len()]范围之内
	// 应该返回错误
	// 如果index == Len()则表示往List末端增加一个值
	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 接口 该接口只定义清楚各个方法的行为和表现

type SkipList added in v0.0.9

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

func NewSkipList added in v0.0.9

func NewSkipList[T any](compare ekit.Comparator[T]) *SkipList[T]
Example
package main

import (
	"fmt"

	"github.com/ecodeclub/ekit"
	"github.com/ecodeclub/ekit/internal/list"
)

func main() {
	l := list.NewSkipList[int](ekit.ComparatorRealNumber[int])
	l.Insert(123)
	val, _ := l.Get(0)
	fmt.Println(val)
}
Output:

123

func (*SkipList[T]) AsSlice added in v0.0.9

func (sl *SkipList[T]) AsSlice() []T

func (*SkipList[T]) Cap added in v0.0.9

func (sl *SkipList[T]) Cap() int

func (*SkipList[T]) DeleteElement added in v0.0.9

func (sl *SkipList[T]) DeleteElement(target T) bool

func (*SkipList[T]) Insert added in v0.0.9

func (sl *SkipList[T]) Insert(Val T)

func (*SkipList[T]) Len added in v0.0.9

func (sl *SkipList[T]) Len() int

func (*SkipList[T]) Search added in v0.0.9

func (sl *SkipList[T]) Search(target T) bool

Jump to

Keyboard shortcuts

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