-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
65 lines (52 loc) · 1.13 KB
/
transactions.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
package main
// Insert inserts a given tx into the Transactions structure
// with the correct time ordering
func (t *Transactions) Insert(tx *Transaction) int {
t.Lock()
defer t.Unlock()
l := len(t.T)
// Trivial case O(1)
if l == 0 {
t.T = append(t.T, tx)
return 0
}
// Given TX becomes the oldest O(n)
if tx.Timestamp.Before(t.T[0].Timestamp) {
t.T = insertTransaction(t.T, 0, tx)
return 0
}
// Given TX becomes the newest O(1)
if tx.Timestamp.After(t.T[l-1].Timestamp) {
t.T = append(t.T, tx)
return l - 1
}
// If not one of the trivial cases
// we follow with a binary search algorithm
low := 0
high := l - 1
var (
mid int
pos int
)
for {
// The integer division floors the result
mid = low + (high-low)/2
if tx.Timestamp.After(t.T[mid].Timestamp) {
low = mid
} else {
high = mid
}
// If we found a space of length one
// we already done
if high-low <= 1 {
if tx.Timestamp.After(t.T[low].Timestamp) {
pos = high
} else {
pos = low
}
break
}
}
t.T = insertTransaction(t.T, pos, tx)
return pos
}