mirror of
https://github.com/fumiama/deepinfra.git
synced 2026-06-05 00:32:46 +08:00
feat: add chat
This commit is contained in:
46
chat/batch.go
Normal file
46
chat/batch.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package chat
|
||||
|
||||
import "strings"
|
||||
|
||||
type batch struct {
|
||||
lst *Log
|
||||
items []item
|
||||
}
|
||||
|
||||
func (l *Log) newbatch(sz int) *batch {
|
||||
if sz == 0 {
|
||||
panic("sz cannot be 0")
|
||||
}
|
||||
return &batch{
|
||||
lst: l,
|
||||
items: make([]item, 0, sz),
|
||||
}
|
||||
}
|
||||
|
||||
func (cl *batch) String() string {
|
||||
sb := strings.Builder{}
|
||||
for _, item := range cl.items {
|
||||
sb.WriteString(cl.lst.sep)
|
||||
item.writeToBuilder(&sb, cl.lst.atprefix, cl.lst.namel, cl.lst.namer)
|
||||
}
|
||||
return sb.String()[len(cl.lst.sep):]
|
||||
}
|
||||
|
||||
// add without mutex
|
||||
func (cl *batch) add(item item) *batch {
|
||||
v := cl.items
|
||||
defer func() {
|
||||
cl.items = v
|
||||
}()
|
||||
if cap(v) == 1 {
|
||||
v[0] = item
|
||||
return cl
|
||||
}
|
||||
if len(v) < cap(v) {
|
||||
v = append(v, item)
|
||||
return cl
|
||||
}
|
||||
copy(v, v[1:])
|
||||
v[len(v)-1] = item
|
||||
return cl
|
||||
}
|
||||
Reference in New Issue
Block a user