Skip to content

Commit 063f403

Browse files
elibengopherbot
authored andcommitted
sort: add Find function
For #50340 Change-Id: I3b4d278affc8e7ec706db8c9777f7a8c8ce7441d Reviewed-on: https://go-review.googlesource.com/c/go/+/396514 Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Cherry Mui <[email protected]> Run-TryBot: Russ Cox <[email protected]> Auto-Submit: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 3a0cda4 commit 063f403

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

api/next/50340.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg sort, func Find(int, func(int) int) (int, bool) #50340

src/sort/search.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,47 @@ func Search(n int, f func(int) bool) int {
7272
return i
7373
}
7474

75+
// Find uses binary search to find and return the smallest index i in [0, n)
76+
// at which cmp(i) <= 0. If there is no such index i, Find returns i = n.
77+
// The found result is true if i < n and cmp(i) == 0.
78+
// Find calls cmp(i) only for i in the range [0, n).
79+
//
80+
// To permit binary search, Find requires that cmp(i) > 0 for a leading
81+
// prefix of the range, cmp(i) == 0 in the middle, and cmp(i) < 0 for
82+
// the final suffix of the range. (Each subrange could be empty.)
83+
// The usual way to establish this condition is to interpret cmp(i)
84+
// as a comparison of a desired target value t against entry i in an
85+
// underlying indexed data structure x, returning <0, 0, and >0
86+
// when t < x[i], t == x[i], and t > x[i], respectively.
87+
//
88+
// For example, to look for a particular string in a sorted, random-access
89+
// list of strings:
90+
// i, found := sort.Find(x.Len(), func(i int) int {
91+
// return strings.Compare(target, x.At(i))
92+
// })
93+
// if found {
94+
// fmt.Printf("found %s at entry %d\n", target, i)
95+
// } else {
96+
// fmt.Printf("%s not found, would insert at %d", target, i)
97+
// }
98+
func Find(n int, cmp func(int) int) (i int, found bool) {
99+
// The invariants here are similar to the ones in Search.
100+
// Define cmp(-1) > 0 and cmp(n) <= 0
101+
// Invariant: cmp(i-1) > 0, cmp(j) <= 0
102+
i, j := 0, n
103+
for i < j {
104+
h := int(uint(i+j) >> 1) // avoid overflow when computing h
105+
// i ≤ h < j
106+
if cmp(h) > 0 {
107+
i = h + 1 // preserves cmp(i-1) > 0
108+
} else {
109+
j = h // preserves cmp(j) <= 0
110+
}
111+
}
112+
// i == j, cmp(i-1) > 0 and cmp(j) <= 0
113+
return i, i < n && cmp(i) == 0
114+
}
115+
75116
// Convenience wrappers for common cases.
76117

77118
// SearchInts searches for x in a sorted slice of ints and returns the index

src/sort/search_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package sort_test
77
import (
88
"runtime"
99
. "sort"
10+
stringspkg "strings"
1011
"testing"
1112
)
1213

@@ -57,6 +58,80 @@ func TestSearch(t *testing.T) {
5758
}
5859
}
5960

61+
func TestFind(t *testing.T) {
62+
str1 := []string{"foo"}
63+
str2 := []string{"ab", "ca"}
64+
str3 := []string{"mo", "qo", "vo"}
65+
str4 := []string{"ab", "ad", "ca", "xy"}
66+
67+
// slice with repeating elements
68+
strRepeats := []string{"ba", "ca", "da", "da", "da", "ka", "ma", "ma", "ta"}
69+
70+
// slice with all element equal
71+
strSame := []string{"xx", "xx", "xx"}
72+
73+
tests := []struct {
74+
data []string
75+
target string
76+
wantPos int
77+
wantFound bool
78+
}{
79+
{[]string{}, "foo", 0, false},
80+
{[]string{}, "", 0, false},
81+
82+
{str1, "foo", 0, true},
83+
{str1, "bar", 0, false},
84+
{str1, "zx", 1, false},
85+
86+
{str2, "aa", 0, false},
87+
{str2, "ab", 0, true},
88+
{str2, "ad", 1, false},
89+
{str2, "ca", 1, true},
90+
{str2, "ra", 2, false},
91+
92+
{str3, "bb", 0, false},
93+
{str3, "mo", 0, true},
94+
{str3, "nb", 1, false},
95+
{str3, "qo", 1, true},
96+
{str3, "tr", 2, false},
97+
{str3, "vo", 2, true},
98+
{str3, "xr", 3, false},
99+
100+
{str4, "aa", 0, false},
101+
{str4, "ab", 0, true},
102+
{str4, "ac", 1, false},
103+
{str4, "ad", 1, true},
104+
{str4, "ax", 2, false},
105+
{str4, "ca", 2, true},
106+
{str4, "cc", 3, false},
107+
{str4, "dd", 3, false},
108+
{str4, "xy", 3, true},
109+
{str4, "zz", 4, false},
110+
111+
{strRepeats, "da", 2, true},
112+
{strRepeats, "db", 5, false},
113+
{strRepeats, "ma", 6, true},
114+
{strRepeats, "mb", 8, false},
115+
116+
{strSame, "xx", 0, true},
117+
{strSame, "ab", 0, false},
118+
{strSame, "zz", 3, false},
119+
}
120+
121+
for _, tt := range tests {
122+
t.Run(tt.target, func(t *testing.T) {
123+
cmp := func(i int) int {
124+
return stringspkg.Compare(tt.target, tt.data[i])
125+
}
126+
127+
pos, found := Find(len(tt.data), cmp)
128+
if pos != tt.wantPos || found != tt.wantFound {
129+
t.Errorf("Find got (%v, %v), want (%v, %v)", pos, found, tt.wantPos, tt.wantFound)
130+
}
131+
})
132+
}
133+
}
134+
60135
// log2 computes the binary logarithm of x, rounded up to the next integer.
61136
// (log2(0) == 0, log2(1) == 0, log2(2) == 1, log2(3) == 2, etc.)
62137
func log2(x int) int {
@@ -158,3 +233,34 @@ func TestSearchExhaustive(t *testing.T) {
158233
}
159234
}
160235
}
236+
237+
// Abstract exhaustive test for Find.
238+
func TestFindExhaustive(t *testing.T) {
239+
// Test Find for different sequence sizes and search targets.
240+
// For each size, we have a (unmaterialized) sequence of integers:
241+
// 2,4...size*2
242+
// And we're looking for every possible integer between 1 and size*2 + 1.
243+
for size := 0; size <= 100; size++ {
244+
for x := 1; x <= size*2+1; x++ {
245+
var wantFound bool
246+
var wantPos int
247+
248+
cmp := func(i int) int {
249+
// Encodes the unmaterialized sequence with elem[i] == (i+1)*2
250+
return x - (i+1)*2
251+
}
252+
pos, found := Find(size, cmp)
253+
254+
if x%2 == 0 {
255+
wantPos = x/2 - 1
256+
wantFound = true
257+
} else {
258+
wantPos = x / 2
259+
wantFound = false
260+
}
261+
if found != wantFound || pos != wantPos {
262+
t.Errorf("Find(%d, %d): got (%v, %v), want (%v, %v)", size, x, pos, found, wantPos, wantFound)
263+
}
264+
}
265+
}
266+
}

0 commit comments

Comments
 (0)