• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang heap.Interface类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中container/heap.Interface的典型用法代码示例。如果您正苦于以下问题:Golang Interface类的具体用法?Golang Interface怎么用?Golang Interface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Interface类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: siftUpMin

func siftUpMin(h heap.Interface, i int) {
	for gp := parent(parent(i)); gp >= 0; i, gp = gp, parent(parent(gp)) {
		if h.Less(gp, i) {
			break
		}
		h.Swap(i, gp)
	}
}
开发者ID:pombredanne,项目名称:algo,代码行数:8,代码来源:doubleheap.go


示例2: minGrandchild

func minGrandchild(h heap.Interface, i, n int) int {
	cg := (i*2+1)*2 + 1
	m := cg
	for i := 1; i <= 3; i++ {
		if h.Less(cg+i, m) {
			m = cg + i
		}
	}
	return m
}
开发者ID:pombredanne,项目名称:algo,代码行数:10,代码来源:doubleheap.go


示例3: minTwoGen

// Index of minimum of children and grandchildren (if any) of i.
// Precondition: i has at least one child.
func minTwoGen(h heap.Interface, i, n int) int {
	c1, c2 := i*2+1, i*2+2
	m := c1
	for _, k := range []int{c2, c1*2 + 1, c1*2 + 2, c2*2 + 1, c2*2 + 2} {
		if k >= n {
			break
		}
		if h.Less(k, m) {
			m = k
		}
	}
	return m
}
开发者ID:pombredanne,项目名称:algo,代码行数:15,代码来源:doubleheap.go


示例4: 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
				}
			})
		})
	})
}
开发者ID:devsaurin,项目名称:mongo-tools,代码行数:50,代码来源:intent_prioritizer_test.go


示例5: 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
}
开发者ID:pombredanne,项目名称:algo,代码行数:17,代码来源:doubleheap.go


示例6: 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
}
开发者ID:pombredanne,项目名称:algo,代码行数:16,代码来源:doubleheap.go


示例7: 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
}
开发者ID:pombredanne,项目名称:algo,代码行数:8,代码来源:doubleheap.go


示例8: siftUp

func siftUp(h heap.Interface, i int) {
	if minLevel(i) {
		if i > 0 && h.Less(parent(i), i) {
			h.Swap(i, parent(i))
			siftUpMax(h, parent(i))
		} else {
			siftUpMin(h, i)
		}
	} else {
		if i > 0 && h.Less(i, parent(i)) {
			h.Swap(i, parent(i))
			siftUpMin(h, parent(i))
		} else {
			siftUpMax(h, i)
		}
	}
}
开发者ID:pombredanne,项目名称:algo,代码行数:17,代码来源:doubleheap.go


示例9: Push

// Adds x to the heap x.
func Push(h heap.Interface, x interface{}) {
	h.Push(x)
	siftUp(h, h.Len()-1)
}
开发者ID:pombredanne,项目名称:algo,代码行数:5,代码来源:doubleheap.go


示例10: 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)
	}
}
开发者ID:pombredanne,项目名称:algo,代码行数:13,代码来源:doubleheap.go


示例11: siftDownMin

func siftDownMin(h heap.Interface, i, n int) {
	for (i*2+2)*2+2 < n { // has four grandchildren
		m := minGrandchild(h, i, n)
		if h.Less(i, m) {
			return
		}
		h.Swap(i, m)
		if h.Less(parent(m), m) {
			h.Swap(m, parent(m))
		}
		i = m
	}

	if hasChild(i, n) {
		m := minTwoGen(h, i, n)
		if m > i*2+2 { // must be a grandchild
			if h.Less(m, i) {
				h.Swap(i, m)
				if h.Less(parent(m), m) {
					h.Swap(m, parent(m))
				}
				siftDownMin(h, m, n)
			}
		} else if h.Less(m, i) {
			h.Swap(i, m)
		}
	}
}
开发者ID:pombredanne,项目名称:algo,代码行数:28,代码来源:doubleheap.go


示例12: siftDownMax

// Iterative version after Bojesen, "Heap implementations and variations",
// http://www.diku.dk/forskning/performance-engineering/Jesper/heaplab/heapsurvey_html/node11.html
func siftDownMax(h heap.Interface, i, n int) {
	for (i*2+2)*2+2 < n { // has four grandchildren
		m := maxGrandchild(h, i, n)
		if h.Less(m, i) {
			return
		}
		h.Swap(i, m)
		if h.Less(m, parent(m)) {
			h.Swap(m, parent(m))
		}
		i = m
	}

	// XXX Following is from the original paper; couldn't get Bojesen's version
	// to work.
	if hasChild(i, n) {
		m := maxTwoGen(h, i, n)
		if m > i*2+2 { // must be a grandchild
			if h.Less(i, m) {
				h.Swap(i, m)
				if h.Less(m, parent(m)) {
					h.Swap(m, parent(m))
				}
				siftDownMax(h, m, n)
			}
		} else if h.Less(i, m) {
			h.Swap(i, m)
		}
	}
}
开发者ID:pombredanne,项目名称:algo,代码行数:32,代码来源:doubleheap.go



注:本文中的container/heap.Interface类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang list.Front函数代码示例发布时间:2022-05-24
下一篇:
Golang heap.Remove函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap