本文整理汇总了Golang中container/heap.Fix函数的典型用法代码示例。如果您正苦于以下问题:Golang Fix函数的具体用法?Golang Fix怎么用?Golang Fix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Fix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestPush
func TestPush(t *testing.T) {
h := &uint64Heap{}
heap.Init(h)
e := elem{val: 5}
heap.Push(h, e)
e.val = 3
heap.Push(h, e)
e.val = 4
heap.Push(h, e)
require.Equal(t, h.Len(), 3)
require.EqualValues(t, (*h)[0].val, 3)
e.val = 10
(*h)[0] = e
heap.Fix(h, 0)
require.EqualValues(t, (*h)[0].val, 4)
e.val = 11
(*h)[0] = e
heap.Fix(h, 0)
require.EqualValues(t, (*h)[0].val, 5)
e = heap.Pop(h).(elem)
require.EqualValues(t, e.val, 5)
e = heap.Pop(h).(elem)
require.EqualValues(t, e.val, 10)
e = heap.Pop(h).(elem)
require.EqualValues(t, e.val, 11)
require.Equal(t, h.Len(), 0)
}
示例2: swapOrder
func (ph *peerHeap) swapOrder(i, j int) {
if i == j {
return
}
ph.peerScores[i].order, ph.peerScores[j].order = ph.peerScores[j].order, ph.peerScores[i].order
heap.Fix(ph, i)
heap.Fix(ph, j)
}
示例3: Next
// Next returns ErrIteratorDone if the iterator is done.
func (iter *iterator) Next() error {
if len(iter.cursors) <= 0 {
return ErrIteratorDone
}
lastK := iter.cursors[0].k
for len(iter.cursors) > 0 {
next := iter.cursors[0]
if next.ssIndex < 0 && next.pos < 0 {
err := iter.lowerLevelIter.Next()
if err == nil {
next.k, next.v, err = iter.lowerLevelIter.Current()
if err == nil && len(iter.cursors) > 1 {
heap.Fix(iter, 0)
}
}
if err != nil {
iter.lowerLevelIter.Close()
iter.lowerLevelIter = nil
heap.Pop(iter)
}
} else {
next.pos++
if next.pos >= next.posEnd {
heap.Pop(iter)
} else {
next.op, next.k, next.v =
iter.ss.a[next.ssIndex].GetOperationKeyVal(next.pos)
if next.op == 0 {
heap.Pop(iter)
} else if len(iter.cursors) > 1 {
heap.Fix(iter, 0)
}
}
}
if len(iter.cursors) <= 0 {
return ErrIteratorDone
}
if !iteratorBytesEqual(iter.cursors[0].k, lastK) {
if !iter.iteratorOptions.IncludeDeletions &&
iter.cursors[0].op == OperationDel {
return iter.Next()
}
return nil
}
}
return ErrIteratorDone
}
示例4: Insert
// Insert adds an element to the stream to be tracked
func (s *Stream) Insert(x string, count int) error {
h := fnv.New32a()
_, err := h.Write([]byte(x))
if err != nil {
return err
}
xhash := int(h.Sum32()) % len(s.Alphas)
// are we tracking this element?
if idx, ok := s.K.M[x]; ok {
s.K.Elts[idx].Count += count
heap.Fix(&s.K, idx)
return nil
}
// can we track more elements?
if len(s.K.Elts) < s.N {
// there is free space
heap.Push(&s.K, Element{Key: x, Count: count})
return nil
}
if s.Alphas[xhash]+count < s.K.Elts[0].Count {
s.Alphas[xhash] += count
return nil
}
// replace the current minimum element
minKey := s.K.Elts[0].Key
h.Reset()
_, err = h.Write([]byte(minKey))
if err != nil {
return err
}
mkhash := int(h.Sum32()) % len(s.Alphas)
s.Alphas[mkhash] = s.K.Elts[0].Count
s.K.Elts[0].Key = x
s.K.Elts[0].Error = s.Alphas[xhash]
s.K.Elts[0].Count = s.Alphas[xhash] + count
// we're not longer monitoring minKey
delete(s.K.M, minKey)
// but 'x' is as array position 0
s.K.M[x] = 0
heap.Fix(&s.K, 0)
return nil
}
示例5: Grow
func (h *ResultHeap) Grow(x Result) {
docId := x.Posting.DocId
if i, ok := h.index[docId]; ok {
h.rank[i] = x
} else if h.Len() < h.cap {
h.Push(x)
heap.Fix(h, h.Len()-1)
} else if h.rank[0].Score < x.Score {
oldDocId := h.rank[0].Posting.DocId
h.rank[0] = x
delete(h.index, oldDocId)
h.index[docId] = 0
heap.Fix(h, 0)
}
}
示例6: TestPush
func TestPush(t *testing.T) {
h := &Uint64Heap{}
heap.Init(h)
e := Elem{Uid: 5}
heap.Push(h, e)
e.Uid = 3
heap.Push(h, e)
e.Uid = 4
heap.Push(h, e)
if h.Len() != 3 {
t.Errorf("Expected len 3. Found: %v", h.Len())
}
if (*h)[0].Uid != 3 {
t.Errorf("Expected min 3. Found: %+v", (*h)[0])
}
e.Uid = 10
(*h)[0] = e
heap.Fix(h, 0)
if (*h)[0].Uid != 4 {
t.Errorf("Expected min 4. Found: %+v", (*h)[0])
}
e.Uid = 11
(*h)[0] = e
heap.Fix(h, 0)
if (*h)[0].Uid != 5 {
t.Errorf("Expected min 5. Found: %+v", (*h)[0])
}
e = heap.Pop(h).(Elem)
if e.Uid != 5 {
t.Errorf("Expected min 5. Found %+v", e)
}
e = heap.Pop(h).(Elem)
if e.Uid != 10 {
t.Errorf("Expected min 10. Found: %+v", e)
}
e = heap.Pop(h).(Elem)
if e.Uid != 11 {
t.Errorf("Expected min 11. Found: %+v", e)
}
if h.Len() != 0 {
t.Errorf("Expected len 0. Found: %v, values: %+v", h.Len(), h)
}
}
示例7: DecreaseKey
func (th *TriangleHeap) DecreaseKey(id int32, weight uint32) {
if index, ok := th.indices[id]; ok {
th.triangles[index].weight = weight
heap.Fix(th, index)
return
}
}
示例8: Touch
// Mark an event happening, using given timestamp.
//
// The implementation assumes time is monotonic, the behaviour is undefined in
// the case of time going back. This operation has logarithmic complexity.
func (ss *Rate) Touch(key string, nowTs time.Time) {
now := nowTs.UnixNano()
var bucket *bucket
if bucketno, found := ss.keytobucketno[key]; found {
bucket = &ss.buckets[bucketno]
} else {
bucketno = uint32(ss.sh.h[0])
bucket = &ss.buckets[bucketno]
delete(ss.keytobucketno, bucket.key)
ss.keytobucketno[key] = bucketno
bucket.key, bucket.errLastTs, bucket.errRate =
key, bucket.lastTs, bucket.rate
}
if bucket.lastTs != 0 {
bucket.rate = ss.count(bucket.rate, bucket.lastTs, now)
}
bucket.lastTs = now
// Even lastTs change may change ordering.
heap.Fix(&ss.sh, int(bucket.idx))
}
示例9: Fix
func (pq *aStarPriorityQueue) Fix(id int, newGScore, newFScore float64) {
if i, ok := pq.indexList[id]; ok {
pq.nodes[i].gscore = newGScore
pq.nodes[i].fscore = newFScore
heap.Fix(pq, i)
}
}
示例10: putWorker
// putWorker puts a worker back in the worker pool.
func (p *Pool) putWorker(w *worker) {
p.mu.Lock()
defer p.mu.Unlock()
w.pending--
// Reorder the queue based on the load of the workers.
heap.Fix(&p.workers, w.index)
}
示例11: updateNode
// updateNode sets the number of tasks for a given node. It ignores the update
// if the node isn't already tracked in the heap.
func (nh *nodeHeap) updateNode(n NodeInfo) {
index, ok := nh.index[n.ID]
if ok {
nh.heap[index] = n
heap.Fix(nh, index)
}
}
示例12: Put
// Add adds a reply to the cache.
// When `ttl` is equal to `nullTTL`, the cache entry will be valid until the closest TTL in the `reply`
func (c *Cache) Put(request *dns.Msg, reply *dns.Msg, ttl int, flags uint8) int {
c.lock.Lock()
defer c.lock.Unlock()
now := c.clock.Now()
question := request.Question[0]
key := cacheKey(question)
ent, found := c.entries[key]
if found {
updated := ent.setReply(reply, ttl, flags, now)
if updated {
heap.Fix(&c.entriesH, ent.index)
}
} else {
// If we will add a new item and the capacity has been exceeded, make some room...
if len(c.entriesH) >= c.capacity {
lowestEntry := heap.Pop(&c.entriesH).(*cacheEntry)
delete(c.entries, cacheKey(lowestEntry.question))
}
ent = newCacheEntry(&question, now)
ent.setReply(reply, ttl, flags, now)
heap.Push(&c.entriesH, ent)
c.entries[key] = ent
}
return ent.ReplyLen
}
示例13: enqueue
// enqueue either adds the detail to the queue or updates its location in the
// priority queue.
func (pq *storePoolPQ) enqueue(detail *storeDetail) {
if detail.index < 0 {
heap.Push(pq, detail)
} else {
heap.Fix(pq, detail.index)
}
}
示例14: run
func (d *DelayingDeliverer) run() {
for {
now := time.Now()
d.deliver(now)
nextWakeUp := now.Add(time.Hour)
if d.heap.Len() > 0 {
nextWakeUp = d.heap.data[0].DeliveryTime
}
sleepTime := nextWakeUp.Sub(now)
select {
case <-time.After(sleepTime):
break // just wake up and process the data
case item := <-d.updateChannel:
if position, found := d.heap.keyPosition[item.Key]; found {
if item.DeliveryTime.Before(d.heap.data[position].DeliveryTime) {
d.heap.data[position] = item
heap.Fix(d.heap, position)
}
// Ignore if later.
} else {
heap.Push(d.heap, item)
}
case <-d.stopChannel:
return
}
}
}
示例15: advance
// advance advances each iterator in the set to the next value for which *any*
// interpolatingIterator has a real value.
func (is unionIterator) advance() {
if !is.isValid() {
return
}
// All iterators in the set currently point to the same offset. Advancement
// begins by pre-advancing any iterators that have a real value for the
// current offset.
current := is[0].offset
for is[0].offset == current {
is[0].advanceTo(current + 1)
heap.Fix(&is, 0)
}
// It is possible that all iterators are now invalid.
if !is.isValid() {
return
}
// The iterator in position zero now has the lowest value for
// nextReal.offset - advance all iterators to that offset.
min := is[0].nextReal.offset
for i := range is {
is[i].advanceTo(min)
}
heap.Init(&is)
}