Java Code Examples for java.util.AbstractList#get()

The following examples show how to use java.util.AbstractList#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WordList.java    From stendhal with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Search for compound names.
 * @param expressions list of expressions
 * @param idx start index of the expression list
 * @return compound name or null
 */
public CompoundName searchCompoundName(AbstractList<Expression> expressions, int idx) {
       Expression first = expressions.get(idx);

   	Set<CompoundName> candidates = compoundNames.get(first.getOriginal().toLowerCase());

	if (candidates != null) {
    	TreeSet<CompoundName> candidatesSortedFromLongestToShortest = new TreeSet<CompoundName>(new ArrayLengthDescSorter<CompoundName>());
    	candidatesSortedFromLongestToShortest.addAll(candidates);
   		for (CompoundName compName : candidatesSortedFromLongestToShortest) {
   			if (compName.matches(expressions, idx)) {
   				return compName;
   			}
   		}
	}

	return null;
}
 
Example 2
Source File: CompoundName.java    From stendhal with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compare this compound name with the words in the expressions list starting at index idx.
 * @param expressions
 * @param idx
 * @return true for matching between expressions and the compound name
 */
public boolean matches(AbstractList<Expression> expressions, int idx) {
	for(int i=0; i<size(); ++idx,++i) {
		if (idx >= expressions.size()) {
			return false;
		}

		Expression curr = expressions.get(idx);
		String word = get(i);

        // compare the current word in a case insensitive way
        if (!curr.getOriginal().equalsIgnoreCase(word)) {
        	return false;
        }

        // don't merge if the break flag is set in-between the compound name
        if (i<size()-1 && curr.getBreakFlag()) {
            return false;
        }
	}

	return true;
}
 
Example 3
Source File: HeapSort.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Sorts a vector of comparable objects into increasing order.
 */
public static void sort(AbstractList array) {

    Object temp;
    int j, n = array.size();

    // turn input array into a heap
    for (j = n / 2; j > 0; j--) {
        adjust(array, j, n);
    }

    // remove largest elements and put them at the end
    // of the unsorted region until you are finished
    for (j = n - 1; j > 0; j--) {
        temp = array.get(0);
        array.set(0, array.get(j));
        array.set(j, temp);
        adjust(array, 1, j);
    }
}
 
Example 4
Source File: HeapSort.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * helps sort an vector of comparable objects.
 * Assumes that array[lower+1] through to array[upper] is
 * already in heap form and then puts array[lower] to
 * array[upper] in heap form.
 */
private static void adjust(AbstractList array, int lower, int upper) {

    int j, k;
    Object temp;

    j = lower;
    k = lower * 2;

    while (k <= upper) {
        if ((k < upper) && (((Comparable) array.get(k - 1)).compareTo(array.get(k)) < 0)) {
            k += 1;
        }
        if (((Comparable) array.get(j - 1)).compareTo(array.get(k - 1)) < 0) {
            temp = array.get(j - 1);
            array.set(j - 1, array.get(k - 1));
            array.set(k - 1, temp);
        }
        j = k;
        k *= 2;
    }
}