clise

package
v0.53.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: BSD-3-Clause Imports: 3 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,

var c *clise.Clise = clise.New(5)
c.Push(1, 2, 3, 4, 5)
fmt.Println(c.Slice()) // [1 2 3 4 5]

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

c.Push(6)
fmt.Println(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
}

Clise define the circular slice implementation.

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) Close added in v0.43.0

func (c *Clise) Close() error

Close implement io.Closer, equal to Reset().

func (*Clise) MarshalJSON added in v0.39.0

func (c *Clise) MarshalJSON() (out []byte, err error)

MarshalJSON call Slice on c and convert it into JSON.

Example
type T struct {
	String string
	Int    int
}

var (
	c = New(3)

	bjson []byte
	err   error
)

c.Push(1, 2, 3, 4)
bjson, err = json.Marshal(c)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(string(bjson))
}

c.Push("Hello", "Clise", "MarshalJSON")
bjson, err = json.Marshal(c)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(string(bjson))
}

c.Push(&T{Int: 1, String: `Hello`}, &T{Int: 2, String: `world`})
bjson, err = json.Marshal(c)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(string(bjson))
}
Output:

[2,3,4]
["Hello","Clise","MarshalJSON"]
["MarshalJSON",{"String":"Hello","Int":1},{"String":"world","Int":2}]

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
var (
	c    = New(5)
	item interface{}
)

c.Push(1, 2, 3, 4, 5, 6)
item = c.Pop()
for item != nil {
	fmt.Println(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
var c = New(5)
c.Push(1, 2, 3)
fmt.Println(c.RecentSlice())
c.Push(4, 5, 6, 7)
fmt.Println(c.RecentSlice())
Output:

[1 2 3]
[6 7]

func (*Clise) Reset

func (c *Clise) Reset()

Reset the slice, start from zero.

Example
var c = New(5)
c.Push(1, 2, 3, 4, 5)
fmt.Println(c.Slice())
c.Reset()
c.Push(1)
fmt.Println(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
var c = New(5)
c.Push(1, 2)
fmt.Println(c.Slice())
c.Push(3, 4, 5)
fmt.Println(c.Slice())
c.Push(6)
fmt.Println(c.Slice())
c.Push(7, 8, 9, 10)
fmt.Println(c.Slice())
Output:

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

func (*Clise) UnmarshalJSON added in v0.43.0

func (c *Clise) UnmarshalJSON(jsonb []byte) (err error)

UnmarshalJSON unmarshal JSON array into Clise. If the size is zero, it will be set to the length of JSON array.

Example
var (
	clise = New(3)

	cases = []string{
		`{"k":1}`, // Non array.
		`[2,3,4]`,
		`[2,3,4,5]`, // Array elements greater that maximum size.
		`["Hello","Clise","MarshalJSON"]`,
		`["MarshalJSON",{"Int":1,"String":"Hello"},{"Int":2,"String":"world"}]`,
	}

	rawJSON string
	err     error
)

for _, rawJSON = range cases {
	err = clise.UnmarshalJSON([]byte(rawJSON))
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(clise.Slice())
	}
}
Output:

UnmarshalJSON: json: cannot unmarshal object into Go value of type []interface {}
[2 3 4]
[3 4 5]
[Hello Clise MarshalJSON]
[MarshalJSON map[Int:1 String:Hello] map[Int:2 String:world]]

func (*Clise) Write added in v0.43.0

func (c *Clise) Write(b []byte) (n int, err error)

Write implement io.Writer, equal to Push(b).

func (*Clise) WriteByte added in v0.43.0

func (c *Clise) WriteByte(b byte) error

WriteByte implement io.ByteWriter, equal to Push(b).

func (*Clise) WriteString added in v0.43.0

func (c *Clise) WriteString(s string) (n int, err error)

WriteString implement io.StringWriter, equal to Push(s).

Jump to

Keyboard shortcuts

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