1
0
mirror of https://github.com/golang/go synced 2024-11-25 04:57:56 -07:00

sort: simplify semantics of Search.

As discussed earlier.

R=gri
CC=golang-dev
https://golang.org/cl/3025042
This commit is contained in:
Roger Peppe 2010-11-12 15:57:33 -08:00 committed by Robert Griesemer
parent 81cb189a06
commit bac478da1c
2 changed files with 40 additions and 39 deletions

View File

@ -12,54 +12,54 @@ package sort
// sorted. It will often be passed as a closure. For instance, given a slice
// of integers, []data, sorted in ascending order, the function
//
// func(i int) bool { return data[i] <= 23 }
// func(i int) bool { return data[i] < 23 }
//
// can be used to search for the value 23 in data. The relationship expressed
// by the function must be "less or equal" if the elements are sorted in ascending
// order or "greater or equal" if they are sorted in descending order.
// by the function must be "less" if the elements are sorted in ascending
// order or "greater" if they are sorted in descending order.
// The function f will be called with values of i in the range 0 to n-1.
//
// For brevity, this discussion assumes ascending sort order. For descending
// order, replace <= with >=, and swap 'smaller' with 'larger'.
// order, replace < with >, and swap 'smaller' with 'larger'.
//
// If data[0] <= x and x <= data[n-1], Search returns the index i with:
// Search returns the index i with:
//
// data[i] <= x && x < data[i+1] (0 <= i < n)
// data[i-1] < x && x <= data[i]
//
// where data[n] is assumed to be larger than any x. Thus, i is the index of x
// if it is present in the data. It is the responsibility of the caller to
// verify the actual presence by testing if data[i] == x.
//
// If n == 0 or if x is smaller than any element in data (f is always false),
// the result is 0. If x is larger than any element in data (f is always true),
// the result is n-1.
// where data[-1] is assumed to be smaller than any x and data[n] is
// assumed to be larger than any x. Thus 0 <= i <= n and i is the first
// index of x if x is present in the data. It is the responsibility of
// the caller to verify the actual presence by testing if i < n and
// data[i] == x.
//
// To complete the example above, the following code tries to find the element
// elem in an integer slice data sorted in ascending order:
//
// elem := 23
// i := sort.Search(len(data), func(i int) bool { return data[i] <= elem })
// if len(data) > 0 && data[i] == elem {
// i := sort.Search(len(data), func(i int) bool { return data[i] < elem })
// if i < len(data) && data[i] == elem {
// // elem is present at data[i]
// } else {
// // elem is not present in data
// }
//
func Search(n int, f func(int) bool) int {
// See "A Method of Programming", E.W. Dijkstra,
// for arguments on correctness and efficiency.
i, j := 0, n
for i+1 < j {
h := i + (j-i)/2 // avoid overflow when computing h
// i < h < j
if f(h) {
// data[h] <= x
i = h
// data[h] < x
i = h + 1
} else {
// x < data[h]
// x <= data[h]
j = h
}
}
// test the final element that the loop did not.
if i < j && f(i) {
i++
}
return i
}
@ -70,7 +70,7 @@ func Search(n int, f func(int) bool) int {
// as specified by Search. The array must be sorted in ascending order.
//
func SearchInts(a []int, x int) int {
return Search(len(a), func(i int) bool { return a[i] <= x })
return Search(len(a), func(i int) bool { return a[i] < x })
}
@ -78,7 +78,7 @@ func SearchInts(a []int, x int) int {
// as specified by Search. The array must be sorted in ascending order.
//
func SearchFloats(a []float, x float) int {
return Search(len(a), func(i int) bool { return a[i] <= x })
return Search(len(a), func(i int) bool { return a[i] < x })
}
@ -86,7 +86,7 @@ func SearchFloats(a []float, x float) int {
// as specified by Search. The array must be sorted in ascending order.
//
func SearchStrings(a []string, x string) int {
return Search(len(a), func(i int) bool { return a[i] <= x })
return Search(len(a), func(i int) bool { return a[i] < x })
}

View File

@ -9,7 +9,7 @@ import "testing"
func f(a []int, x int) func(int) bool {
return func(i int) bool {
return a[i] <= x
return a[i] < x
}
}
@ -23,25 +23,26 @@ var tests = []struct {
i int
}{
{"empty", 0, nil, 0},
{"1 1", 1, func(i int) bool { return i <= 1 }, 0},
{"1 1", 1, func(i int) bool { return i < 1 }, 1},
{"1 false", 1, func(i int) bool { return false }, 0},
{"1 true", 1, func(i int) bool { return true }, 0},
{"1e9 991", 1e9, func(i int) bool { return i <= 991 }, 991},
{"1 true", 1, func(i int) bool { return true }, 1},
{"1e9 991", 1e9, func(i int) bool { return i < 991 }, 991},
{"1e9 false", 1e9, func(i int) bool { return false }, 0},
{"1e9 true", 1e9, func(i int) bool { return true }, 1e9 - 1},
{"1e9 true", 1e9, func(i int) bool { return true }, 1e9},
{"data -20", len(data), f(data, -20), 0},
{"data -10", len(data), f(data, -10), 0},
{"data -9", len(data), f(data, -9), 0},
{"data -6", len(data), f(data, -6), 0},
{"data -9", len(data), f(data, -9), 1},
{"data -6", len(data), f(data, -6), 1},
{"data -5", len(data), f(data, -5), 1},
{"data 3", len(data), f(data, 3), 5},
{"data 99", len(data), f(data, 99), 8},
{"data 100", len(data), f(data, 100), 11},
{"data 101", len(data), f(data, 101), 11},
{"data 11", len(data), f(data, 11), 8},
{"data 99", len(data), f(data, 99), 9},
{"data 100", len(data), f(data, 100), 9},
{"data 101", len(data), f(data, 101), 12},
{"data 10000", len(data), f(data, 10000), 13},
{"data 10001", len(data), f(data, 10001), 13},
{"descending a", 7, func(i int) bool { return []int{99, 99, 59, 42, 7, 0, -1, -1}[i] >= 7 }, 4},
{"descending 7", 1e9, func(i int) bool { return 1e9-i >= 7 }, 1e9 - 7},
{"data 10001", len(data), f(data, 10001), 14},
{"descending a", 7, func(i int) bool { return []int{99, 99, 59, 42, 7, 0, -1, -1}[i] > 7 }, 4},
{"descending 7", 1e9, func(i int) bool { return 1e9-i > 7 }, 1e9 - 7},
}
@ -78,7 +79,7 @@ func TestSearchEfficiency(t *testing.T) {
max := log2(n)
for x := 0; x < n; x += step {
count := 0
i := Search(n, func(i int) bool { count++; return i <= x })
i := Search(n, func(i int) bool { count++; return i < x })
if i != x {
t.Errorf("n = %d: expected index %d; got %d", n, x, i)
}
@ -103,7 +104,7 @@ var wrappertests = []struct {
i int
}{
{"SearchInts", SearchInts(data, 11), 8},
{"SearchFloats", SearchFloats(fdata, 2.1), 3},
{"SearchFloats", SearchFloats(fdata, 2.1), 4},
{"SearchStrings", SearchStrings(sdata, ""), 0},
{"IntArray.Search", IntArray(data).Search(0), 2},
{"FloatArray.Search", FloatArray(fdata).Search(2.0), 3},