org.apache.lucene.util
Class LongList

Object
  extended by LongList

public class LongList
extends Object

A fast, array-based, expandable list of longs.

Author:
Martin Haye

Field Summary
private  long[] data
           
private  int size
           
 
Constructor Summary
LongList()
          Basic constructor - initializes with capacity of 10
LongList(int initialCapacity)
          Constructor to specify initial capacity explicitly
 
Method Summary
 void add(long value)
          Add a value to the end of the list, expanding the array if necessary
 int binarySearch(long searchFor)
          Perform a binary search for the given value.
 int[] calcSortMap()
          Calculate a reordering of the elements of the list that would put them in sorted order.
 void clear()
          Remove all elements from the list (but doesn't resize the array)
 void compact()
          Resize the array so it exactly fits the current elements
 void ensureCapacity(int cap)
          Ensure that at least the given number of elements can be stored
 void fill(long value)
          Fill the list with a given data value
 long get(int index)
          Get an element from the list
 long getLast()
          Get the last element from the list
 boolean isEmpty()
          Check if the list is empty (i.e. size() == 0)
 void remap(int[] map)
          Apply a sort order to the elements -- see calcSortMap()
 void resize(int newSize)
          Resize the array to the specified size.
 void set(int index, long value)
          Set an element in the list
 int size()
          Retrieve the current number of elements in the list
 void sort()
          Sort all the elements in the list in ascending order
 long[] toArray()
          Get an array of the elements.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

private long[] data

size

private int size
Constructor Detail

LongList

public LongList()
Basic constructor - initializes with capacity of 10


LongList

public LongList(int initialCapacity)
Constructor to specify initial capacity explicitly

Method Detail

add

public final void add(long value)
Add a value to the end of the list, expanding the array if necessary


ensureCapacity

public final void ensureCapacity(int cap)
Ensure that at least the given number of elements can be stored


compact

public final void compact()
Resize the array so it exactly fits the current elements


resize

public final void resize(int newSize)
Resize the array to the specified size. If smaller than the current number of elements, the ones at the end will be lost.


toArray

public final long[] toArray()
Get an array of the elements. This is a copy, so it's safe to modify without disturbing the contents of the list.


isEmpty

public final boolean isEmpty()
Check if the list is empty (i.e. size() == 0)


clear

public final void clear()
Remove all elements from the list (but doesn't resize the array)


size

public final int size()
Retrieve the current number of elements in the list


get

public final long get(int index)
Get an element from the list


getLast

public final long getLast()
Get the last element from the list


set

public final void set(int index,
                      long value)
Set an element in the list


fill

public final void fill(long value)
Fill the list with a given data value


sort

public final void sort()
Sort all the elements in the list in ascending order


binarySearch

public final int binarySearch(long searchFor)
Perform a binary search for the given value. Note that the list must already be in ascending sorted order for this to work correctly; call sort() if necessary.

Returns:
Same as for Arrays.binarySearch(int[], int)

calcSortMap

public final int[] calcSortMap()
Calculate a reordering of the elements of the list that would put them in sorted order. Useful for maintaining multiple lists, and sorting them by a primary key; this is done by calling this method to get the mapping, then calling remap(int[]) on each array including the original.

Returns:
An array of the same size as the number of elements. Each element of the array specifies the position that corresponding element should be placed in a sorted array.

remap

public final void remap(int[] map)
Apply a sort order to the elements -- see calcSortMap()