-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexorank.go
71 lines (59 loc) · 1.33 KB
/
lexorank.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package lexorank
import (
"errors"
"fmt"
"strconv"
)
const (
minChar = '0'
maxChar = 'z'
)
// Rank returns a new Lexorank between prev and next.
// Uses 0-9A-Za-z alphabet.
func Rank(prev, next string) (string, error) {
if prev == "" {
prev = string(minChar)
}
if next == "" {
next = string(maxChar)
}
if !isValid(prev) || !isValid(next) {
return "", errors.New("lexorank: incorrect prev or next")
}
rank := make([]byte, 0, max(len(prev), len(next)))
// will take upto |prev| + |next| + C iterations
for i := 0; ; i++ {
prev := getChar(prev, i, minChar)
next := getChar(next, i, maxChar)
if prev == next {
rank = append(rank, prev)
continue
}
mid := avg(prev, next)
if mid == prev || mid == next {
rank = append(rank, prev)
continue
}
rank = append(rank, mid)
break
}
if r := string(rank); r < next {
return r, nil
}
return prev, nil
}
// RankN returns n Lexoranks between prev and next (via simple iteration).
// Uses Rank under the hood.
func RankN(prev, next string, n int) ([]string, error) {
idx, err := Rank(prev, next)
if err != nil {
return nil, err
}
suffixRankLen := len(strconv.Itoa(n))
suffixRankFormat := fmt.Sprintf("%%0%dd", suffixRankLen)
res := make([]string, 0, n)
for i := 0; i < n; i++ {
res = append(res, idx+fmt.Sprintf(suffixRankFormat, i))
}
return res, nil
}