An Array-based Implementation of IDirectory


Arrays in Java


Array Types


Array Variables


Array Creation


Array Accesses



package dict;

import java.lang.*;
import schemeFactory.*;

/**
 * An implementation of IDictionary using an array to hold the
 * DictionaryPairs.
 *
 * @author Alan L. Cox
 * @since 03/26/02
 */
public class DictArray implements IDictionary {
    /*
     * An array of DictionaryPairs ordered by key
     */
    private int              _firstEmptyPair = 0;
    private DictionaryPair[] _pairs = new DictionaryPair[1];

    /*
     * An AList factory used by elements()
     */
    private IListFactory _lf = new CompositeListFactory();

    /**
     * Clears the contents of the dictionary leaving it empty.
     *
     * Implemented by replacing the existing array with a new,
     * empty one.
     */
    public void clear() {
        _firstEmptyPair = 0;
        _pairs = new DictionaryPair[1];
    }

    /**
     * Returns true if the dictionary is empty and false otherwise.
     */
    public boolean isEmpty() {
        return _firstEmptyPair == 0;
    }

    /**
     * Returns an AList of DictionaryPairs corresponding to the entire
     * contents of the dictionary.
     */
    public AList elements() {
        AList l = _lf.makeEmptyList();

        for (int i = _firstEmptyPair - 1; i >= 0; i--)
            l = _lf.makeNEList(_pairs[i], l);

        return l;
    }

    /**
     * Returns either (1) the index of the key if it is stored
     * in the array or (2) the index of the next smaller key
     * if it is NOT stored in the array.  (If there is NOT
     * a smaller key in the array, returns -1.)
     */
    private int findIndex(Comparable key)
    {
        int lo = -1;
        int hi = _firstEmptyPair;

        while (lo + 1 != hi) {
            int mid = (lo + hi)/2;
            int result = _pairs[mid].getKey().compareTo(key);

            if (result > 0)  // _pairs[mid].getKey() > key
                hi = mid;
            else if (result == 0) // _pairs[mid].getKey() == key
                return mid;
            else   // _pairs[mid].getKey() < key
                lo = mid;
        }
        return lo;
    }

    /**
     * Returns the DictionaryPair with the given key.  If there is not
     * a DictionaryPair with the given key, returns null.
     *
     * Returns a DictionaryPair rather than the value alone so that
     * the user can distinguish between not finding the key and
     * finding the pair (key, null).
     */
    public DictionaryPair lookup(Comparable key) {
        int index = findIndex(key);

        if ((index >= 0) &&
            (_pairs[index].getKey().compareTo(key) == 0))
            return _pairs[index];

        return null;
    }

    /**
     * Inserts the given key and value.  If the given key is already
     * in the dictionary, the given value replaces the key's old
     * value.
     */
    public void insert(Comparable key, Object value) {
        int index = findIndex(key);

        if ((index >= 0) &&
            (_pairs[index].getKey().compareTo(key) == 0)) {
            _pairs[index] = new DictionaryPair(key, value);
            return;
        }
        if (_firstEmptyPair == _pairs.length) {

            DictionaryPair[] newPairs =
                new DictionaryPair[2*_pairs.length];

            for (int i = 0; i < _pairs.length; i++)
                newPairs[i] = _pairs[i];

            _pairs = newPairs;
        }
        int i = _firstEmptyPair;

        for (index++; i > index; i--)
            _pairs[i] = _pairs[i - 1];
        _pairs[i] = new DictionaryPair(key, value);

        _firstEmptyPair++;
    }

    /**
     * Removes the DictionaryPair with the given key and returns it.
     * If there is not a DictionaryPair with the given key, returns
     * null.
     */
    public DictionaryPair remove(Comparable key) {
        int index = findIndex(key);

        if ((index >= 0) &&
            (_pairs[index].getKey().compareTo(key) == 0)) {

            DictionaryPair pair = _pairs[index];

            int i = index;

            for (_firstEmptyPair--; i < _firstEmptyPair; i++)
                _pairs[i] = _pairs[i + 1];
            _pairs[i] = null;

            return pair;
        }
        return null;
    }
}


Calculating the Computational Cost of findIndex()