import (
"fmt"
"math/rand"
"sort"
)
// Return leftmost offset where val can be inserted in sl while
// maintaining sorted orter. Assumes sl is sorted.
func bisectLeftInt(sl []int, val int) int {
return sort.Search(len(sl), func(i int) bool {return sl[i] >= val})
}
// Same but for rightmost offset
func bisectRightInt(sl []int, val int) int {
return sort.Search(len(sl), func(i int) bool {return sl[i] > val})
}
// Same as bisectLeftInt but where val and sl contain strings
func bisectLeftStr(sl []string, val string) int {
return sort.Search(len(sl), func(i int) bool {return sl[i] >= val})
}
// Same but for rightmost offset
func bisectRightStr(sl []string, val string) int {
return sort.Search(len(sl), func(i int) bool {return sl[i] > val})
}
a := []int{1, 2, 3, 3, 3, 4, 5}
bisectLeftInt(a, 3)
2
bisectRightInt(a, 3)
5
a = []int{2, 4, 6, 8, 10}
bisectLeftInt(a, 5)
2
bisectRightInt(a, 5)
2
// Make a list of pseudo-random integers in [1, 99]
rand.Seed(77)
ls := make([]int, 30)
for i := range ls {
ls[i] = rand.Intn(100) + 1
}
ls
[61 71 6 70 39 55 13 19 33 71 1 72 15 34 62 72 77 52 23 95 29 9 86 40 28 50 31 59 25 62]
sort.Ints(ls)
ls
[1 6 9 13 15 19 23 25 28 29 31 33 34 39 40 50 52 55 59 61 62 62 70 71 71 72 72 77 86 95]
// The number 30 is not in the sorted list
// If it were, where would it go?
val := 30
insPt := bisectLeftInt(ls, val)
insPt
10
// Insert it
ls = append(ls, 0)
copy(ls[insPt+1:], ls[insPt:])
ls[insPt] = val
ls
[1 6 9 13 15 19 23 25 28 29 30 31 33 34 39 40 50 52 55 59 61 62 62 70 71 71 72 72 77 86 95]
// 62 appears multiple times
st := bisectLeftInt(ls, 62)
en := bisectRightInt(ls, 62)
st
21
en
23
ls[st:en]
[62 62]
// Of course, there also exists a total ordering over strings
// (lexicographical ordering), so we can do all the same things
// with strings
strls := []string{"a", "awkward", "awl", "awls", "axe", "axes", "bee"}
st = bisectLeftStr(strls, "awl")
en = bisectRightStr(strls, "awl")
st
2
en
3
// Say we want to get the range of elements that have some prefix,
// e.g. 'aw' and say that 'z' is the lexicographically greatest
// character in the alphabet.
st, en = bisectLeftStr(strls, "aw"), bisectLeftStr(strls, "ax")
st
1
en
4
strls[st:en]
[awkward awl awls]
// If we can do this for any sorted list of strings, we can do it for
// a sorted list of suffixes
t := "abaaba$"
suffixes := []string{t, t[1:], t[2:], t[3:], t[4:], t[5:], t[6:]}
sort.Strings(suffixes)
suffixes
[$ a$ aaba$ aba$ abaaba$ ba$ baaba$]
// Search for suffixes starting with aba
st = bisectLeftStr(suffixes, "aba")
en = bisectLeftStr(suffixes, "abb")
st
3
en
5