什么是并行化sort.search的Go方式?

时间:2017-06-15 14:08:33

标签: algorithm go parallel-processing

Go提供func Search(n int, f func(int) bool) int函数,该函数返回 f(n)为真的最低 n

以下是带注释的完整功能:

  // Search uses binary search to find and return the smallest index i
  // in [0, n) at which f(i) is true, assuming that on the range [0, n),
  // f(i) == true implies f(i+1) == true. That is, Search requires that
  // f is false for some (possibly empty) prefix of the input range [0, n)
  // and then true for the (possibly empty) remainder; Search returns
  // the first true index. If there is no such index, Search returns n.
  // (Note that the "not found" return value is not -1 as in, for instance,
  // strings.Index.)
  // Search calls f(i) only for i in the range [0, n).
  //
  // A common use of Search is to find the index i for a value x in
  // a sorted, indexable data structure such as an array or slice.
  // In this case, the argument f, typically a closure, captures the value
  // to be searched for, and how the data structure is indexed and
  // ordered.
  //
  // For instance, given a slice data sorted in ascending order,
  // the call Search(len(data), func(i int) bool { return data[i] >= 23 })
  // returns the smallest index i such that data[i] >= 23.  If the caller
  // wants to find whether 23 is in the slice, it must test data[i] == 23
  // separately.
  //
  // Searching data sorted in descending order would use the <=
  // operator instead of the >= operator.
  //
  // To complete the example above, the following code tries to find the value
  // x in an integer slice data sorted in ascending order:
  //
  //    x := 23
  //    i := sort.Search(len(data), func(i int) bool { return data[i] >= x })
  //    if i < len(data) && data[i] == x {
  //        // x is present at data[i]
  //    } else {
  //        // x is not present in data,
  //        // but i is the index where it would be inserted.
  //    }
  //
  // As a more whimsical example, this program guesses your number:
  //
  //    func GuessingGame() {
  //        var s string
  //        fmt.Printf("Pick an integer from 0 to 100.\n")
  //        answer := sort.Search(100, func(i int) bool {
  //            fmt.Printf("Is your number <= %d? ", i)
  //            fmt.Scanf("%s", &s)
  //            return s != "" && s[0] == 'y'
  //        })
  //        fmt.Printf("Your number is %d.\n", answer)
  //    }
  //
  func Search(n int, f func(int) bool) int {
    // Define f(-1) == false and f(n) == true.
    // Invariant: f(i-1) == false, f(j) == true.
    i, j := 0, n
    for i < j {
        h := i + (j-i)/2 // avoid overflow when computing h
        // i ≤ h < j
        if !f(h) {
            i = h + 1 // preserves f(i-1) == false
        } else {
            j = h // preserves f(j) == true
        }
    }
    // i == j, f(i-1) == false, and f(j) (= f(i)) == true  =>  answer is i.
    return i
  }

我可以简单地在 k -slices中拆分 n ,其中 k 是我想要的工作量,然后计算 k 搜索 n / k 元素。最后,我会采取工人返回的 n 的最小值。

问题是,如果对于给定的 k (处理条目[ kn / k kn / k + n / k [ ) f(n)是真的,然后,它没有任何意图继续计算 k 的更高等级,因为它们会为 n 返回更高的值我们寻找最低限度。

为这样的问题创建可取消作业的惯用方法是什么?

谢谢,

0 个答案:

没有答案