org.hypergraphdb.util
Class ArrayBasedSet<E>

java.lang.Object
  extended by org.hypergraphdb.util.ArrayBasedSet<E>
Type Parameters:
E -
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>, java.util.SortedSet<E>, HGSortedSet<E>

public class ArrayBasedSet<E>
extends java.lang.Object
implements HGSortedSet<E>

An implementation SortedSet based on primitive arrays that grow as needed without ever shrinking. Lookup is log(n), but insertion and removal take longer obviously so this implementation is mainly suitable for small sets of a few dozen elements, or for sets that are searched/iterated much more frequently than they are changed. The main reason for being of this implementation is space efficiency: a red-black tree holds additional 12 bytes per datum. So while for a large set, a tree should be used, the array-based implementation is preferable for many small sets like many of HyperGraphDB's incidence sets.

Some benchmarking experiments comparing this (rather simple) implementation to red-black trees (both LLRBTree and the standard Java TreeSet): working with about up to 10000 elements, insertion and removal have a comparable performance, with the array-based implementation being about 15% slower (elements inserted/removed in random order). The LLRBTree implementation is actually noticeably slower than TreeSet, probably due to recursion. However, in "read-mode", when iterating over the set, using it as a HGRandomAccessResult, the array-based implementation is 8-10 faster. Understandable since here moving to the next element amounts to incrementing an integer while in a tree a lookup for the successor must be performed (e.g. min(parent(current))). So for set of this order of magnitude and/or sets that are being read more than they are modified, it is strongly advisable to use the ArrayBasedSet.

Author:
Borislav Iordanov

Constructor Summary
ArrayBasedSet(E[] A)
           Initialize from the given array.
ArrayBasedSet(E[] A, java.util.Comparator<E> comparator)
           Initialize from the given array and with the given Comparator.
ArrayBasedSet(E[] A, int capacity)
           Initialize an empty set with a given initial capacity.
ArrayBasedSet(E[] A, int capacity, java.util.Comparator<E> comparator)
           Initialize an empty set with a given initial capacity, and a given comparator.
 
Method Summary
 boolean add(E o)
           
 boolean addAll(java.util.Collection<? extends E> c)
           
 void clear()
           
 java.util.Comparator<? super E> comparator()
           
 boolean contains(java.lang.Object o)
           
 boolean containsAll(java.util.Collection<?> c)
           
 E first()
           
 E getAt(int i)
           
 java.util.concurrent.locks.ReadWriteLock getLock()
           
 HGRandomAccessResult<E> getSearchResult()
           
 java.util.SortedSet<E> headSet(E toElement)
           
 boolean isEmpty()
           
 java.util.Iterator<E> iterator()
           
 E last()
           
 boolean remove(java.lang.Object o)
           
 boolean removeAll(java.util.Collection<?> c)
           
 E removeAt(int i)
           
 boolean retainAll(java.util.Collection<?> c)
           
 void setFromArray(E[] A)
           
 void setLock(java.util.concurrent.locks.ReadWriteLock lock)
           
 int size()
           
 java.util.SortedSet<E> subSet(E fromElement, E toElement)
           
 java.util.SortedSet<E> tailSet(E fromElement)
           
 java.lang.Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Set
equals, hashCode
 

Constructor Detail

ArrayBasedSet

public ArrayBasedSet(E[] A)

Initialize from the given array.

Parameters:
A - The array used to initialize. An internal array is created with the same size as A and all its elements are copied.

ArrayBasedSet

public ArrayBasedSet(E[] A,
                     int capacity)

Initialize an empty set with a given initial capacity.

Parameters:
A - The array used to initialize. An internal array is created with the same type as as A and with size number of slots.

ArrayBasedSet

public ArrayBasedSet(E[] A,
                     int capacity,
                     java.util.Comparator<E> comparator)

Initialize an empty set with a given initial capacity, and a given comparator.

Parameters:
A - The array used to initialize. An internal array is created with the same type as as A and with size number of slots.
Comparator - The comparator used to compare elements.

ArrayBasedSet

public ArrayBasedSet(E[] A,
                     java.util.Comparator<E> comparator)

Initialize from the given array and with the given Comparator.

Parameters:
A - The array used to initialize. An internal array is created with the same size as A and all its elements are copied.
Comparator - The comparator used to compare elements.
Method Detail

setFromArray

public void setFromArray(E[] A)

getLock

public java.util.concurrent.locks.ReadWriteLock getLock()

setLock

public void setLock(java.util.concurrent.locks.ReadWriteLock lock)

comparator

public java.util.Comparator<? super E> comparator()
Specified by:
comparator in interface java.util.SortedSet<E>

getAt

public E getAt(int i)

removeAt

public E removeAt(int i)

first

public E first()
Specified by:
first in interface java.util.SortedSet<E>

headSet

public java.util.SortedSet<E> headSet(E toElement)
Specified by:
headSet in interface java.util.SortedSet<E>

last

public E last()
Specified by:
last in interface java.util.SortedSet<E>

subSet

public java.util.SortedSet<E> subSet(E fromElement,
                                     E toElement)
Specified by:
subSet in interface java.util.SortedSet<E>

tailSet

public java.util.SortedSet<E> tailSet(E fromElement)
Specified by:
tailSet in interface java.util.SortedSet<E>

add

public boolean add(E o)
Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<E>

addAll

public boolean addAll(java.util.Collection<? extends E> c)
Specified by:
addAll in interface java.util.Collection<E>
Specified by:
addAll in interface java.util.Set<E>

clear

public void clear()
Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.Set<E>

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.Set<E>

containsAll

public boolean containsAll(java.util.Collection<?> c)
Specified by:
containsAll in interface java.util.Collection<E>
Specified by:
containsAll in interface java.util.Set<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Collection<E>
Specified by:
isEmpty in interface java.util.Set<E>

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.Set<E>

getSearchResult

public HGRandomAccessResult<E> getSearchResult()
Specified by:
getSearchResult in interface HGSortedSet<E>

remove

public boolean remove(java.lang.Object o)
Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Set<E>

removeAll

public boolean removeAll(java.util.Collection<?> c)
Specified by:
removeAll in interface java.util.Collection<E>
Specified by:
removeAll in interface java.util.Set<E>

retainAll

public boolean retainAll(java.util.Collection<?> c)
Specified by:
retainAll in interface java.util.Collection<E>
Specified by:
retainAll in interface java.util.Set<E>

size

public int size()
Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.Set<E>

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.Set<E>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.Set<E>