From 84369286dfbcb3e3816cf8de488bd878bb1a32dc Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 13 Mar 2020 17:09:37 +0000 Subject: [PATCH] vendor: add github.com/aalpar/deheap --- go.mod | 1 + go.sum | 3 + vendor/github.com/aalpar/deheap/LICENSE.txt | 20 ++ vendor/github.com/aalpar/deheap/README.md | 14 ++ vendor/github.com/aalpar/deheap/deheap.go | 228 ++++++++++++++++++++ vendor/modules.txt | 2 + 6 files changed, 268 insertions(+) create mode 100644 vendor/github.com/aalpar/deheap/LICENSE.txt create mode 100644 vendor/github.com/aalpar/deheap/README.md create mode 100644 vendor/github.com/aalpar/deheap/deheap.go diff --git a/go.mod b/go.mod index 5104f598e..c8cf6fb93 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/Azure/go-autorest/autorest/adal v0.6.0 // indirect github.com/Unknwon/goconfig v0.0.0-20191126170842-860a72fb44fd github.com/a8m/tree v0.0.0-20181222104329-6a0b80129de4 + github.com/aalpar/deheap v0.0.0-20191229192855-f837f7a9ba26 github.com/abbot/go-http-auth v0.4.0 github.com/anacrolix/dms v1.1.0 github.com/atotto/clipboard v0.1.2 diff --git a/go.sum b/go.sum index fba59e59f..bde8a5e86 100644 --- a/go.sum +++ b/go.sum @@ -48,6 +48,8 @@ github.com/Unknwon/goconfig v0.0.0-20191126170842-860a72fb44fd h1:+CYOsXi89xOqBk github.com/Unknwon/goconfig v0.0.0-20191126170842-860a72fb44fd/go.mod h1:wngxua9XCNjvHjDiTiV26DaKDT+0c63QR6H5hjVUUxw= github.com/a8m/tree v0.0.0-20181222104329-6a0b80129de4 h1:mK1/QgFPU4osbhjJ26B1w738kjQHaGJcon8uCLMS8fk= github.com/a8m/tree v0.0.0-20181222104329-6a0b80129de4/go.mod h1:FSdwKX97koS5efgm8WevNf7XS3PqtyFkKDDXrz778cg= +github.com/aalpar/deheap v0.0.0-20191229192855-f837f7a9ba26 h1:vn0bf9tSbwrl9aBeG5hqGPpWeZGEybrB3LUJkHP/x6w= +github.com/aalpar/deheap v0.0.0-20191229192855-f837f7a9ba26/go.mod h1:EJFoWbcEEVK22GYKONJjtMNamGYe6p+3x1Pr6zV5gFs= github.com/abbot/go-http-auth v0.4.0 h1:QjmvZ5gSC7jm3Zg54DqWE/T5m1t2AfDu6QlXJT0EVT0= github.com/abbot/go-http-auth v0.4.0/go.mod h1:Cz6ARTIzApMJDzh5bRMSUou6UMSp0IEXg9km/ci7TJM= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -576,6 +578,7 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= diff --git a/vendor/github.com/aalpar/deheap/LICENSE.txt b/vendor/github.com/aalpar/deheap/LICENSE.txt new file mode 100644 index 000000000..d8ef07fed --- /dev/null +++ b/vendor/github.com/aalpar/deheap/LICENSE.txt @@ -0,0 +1,20 @@ +Copyright 2019 Aaron H. Alpar + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/aalpar/deheap/README.md b/vendor/github.com/aalpar/deheap/README.md new file mode 100644 index 000000000..a3a24f0ca --- /dev/null +++ b/vendor/github.com/aalpar/deheap/README.md @@ -0,0 +1,14 @@ +# deheap + +Package deheap provides the implementation of a doubly ended heap. +Doubly ended heaps are heaps with two sides, a min side and a max side. +Like normal single-sided heaps, elements can be pushed onto and pulled +off of a deheap. deheaps have an additional `Pop` function, `PopMax`, that +returns elements from the opposite side of the ordering. + +This implementation has emphasized compatibility with existing libraries +in the sort and heap packages. + +Performace of the deheap functions should be very close to the +performance of the functions of the heap library + diff --git a/vendor/github.com/aalpar/deheap/deheap.go b/vendor/github.com/aalpar/deheap/deheap.go new file mode 100644 index 000000000..6f53f6fea --- /dev/null +++ b/vendor/github.com/aalpar/deheap/deheap.go @@ -0,0 +1,228 @@ +// +// Copyright 2019 Aaron H. Alpar +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files +// (the "Software"), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +// +// Package deheap provides the implementation of a doubly ended heap. +// Doubly ended heaps are heaps with two sides, a min side and a max side. +// Like normal single-sided heaps, elements can be pushed onto and pulled +// off of a deheap. deheaps have an additional Pop function, PopMax, that +// returns elements from the opposite side of the ordering. +// +// This implementation has emphasized compatibility with existing libraries +// in the sort and heap packages. +// +// Performace of the deheap functions should be very close to the +// performance of the functions of the heap library +// +package deheap + +import ( + "container/heap" + "math/bits" +) + +func hparent(i int) int { + return (i - 1) / 2 +} + +func hlchild(i int) int { + return (i * 2) + 1 +} + +func parent(i int) int { + return ((i + 1) / 4) - 1 +} + +func lchild(i int) int { + return ((i + 1 ) * 4) - 1 +} + +func level(i int) int { + return bits.Len(uint(i)+1) - 1 +} + +func isMinHeap(i int) bool { + return level(i) % 2 == 0 +} + +func min4(h heap.Interface, l int, min bool, i int) int { + q := i + i++ + if i >= l { + return q + } + if min == h.Less(i, q) { + q = i + } + i++ + if i >= l { + return q + } + if min == h.Less(i, q) { + q = i + } + i++ + if i >= l { + return q + } + if min == h.Less(i, q) { + q = i + } + return q +} + +// min2 +func min2(h heap.Interface, l int, min bool, i int) int { + if i+1 >= l { + return i + } + if min != h.Less(i+1, i) { + return i + } + return i + 1 +} + +// min3 +func min3(h heap.Interface, l int, min bool, i, j, k int) int { + q := i + if j < l && h.Less(j, q) == min { + q = j + } + if k < l && h.Less(k, q) == min { + q = k + } + return q +} + +// bubbledown +func bubbledown(h heap.Interface, l int, min bool, i int) (q int, r int) { + q = i + r = i + for { + // find min of children + j := min2(h, l, min, hlchild(i)) + if j >= l { + break + } + // find min of grandchildren + k := min4(h, l, min, lchild(i)) + // swap of less than the element at i + v := min3(h, l, min, i, j, k) + if v == i || v >= l { + break + } + // v == k + q = v + h.Swap(v, i) + if v == j { + break + } + p := hparent(v) + if h.Less(p, v) == min { + h.Swap(p, v) + r = p + } + i = v + } + return q, r +} + +// bubbleup +func bubbleup(h heap.Interface, min bool, i int) (q bool) { + if i < 0 { + return false + } + j := parent(i) + for j >= 0 && min == h.Less(i, j) { + q = true + h.Swap(i, j) + i = j + j = parent(i) + } + min = !min + j = hparent(i) + for j >= 0 && min == h.Less(i, j) { + q = true + h.Swap(i, j) + i = j + j = parent(i) + } + return q +} + +// Pop the smallest value off the heap. See heap.Pop(). +// Time complexity is O(log n), where n = h.Len() +func Pop(h heap.Interface) interface{} { + l := h.Len()-1 + h.Swap(0, l) + q := h.Pop() + bubbledown(h, l, true, 0) + return q +} + +// Pop the largest value off the heap. See heap.Pop(). +// Time complexity is O(log n), where n = h.Len() +func PopMax(h heap.Interface) interface{} { + l := h.Len() + j := 0 + if l > 1 { + j = min2(h, l,false, 1) + } + l = l - 1 + h.Swap(j, l) + q := h.Pop() + bubbledown(h, l,false, j) + return q +} + +// Remove element at index i. See heap.Remove(). +// The complexity is O(log n) where n = h.Len(). +func Remove(h heap.Interface, i int) (q interface{}) { + l := h.Len() - 1 + h.Swap(i, l) + q = h.Pop() + if l != i { + q, r := bubbledown(h, l, isMinHeap(i), i) + bubbleup(h, isMinHeap(q), q) + bubbleup(h, isMinHeap(r), r) + } + return q +} + +// Push an element onto the heap. See heap.Push() +// Time complexity is O(log n), where n = h.Len() +func Push(h heap.Interface, o interface{}) { + h.Push(o) + l := h.Len() + i := l - 1 + bubbleup(h, isMinHeap(i), i) +} + +// Init initializes the heap. +// This should be called once on non-empty heaps before calling Pop(), PopMax() or Push(). See heap.Init() +func Init(h heap.Interface) { + l := h.Len() + for i := 0; i < l; i++ { + bubbleup(h, isMinHeap(i), i) + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index a7e8e167b..01d66f5d5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -12,6 +12,8 @@ github.com/Azure/azure-storage-blob-go/azblob github.com/Unknwon/goconfig # github.com/a8m/tree v0.0.0-20181222104329-6a0b80129de4 github.com/a8m/tree +# github.com/aalpar/deheap v0.0.0-20191229192855-f837f7a9ba26 +github.com/aalpar/deheap # github.com/abbot/go-http-auth v0.4.0 github.com/abbot/go-http-auth # github.com/anacrolix/dms v1.1.0