package clise
import "git.sr.ht/~shulhan/pakakeh.go/lib/clise"
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 ¶
- type Clise
- func New(size int) (c *Clise)
- func (c *Clise) Close() error
- func (c *Clise) MarshalJSON() (out []byte, err error)
- func (c *Clise) Pop() (item any)
- func (c *Clise) Push(src ...any)
- func (c *Clise) RecentSlice() (dst []any)
- func (c *Clise) Reset()
- func (c *Clise) Slice() (dst []any)
- func (c *Clise) UnmarshalJSON(jsonb []byte) (err error)
- func (c *Clise) Write(b []byte) (n int, err error)
- func (c *Clise) WriteByte(b byte) error
- func (c *Clise) WriteString(s string) (n int, err error)
Examples ¶
Types ¶
type Clise ¶
Clise define the circular slice implementation.
func New ¶
New create and initialize circular slice with fixed size. It will return nil if size <= 0.
func (*Clise) Close ¶
Close implement io.Closer, equal to Reset().
func (*Clise) MarshalJSON ¶
MarshalJSON call Slice on c and convert it into JSON.
Code:
Output: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}]
}
[2,3,4]
["Hello","Clise","MarshalJSON"]
["MarshalJSON",{"String":"Hello","Int":1},{"String":"world","Int":2}]
func (*Clise) Pop ¶
Pop remove the last Push()-ed item and return it to caller.
It will return nil if no more item inside it.
Code:
Output:Example¶
{
var (
c = New(5)
item any
)
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
}
6
5
4
3
2
func (*Clise) Push ¶
Push the item into the slice.
func (*Clise) RecentSlice ¶
RecentSlice return the slice from index zero until the recent item.
Code:
Output: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]
}
[1 2 3]
[6 7]
func (*Clise) Reset ¶
func (c *Clise) Reset()
Reset the slice, start from zero.
Code:
Output: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]
}
[1 2 3 4 5]
[1]
func (*Clise) Slice ¶
Slice return the content of circular slice as slice in the order of the
last item to the recent item.
Code:
Output: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]
}
[1 2]
[1 2 3 4 5]
[2 3 4 5 6]
[6 7 8 9 10]
func (*Clise) UnmarshalJSON ¶
UnmarshalJSON unmarshal JSON array into Clise.
If the size is zero, it will be set to the length of JSON array.
Code:
Output: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]]
}
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 ¶
Write implement io.Writer, equal to Push(b).
func (*Clise) WriteByte ¶
WriteByte implement io.ByteWriter, equal to Push(b).
func (*Clise) WriteString ¶
WriteString implement io.StringWriter, equal to Push(s).
Source Files ¶
- Version
- v0.60.0 (latest)
- Published
- Feb 1, 2025
- Platform
- linux/amd64
- Imports
- 3 packages
- Last checked
- 11 minutes ago –
Tools for package owners.