本文整理匯總了Golang中container/heap.Interface.Len方法的典型用法代碼示例。如果您正苦於以下問題:Golang Interface.Len方法的具體用法?Golang Interface.Len怎麽用?Golang Interface.Len使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類container/heap.Interface
的用法示例。
在下文中一共展示了Interface.Len方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PopMin
// Removes and returns the minimum element of h.
func PopMin(h heap.Interface) interface{} {
n := h.Len() - 1
h.Swap(0, n)
x := h.Pop()
siftDownMin(h, 0, n)
return x
}
示例2: TestBasicDBHeapBehavior
func TestBasicDBHeapBehavior(t *testing.T) {
var dbheap heap.Interface
testutil.VerifyTestType(t, testutil.UnitTestType)
Convey("With an empty dbHeap", t, func() {
dbheap = &DBHeap{}
heap.Init(dbheap)
Convey("when inserting unordered dbCounters with different active counts", func() {
heap.Push(dbheap, &dbCounter{75, nil})
heap.Push(dbheap, &dbCounter{121, nil})
heap.Push(dbheap, &dbCounter{76, nil})
heap.Push(dbheap, &dbCounter{51, nil})
heap.Push(dbheap, &dbCounter{82, nil})
heap.Push(dbheap, &dbCounter{117, nil})
heap.Push(dbheap, &dbCounter{49, nil})
heap.Push(dbheap, &dbCounter{101, nil})
heap.Push(dbheap, &dbCounter{122, nil})
heap.Push(dbheap, &dbCounter{33, nil})
heap.Push(dbheap, &dbCounter{0, nil})
Convey("they should pop in active order, least to greatest", func() {
prev := -1
for dbheap.Len() > 0 {
popped := heap.Pop(dbheap).(*dbCounter)
So(popped.active, ShouldBeGreaterThan, prev)
prev = popped.active
}
})
})
Convey("when inserting unordered dbCounters with different bson sizes", func() {
heap.Push(dbheap, &dbCounter{0, []*Intent{&Intent{Size: 70}}})
heap.Push(dbheap, &dbCounter{0, []*Intent{&Intent{Size: 1024}}})
heap.Push(dbheap, &dbCounter{0, []*Intent{&Intent{Size: 97}}})
heap.Push(dbheap, &dbCounter{0, []*Intent{&Intent{Size: 3}}})
heap.Push(dbheap, &dbCounter{0, []*Intent{&Intent{Size: 1024 * 1024}}})
Convey("they should pop in bson size order, greatest to least", func() {
prev := int64(1024*1024 + 1) // Maximum
for dbheap.Len() > 0 {
popped := heap.Pop(dbheap).(*dbCounter)
So(popped.collections[0].Size, ShouldBeLessThan, prev)
prev = popped.collections[0].Size
}
})
})
})
}
示例3: MaxInd
// Index of maximum element in h.
//
// There's no MinInd because the minimum of a heap is always at index zero.
func MaxInd(h heap.Interface) (maxind int) {
switch h.Len() {
case 1:
maxind = 0
case 2:
maxind = 1
default:
maxind = 1
if h.Less(1, 2) {
maxind = 2
}
}
return
}
示例4: PopMax
// Removes and returns the maximum element of h.
func PopMax(h heap.Interface) interface{} {
n := h.Len()
if n <= 2 {
return h.Pop()
}
i := 1
if h.Less(1, 2) {
i = 2
}
h.Swap(i, n-1)
x := h.Pop()
siftDownMax(h, i, n-1)
return x
}
示例5: Push
// Adds x to the heap x.
func Push(h heap.Interface, x interface{}) {
h.Push(x)
siftUp(h, h.Len()-1)
}
示例6: Init
// Establishes heap order in h.
//
// Every non-empty heap must be initialized using this function before any of
// the other functions of this package is used on it.
//
// Note that, although this package uses the interface from container/heap,
// you must not mix operations from both packages.
func Init(h heap.Interface) {
n := h.Len()
for i := parent(n); i >= 0; i-- {
siftDown(h, i, n)
}
}