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 ¶
- type ArrayList
- func (a *ArrayList[T]) Add(index int, t T) (err error)
- func (a *ArrayList[T]) Append(ts ...T) error
- func (a *ArrayList[T]) AsSlice() []T
- func (a *ArrayList[T]) Cap() int
- func (a *ArrayList[T]) Delete(index int) (T, error)
- func (a *ArrayList[T]) Get(index int) (t T, e error)
- func (a *ArrayList[T]) Len() int
- func (a *ArrayList[T]) Range(fn func(index int, t T) error) error
- func (a *ArrayList[T]) Set(index int, t T) error
- type ConcurrentList
- func (c *ConcurrentList[T]) Add(index int, t T) error
- func (c *ConcurrentList[T]) Append(ts ...T) error
- func (c *ConcurrentList[T]) AsSlice() []T
- func (c *ConcurrentList[T]) Cap() int
- func (c *ConcurrentList[T]) Delete(index int) (T, error)
- func (c *ConcurrentList[T]) Get(index int) (T, error)
- func (c *ConcurrentList[T]) Len() int
- func (c *ConcurrentList[T]) Range(fn func(index int, t T) error) error
- func (c *ConcurrentList[T]) Set(index int, t T) error
- type CopyOnWriteArrayList
- func (a *CopyOnWriteArrayList[T]) Add(index int, t T) (err error)
- func (a *CopyOnWriteArrayList[T]) Append(ts ...T) error
- func (a *CopyOnWriteArrayList[T]) AsSlice() []T
- func (a *CopyOnWriteArrayList[T]) Cap() int
- func (a *CopyOnWriteArrayList[T]) Delete(index int) (T, error)
- func (a *CopyOnWriteArrayList[T]) Get(index int) (t T, e error)
- func (a *CopyOnWriteArrayList[T]) Len() int
- func (a *CopyOnWriteArrayList[T]) Range(fn func(index int, t T) error) error
- func (a *CopyOnWriteArrayList[T]) Set(index int, t T) error
- type LinkedList
- func (l *LinkedList[T]) Add(index int, t T) error
- func (l *LinkedList[T]) Append(ts ...T) error
- func (l *LinkedList[T]) AsSlice() []T
- func (l *LinkedList[T]) Cap() int
- func (l *LinkedList[T]) Delete(index int) (T, error)
- func (l *LinkedList[T]) Get(index int) (T, error)
- func (l *LinkedList[T]) Len() int
- func (l *LinkedList[T]) Range(fn func(index int, t T) error) error
- func (l *LinkedList[T]) Set(index int, t T) error
- type List
- type SkipList
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 ¶
NewArrayList 初始化一个len为0,cap为cap的ArrayList
func NewArrayListOf ¶
NewArrayListOf 直接使用 ts,而不会执行复制
type ConcurrentList ¶
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 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]) 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
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