File: Heap.java

package info (click to toggle)
concurrent-dfsg 1.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 976 kB
  • ctags: 2,018
  • sloc: java: 10,704; xml: 49; makefile: 12
file content (147 lines) | stat: -rw-r--r-- 4,004 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
  File: Heap.java

  Originally written by Doug Lea and released into the public domain.
  This may be used for any purposes whatsoever without acknowledgment.
  Thanks for the assistance and support of Sun Microsystems Labs,
  and everyone contributing, testing, and using this code.

  History:
  Date       Who                What
  29Aug1998  dl               Refactored from BoundedPriorityQueue
  08dec2001  dl               Null out slots of removed items
  03feb2002  dl               Also null out in clear
*/

package EDU.oswego.cs.dl.util.concurrent;
import java.util.Comparator;

/**
 * A heap-based priority queue, without any concurrency control
 * (i.e., no blocking on empty/full states).
 * This class provides the data structure mechanics for BoundedPriorityQueue.
 * <p>
 * The class currently uses a standard array-based heap, as described
 * in, for example, Sedgewick's Algorithms text. All methods
 * are fully synchronized. In the future,
 * it may instead use structures permitting finer-grained locking.
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
 **/

public class Heap  {
  protected Object[] nodes_;  // the tree nodes, packed into an array
  protected int count_ = 0;   // number of used slots
  protected final Comparator cmp_;  // for ordering

  /**
   * Create a Heap with the given initial capacity and comparator
   * @exception IllegalArgumentException if capacity less or equal to zero
   **/

  public Heap(int capacity, Comparator cmp) 
   throws IllegalArgumentException {
    if (capacity <= 0) throw new IllegalArgumentException();
    nodes_ = new Object[capacity];
    cmp_ = cmp;
  }

  /**
   * Create a Heap with the given capacity,
   * and relying on natural ordering.
   **/

  public Heap(int capacity) { 
    this(capacity, null); 
  }


  /** perform element comaprisons using comparator or natural ordering **/
  protected int compare(Object a, Object b) {
    if (cmp_ == null) 
      return ((Comparable)a).compareTo(b);
    else
      return cmp_.compare(a, b);
  }


  // indexes of heap parents and children
  protected final int parent(int k) { return (k - 1) / 2;  }
  protected final int left(int k)   { return 2 * k + 1; }
  protected final int right(int k)  { return 2 * (k + 1); }

  /**
   * insert an element, resize if necessary
   **/
  public synchronized void insert(Object x) {
    if (count_ >= nodes_.length) {
      int newcap =  3 * nodes_.length / 2 + 1;
      Object[] newnodes = new Object[newcap];
      System.arraycopy(nodes_, 0, newnodes, 0, nodes_.length);
      nodes_ = newnodes;
    }

    int k = count_;
    ++count_;
    while (k > 0) {
      int par = parent(k);
      if (compare(x, nodes_[par]) < 0) {
        nodes_[k] = nodes_[par];
        k = par;
      }
      else break;
    }
    nodes_[k] = x;
  }
    

  /**
   * Return and remove least element, or null if empty
   **/

  public synchronized Object extract() {
    if (count_ < 1) return null;

    int k = 0; // take element at root;
    Object least = nodes_[k];
    --count_;
    Object x = nodes_[count_];
    nodes_[count_] = null;
    for (;;) {
      int l = left(k);
      if (l >= count_)
        break;
      else {
        int r = right(k);
        int child = (r >= count_ || compare(nodes_[l], nodes_[r]) < 0)? l : r; 
        if (compare(x, nodes_[child]) > 0) {
          nodes_[k] = nodes_[child];
          k = child;
        }
        else break;
      }
    }
    nodes_[k] = x;
    return least;
  }

  /** Return least element without removing it, or null if empty **/
  public synchronized Object peek() {
    if (count_ > 0) 
      return nodes_[0];
    else
      return null;
  }

  /** Return number of elements **/
  public synchronized int size() {
    return count_;
  }
  
  /** remove all elements **/
  public synchronized void clear() {
    for (int i = 0; i < count_; ++i)
      nodes_[i] = null;
    count_ = 0;
  }

}