com.cosylab.epics.caj.util
Class Heap

java.lang.Object
  extended by com.cosylab.epics.caj.util.Heap

public class Heap
extends Object

A heap-based priority queue. The class currently uses a standard array-based heap, as described in, for example, Sedgewick's Algorithms text. All methods are fully synchronized.


Field Summary
protected  Comparator cmp_
          Ordering comparator.
protected  int count_
          Number of used slots.
protected  Object[] nodes_
          The tree nodes, packed into an array.
 
Constructor Summary
Heap(int capacity)
          Create a Heap with the given capacity, and relying on natural ordering.
Heap(int capacity, Comparator cmp)
          Create a Heap with the given initial capacity and comparator
 
Method Summary
 void clear()
          Remove all elements.
protected  int compare(Object a, Object b)
          Perform element comparisons using comparator or natural ordering.
 Object extract()
          Return and remove least element, or null if empty.
 void insert(Object x)
          Insert an element, resize if necessary.
protected  int left(int k)
          Get left child.
protected  int parent(int k)
          Get parent index.
 Object peek()
          Return least element without removing it, or null if empty.
protected  int right(int k)
          Get right child.
 int size()
          Return number of elements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nodes_

protected Object[] nodes_
The tree nodes, packed into an array.


count_

protected int count_
Number of used slots.


cmp_

protected final Comparator cmp_
Ordering comparator.

Constructor Detail

Heap

public Heap(int capacity,
            Comparator cmp)
     throws IllegalArgumentException
Create a Heap with the given initial capacity and comparator

Parameters:
capacity - initial capacity.
cmp - comparator instance.
Throws:
IllegalArgumentException - if capacity less or equal to zero

Heap

public Heap(int capacity)
Create a Heap with the given capacity, and relying on natural ordering.

Parameters:
capacity - initial capacity.
Method Detail

compare

protected int compare(Object a,
                      Object b)
Perform element comparisons using comparator or natural ordering.


parent

protected final int parent(int k)
Get parent index.


left

protected final int left(int k)
Get left child.


right

protected final int right(int k)
Get right child.


insert

public void insert(Object x)
Insert an element, resize if necessary.

Parameters:
x - object to insert.

extract

public Object extract()
Return and remove least element, or null if empty.

Returns:
extraced least element.

peek

public Object peek()
Return least element without removing it, or null if empty.

Returns:
least element.

size

public int size()
Return number of elements.

Returns:
number of elements.

clear

public void clear()
Remove all elements.



Copyright © 2004-2013 Cosylab. All Rights Reserved.