Documentation
¶
Overview ¶
Package gring provides a concurrent-safe/unsafe ring(circular lists).
Deprecated.
Index ¶
- type Ring
- func (r *Ring) Cap() int
- func (r *Ring) Len() int
- func (r *Ring) Link(s *Ring) *Ring
- func (r *Ring) Move(n int) *Ring
- func (r *Ring) Next() *Ring
- func (r *Ring) Prev() *Ring
- func (r *Ring) Put(value any) *Ring
- func (r *Ring) RLockIteratorNext(f func(value any) bool)
- func (r *Ring) RLockIteratorPrev(f func(value any) bool)
- func (r *Ring) Set(value any) *Ring
- func (r *Ring) SliceNext() []any
- func (r *Ring) SlicePrev() []any
- func (r *Ring) Unlink(n int) *Ring
- func (r *Ring) Val() any
- type TRing
- func (r *TRing[T]) Cap() int
- func (r *TRing[T]) Len() int
- func (r *TRing[T]) Link(s *TRing[T]) *TRing[T]
- func (r *TRing[T]) Move(n int) *TRing[T]
- func (r *TRing[T]) Next() *TRing[T]
- func (r *TRing[T]) Prev() *TRing[T]
- func (r *TRing[T]) Put(value T) *TRing[T]
- func (r *TRing[T]) RLockIteratorNext(f func(value T) bool)
- func (r *TRing[T]) RLockIteratorPrev(f func(value T) bool)
- func (r *TRing[T]) Set(value T) *TRing[T]
- func (r *TRing[T]) SliceNext() []T
- func (r *TRing[T]) SlicePrev() []T
- func (r *TRing[T]) Unlink(n int) *TRing[T]
- func (r *TRing[T]) Val() T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Ring ¶
Ring is a struct of ring structure.
Deprecated.
func New ¶
New creates and returns a Ring structure of `cap` elements. The optional parameter `safe` specifies whether using this structure in concurrent safety, which is false in default.
Deprecated.
Example ¶
package main
import (
"github.com/gogf/gf/v2/container/gring"
)
func main() {
// Non concurrent safety
gring.New(10)
// Concurrent safety
gring.New(10, true)
}
func (*Ring) Cap ¶
Cap returns the capacity of ring.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r1 := gring.New(10)
for i := 0; i < 5; i++ {
r1.Set(i).Next()
}
fmt.Println("Cap:", r1.Cap())
r2 := gring.New(10, true)
for i := 0; i < 10; i++ {
r2.Set(i).Next()
}
fmt.Println("Cap:", r2.Cap())
}
Output: Cap: 10 Cap: 10
func (*Ring) Len ¶
Len returns the size of ring.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r1 := gring.New(10)
for i := 0; i < 5; i++ {
r1.Set(i).Next()
}
fmt.Println("Len:", r1.Len())
r2 := gring.New(10, true)
for i := 0; i < 10; i++ {
r2.Set(i).Next()
}
fmt.Println("Len:", r2.Len())
}
Output: Len: 5 Len: 10
func (*Ring) Link ¶
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 sub-ring and the result is a reference to that sub-ring (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.
Example (Common) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 5; i++ {
r.Set(i).Next()
}
s := gring.New(10)
for i := 0; i < 10; i++ {
val := i + 5
s.Set(val).Next()
}
r.Link(s) // Link Ring s to Ring r
fmt.Println("Len:", r.Len())
fmt.Println("Cap:", r.Cap())
fmt.Println(r.SlicePrev())
fmt.Println(r.SliceNext())
}
Output: Len: 15 Cap: 20 [4 3 2 1 0] [5 6 7 8 9 10 11 12 13 14]
Example (SameRing) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 5; i++ {
r.Set(i).Next()
}
same_r := r.Link(r.Prev())
fmt.Println("Len:", same_r.Len())
fmt.Println("Cap:", same_r.Cap())
fmt.Println(same_r.SlicePrev())
fmt.Println(same_r.SliceNext())
}
Output: Len: 1 Cap: 1 [4] [4]
func (*Ring) Move ¶
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.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
// ring at Pos 0
fmt.Println("CurVal:", r.Val())
r.Move(5)
// ring at Pos 5
fmt.Println("CurVal:", r.Val())
}
Output: CurVal: 0 CurVal: 5
func (*Ring) Next ¶
Next returns the next ring element. r must not be empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 5; i > 0; i-- {
r.Set(i).Prev()
}
fmt.Println("Prev:", r.Next().Val())
fmt.Println("Prev:", r.Next().Val())
}
Output: Prev: 1 Prev: 2
func (*Ring) Prev ¶
Prev returns the previous ring element. r must not be empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 5; i++ {
r.Set(i).Next()
}
fmt.Println("Prev:", r.Prev().Val())
fmt.Println("Prev:", r.Prev().Val())
}
Output: Prev: 4 Prev: 3
func (*Ring) Put ¶
Put sets `value` to current item of ring and moves position to next item.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
r.Put(1)
fmt.Println("Val:", r.Val())
fmt.Println("Val:", r.Prev().Val())
}
Output: Val: <nil> Val: 1
func (*Ring) RLockIteratorNext ¶
RLockIteratorNext iterates and locks reading forward with given callback function `f` within RWMutex.RLock. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
r.RLockIteratorNext(func(value any) 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 ¶
RLockIteratorPrev iterates and locks reading backward with given callback function `f` within RWMutex.RLock. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
// move r to pos 9
r.Prev()
r.RLockIteratorPrev(func(value any) 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) Set ¶
Set sets value to the item of current position.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
r.Set(1)
fmt.Println("Val:", r.Val())
r.Next().Set("GoFrame")
fmt.Println("Val:", r.Val())
}
Output: Val: 1 Val: GoFrame
func (*Ring) SliceNext ¶
SliceNext returns a copy of all item values as slice forward from current position.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
fmt.Println(r.SliceNext())
}
Output: [0 1 2 3 4 5 6 7 8 9]
func (*Ring) SlicePrev ¶
SlicePrev returns a copy of all item values as slice backward from current position.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
fmt.Println(r.SlicePrev())
}
Output: [0 9 8 7 6 5 4 3 2 1]
func (*Ring) Unlink ¶
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 sub-ring. r must not be empty.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
for i := 0; i < 10; i++ {
r.Set(i).Next()
}
fmt.Println("Before Unlink, Len:", r.Len())
fmt.Println("Before Unlink, Cap:", r.Cap())
fmt.Println("Before Unlink, ", r.SlicePrev())
fmt.Println("Before Unlink, ", r.SliceNext())
r.Unlink(7)
fmt.Println("After Unlink, Len:", r.Len())
fmt.Println("After Unlink, Cap:", r.Cap())
fmt.Println("After Unlink, ", r.SlicePrev())
fmt.Println("After Unlink, ", r.SliceNext())
}
Output: Before Unlink, Len: 10 Before Unlink, Cap: 10 Before Unlink, [0 9 8 7 6 5 4 3 2 1] Before Unlink, [0 1 2 3 4 5 6 7 8 9] After Unlink, Len: 3 After Unlink, Cap: 3 After Unlink, [0 9 8] After Unlink, [0 8 9]
func (*Ring) Val ¶
Val returns the item's value of current position.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gring"
)
func main() {
r := gring.New(10)
r.Set(1)
fmt.Println("Val:", r.Val())
r.Next().Set("GoFrame")
fmt.Println("Val:", r.Val())
}
Output: Val: 1 Val: GoFrame
type TRing ¶ added in v2.9.6
type TRing[T any] struct { // contains filtered or unexported fields }
TRing is a struct of ring structure.
func NewTRing ¶ added in v2.9.6
NewTRing creates and returns a Ring structure of `cap` elements. The optional parameter `safe` specifies whether using this structure in concurrent safety, which is false in default.
func (*TRing[T]) Link ¶ added in v2.9.6
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 sub-ring and the result is a reference to that sub-ring (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.
func (*TRing[T]) Move ¶ added in v2.9.6
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.
func (*TRing[T]) Prev ¶ added in v2.9.6
Prev returns the previous ring element. r must not be empty.
func (*TRing[T]) Put ¶ added in v2.9.6
Put sets `value` to current item of ring and moves position to next item.
func (*TRing[T]) RLockIteratorNext ¶ added in v2.9.6
RLockIteratorNext iterates and locks reading forward with given callback function `f` within RWMutex.RLock. If `f` returns true, then it continues iterating; or false to stop.
func (*TRing[T]) RLockIteratorPrev ¶ added in v2.9.6
RLockIteratorPrev iterates and locks reading backward with given callback function `f` within RWMutex.RLock. If `f` returns true, then it continues iterating; or false to stop.
func (*TRing[T]) SliceNext ¶ added in v2.9.6
func (r *TRing[T]) SliceNext() []T
SliceNext returns a copy of all item values as slice forward from current position.
func (*TRing[T]) SlicePrev ¶ added in v2.9.6
func (r *TRing[T]) SlicePrev() []T
SlicePrev returns a copy of all item values as slice backward from current position.