forked from JaderDias/movingmedian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
movingmedian.go
147 lines (121 loc) · 3.2 KB
/
movingmedian.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
// Package movingmedian computes the median of a windowed stream of data.
package movingmedian
import "container/heap"
type item struct {
f float64
heapIndex int
}
type itemHeap []*item
func (h itemHeap) Len() int { return len(h) }
func (h itemHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
h[i].heapIndex = i
h[j].heapIndex = j
}
func (h *itemHeap) Push(x interface{}) {
e := x.(*item)
e.heapIndex = len(*h)
*h = append(*h, e)
}
func (h *itemHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
type minItemHeap struct {
itemHeap
}
func (h minItemHeap) Less(i, j int) bool { return h.itemHeap[i].f < h.itemHeap[j].f }
type maxItemHeap struct {
itemHeap
}
func (h maxItemHeap) Less(i, j int) bool { return h.itemHeap[i].f > h.itemHeap[j].f }
// MovingMedian computes the moving median of a windowed stream of numbers.
type MovingMedian struct {
queueIndex int
nitems int
queue []item
maxHeap maxItemHeap
minHeap minItemHeap
}
// NewMovingMedian returns a MovingMedian with the given window size.
func NewMovingMedian(size int) MovingMedian {
m := MovingMedian{
queue: make([]item, size),
maxHeap: maxItemHeap{},
minHeap: minItemHeap{},
}
heap.Init(&m.maxHeap)
heap.Init(&m.minHeap)
return m
}
// Push adds an element to the stream, removing old data which has expired from the window. It runs in O(log windowSize).
func (m *MovingMedian) Push(v float64) {
if len(m.queue) == 1 {
m.queue[0].f = v
return
}
itemPtr := &m.queue[m.queueIndex]
m.queueIndex++
if m.queueIndex >= len(m.queue) {
m.queueIndex = 0
}
if m.nitems == len(m.queue) {
minAbove := m.minHeap.itemHeap[0].f
maxBelow := m.maxHeap.itemHeap[0].f
itemPtr.f = v
if itemPtr.heapIndex < m.minHeap.Len() && itemPtr == m.minHeap.itemHeap[itemPtr.heapIndex] {
if v >= maxBelow {
heap.Fix(&m.minHeap, itemPtr.heapIndex)
return
}
rotate(&m.maxHeap, &m.minHeap, m.maxHeap.itemHeap, m.minHeap.itemHeap, itemPtr)
return
}
if v <= minAbove {
heap.Fix(&m.maxHeap, itemPtr.heapIndex)
return
}
rotate(&m.minHeap, &m.maxHeap, m.minHeap.itemHeap, m.maxHeap.itemHeap, itemPtr)
return
}
m.nitems++
itemPtr.f = v
if m.minHeap.Len() == 0 || v > m.minHeap.itemHeap[0].f {
heap.Push(&m.minHeap, itemPtr)
rebalance(&m.minHeap, &m.maxHeap)
} else {
heap.Push(&m.maxHeap, itemPtr)
rebalance(&m.maxHeap, &m.minHeap)
}
}
func rebalance(heapA, heapB heap.Interface) {
if heapA.Len() == (heapB.Len() + 2) {
moveItem := heap.Pop(heapA)
heap.Push(heapB, moveItem)
}
}
func rotate(heapA, heapB heap.Interface, itemHeapA, itemHeapB itemHeap, itemPtr *item) {
moveItem := itemHeapA[0]
moveItem.heapIndex = itemPtr.heapIndex
itemHeapB[itemPtr.heapIndex] = moveItem
itemHeapA[0] = itemPtr
heap.Fix(heapB, itemPtr.heapIndex)
itemPtr.heapIndex = 0
heap.Fix(heapA, 0)
}
// Median returns the current value of the median from the window.
func (m *MovingMedian) Median() float64 {
if len(m.queue) == 1 {
return m.queue[0].f
}
if m.maxHeap.Len() == m.minHeap.Len() {
return (m.maxHeap.itemHeap[0].f + m.minHeap.itemHeap[0].f) / 2
}
if m.maxHeap.Len() > m.minHeap.Len() {
return m.maxHeap.itemHeap[0].f
}
return m.minHeap.itemHeap[0].f
}