-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccountrecindex.go
182 lines (147 loc) · 3.8 KB
/
accountrecindex.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
package main
import (
"math/bits"
"sort"
)
type account struct {
country uint
city uint
birth int
premium int
interestsA uint64
interestsB uint64
}
const countryI = 0
const cityI = 8
type AccountRecIndex struct {
InterestDict *Dict
CountryDict *Dict
CityDict *Dict
fFree map[int]account
fBusy map[int]account
fWtf map[int]account
mFree map[int]account
mBusy map[int]account
mWtf map[int]account
}
func (i *AccountRecIndex) Init() {
i.fFree = make(map[int]account)
i.fBusy = make(map[int]account)
i.fWtf = make(map[int]account)
i.mFree = make(map[int]account)
i.mBusy = make(map[int]account)
i.mWtf = make(map[int]account)
}
func (i *AccountRecIndex) getCollection(sex string, status int) *map[int]account {
if sex == "f" {
if status == StatusFree {
return &i.fFree
} else if status == StatusWtf {
return &i.fWtf
} else if status == StatusBusy {
return &i.fBusy
}
}
if sex == "m" {
if status == StatusFree {
return &i.mFree
} else if status == StatusWtf {
return &i.mWtf
} else if status == StatusBusy {
return &i.mBusy
}
}
return nil
}
func (i *AccountRecIndex) Add(a Account) {
col := i.getCollection(*a.Sex, a.StatusId)
if col == nil {
panic("accounts collection is nil")
}
indexedAccount := account{}
if a.City == nil {
indexedAccount.city = 0
} else {
indexedAccount.city = i.CityDict.GetId(*a.City)
}
if a.Country == nil {
indexedAccount.country = 0
} else {
indexedAccount.country = i.CountryDict.GetId(*a.Country)
}
indexedAccount.birth = *a.Birth
indexedAccount.premium = a.PremiumStatus
if a.Interests != nil {
for _, interest := range *a.Interests {
id := i.InterestDict.GetId(interest)
if id >= 64 {
var byte = uint64(1) << (id - 64)
indexedAccount.interestsB |= byte
} else {
var byte = uint64(1) << id
indexedAccount.interestsA |= byte
}
}
}
(*col)[a.Id] = indexedAccount
}
type AccountRecArray []AccountRec
type AccountRec struct {
Id int
Score int
}
func (a AccountRecArray) Len() int {
return len(a)
}
func (a AccountRecArray) Less(i, j int) bool {
return a[i].Score > a[j].Score
}
func (a AccountRecArray) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
const PremiumСoefficient__ = 1000000000000000
const StatusСoefficient___ = 100000000000000
const InterestsСoefficient = 100000000000
const MaxBirthScore_______ = 2147483648
func (i *AccountRecIndex) Recommend(a Account, country string, city string, limit int) (result []int) {
sex := "f"
if *a.Sex == "f" {
sex = "m"
}
target := (*i.getCollection(*a.Sex, a.StatusId))[a.Id]
countryId := i.CountryDict.GetId(country)
cityId := i.CityDict.GetId(city)
ids := make([]AccountRec, 0, 100)
for status := StatusFree; status >= StatusBusy; status-- {
col := *i.getCollection(sex, status)
statusScore := status * StatusСoefficient___
for id, binary := range col {
if countryId != 0 && binary.country != countryId {
continue
}
if cityId != 0 && binary.city != cityId {
continue
}
if ((target.interestsA & binary.interestsA) | (target.interestsB & binary.interestsB)) != 0 {
birthScore := MaxBirthScore_______ - int(Abs(int64(target.birth)-int64(binary.birth)))
premiumScore := PremiumСoefficient__
if binary.premium != PremiumActive {
premiumScore = 0
}
commonInterestsA := bits.OnesCount64(target.interestsA & binary.interestsA)
commonInterestsB := bits.OnesCount64(target.interestsB & binary.interestsB)
interestsScore := (commonInterestsA + commonInterestsB) * InterestsСoefficient
ids = append(ids, AccountRec{
id,
statusScore + interestsScore + birthScore + premiumScore,
})
}
}
}
sort.Sort(AccountRecArray(ids))
result = make([]int, 0, limit)
for i := 0; i < limit && i < len(ids); i++ {
result = append(result, ids[i].Id)
}
return
}