Used closures instead of strings for the generic sorter. This removed the need for reflection.

This commit is contained in:
Pradyumna Kaushik 2017-08-26 15:19:30 -04:00
parent 3c65bdf02e
commit 0869bea2d8
2 changed files with 10 additions and 30 deletions

View file

@ -4,7 +4,6 @@ import (
"github.com/mdesenfants/gokmeans"
"sort"
"log"
"reflect"
)
// Information about a cluster of tasks
@ -120,12 +119,9 @@ func labelAndOrder(clusters map[int][]Task, numberOfClusters int, taskObservatio
// Be able to sort an array of tasks based on any of the tasks' resources.
// Retrieve a sorter (same signature as 'Less' function in sort.Interface) for the given sorting criteria.
func TaskSorter(sc sortCriteria, tasks []Task) func (i, j int) bool {
type TasksToSort []Task
func (ts TasksToSort) TaskSorter(sb sortBy) func (i, j int) bool {
return func (i, j int) bool {
taskIFields := reflect.Indirect(reflect.ValueOf(tasks[i]))
tasksJFields := reflect.Indirect(reflect.ValueOf(tasks[j]))
resourceI := taskIFields.FieldByName(sc.String()).Float()
resourceJ := tasksJFields.FieldByName(sc.String()).Float()
return resourceI <= resourceJ
return sb(&ts[i]) <= sb(&ts[j])
}
}