-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtop_token_array.go
More file actions
55 lines (46 loc) · 1.05 KB
/
top_token_array.go
File metadata and controls
55 lines (46 loc) · 1.05 KB
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
package stats
const topTokenMaxSize = 50
type TopTokenArray []TopToken
type TopToken struct {
Token string `json:"token"`
Count uint `json:"count"`
}
func (a *TopTokenArray) insert(token string, count uint) {
ta := *a // allow accessing token array without indirection everywhere
insertAt := -1
currentIndex := -1
if len(ta) == 0 {
ta = append(ta, TopToken{token, count})
*a = ta
return
}
for i, t := range ta {
if insertAt == -1 && count > t.Count {
insertAt = i
}
if currentIndex == -1 && token == t.Token {
currentIndex = i
}
if currentIndex != -1 && insertAt != -1 {
break
}
}
if currentIndex >= 0 {
if insertAt < 0 {
return
}
if insertAt < currentIndex {
ta[currentIndex].Token, ta[insertAt].Token =
ta[insertAt].Token, ta[currentIndex].Token
ta[insertAt].Count = count
} else {
ta[currentIndex].Count = count
}
} else if len(ta) < topTokenMaxSize {
ta = append(ta, TopToken{token, count})
*a = ta
} else if insertAt >= 0 {
ta[insertAt].Token = token
ta[insertAt].Count = count
}
}