-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
36 lines (30 loc) · 880 Bytes
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package llmflow
// BoundedContainer is a container with a bounded length.
type BoundedContainer[E any] struct {
buffer []E
size int
}
func NewBoundedContainer[E any](size int) *BoundedContainer[E] {
return &BoundedContainer[E]{
buffer: make([]E, 0, size),
size: size,
}
}
// Append adds item to the right side of the container. It will return a boolean
// value indicates whether the container is full after this addition.
func (bc *BoundedContainer[E]) Append(item E) (isFull bool) {
bc.buffer = append(bc.buffer, item)
return len(bc.buffer) == bc.size
}
// PopAll removes and returns all items from the container.
func (bc *BoundedContainer[E]) PopAll() []E {
if len(bc.buffer) == 0 {
return nil
}
// Make a copy of all the items.
temp := make([]E, len(bc.buffer))
copy(temp, bc.buffer)
// Clear the buffer.
bc.buffer = bc.buffer[:0]
return temp
}