github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/algo/heap.go (about)

     1  /*
     2   * Copyright 2016-2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package algo
    18  
    19  type elem struct {
    20  	val     uint64 // Value of this element.
    21  	listIdx int    // Which list this element comes from.
    22  }
    23  
    24  type uint64Heap []elem
    25  
    26  func (h uint64Heap) Len() int           { return len(h) }
    27  func (h uint64Heap) Less(i, j int) bool { return h[i].val < h[j].val }
    28  func (h uint64Heap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
    29  func (h *uint64Heap) Push(x interface{}) {
    30  	*h = append(*h, x.(elem))
    31  }
    32  
    33  func (h *uint64Heap) Pop() interface{} {
    34  	old := *h
    35  	n := len(old)
    36  	x := old[n-1]
    37  	*h = old[0 : n-1]
    38  	return x
    39  }