如何将排序接口实现类型作为函数参数传递?

huangapple go评论74阅读模式
英文:

How do I pass a sort interface implementation type as a function parameter?

问题

我正在编写不同的调度算法,并希望比较各种作业排序的方式。我有一个在结构体上的函数,我想将排序接口类型的类型传递给该函数中的排序。

type Schedule struct {
    Jobs []Job
}

type ByDifference []Job
// ByDifference 实现了 sort.Interface 接口

type ByRatio []Job
// ByRatio 实现了 sort.Interface 接口

func (s *Schedule) Schedule(OrderBy sort.Interface) {
    // 省略求和变量

    // 这里会失败,因为没有 OrderBy() 函数
    sort.(OrderBy(q.Jobs))

    for _, v := range q.Jobs {
        // 省略计算加权和的代码
    }

    // 省略输出代码

}

当然,我希望调用 Schedule 函数并传递 ByDifference 或 ByRatio 的某种表示,并让排序使用该类型。我的初步阅读似乎指向类型反射。是否可以使用这种设计,在函数内部传递实现了接口的类型给排序使用呢?

英文:

I am writing different scheduling algorithm and would like to compare various ways of ordering jobs. I have a function on a struct that I would like to pass the type of a sort interface type to be used by the sort within the function.

type Schedule struct {
    Jobs []Job
}

type ByDifference []Job
// ByDifference implements sort.Interface

type ByRatio []Job
// ByRatio implements sort.Interface

func (s *Schedule) Schedule(OrderBy sort.Interface) {
    // Summation variables omitted

    // This fails as there is no function OrderBy()
    sort.(OrderBy(q.Jobs))

    for _, v := range q.Jobs {
        // Compute weighted sum omitted
    }

    // Output code omitted

}

Naturally, I would like to to call the Schedule function and pass some representation of ByDifference or ByRatio and have the sort use that type. My initial reading seems to lead to type reflection. Is it possible to use this design to pass a type that implements an interface to be used by sort within a function?

答案1

得分: 2

也许这样

type Schedule struct {
    Jobs []Job
}
const(
Difference=iota
Ratio=iota
)
type ByDifference Schedule
// ByDifference 实现了 sort.Interface 接口

type ByRatio Schedule
// ByRatio 实现了 sort.Interface 接口

func (s *Schedule) Schedule(order int) { // 例如,s.Schedule(Difference)
    // 省略了求和变量
	switch order{
		case Difference: ss:= ByDifference(*s);  sort(ss); s=&Schedule(ss)
		case Ratio: ss:= ByRatio(*s);  sort(ss); s=&Schedule(ss)
	}
 
    for _, v := range s.Jobs {
        // 省略了计算加权和
    }

    // 省略了输出代码

}
英文:

Maybe this way

type Schedule struct {
    Jobs []Job
}
const(
Difference=iota
Ratio=iota
)
type ByDifference Schedule
// ByDifference implements sort.Interface

type ByRatio Schedule
// ByRatio implements sort.Interface

func (s *Schedule) Schedule(order int) { // s.Schedule(Difference) for example
    // Summation variables omitted
	switch order{
		case Difference: ss:= ByDifference(*s);  sort(ss); s=&Schedule(ss)
		case Ratio: ss:= ByRatio(*s);  sort(ss); s=&Schedule(ss)
	}
 
    for _, v := range s.Jobs {
        // Compute weighted sum omitted
    }

    // Output code omitted

}

huangapple
  • 本文由 发表于 2015年3月22日 23:41:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/29196323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定