FileDocCategorySizeDatePackage
LabeledList.javaAPI DocAndroid 1.5 API4152Wed May 06 22:41:02 BST 2009com.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. -1 = invalid label.
Constructors Summary
public LabeledList(int size)

inheritDoc

        super(size);

        labelToIndex = new IntList(size);
    
protected 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
protected 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 intgetMaxLabel()
Gets the maximum label (exclusive) of any block added to this instance.

return
>= 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--)
            ;

        int newSize = i+1;

        labelToIndex.shrink(newSize);

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

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

        if (label >= labelToIndex.size()) {
            return -1;
        } else {
            return labelToIndex.get(label);
        }
    
protected voidrebuildLabelToIndex()
Rebuilds the label-to-index mapping after a shrinkToFit(). Note: 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);
            }
        }
    
protected 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 >= 0, < size(); which element
param
item 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();