forked from leonid-shevtsov/split_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_files.go
52 lines (42 loc) · 1.28 KB
/
split_files.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
42
43
44
45
46
47
48
49
50
51
52
package main
import "sort"
func splitFiles(fileTimesMap map[string]float64, splitTotal int) ([][]string, []float64) {
buckets := make([][]string, splitTotal)
bucketTimes := make([]float64, splitTotal)
// Build a sorted list of files
fileTimesList := make(fileTimesList, len(fileTimesMap))
for file, time := range fileTimesMap {
fileTimesList = append(fileTimesList, fileTimesListItem{file, time})
}
sort.Sort(fileTimesList)
for _, file := range fileTimesList {
// find bucket with min weight
minBucket := 0
for bucket := 1; bucket < splitTotal; bucket++ {
if bucketTimes[bucket] < bucketTimes[minBucket] {
minBucket = bucket
}
}
// add file to bucket
buckets[minBucket] = append(buckets[minBucket], file.name)
bucketTimes[minBucket] += file.time
}
return buckets, bucketTimes
}
type fileTimesListItem struct {
name string
time float64
}
type fileTimesList []fileTimesListItem
func (l fileTimesList) Len() int { return len(l) }
// Sorts by time descending, then by name ascending
// Sort by name is required for deterministic order across machines
func (l fileTimesList) Less(i, j int) bool {
return l[i].time > l[j].time ||
(l[i].time == l[j].time && l[i].name < l[j].name)
}
func (l fileTimesList) Swap(i, j int) {
temp := l[i]
l[i] = l[j]
l[j] = temp
}