OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [container/] [heap/] [heap.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// Package heap provides heap operations for any type that implements
6
// heap.Interface. A heap is a tree with the property that each node is the
7
// highest-valued node in its subtree.
8
//
9
// A heap is a common way to impement a priority queue. To build a priority
10
// queue, implement the Heap interface with the (negative) priority as the
11
// ordering for the Less method, so Push adds items while Pop removes the
12
// highest-priority item from the queue.
13
//
14
package heap
15
 
16
import "sort"
17
 
18
// Any type that implements heap.Interface may be used as a
19
// min-heap with the following invariants (established after
20
// Init has been called or if the data is empty or sorted):
21
//
22
//      !h.Less(j, i) for 0 <= i < h.Len() and j = 2*i+1 or 2*i+2 and j < h.Len()
23
//
24
// Note that Push and Pop in this interface are for package heap's
25
// implementation to call.  To add and remove things from the heap,
26
// use heap.Push and heap.Pop.
27
type Interface interface {
28
        sort.Interface
29
        Push(x interface{}) // add x as element Len()
30
        Pop() interface{}   // remove and return element Len() - 1.
31
}
32
 
33
// A heap must be initialized before any of the heap operations
34
// can be used. Init is idempotent with respect to the heap invariants
35
// and may be called whenever the heap invariants may have been invalidated.
36
// Its complexity is O(n) where n = h.Len().
37
//
38
func Init(h Interface) {
39
        // heapify
40
        n := h.Len()
41
        for i := n/2 - 1; i >= 0; i-- {
42
                down(h, i, n)
43
        }
44
}
45
 
46
// Push pushes the element x onto the heap. The complexity is
47
// O(log(n)) where n = h.Len().
48
//
49
func Push(h Interface, x interface{}) {
50
        h.Push(x)
51
        up(h, h.Len()-1)
52
}
53
 
54
// Pop removes the minimum element (according to Less) from the heap
55
// and returns it. The complexity is O(log(n)) where n = h.Len().
56
// Same as Remove(h, 0).
57
//
58
func Pop(h Interface) interface{} {
59
        n := h.Len() - 1
60
        h.Swap(0, n)
61
        down(h, 0, n)
62
        return h.Pop()
63
}
64
 
65
// Remove removes the element at index i from the heap.
66
// The complexity is O(log(n)) where n = h.Len().
67
//
68
func Remove(h Interface, i int) interface{} {
69
        n := h.Len() - 1
70
        if n != i {
71
                h.Swap(i, n)
72
                down(h, i, n)
73
                up(h, i)
74
        }
75
        return h.Pop()
76
}
77
 
78
func up(h Interface, j int) {
79
        for {
80
                i := (j - 1) / 2 // parent
81
                if i == j || h.Less(i, j) {
82
                        break
83
                }
84
                h.Swap(i, j)
85
                j = i
86
        }
87
}
88
 
89
func down(h Interface, i, n int) {
90
        for {
91
                j1 := 2*i + 1
92
                if j1 >= n {
93
                        break
94
                }
95
                j := j1 // left child
96
                if j2 := j1 + 1; j2 < n && !h.Less(j1, j2) {
97
                        j = j2 // = 2*i + 2  // right child
98
                }
99
                if h.Less(i, j) {
100
                        break
101
                }
102
                h.Swap(i, j)
103
                i = j
104
        }
105
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.