1 /*
2 * Copyright (C) 2008, Google Inc.
3 * Copyright (C) 2009, Johannes Schindelin <johannes.schindelin@gmx.de> and others
4 *
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Distribution License v. 1.0 which is available at
7 * https://www.eclipse.org/org/documents/edl-v10.php.
8 *
9 * SPDX-License-Identifier: BSD-3-Clause
10 */
11
12 package org.eclipse.jgit.util;
13
14 /**
15 * A more efficient List<Integer> using a primitive integer array.
16 */
17 public class IntList {
18 private int[] entries;
19
20 private int count;
21
22 /**
23 * Create an empty list with a default capacity.
24 */
25 public IntList() {
26 this(10);
27 }
28
29 /**
30 * Create an empty list with the specified capacity.
31 *
32 * @param capacity
33 * number of entries the list can initially hold.
34 */
35 public IntList(int capacity) {
36 entries = new int[capacity];
37 }
38
39 /**
40 * Get number of entries in this list.
41 *
42 * @return number of entries in this list.
43 */
44 public int size() {
45 return count;
46 }
47
48 /**
49 * Check if an entry appears in this collection.
50 *
51 * @param value
52 * the value to search for.
53 * @return true of {@code value} appears in this list.
54 * @since 4.9
55 */
56 public boolean contains(int value) {
57 for (int i = 0; i < count; i++)
58 if (entries[i] == value)
59 return true;
60 return false;
61 }
62
63 /**
64 * Get the value at the specified index
65 *
66 * @param i
67 * index to read, must be in the range [0, {@link #size()}).
68 * @return the number at the specified index
69 * @throws java.lang.ArrayIndexOutOfBoundsException
70 * the index outside the valid range
71 */
72 public int get(int i) {
73 if (count <= i)
74 throw new ArrayIndexOutOfBoundsException(i);
75 return entries[i];
76 }
77
78 /**
79 * Empty this list
80 */
81 public void clear() {
82 count = 0;
83 }
84
85 /**
86 * Add an entry to the end of the list.
87 *
88 * @param n
89 * the number to add.
90 */
91 public void add(int n) {
92 if (count == entries.length)
93 grow();
94 entries[count++] = n;
95 }
96
97 /**
98 * Assign an entry in the list.
99 *
100 * @param index
101 * index to set, must be in the range [0, {@link #size()}).
102 * @param n
103 * value to store at the position.
104 */
105 public void set(int index, int n) {
106 if (count < index)
107 throw new ArrayIndexOutOfBoundsException(index);
108 else if (count == index)
109 add(n);
110 else
111 entries[index] = n;
112 }
113
114 /**
115 * Pad the list with entries.
116 *
117 * @param toIndex
118 * index position to stop filling at. 0 inserts no filler. 1
119 * ensures the list has a size of 1, adding <code>val</code> if
120 * the list is currently empty.
121 * @param val
122 * value to insert into padded positions.
123 */
124 public void fillTo(int toIndex, int val) {
125 while (count < toIndex)
126 add(val);
127 }
128
129 private void grow() {
130 final int[] n = new int[(entries.length + 16) * 3 / 2];
131 System.arraycopy(entries, 0, n, 0, count);
132 entries = n;
133 }
134
135 /** {@inheritDoc} */
136 @Override
137 public String toString() {
138 final StringBuilder r = new StringBuilder();
139 r.append('[');
140 for (int i = 0; i < count; i++) {
141 if (i > 0)
142 r.append(", "); //$NON-NLS-1$
143 r.append(entries[i]);
144 }
145 r.append(']');
146 return r.toString();
147 }
148 }