-
Notifications
You must be signed in to change notification settings - Fork 202
/
dataSplit.go
41 lines (32 loc) · 949 Bytes
/
dataSplit.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
37
38
39
40
41
package partitioning
import (
"github.com/ElrondNetwork/elrond-go/core"
)
const minimumMaxPacketNum = 1
// DataSplit can split a large slice of byte slices in chunks with len(chunks) <= limit
// It does not marshal the data
type DataSplit struct {
}
// SplitDataInChunks splits the provided data into smaller chunks
// limit is expressed in number of elements
func (ds *DataSplit) SplitDataInChunks(data [][]byte, limit int) ([][][]byte, error) {
if limit < minimumMaxPacketNum {
return nil, core.ErrInvalidValue
}
if data == nil {
return nil, core.ErrNilInputData
}
returningBuff := make([][][]byte, 0)
elements := make([][]byte, 0)
for idx, element := range data {
elements = append(elements, element)
if (idx+1)%limit == 0 {
returningBuff = append(returningBuff, elements)
elements = make([][]byte, 0)
}
}
if len(elements) > 0 {
returningBuff = append(returningBuff, elements)
}
return returningBuff, nil
}