clise

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: BSD-3-Clause Imports: 1 Imported by: 1

Documentation

Overview

Package clise implements circular slice. A circular slice is a slice that have fixed size. An append to slice that has reached its length will overwrite and start again from index 0.

For example, a clise with size 5,

c := clise.New(5)
c.Push(1, 2, 3, 4, 5)
fmt.Printf("%v\n", c.Slice()) // [1 2 3 4 5]

If we push another item, it will overwrite the first index,

c.Push(6)
fmt.Printf("%v\n", c.Slice()) // [6 2 3 4 5]

See the examples for usage of the package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clise

type Clise struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(size int) (c *Clise)

New create and initialize circular slice with fixed size. It will return nil if size <= 0.

func (*Clise) Pop added in v0.24.0

func (c *Clise) Pop() (item interface{})

Pop remove the last Push()-ed item and return it to caller. It will return nil if no more item inside it.

Example
c := New(5)
c.Push(1, 2, 3, 4, 5, 6)
item := c.Pop()
for item != nil {
	fmt.Printf("%v\n", item)
	item = c.Pop()
}
Output:

6
5
4
3
2

func (*Clise) Push

func (c *Clise) Push(src ...interface{})

Push the item into the slice.

func (*Clise) RecentSlice

func (c *Clise) RecentSlice() (dst []interface{})

RecentSlice return the slice from index zero until the recent item.

Example
c := New(5)
c.Push(1, 2, 3)
fmt.Printf("%v\n", c.RecentSlice())
c.Push(4, 5, 6, 7)
fmt.Printf("%v\n", c.RecentSlice())
Output:

[1 2 3]
[6 7]

func (*Clise) Reset

func (c *Clise) Reset()

Reset the slice, start from zero.

Example
c := New(5)
c.Push(1, 2, 3, 4, 5)
fmt.Printf("%v\n", c.Slice())
c.Reset()
c.Push(1)
fmt.Printf("%v\n", c.Slice())
Output:

[1 2 3 4 5]
[1]

func (*Clise) Slice

func (c *Clise) Slice() (dst []interface{})

Slice return the content of circular slice as slice in the order of the last item to the recent item.

Example
c := New(5)
c.Push(1, 2)
fmt.Printf("%v\n", c.Slice())
c.Push(3, 4, 5)
fmt.Printf("%v\n", c.Slice())
c.Push(6)
fmt.Printf("%v\n", c.Slice())
c.Push(7, 8, 9, 10)
fmt.Printf("%v\n", c.Slice())
Output:

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

Jump to

Keyboard shortcuts

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