summaryrefslogtreecommitdiffstats
path: root/dx/src/com/android/jack/dx/util/LabeledList.java
blob: 3579dbfb2381f64216e8d9b4ac952adac74b30b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack.dx.util;

import java.util.Arrays;

/**
 * A list of labeled items, allowing easy lookup by label.
 */
public class LabeledList extends FixedSizeList {
  /**
   * Sparse array indexed by label to FixedSizeList index;
   * {@code -1} for an invalid label.
   */
  private final IntList labelToIndex;

  public LabeledList(int size) {
    super(size);

    labelToIndex = new IntList(size);
  }

  /**
   * Constructs a new instance that is a copy of the old instance.
   *
   * @param old instance to copy
   */
  public LabeledList(LabeledList old) {
    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);
      }
    }
  }

  /**
   * Gets the maximum label (exclusive) of any block added to this instance.
   *
   * @return {@code >= 0;} the maximum label
   */
  public final int getMaxLabel() {
    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;
  }

  /**
   * Removes a label from the label-to-index mapping.
   *
   * @param oldLabel label to remove
   */
  private void removeLabel(int oldLabel) {
    labelToIndex.set(oldLabel, -1);
  }

  /**
   * Adds a label and index to the label-to-index mapping.
   *
   * @param label new label
   * @param index index of block.
   */
  private void addLabelIndex(int label, int index) {
    int origSz = labelToIndex.size();

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

    labelToIndex.set(label, index);
  }

  /**
   * 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
   */
  public final int indexOfLabel(int label) {
    if (label >= labelToIndex.size()) {
      return -1;
    } else {
      return labelToIndex.get(label);
    }
  }

  /**
   * 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
   */
  public final int[] getLabelsInOrder() {
    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;
  }

  @Override
  /** @inheritDoc */
  public void shrinkToFit() {
    super.shrinkToFit();

    rebuildLabelToIndex();
  }

  /**
   * 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.
   */
  private void rebuildLabelToIndex() {
    int szItems = size();

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

      if (li != null) {
        labelToIndex.set(li.getLabel(), i);
      }
    }
  }

  /**
   * Sets the element at the given index.
   *
   * @param n {@code >= 0, < size();} which element
   * @param item {@code null-ok;} the value to store
   */
  protected void set(int n, LabeledItem item) {
    LabeledItem old = (LabeledItem) getOrNull0(n);

    set0(n, item);

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

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