FileDocCategorySizeDatePackage
LabeledList.javaAPI DocAndroid 5.1 API5021Thu Mar 12 22:18:30 GMT 2015com.android.dx.util

LabeledList

public class LabeledList extends FixedSizeList
A list of labeled items, allowing easy lookup by label.

Fields Summary
private final IntList
labelToIndex
Sparse array indexed by label to FixedSizeList index; {@code -1} for an invalid label.
Constructors Summary
public LabeledList(int size)

inheritDoc

        super(size);

        labelToIndex = new IntList(size);
    
public LabeledList(LabeledList old)
Constructs a new instance that is a copy of the old instance.

param
old instance to copy

        super(old.size());
        labelToIndex = old.labelToIndex.mutableCopy();

        int sz = old.size();

        for (int i = 0; i < sz; i++) {
            Object one = old.get0(i);
            if (one != null) {
                set0(i, one);
            }
        }
    
Methods Summary
private voidaddLabelIndex(int label, int index)
Adds a label and index to the label-to-index mapping.

param
label new label
param
index index of block.

        int origSz = labelToIndex.size();

        for (int i = 0; i <= (label - origSz); i++) {
            labelToIndex.add(-1);
        }

        labelToIndex.set(label, index);
    
public final int[]getLabelsInOrder()
Gets an array containing all of the labels used in this instance, in order. The returned array is freshly-allocated and so may be modified safely by the caller without impacting this instance.

return
{@code non-null;} ordered array of labels
throws
NullPointerException thrown if there are any {@code null} items in this instance

        int sz = size();
        int[] result = new int[sz];

        for (int i = 0; i < sz; i++) {
            LabeledItem li = (LabeledItem) get0(i);
            if (li == null) {
                throw new NullPointerException("null at index " + i);
            }
            result[i] = li.getLabel();
        }

        Arrays.sort(result);
        return result;
    
public final intgetMaxLabel()
Gets the maximum label (exclusive) of any block added to this instance.

return
{@code >= 0;} the maximum label

        int sz = labelToIndex.size();

        // Gobble any deleted labels that may be at the end.
        int i;
        for (i = sz - 1; (i >= 0) && (labelToIndex.get(i) < 0); i--)
            /*empty*/ ;

        int newSize = i + 1;

        labelToIndex.shrink(newSize);

        return newSize;
    
public final intindexOfLabel(int label)
Gets the index of the first item in the list with the given label, if any.

param
label {@code >= 0;} the label to look for
return
{@code >= -1;} the index of the so-labelled item, or {@code -1} if none is found

        if (label >= labelToIndex.size()) {
            return -1;
        } else {
            return labelToIndex.get(label);
        }
    
private voidrebuildLabelToIndex()
Rebuilds the label-to-index mapping after a {@code shrinkToFit()}. Note: This assumes that the labels that are in the list are the same, although the indicies may have changed.

        int szItems = size();

        for (int i = 0; i < szItems; i++) {
            LabeledItem li = (LabeledItem) get0(i);

            if (li != null) {
                labelToIndex.set(li.getLabel(), i);
            }
        }
    
private voidremoveLabel(int oldLabel)
Removes a label from the label-to-index mapping.

param
oldLabel label to remove

        labelToIndex.set(oldLabel, -1);
    
protected voidset(int n, LabeledItem item)
Sets the element at the given index.

param
n {@code >= 0, < size();} which element
param
item {@code null-ok;} the value to store

        LabeledItem old = (LabeledItem) getOrNull0(n);

        set0(n, item);

        if (old != null) {
            removeLabel(old.getLabel());
        }

        if (item != null) {
            addLabelIndex(item.getLabel(), n);
        }
    
public voidshrinkToFit()

inheritDoc

        super.shrinkToFit();

        rebuildLabelToIndex();