saving development state...

This commit is contained in:
Jae Kwon
2014-08-30 04:17:10 -07:00
parent 50544c50af
commit d300a67bb1
46 changed files with 2458 additions and 1058 deletions
+10
View File
@@ -0,0 +1,10 @@
package common
import (
"errors"
"fmt"
)
func Errorf(s string, args ...interface{}) error {
return errors.New(fmt.Sprintf(s, args...))
}
+81 -24
View File
@@ -2,30 +2,12 @@ package common
import (
"container/heap"
"sync"
)
type Heap struct {
pq priorityQueue
}
func NewHeap() *Heap {
return &Heap{pq: make([]*pqItem, 0)}
}
func (h *Heap) Len() int64 {
return len(h.pq)
}
func (h *Heap) Push(value interface{}, priority int64) {
heap.Push(&h.pq, &pqItem{value: value, priority: priority})
}
func (h *Heap) Pop() interface{} {
item := heap.Pop(&h.pq).(*pqItem)
return item.value
}
/*
Example usage:
func main() {
h := NewHeap()
@@ -39,12 +21,87 @@ func main() {
}
*/
type Comparable interface {
Less(o interface{}) bool
}
//-----------------------------------------------------------------------------
type Heap struct {
pq priorityQueue
}
func NewHeap() *Heap {
return &Heap{pq: make([]*pqItem, 0)}
}
func (h *Heap) Len() int64 {
return int64(len(h.pq))
}
func (h *Heap) Push(value interface{}, priority Comparable) {
heap.Push(&h.pq, &pqItem{value: value, priority: priority})
}
func (h *Heap) Peek() interface{} {
if len(h.pq) == 0 {
return nil
}
return h.pq[0].value
}
func (h *Heap) Pop() interface{} {
item := heap.Pop(&h.pq).(*pqItem)
return item.value
}
//-----------------------------------------------------------------------------
type CHeap struct {
mtx sync.Mutex
pq priorityQueue
}
func NewCHeap() *CHeap {
return &CHeap{pq: make([]*pqItem, 0)}
}
func (h *CHeap) Len() int64 {
h.mtx.Lock()
defer h.mtx.Unlock()
return int64(len(h.pq))
}
func (h *CHeap) Push(value interface{}, priority Comparable) {
h.mtx.Lock()
defer h.mtx.Unlock()
heap.Push(&h.pq, &pqItem{value: value, priority: priority})
}
func (h *CHeap) Peek() interface{} {
h.mtx.Lock()
defer h.mtx.Unlock()
if len(h.pq) == 0 {
return nil
}
return h.pq[0].value
}
func (h *CHeap) Pop() interface{} {
h.mtx.Lock()
defer h.mtx.Unlock()
item := heap.Pop(&h.pq).(*pqItem)
return item.value
}
//-----------------------------------------------------------------------------
///////////////////////
// From: http://golang.org/pkg/container/heap/#example__priorityQueue
type pqItem struct {
value interface{}
priority int64
priority Comparable
index int
}
@@ -53,7 +110,7 @@ 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.Less(pq[j].priority)
}
func (pq priorityQueue) Swap(i, j int) {
@@ -78,7 +135,7 @@ func (pq *priorityQueue) Pop() interface{} {
return item
}
func (pq *priorityQueue) Update(item *pqItem, value interface{}, priority int64) {
func (pq *priorityQueue) Update(item *pqItem, value interface{}, priority Comparable) {
heap.Remove(pq, item.index)
item.value = value
item.priority = priority
+2 -2
View File
@@ -17,11 +17,11 @@ func NewRepeatTimer(dur time.Duration) *RepeatTimer {
var ch = make(chan struct{})
var quit = make(chan struct{})
var t = &RepeatTimer{Ch: ch, dur: dur, quit: quit}
t.timer = time.AfterFunc(dur, t.fireHandler)
t.timer = time.AfterFunc(dur, t.fireRoutine)
return t
}
func (t *RepeatTimer) fireHandler() {
func (t *RepeatTimer) fireRoutine() {
select {
case t.Ch <- struct{}{}:
t.timer.Reset(t.dur)
+2 -2
View File
@@ -23,12 +23,12 @@ func NewThrottleTimer(dur time.Duration) *ThrottleTimer {
var ch = make(chan struct{})
var quit = make(chan struct{})
var t = &ThrottleTimer{Ch: ch, dur: dur, quit: quit}
t.timer = time.AfterFunc(dur, t.fireHandler)
t.timer = time.AfterFunc(dur, t.fireRoutine)
t.timer.Stop()
return t
}
func (t *ThrottleTimer) fireHandler() {
func (t *ThrottleTimer) fireRoutine() {
select {
case t.Ch <- struct{}{}:
atomic.StoreUint32(&t.isSet, 0)