Files
ollama/x/utils/upload/upload.go
Blake Mizerany adc23d5f96 Add 'x/' from commit 'a10a11b9d371f36b7c3510da32a1d70b74e27bd1'
git-subtree-dir: x
git-subtree-mainline: 7d05a6ee8f
git-subtree-split: a10a11b9d3
2024-04-03 10:40:23 -07:00

28 lines
652 B
Go

package upload
import (
"iter"
"golang.org/x/exp/constraints"
)
type Chunk[I constraints.Integer] struct {
Offset I
Size I
}
// Chunks yields a sequence of a part number and a Chunk. The Chunk is the offset
// and size of the chunk. The last chunk may be smaller than chunkSize if size is
// not a multiple of chunkSize.
//
// The first part number is 1 and increases monotonically.
func Chunks[I constraints.Integer](size, chunkSize I) iter.Seq2[int, Chunk[I]] {
return func(yield func(int, Chunk[I]) bool) {
var n int
for off := I(0); off < size; off += chunkSize {
n++
yield(n, Chunk[I]{off, min(chunkSize, size-off)})
}
}
}