-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexorank.go
229 lines (196 loc) · 4.82 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Package lexorank is a simple implementation of LexoRank.
//
// LexoRank is a ranking system introduced by Atlassian JIRA.
// For details - https://www.youtube.com/watch?v=OjQv9xMoFbg
package lexorank
import (
"fmt"
)
func byteToOrder(b byte) byte {
n := _byteToOrder(b)
if n < 0 || n > 62 {
panic("bad value")
}
return n
}
func _byteToOrder(b byte) byte {
if b >= '0' && b <= '9' {
return b - '0'
} else if b >= 'A' && b <= 'Z' {
return b - 'A' + 10
} else if b >= 'a' && b <= 'z' {
return b - 'a' + 10 + 26
} else if b < 'A' {
return 0
} else {
return 10 + 26 + 26 - 1
}
}
//ParseJira Parsing a LexoRank String
func ParseJira(rank string) (Position, bool) {
m := jiraRank.FindStringSubmatch(rank)
if m == nil {
return Position{}, false
}
return Position{
Bucket: m[1][0] - '0',
Major: m[2],
Minor: m[3],
}, true
}
//Rank of orders
func Rank(prev, next string) (string, bool) {
sPrev, okPrev := ParseJira(prev)
sNext, okNext := ParseJira(next)
if okNext && okPrev {
pos, ok := Ranks(1, &sPrev, &sNext)
if ok {
return pos[0].String(), true
}
}
return "", false
}
func RanksMore(n int, prev, next string) ([]string, bool) {
sPrev, okPrev := ParseJira(prev)
sNext, okNext := ParseJira(next)
if okNext && okPrev {
pos, ok := Ranks(n, &sPrev, &sNext)
if ok {
result := []string{}
for _, vstr := range pos {
result = append(result, vstr.String())
}
return result, true
}
}
return nil, false
}
// Ranks arranges for there to be N ranks between `prev` and `next`
// and returns them. This is useful when re-ranking a group of
// objects together at onces.
func Ranks(n int, prev, next *Position) ([]Position, bool) {
if n > maxMultiRank {
// can't accommodate that many all at once
return nil, false
}
if prev == nil {
prev = &Position{
Major: "000000",
Minor: ":",
}
// if there *is* a next, adopt its bucket
if next != nil {
prev.Bucket = next.Bucket
}
}
if next == nil {
next = &Position{
Major: "zzzzzz",
Minor: ":",
}
// if there *is* a prev, adopt its bucket
if prev != nil {
next.Bucket = prev.Bucket
}
}
if prev.Major != next.Major {
p, ok := majorRanks(n, *prev, *next)
if ok {
return p, true
}
}
return minorRanks(n, *prev, *next)
}
func minorRanks(n int, prev, next Position) ([]Position, bool) {
panic("TODO")
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func majorRanks(n int, prev, next Position) ([]Position, bool) {
rank := ""
i := 0
majorLen := max(len(prev.Major), len(next.Major))
for {
prevChar := getChar(prev.Major, i, minChar)
nextChar := getChar(next.Major, i, maxChar)
if prevChar == nextChar {
// common prefix
fmt.Printf("common prefix at [%c]\n", prevChar)
rank += string(prevChar)
i++
continue
}
midChars, ok := mids(n, prevChar, nextChar)
if !ok {
// we need to adjust the bounds in which we're searching for ranks
// at this point we have an uncommon prefix, e.g.,
// |||
// 005z
// 006b
// for the next iteration we want to use one with the most space
// available, which means going forward with
// 0060
// 006b
fmt.Printf("fork in the road at [%c <> %c]\n", prevChar, nextChar)
prevAfter := byteToOrder(getChar(prev.Major, i+1, minChar))
nextAfter := byteToOrder(getChar(next.Major, i+1, maxChar))
spaceAfterPrev := maxMultiRank - prevAfter
spaceBeforeNext := nextAfter
fmt.Printf(" after this, PREV has order %d (space %d)\n", prevAfter, spaceAfterPrev)
fmt.Printf(" NEXT has order %d (space %d)\n", nextAfter, spaceBeforeNext)
if spaceAfterPrev > spaceBeforeNext {
next.Major = next.Major[:i] + string(prevChar)
fmt.Printf(" go forward with NEXT [%s]\n", next.Major)
rank += string(prevChar)
} else {
prev.Major = prev.Major[:i] + string(nextChar)
fmt.Printf(" go forward with PREV [%s]\n", prev.Major)
rank += string(nextChar)
}
i++
continue
}
if len(rank) == majorLen {
return nil, false
}
out := make([]Position, n)
// arrange for the major parts to all be the same size
// by attaching a trailer to newly generated major ranks
trailer := trailer[:majorLen-1-len(rank)]
for j, mid := range midChars {
out[j] = Position{
Bucket: prev.Bucket,
Major: rank + string(mid) + trailer,
Minor: ":",
}
}
return out, true
}
}
func mids(n int, prev, next byte) ([]byte, bool) {
prevo := byteToOrder(prev)
nexto := byteToOrder(next)
per := int(nexto-prevo) / (n + 1)
if per < 1 {
return nil, false
}
fmt.Printf("(%c ... %c) is (%d ... %d) per is %d\n",
prev, next,
prevo, nexto,
per)
ch := make([]byte, n)
for i := 0; i < n; i++ {
ch[i] = orderToByte[int(prevo)+per*(i+1)]
}
return ch, true
}
func getChar(s string, i int, defaultChar byte) byte {
if i >= len(s) {
return defaultChar
}
return s[i]
}