2 * Copyright (C) 2008, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.util
;
40 /** A more efficient List<Integer> using a primitive integer array. */
41 public class IntList
{
42 private int[] entries
;
46 /** Create an empty list with a default capacity. */
52 * Create an empty list with the specified capacity.
55 * number of entries the list can initially hold.
57 public IntList(final int capacity
) {
58 entries
= new int[capacity
];
61 /** @return number of entries in this list */
68 * index to read, must be in the range [0, {@link #size()}).
69 * @return the number at the specified index
70 * @throws ArrayIndexOutOfBoundsException
71 * the index outside the valid range
73 public int get(final int i
) {
75 throw new ArrayIndexOutOfBoundsException(i
);
79 /** Empty this list */
85 * Add an entry to the end of the list.
90 public void add(final int n
) {
91 if (count
== entries
.length
)
97 * Pad the list with entries.
100 * index position to stop filling at. 0 inserts no filler. 1
101 * ensures the list has a size of 1, adding <code>val</code> if
102 * the list is currently empty.
104 * value to insert into padded positions.
106 public void fillTo(int toIndex
, final int val
) {
107 while (count
< toIndex
)
111 private void grow() {
112 final int[] n
= new int[(entries
.length
+ 16) * 3 / 2];
113 System
.arraycopy(entries
, 0, n
, 0, count
);
117 public String
toString() {
118 final StringBuilder r
= new StringBuilder();
120 for (int i
= 0; i
< count
; i
++) {
123 r
.append(entries
[i
]);