循环链表类

package
v0.0.0-...-2910145 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

gring包提供了一种并发安全/不安全的循环链表(环形列表)。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ring

type Ring struct {
	// contains filtered or unexported fields
}

Ring 是一个环形结构的结构体。

func New

func New(cap int, safe ...bool) *Ring

New 创建并返回一个容量为`cap`的Ring结构体。 可选参数`sage`用于指定该结构体是否在并发场景下安全使用,默认为false(不安全)。

Example
// 非并发安全
循环链表类.New(10)

// Concurrent safety
循环链表类.New(10, true)
Output:

func (*Ring) Cap

func (r *Ring) Cap() int

Cap 返回环形缓冲区的容量。

Example
r1 := 循环链表类.New(10)
for i := 0; i < 5; i++ {
	r1.X设置值(i).Next()
}
fmt.Println("Cap:", r1.Cap())

r2 := 循环链表类.New(10, true)
for i := 0; i < 10; i++ {
	r2.X设置值(i).Next()
}
fmt.Println("Cap:", r2.Cap())
Output:

Cap: 10
Cap: 10

func (*Ring) Len

func (r *Ring) Len() int

Len 返回环形结构的大小。

Example
r1 := 循环链表类.New(10)
for i := 0; i < 5; i++ {
	r1.X设置值(i).Next()
}
fmt.Println("Len:", r1.Len())

r2 := 循环链表类.New(10, true)
for i := 0; i < 10; i++ {
	r2.X设置值(i).Next()
}
fmt.Println("Len:", r2.Len())
Output:

Len: 5
Len: 10
func (r *Ring) Link(s *Ring) *Ring

Link 将环 r 与环 s 连接,使得 r.Next() 指向 s,并返回连接前 r.Next() 的原始值。 r 必须非空。

如果 r 和 s 指向同一个环,将它们连接会从环中移除 r 和 s 之间的元素。被移除的元素形成一个子环,结果是对该子环的一个引用(如果未移除任何元素,则结果仍然是原始的 r.Next() 值,而非 nil)。

如果 r 和 s 指向不同的环,将它们连接会创建一个新的单个环,在 r 后面插入 s 中的所有元素。结果指向在插入后 s 的最后一个元素之后的那个元素。

func (*Ring) Move

func (r *Ring) Move(n int) *Ring

Move 函数将循环队列中的元素向后(n < 0)或向前(n >= 0)移动 n % r.Len() 个位置, 并返回该移动后的位置上的元素。注意,r 不得为空。

Example
r := 循环链表类.New(10)
for i := 0; i < 10; i++ {
	r.X设置值(i).Next()
}
// ring at Pos 0
fmt.Println("CurVal:", r.X取值())

r.Move(5)

// ring at Pos 5
fmt.Println("CurVal:", r.X取值())
Output:

CurVal: 0
CurVal: 5

func (*Ring) Next

func (r *Ring) Next() *Ring

Next 返回下一个环形元素。r 必须非空。

Example
r := 循环链表类.New(10)
for i := 5; i > 0; i-- {
	r.X设置值(i).Prev()
}

fmt.Println("Prev:", r.Next().X取值())
fmt.Println("Prev:", r.Next().X取值())
Output:

Prev: 1
Prev: 2

func (*Ring) Prev

func (r *Ring) Prev() *Ring

Prev 返回上一个环形元素。r不能为空。

Example
r := 循环链表类.New(10)
for i := 0; i < 5; i++ {
	r.X设置值(i).Next()
}

fmt.Println("Prev:", r.Prev().X取值())
fmt.Println("Prev:", r.Prev().X取值())
Output:

Prev: 4
Prev: 3

func (*Ring) Put

func (r *Ring) Put(value interface{}) *Ring

Put 将 `value` 设置为环形结构当前项的值,并将位置移动到下一个项。

Example
r := 循环链表类.New(10)
r.Put(1)
fmt.Println("Val:", r.X取值())
fmt.Println("Val:", r.Prev().X取值())
Output:

Val: <nil>
Val: 1

func (*Ring) RLockIteratorNext

func (r *Ring) RLockIteratorNext(f func(value interface{}) bool)

RLockIteratorNext 在 RWMutex.RLock 的保护下,以给定的回调函数 `f` 进行正向迭代并进行读取锁定。 若 `f` 返回 true,则继续进行迭代;若返回 false,则停止迭代。 这段代码的中文注释如下: ```go RLockIteratorNext 函数在读写互斥锁(RWMutex)的读锁状态下,通过给定的回调函数 `f` 实现向前遍历并加读锁。 当 `f` 返回值为 true 时,将继续进行遍历操作;若返回 false,则停止遍历。

Example
r := 循环链表类.New(10)
for i := 0; i < 10; i++ {
	r.X设置值(i).Next()
}

r.RLockIteratorNext(func(value interface{}) bool {
	if value.(int) < 5 {
		fmt.Println("IteratorNext Success, Value:", value)
		return true
	}

	return false
})
Output:

IteratorNext Success, Value: 0
IteratorNext Success, Value: 1
IteratorNext Success, Value: 2
IteratorNext Success, Value: 3
IteratorNext Success, Value: 4

func (*Ring) RLockIteratorPrev

func (r *Ring) RLockIteratorPrev(f func(value interface{}) bool)

RLockIteratorPrev 在RWMutex.RLock的保护下,以给定回调函数`f`向后遍历并加写锁。 如果`f`返回true,则继续迭代;若返回false,则停止遍历。

Example
r := 循环链表类.New(10)
for i := 0; i < 10; i++ {
	r.X设置值(i).Next()
}

// move r to pos 9
r.Prev()

r.RLockIteratorPrev(func(value interface{}) bool {
	if value.(int) >= 5 {
		fmt.Println("IteratorPrev Success, Value:", value)
		return true
	}

	return false
})
Output:

IteratorPrev Success, Value: 9
IteratorPrev Success, Value: 8
IteratorPrev Success, Value: 7
IteratorPrev Success, Value: 6
IteratorPrev Success, Value: 5

func (*Ring) SliceNext

func (r *Ring) SliceNext() []interface{}

SliceNext 从当前位置开始向前复制所有项目值,并以切片形式返回。

Example
r := 循环链表类.New(10)
for i := 0; i < 10; i++ {
	r.X设置值(i).Next()
}

fmt.Println(r.SliceNext())
Output:

[0 1 2 3 4 5 6 7 8 9]

func (*Ring) SlicePrev

func (r *Ring) SlicePrev() []interface{}

SlicePrev 从当前位置开始向后返回所有项值的切片副本。

Example
r := 循环链表类.New(10)
for i := 0; i < 10; i++ {
	r.X设置值(i).Next()
}

fmt.Println(r.SlicePrev())
Output:

[0 9 8 7 6 5 4 3 2 1]
func (r *Ring) Unlink(n int) *Ring

Unlink 从环形链表 r 中移除 n % r.Len() 个元素,从 r.Next() 开始移除。 若 n % r.Len() 等于 0,则 r 保持不变。 返回值为被移除的子环。r 必须非空。

func (*Ring) X取值

func (r *Ring) X取值() interface{}

Val 返回当前位置项的值。

func (*Ring) X设置值

func (r *Ring) X设置值(value interface{}) *Ring

Set 将值设置为当前位置的项。

Jump to

Keyboard shortcuts

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