/
algorithm.go
505 lines (427 loc) · 11.3 KB
/
algorithm.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Package algorithm contains some useful algorithms
package algorithm
import (
"container/heap"
"sync"
"sync/atomic"
"unsafe"
"github.com/Laisky/errors"
skiplist "github.com/Laisky/fast-skiplist"
"github.com/Laisky/zap"
"github.com/gammazero/deque"
gutils "github.com/Laisky/go-utils/v3"
"github.com/Laisky/go-utils/v3/log"
)
// -------------------------------------
// deque
// -------------------------------------
// Deque
//
// https://pkg.go.dev/github.com/gammazero/deque#Deque
type Deque[T any] interface {
PushBack(T)
PushFront(T)
PopFront() T
PopBack() T
Len() int
Front() T
Back() T
}
type dequeOpt struct {
currentCapacity,
minimalCapacity int
}
func (o *dequeOpt) applyFuncs(optfs ...DequeOptFunc) (*dequeOpt, error) {
for _, optf := range optfs {
if err := optf(o); err != nil {
return nil, err
}
}
return o, nil
}
// DequeOptFunc optional arguments for deque
type DequeOptFunc func(*dequeOpt) error
// WithDequeCurrentCapacity preallocate memory for deque
func WithDequeCurrentCapacity(size int) DequeOptFunc {
return func(opt *dequeOpt) error {
if size < 0 {
return errors.Errorf("size must greater than 0")
}
opt.currentCapacity = size
return nil
}
}
// WithDequeMinimalCapacity set deque minimal capacity
func WithDequeMinimalCapacity(size int) DequeOptFunc {
return func(opt *dequeOpt) error {
if size < 0 {
return errors.Errorf("size must greater than 0")
}
opt.minimalCapacity = size
return nil
}
}
// NewDeque new deque
func NewDeque[T any](optfs ...DequeOptFunc) (Deque[T], error) {
opt, err := new(dequeOpt).applyFuncs(optfs...)
if err != nil {
return nil, err
}
return deque.New[T](opt.currentCapacity, opt.minimalCapacity), nil
}
// -------------------------------------
// skiplist
// -------------------------------------
// NewSkiplist new skiplist
//
// https://github.com/sean-public/fast-skiplist
func NewSkiplist() *skiplist.SkipList {
return skiplist.New()
}
// -------------------------------------
// Heap
// -------------------------------------
// itemType item that need to sort
type itemType[T gutils.Sortable] struct {
priority T
key any
}
// GetKey get key of item
func (it *itemType[T]) GetKey() any {
return it.key
}
// GetPriority get priority of item
func (it *itemType[T]) GetPriority() T {
return it.priority
}
// HeapSlice slice that could be used by heap
type HeapSlice[T gutils.Sortable] []HeapItemItf[T]
// innerHeapQ lower structure used by heap
//
// do not use this structure directly
type innerHeapQ[T gutils.Sortable] struct {
isMaxTop bool
q []HeapItemItf[T]
}
// newInnerHeapQ create new PriorityQ
func newInnerHeapQ[T gutils.Sortable](isMaxTop bool) *innerHeapQ[T] {
return &innerHeapQ[T]{
isMaxTop: isMaxTop,
q: []HeapItemItf[T]{},
}
}
// Len get length of items in heapq
func (p *innerHeapQ[T]) Len() int {
return len(p.q)
}
// Less compare two items in heapq
func (p *innerHeapQ[T]) Less(i, j int) bool {
if p.isMaxTop {
return p.q[i].GetPriority() < p.q[j].GetPriority()
}
return p.q[i].GetPriority() >= p.q[j].GetPriority()
}
// Swap swat two items in heapq
func (p *innerHeapQ[T]) Swap(i, j int) {
p.q[i], p.q[j] = p.q[j], p.q[i]
}
// Push push new item into heapq
func (p *innerHeapQ[T]) Push(x any) {
item := x.(HeapItemItf[T])
p.q = append(p.q, item)
}
// Remove remove an specific item
func (p *innerHeapQ[T]) Remove(key any) (ok bool) {
for i, it := range p.q {
if it.GetKey() == key {
p.q = append(p.q[:i], p.q[i+1:]...)
return true
}
}
return false
}
// Get get item by key
func (p *innerHeapQ[T]) Get(key any) HeapItemItf[T] {
for i := range p.q {
if p.q[i].GetKey() == key {
return p.q[i]
}
}
return nil
}
// GetIdx get item by idx
func (p *innerHeapQ[T]) GetIdx(idx int) HeapItemItf[T] {
return p.q[idx]
}
// Pop pop from the tail.
// if `isMaxTop=True`, pop the tail(smallest) item
func (p *innerHeapQ[T]) Pop() (popped any) {
n := len(p.q)
if n == 0 {
return nil
}
popped = p.q[n-1]
p.q[n-1] = nil // avoid memory leak
p.q = p.q[:n-1]
return popped
}
// HeapItemItf items need to sort
//
// T is the type of priority
type HeapItemItf[T gutils.Sortable] interface {
GetKey() any
GetPriority() T
}
// GetLargestNItems get N highest priority items
func GetLargestNItems[T gutils.Sortable](inputChan <-chan HeapItemItf[T], topN int) ([]HeapItemItf[T], error) {
return GetTopKItems(inputChan, topN, false)
}
// GetSmallestNItems get N smallest priority items
func GetSmallestNItems[T gutils.Sortable](inputChan <-chan HeapItemItf[T], topN int) ([]HeapItemItf[T], error) {
return GetTopKItems(inputChan, topN, true)
}
// GetTopKItems calculate topN by heap
//
// Arg isHighest:
// - use min-heap to calculates topN Highest items.
// - use max-heap to calculates topN Lowest items.
func GetTopKItems[T gutils.Sortable](
inputChan <-chan HeapItemItf[T],
topN int,
isHighest bool,
) ([]HeapItemItf[T], error) {
log.Shared.Debug("GetMostFreqWords for key2PriMap", zap.Int("topN", topN))
if topN < 2 {
return nil, errors.Errorf("GetMostFreqWords topN must larger than 2")
}
var (
i int
ok bool
item, thresItem HeapItemItf[T]
items = make([]HeapItemItf[T], topN)
nTotal = 0
p = newInnerHeapQ[T](isHighest)
)
LOAD_LOOP:
for i = 0; i < topN; i++ { // load first topN items
item, ok = <-inputChan
if !ok { // channel closed
inputChan = nil
break LOAD_LOOP
}
nTotal++
// is `isHighest=true`, thresItem is the smallest item
// is `isHighest=false`, thresItem is the biggest item
if thresItem == nil ||
(isHighest && item.GetPriority() < thresItem.GetPriority()) ||
(!isHighest && item.GetPriority() > thresItem.GetPriority()) {
thresItem = item
}
p.Push(&itemType[T]{
key: item.GetKey(),
priority: item.GetPriority(),
})
}
if inputChan == nil {
switch p.Len() {
case 1: // only one item
return []HeapItemItf[T]{item}, nil
case 0:
return []HeapItemItf[T]{}, nil
}
}
heap.Init(p) // initialize heap
// load all remain items
if inputChan != nil {
for item = range inputChan {
nTotal++
if (isHighest && item.GetPriority() <= thresItem.GetPriority()) ||
(!isHighest && item.GetPriority() >= thresItem.GetPriority()) {
continue
}
heap.Push(p, &itemType[T]{
priority: item.GetPriority(),
key: item.GetKey(),
})
thresItem = heap.Pop(p).(*itemType[T])
}
}
log.Shared.Debug("process all items", zap.Int("total", nTotal))
for i := 1; i <= topN; i++ { // pop all needed items
item = heap.Pop(p).(*itemType[T])
items[topN-i] = item
}
return items, nil
}
// LimitSizeHeap heap that with limited size
type LimitSizeHeap[T gutils.Sortable] interface {
Push(item HeapItemItf[T]) HeapItemItf[T]
Pop() HeapItemItf[T]
}
// limitSizeHeap heap with limit size
type limitSizeHeap[T gutils.Sortable] struct {
q *innerHeapQ[T]
thresItem HeapItemItf[T]
isHighest bool
size, maxSize int64
}
// NewLimitSizeHeap create new LimitSizeHeap
func NewLimitSizeHeap[T gutils.Sortable](size int, isHighest bool) (LimitSizeHeap[T], error) {
if size < 1 {
return nil, errors.Errorf("size must greater than 0")
}
h := &limitSizeHeap[T]{
q: newInnerHeapQ[T](!isHighest),
maxSize: int64(size),
isHighest: isHighest,
}
heap.Init(h.q)
return h, nil
}
// Push push item into heap, return popped item if exceed size
func (h *limitSizeHeap[T]) Push(item HeapItemItf[T]) HeapItemItf[T] {
if h.size == h.maxSize && h.thresItem != nil {
if h.isHighest && item.GetPriority() <= h.thresItem.GetPriority() {
return item // item <= minimal member
} else if !h.isHighest && item.GetPriority() >= h.thresItem.GetPriority() {
return item // item >= maximal member
}
}
// update thresItem
if h.thresItem == nil {
h.thresItem = item
} else if h.isHighest && item.GetPriority() < h.thresItem.GetPriority() {
h.thresItem = item
} else if !h.isHighest && item.GetPriority() > h.thresItem.GetPriority() {
h.thresItem = item
}
h.size++
heap.Push(h.q, item)
if h.size > h.maxSize {
h.size--
h.thresItem = heap.Pop(h.q).(HeapItemItf[T])
return h.thresItem
}
return nil
}
// Pop pop from the tail.
// if `isHighest=True`, pop the biggest item
func (h *limitSizeHeap[T]) Pop() HeapItemItf[T] {
if h.size == 0 {
return nil
}
h.size--
return heap.Pop(h.q).(HeapItemItf[T])
}
// -------------------------------------
// FIFO
// -------------------------------------
var fifoPool = sync.Pool{
New: func() any {
return &fifoNode{
next: unsafe.Pointer(emptyNode),
}
},
}
type fifoNode struct {
next unsafe.Pointer
d any
// refcnt to avoid ABA problem
// refcnt int32
}
// CompareAndAdd add ref count
// func (f *fifoNode) CompareAndAdd(expect int32) bool {
// return atomic.CompareAndSwapInt32(&f.refcnt, expect, expect+1)
// }
// Refcnt get ref count
// func (f *fifoNode) Refcnt() int32 {
// return atomic.LoadInt32(&f.refcnt)
// }
// FIFO is a lock-free First-In-First-Out queue
//
// paper: https://1drv.ms/b/s!Au45o0W1gVVLuNxYkPzfBo4fOssFPQ?e=TYxHKl
type FIFO struct {
// head the node that before real head node
//
// head.next is the real head node
//
// unsafe.pointer will tell gc not to remove object in heap
head unsafe.Pointer
// tail maybe(maynot) the tail node in queue
tail unsafe.Pointer
len int64
dummy unsafe.Pointer
}
// emptyNode is the default value to unsafe.pointer as an empty pointer
var emptyNode = &fifoNode{
d: "empty",
}
// NewFIFO create a new FIFO queue
func NewFIFO() *FIFO {
// add a dummy node to the queue to avoid contention
// betweet head & tail when queue is empty
var dummyNode = fifoPool.Get().(*fifoNode)
dummyNode.d = "dummy"
dummyNode.next = unsafe.Pointer(emptyNode)
return &FIFO{
head: unsafe.Pointer(dummyNode),
tail: unsafe.Pointer(dummyNode),
dummy: unsafe.Pointer(dummyNode),
}
}
// Put put an data into queue's tail
func (f *FIFO) Put(d any) {
newNode := fifoPool.Get().(*fifoNode)
// for {
// newNode = fifoPool.Get().(*fifoNode)
// if newNode.AddRef(1) == 1 {
// break
// }
// runtime.Gosched()
// continue
// }
newNode.d = d
newNode.next = unsafe.Pointer(emptyNode)
newAddr := unsafe.Pointer(newNode)
var tailAddr unsafe.Pointer
for {
tailAddr = atomic.LoadPointer(&f.tail)
tailNode := (*fifoNode)(tailAddr)
if atomic.CompareAndSwapPointer(&tailNode.next, unsafe.Pointer(emptyNode), newAddr) {
atomic.AddInt64(&f.len, 1)
break
}
// tail may not be the exact tail node, so we need to check again
atomic.CompareAndSwapPointer(&f.tail, tailAddr, atomic.LoadPointer(&tailNode.next))
}
atomic.CompareAndSwapPointer(&f.tail, tailAddr, newAddr)
}
// Get pop data from the head of queue
func (f *FIFO) Get() any {
for {
headAddr := atomic.LoadPointer(&f.head)
headNode := (*fifoNode)(headAddr)
// if !headNode.CompareAndAdd(1) {
// // someone already get this node from pool
// runtime.Gosched()
// continue
// }
nextAddr := atomic.LoadPointer(&headNode.next)
if nextAddr == unsafe.Pointer(emptyNode) {
// queue is empty
return nil
}
nextNode := (*fifoNode)(nextAddr)
if atomic.CompareAndSwapPointer(&f.head, headAddr, nextAddr) {
// do not release refcnt
atomic.AddInt64(&f.len, -1)
// atomic.StoreInt32(&headNode.refcnt, 0)
// fifoPool.Put(headNode)
return nextNode.d
}
}
}
// Len return the length of queue
func (f *FIFO) Len() int {
return int(atomic.LoadInt64(&f.len))
}