循环链表类

package
v0.0.0-...-fcd50c7 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

包gring提供了一个并发安全/不安全的环形列表(圆形队列)。 md5:bc78eee87d7b5c4b

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是一个环形结构的结构体。 md5:f371ac74ef187b03

func New

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

New 创建并返回一个具有`cap`个元素的Ring结构。 可选参数`safe`指定是否在并发安全环境下使用该结构,默认为false。 md5:70892e7ec9ed75d6

Example
package main

import (
	gring "github.com/888go/goframe/container/gring"
)

func main() {
	// Non concurrent safety
	gring.New(10)

	// Concurrent safety
	gring.New(10, true)

}
Output:

func (*Ring) Cap

func (r *Ring) Cap() int

Cap 返回环形缓冲区的容量。 md5:2ac015d8e20dce37

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r1 := gring.New(10)
	for i := 0; i < 5; i++ {
		r1.X设置值(i).Next()
	}
	fmt.Println("Cap:", r1.Cap())

	r2 := gring.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 返回环的大小。 md5:c4ff976cf0b72c58

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r1 := gring.New(10)
	for i := 0; i < 5; i++ {
		r1.X设置值(i).Next()
	}
	fmt.Println("Len:", r1.Len())

	r2 := gring.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 的最后一个元素之后的元素。 md5:faa73e3f5f43468a

func (*Ring) Move

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

Move 函数根据给定的 n 值,将环(ring)中的元素向后(n < 0)或向前(n >= 0)移动 n % r.Len() 个位置,并返回移动后所在位置的元素。环(r)不能为空。 md5:92f786c9a5a8b8cd

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r := gring.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必须不为空。 md5:f16b811ce23ee06b

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r := gring.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 不能为空。 md5:574e755d8883dc1f

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r := gring.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`设置为环形列表的当前项,并将位置移动到下一项。 md5:737e9a607801eee9

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r := gring.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`返回true,那么将继续迭代;如果返回false,则停止迭代。 md5:8cb144956023168f

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r := gring.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,那么继续迭代;否则停止。 md5:31f0f0af041e234a

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r := gring.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 返回一个从当前位置开始向前的所有项值的切片副本。 md5:54ba7b6ac01a38f8

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r := gring.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 从当前位置向前返回所有项目值的副本作为切片。 md5:632f85c2939f2e91

Example
package main

import (
	"fmt"

	gring "github.com/888go/goframe/container/gring"
)

func main() {
	r := gring.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 必须非空。 md5:00909914d8e87d32

func (*Ring) X取值

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

X取值 返回当前位置的项的值。 md5:b1027c8df14f08d2

func (*Ring) X设置值

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

X设置值 将值设置为当前位置的项目。 md5:7140c77dfa3aa5dc

Jump to

Keyboard shortcuts

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