-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderqueue.go
69 lines (61 loc) · 1.11 KB
/
orderqueue.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
package orderbook
import (
"fmt"
)
// OrderQueue ...
type OrderQueue struct {
_padding0 [8]uint64
r uint64 // next position to read
_padding1 [8]uint64
w uint64 // next position to write
_padding2 [8]uint64
len uint64
_padding3 [8]uint64
cap uint64
_padding4 [8]uint64
mask uint64
_padding5 [8]uint64
contents []*Order
_padding6 [8]uint64
}
// NewOrderQueue ...
func NewOrderQueue(size uint64) *OrderQueue {
return &OrderQueue{
cap: size,
mask: size - 1,
contents: make([]*Order, int(size)),
}
}
// Put ...
func (q *OrderQueue) Put(value *Order) bool {
if q.len == q.cap || (q.w == q.r && q.len > 0) {
return false
}
q.contents[q.w&q.mask] = value
q.w++
q.len++
return true
}
// Get ...
func (q *OrderQueue) Get() *Order {
if q.r == q.w {
return nil
}
content := q.contents[q.r&q.mask]
q.r++
q.len--
return content
}
// Reset ...
func (q *OrderQueue) Reset() {
q.len = 0
q.r = 0
q.w = 0
}
// Len ...
func (q *OrderQueue) Len() int {
return int(q.len)
}
func (q *OrderQueue) String() string {
return fmt.Sprintf("Read: %d, Write: %d", q.r, q.w)
}