ring

package
v0.0.0-...-9ea5951 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2020 License: BSD-3-Clause Imports: 0 Imported by: 0

Documentation

Overview

Package ring implements operations on circular lists.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ring

type Ring struct {
	Value interface{} // for use by client; untouched by this library
	// contains filtered or unexported fields
}

A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value. Ring 是循环链表,或者叫环。环形链表没有头尾;指向环形链表任一元素的指针都可以作为整个环形链表看待。 Ring 零值是具有一个(Value字段为nil的)元素的 ring 。

func New

func New(n int) *Ring

New creates a ring of n elements. New 创建一个长度为n的环形链表

func (*Ring) Do

func (r *Ring) Do(f func(interface{}))

Do calls function f on each element of the ring, in forward order. The behavior of Do is undefined if f changes *r. Do 对链表中任意元素执行 f 操作,如果 f 改变了 r ,则该操作造成的后果是不可预期的。

Example
package main

import (
	"container/ring"
	"fmt"
)

func main() {
	// Create a new ring of size 5
	r := ring.New(5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Iterate through the ring and print its contents
	r.Do(func(p interface{}) {
		fmt.Println(p.(int))
	})

}
Output:

0
1
2
3
4

func (*Ring) Len

func (r *Ring) Len() int

Len computes the number of elements in ring r. It executes in time proportional to the number of elements. Len 求环长度,返回环中元素数量。

Example
package main

import (
	"container/ring"
	"fmt"
)

func main() {
	// Create a new ring of size 4
	r := ring.New(4)

	// Print out its length
	fmt.Println(r.Len())

}
Output:

4
func (r *Ring) Link(s *Ring) *Ring

Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.

If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).

If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion. Link 连接 r 和 s ,并返回 r 原本的后继元素 r.Next()

func (*Ring) Move

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

Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty. Move 返回移动n个位置(n>=0向前移动,n<0向后移动)后的元素

Example
package main

import (
	"container/ring"
	"fmt"
)

func main() {
	// Create a new ring of size 5
	r := ring.New(5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Move the pointer forward by three steps
	r = r.Move(3)

	// Iterate through the ring and print its contents
	r.Do(func(p interface{}) {
		fmt.Println(p.(int))
	})

}
Output:

3
4
0
1
2

func (*Ring) Next

func (r *Ring) Next() *Ring

Next returns the next ring element. r must not be empty. Next 获取当前元素的下个元素

Example
package main

import (
	"container/ring"
	"fmt"
)

func main() {
	// Create a new ring of size 5
	r := ring.New(5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Iterate through the ring and print its contents
	for j := 0; j < n; j++ {
		fmt.Println(r.Value)
		r = r.Next()
	}

}
Output:

0
1
2
3
4

func (*Ring) Prev

func (r *Ring) Prev() *Ring

Prev returns the previous ring element. r must not be empty. Next 获取当前元素的上个元素

Example
package main

import (
	"container/ring"
	"fmt"
)

func main() {
	// Create a new ring of size 5
	r := ring.New(5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Iterate through the ring backwards and print its contents
	for j := 0; j < n; j++ {
		r = r.Prev()
		fmt.Println(r.Value)
	}

}
Output:

4
3
2
1
0
func (r *Ring) Unlink(n int) *Ring

Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty. Unlink 删除链表中 n % r.Len() 个元素,从 r.Next() 开始删除。如果 n % r.Len() ,不修改r。返回删除的元素构成的 Ring。

Jump to

Keyboard shortcuts

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