This commit is contained in:
Jae Kwon
2014-07-01 14:50:24 -07:00
parent fa07748d23
commit c40fc65e6b
41 changed files with 3176 additions and 2938 deletions
+31 -28
View File
@@ -1,45 +1,48 @@
package common
import (
"time"
"sync"
"sync"
"time"
)
/* Debouncer */
type Debouncer struct {
Ch chan struct{}
quit chan struct{}
dur time.Duration
mtx sync.Mutex
timer *time.Timer
Ch chan struct{}
quit chan struct{}
dur time.Duration
mtx sync.Mutex
timer *time.Timer
}
func NewDebouncer(dur time.Duration) *Debouncer {
var timer *time.Timer
var ch = make(chan struct{})
var quit = make(chan struct{})
var mtx sync.Mutex
fire := func() {
go func() {
select {
case ch <- struct{}{}:
case <-quit:
}
}()
mtx.Lock(); defer mtx.Unlock()
timer.Reset(dur)
}
timer = time.AfterFunc(dur, fire)
return &Debouncer{Ch:ch, dur:dur, quit:quit, mtx:mtx, timer:timer}
var timer *time.Timer
var ch = make(chan struct{})
var quit = make(chan struct{})
var mtx sync.Mutex
fire := func() {
go func() {
select {
case ch <- struct{}{}:
case <-quit:
}
}()
mtx.Lock()
defer mtx.Unlock()
timer.Reset(dur)
}
timer = time.AfterFunc(dur, fire)
return &Debouncer{Ch: ch, dur: dur, quit: quit, mtx: mtx, timer: timer}
}
func (d *Debouncer) Reset() {
d.mtx.Lock(); defer d.mtx.Unlock()
d.timer.Reset(d.dur)
d.mtx.Lock()
defer d.mtx.Unlock()
d.timer.Reset(d.dur)
}
func (d *Debouncer) Stop() bool {
d.mtx.Lock(); defer d.mtx.Unlock()
close(d.quit)
return d.timer.Stop()
d.mtx.Lock()
defer d.mtx.Unlock()
close(d.quit)
return d.timer.Stop()
}
+28 -29
View File
@@ -1,28 +1,28 @@
package common
import (
"container/heap"
"container/heap"
)
type Heap struct {
pq priorityQueue
pq priorityQueue
}
func NewHeap() *Heap {
return &Heap{pq:make([]*pqItem, 0)}
return &Heap{pq: make([]*pqItem, 0)}
}
func (h *Heap) Len() int {
return len(h.pq)
return len(h.pq)
}
func (h *Heap) Push(value interface{}, priority int) {
heap.Push(&h.pq, &pqItem{value:value, priority:priority})
heap.Push(&h.pq, &pqItem{value: value, priority: priority})
}
func (h *Heap) Pop() interface{} {
item := heap.Pop(&h.pq).(*pqItem)
return item.value
item := heap.Pop(&h.pq).(*pqItem)
return item.value
}
/*
@@ -43,9 +43,9 @@ func main() {
// From: http://golang.org/pkg/container/heap/#example__priorityQueue
type pqItem struct {
value interface{}
priority int
index int
value interface{}
priority int
index int
}
type priorityQueue []*pqItem
@@ -53,35 +53,34 @@ type priorityQueue []*pqItem
func (pq priorityQueue) Len() int { return len(pq) }
func (pq priorityQueue) Less(i, j int) bool {
return pq[i].priority < pq[j].priority
return pq[i].priority < pq[j].priority
}
func (pq priorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *priorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*pqItem)
item.index = n
*pq = append(*pq, item)
n := len(*pq)
item := x.(*pqItem)
item.index = n
*pq = append(*pq, item)
}
func (pq *priorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
old := *pq
n := len(old)
item := old[n-1]
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}
func (pq *priorityQueue) Update(item *pqItem, value interface{}, priority int) {
heap.Remove(pq, item.index)
item.value = value
item.priority = priority
heap.Push(pq, item)
heap.Remove(pq, item.index)
item.value = value
item.priority = priority
heap.Push(pq, item)
}
+2 -2
View File
@@ -1,9 +1,9 @@
package common
import (
"fmt"
"fmt"
)
func Panicf(s string, args ...interface{}) {
panic(fmt.Sprintf(s, args...))
panic(fmt.Sprintf(s, args...))
}