-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdomain.go
436 lines (382 loc) · 8.77 KB
/
domain.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"encoding/binary"
"fmt"
"reflect"
"sort"
"strings"
"time"
bitmap "github.com/funny-falcon/highloadcup2018/bitmap3"
)
const (
SexBitsMale = 0x01
SexMale = "m"
SexFemale = "f"
StatusBitsMask = 0x06
StatusBitsFree = 0x02
StatusBitsMeeting = 0x04
StatusBitsComplex = 0x06
StatusComplexIx = 1
StatusMeetingIx = 2
StatusFreeIx = 3
StatusComplex = "всё сложно"
StatusMeeting = "заняты"
StatusFree = "свободны"
StatusPremiumNow = 0x08
StatusPremiumHas = 0x10
StatusPremiumMask = 0x60
StatusPremiumShift = 0x20
)
var PremiumLengths = [5]int32{0, 30 * 24 * 3600, 91 * 24 * 3600, 182 * 24 * 3600, 365 * 24 * 3600}
var CurTs int32
type Account struct {
Uid int32
Email uint32
EmailStart uint32
Phone uint32
Domain uint8
Code uint8
Sex bool
Status uint8
Sname uint16
Fname uint8
Country uint8
City uint16
PremiumNow bool
PremiumLength uint8
PremiumStart int32
Birth int32
Joined int32
Likes uintptr
}
type SmallAccount struct {
Birth int32
SmallerAccount
}
type SmallerAccount struct {
City uint16
Country uint8
StatusSexPremium uint8
}
func (a *Account) SmallAccount() SmallAccount {
s := SmallAccount{
SmallerAccount: SmallerAccount{City: a.City, Country: a.Country, StatusSexPremium: a.Status},
Birth: a.Birth,
}
if a.Sex {
s.StatusSexPremium |= 4
}
if a.PremiumNow {
s.StatusSexPremium |= 8
}
return s
}
func (s SmallerAccount) Status() int {
return int(s.StatusSexPremium & 3)
}
func (s SmallerAccount) Sex() bool {
return s.StatusSexPremium&4 != 0
}
func (s SmallerAccount) SexIx() int {
return int((s.StatusSexPremium & 4) >> 2)
}
func (s SmallerAccount) Premium() bool {
return s.StatusSexPremium&8 != 0
}
func (a *Account) SexIx() int {
if a.Sex {
return 1
}
return 0
}
func (a *Account) StatusIx() int {
return int(a.Status - 1)
}
/*
func (acc *Account) SetInterest(ix uint32) {
acc.Interests[ix/64] |= 1 << (ix & 63)
}
*/
const Init = 1536 * 1024
var Accounts = make([]Account, Init)
var SmallAccounts = make([]SmallAccount, Init)
var SmallerAccounts = make([]SmallerAccount, Init)
var AccountsMap bitmap.Bitmap
var MaxId int32
func SureCapa(slicePtr interface{}, capa int) {
val := reflect.ValueOf(slicePtr)
tpe := reflect.TypeOf(slicePtr).Elem()
newVal := reflect.MakeSlice(tpe, capa, capa)
reflect.Copy(newVal, val.Elem())
val.Set(newVal)
}
func SureAccount(i int32) *Account {
if int(i) >= len(Accounts) {
ln := int32(1)
for ; ln < i; ln *= 2 {
}
if ln-ln/4 > i {
ln -= ln / 4
}
SureCapa(&Accounts, int(ln))
SureCapa(&SmallAccounts, int(ln))
SureCapa(&SmallerAccounts, int(ln))
SureCapa(&Interests, int(ln))
}
if i >= MaxId {
MaxId = i + 1
}
AccountsMap.Set(i)
acc := &Accounts[i]
acc.Uid = int32(i)
return acc
}
func HasAccount(i int32) *Account {
if i >= MaxId {
//log.Printf("i > MaxId : %d > %d, Acc.Has:%v", i, MaxId, AccountsMap.Has(i))
return nil
}
if Accounts[i].Uid == 0 {
//log.Printf("Acc[%d].Uid == 0, Acc.Has:%v", i, AccountsMap.Has(i))
return nil
}
return &Accounts[i]
}
func RefAccount(i int32) *Account {
return &Accounts[i]
}
func SetSmallAccount(i int32, s SmallAccount) {
SmallAccounts[i] = s
SmallerAccounts[i] = s.SmallerAccount
}
func GetSmallAccount(i int32) SmallAccount {
return SmallAccounts[i]
}
func GetSmallerAccount(i int32) SmallerAccount {
return SmallerAccounts[i]
}
func DomainFromEmail(e string) string {
ix := strings.LastIndexByte(e, '@')
return e[ix+1:]
}
func CodeFromPhone(p string) string {
ixl := strings.IndexByte(p, '(')
ixr := strings.IndexByte(p, ')')
return p[ixl+1 : ixr]
}
var EmailIndex UniqStrings
var PhoneIndex UniqStrings
//var MaleMap = bitmap.Bitmap{}
//var FemaleMap = bitmap.Bitmap{}
var MaleMap = bitmap.SexMap{Mask: bitmap.MaleMask}
var FemaleMap = bitmap.SexMap{Mask: bitmap.FemaleMask}
var FreeMap = bitmap.Bitmap{}
var MeetingMap = bitmap.Bitmap{}
var ComplexMap = bitmap.Bitmap{}
var FreeOrMeetingMap = bitmap.Bitmap{}
var MeetingOrComplexMap = bitmap.Bitmap{}
var FreeOrComplexMap = bitmap.Bitmap{}
var StatusMaps = func() [4][3]*bitmap.Bitmap {
r := [4][3]*bitmap.Bitmap{
{},
}
r[StatusFreeIx] = [3]*bitmap.Bitmap{&FreeMap, &FreeOrMeetingMap, &FreeOrComplexMap}
r[StatusComplexIx] = [3]*bitmap.Bitmap{&ComplexMap, &FreeOrComplexMap, &MeetingOrComplexMap}
r[StatusMeetingIx] = [3]*bitmap.Bitmap{&MeetingMap, &FreeOrMeetingMap, &MeetingOrComplexMap}
return r
}()
var PremiumNow = bitmap.Bitmap{}
var PremiumNotNow = bitmap.Bitmap{}
var PremiumNull = bitmap.Bitmap{}
var PremiumNotNull = bitmap.Bitmap{}
var EmailGtIndexes [26]bitmap.Bitmap
var EmailLtIndexes [26]bitmap.Bitmap
func IndexGtLtEmail(e string, uid int32, set bool) {
ch := e[0]
// lt
start, end := int(ch)-'a', 25
if start < 0 {
start = 0
}
for ; start <= end; start++ {
if set {
EmailLtIndexes[start].Set(uid)
} else {
EmailLtIndexes[start].Unset(uid)
}
}
// gt
start, end = 0, int(ch)-'a'
if end > 25 {
end = 25
}
for ; start <= end; start++ {
if set {
EmailGtIndexes[start].Set(uid)
} else {
EmailGtIndexes[start].Unset(uid)
}
}
}
var BirthYearIndexes [61]bitmap.Bitmap
func GetBirthYear(ts int32) int32 {
return int32(time.Unix(int64(ts), 0).UTC().Year() - 1950)
}
var JoinYearIndexes [10]bitmap.Bitmap
func GetJoinYear(ts int32) int32 {
return int32(time.Unix(int64(ts), 0).UTC().Year() - 2011)
}
var DomainsStrings = SomeStrings{Huge: true}
var PhoneCodesStrings = SomeStrings{}
var FnameStrings = SomeStrings{}
var SnameStrings SomeStrings
var SnameSorted SnameSorting
var SnameOnce = NewOnce(SnameSorted.Init)
var CityStrings SomeStrings
var CountryStrings SomeStrings
//var InterestStrings = SomeStrings{}
var InterestStrings = SomeStrings{Huge: true}
func GetStatusIx(status string) (uint8, bool) {
switch status {
case StatusFree:
return StatusFreeIx, true
case StatusMeeting:
return StatusMeetingIx, true
case StatusComplex:
return StatusComplexIx, true
default:
return 0, false
}
}
func GetStatus(status uint8) string {
switch status {
case StatusFreeIx:
return StatusFree
case StatusMeetingIx:
return StatusMeeting
case StatusComplexIx:
return StatusComplex
default:
panic(fmt.Sprintf("Unknown statusIx %d", status))
}
}
func GetPremiumLength(start, finish int32) uint8 {
switch lngth := finish - start; lngth {
case 0:
return 0
case 30 * 24 * 3600:
return 1
case 91 * 24 * 3600:
return 2
case 182 * 24 * 3600:
return 3
case 365 * 24 * 3600:
return 4
default:
panic(fmt.Sprintf("wrong premium length %d %d %d", lngth, start, finish))
}
}
func GetEmailStart(s string) uint32 {
if len(s) >= 4 {
return binary.BigEndian.Uint32([]byte(s))
}
return GetEmailPrefix(s)
}
func GetEmailPrefix(s string) uint32 {
l := len(s)
r := uint32(0)
switch {
case l >= 4:
r = uint32(s[3])
fallthrough
case l >= 3:
r |= uint32(s[2]) << 8
fallthrough
case l >= 2:
r |= uint32(s[1]) << 16
fallthrough
case l >= 1:
r |= uint32(s[0]) << 24
}
return r
}
/*
func GetEmailLte(s string) uint32 {
l := len(s)
r := uint32(0)
if l >= 4 {
r = uint32(s[3])
} else {
r = 0xff
}
if l == 3 {
r |= uint32(s[2]) << 8
} else {
r |= 0xff00
}
if l == 2 {
r |= uint32(s[1]) << 16
} else {
r |= 0xff0000
}
if l == 1 {
r |= uint32(s[0]) << 24
} else {
r |= 0xff000000
}
return r
}
*/
type SnameSorting struct {
Ix []uint32
Str []string
}
func (s *SnameSorting) Init() {
s.Ix = make([]uint32, len(SnameStrings.Arr))
s.Str = make([]string, len(SnameStrings.Arr))
for i := range s.Ix {
s.Ix[i] = uint32(i + 1)
s.Str[i] = SnameStrings.GetStr(uint32(i + 1))
}
sort.Sort(s)
}
func (s *SnameSorting) Len() int { return len(s.Ix) }
func (s *SnameSorting) Less(i, j int) bool { return s.Str[i] < s.Str[j] }
func (s *SnameSorting) Swap(i, j int) {
s.Ix[i], s.Ix[j] = s.Ix[j], s.Ix[i]
s.Str[i], s.Str[j] = s.Str[j], s.Str[i]
}
func (s *SnameSorting) PrefixRange(pref string) (i, j int) {
i = sort.Search(len(s.Str), func(i int) bool {
return pref <= s.Str[i]
})
for j = i; j < len(s.Str) && strings.HasPrefix(s.Str[j], pref); j++ {
}
return
}
var Likers = make([]uintptr, 1536*1024)
func SureLikers(i int32, f func(*bitmap.Likes)) {
if int(i) >= len(Likers) {
ln := int32(1)
for ; ln < i; ln *= 2 {
}
newLikers := make([]uintptr, ln, ln)
copy(newLikers, Likers)
Likers = newLikers
}
f(bitmap.GetLikes(&Likers[i]))
}
func GetLikers(i int32) *bitmap.Likes {
if int(i) >= len(Likers) {
return nil
}
if Likers[i] == 0 {
return nil
}
return bitmap.GetLikes(&Likers[i])
}
var CityGroups [1000][6]uint32
var CountryGroups [100][6]uint32
var InterestJoinedGroups [10][100]uint32
var InterestBirthGroups [61][100]uint32
var InterestCountryGroups [100][100]uint32