Split evenly divides the execution segment into the specified number of equal consecutive execution sub-segments.
(numParts int64)
| 164 | // Split evenly divides the execution segment into the specified number of |
| 165 | // equal consecutive execution sub-segments. |
| 166 | func (es *ExecutionSegment) Split(numParts int64) ([]*ExecutionSegment, error) { |
| 167 | if numParts < 1 { |
| 168 | return nil, fmt.Errorf("the number of parts must be at least 1, %d received", numParts) |
| 169 | } |
| 170 | |
| 171 | from, to := zeroRat, oneRat |
| 172 | if es != nil { |
| 173 | from, to = es.from, es.to |
| 174 | } |
| 175 | |
| 176 | increment := new(big.Rat).Sub(to, from) |
| 177 | increment.Denom().Mul(increment.Denom(), big.NewInt(numParts)) |
| 178 | |
| 179 | results := make([]*ExecutionSegment, numParts) |
| 180 | for i := range numParts { |
| 181 | segmentTo := new(big.Rat).Add(from, increment) |
| 182 | segment, err := NewExecutionSegment(from, segmentTo) |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | results[i] = segment |
| 187 | from = segmentTo |
| 188 | } |
| 189 | |
| 190 | if from.Cmp(to) != 0 { |
| 191 | return nil, fmt.Errorf("expected %s and %s to be equal", from, to) |
| 192 | } |
| 193 | |
| 194 | return results, nil |
| 195 | } |
| 196 | |
| 197 | // Equal returns true only if the two execution segments have the same from and |
| 198 | // to values. |