1 /*
2 * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
3 * Copyright (C) 2009, Google Inc. 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 import java.util.Arrays;
15
16 /**
17 * A more efficient List<Long> using a primitive long array.
18 */
19 public class LongList {
20 private long[] entries;
21
22 private int count;
23
24 /**
25 * Create an empty list with a default capacity.
26 */
27 public LongList() {
28 this(10);
29 }
30
31 /**
32 * Create an empty list with the specified capacity.
33 *
34 * @param capacity
35 * number of entries the list can initially hold.
36 */
37 public LongList(int capacity) {
38 entries = new long[capacity];
39 }
40
41 /**
42 * Get number of entries in this list
43 *
44 * @return number of entries in this list
45 */
46 public int size() {
47 return count;
48 }
49
50 /**
51 * Get the value at the specified index
52 *
53 * @param i
54 * index to read, must be in the range [0, {@link #size()}).
55 * @return the number at the specified index
56 * @throws java.lang.ArrayIndexOutOfBoundsException
57 * the index outside the valid range
58 */
59 public long get(int i) {
60 if (count <= i)
61 throw new ArrayIndexOutOfBoundsException(i);
62 return entries[i];
63 }
64
65 /**
66 * Determine if an entry appears in this collection.
67 *
68 * @param value
69 * the value to search for.
70 * @return true of {@code value} appears in this list.
71 */
72 public boolean contains(long value) {
73 for (int i = 0; i < count; i++)
74 if (entries[i] == value)
75 return true;
76 return false;
77 }
78
79 /**
80 * Clear this list
81 */
82 public void clear() {
83 count = 0;
84 }
85
86 /**
87 * Add an entry to the end of the list.
88 *
89 * @param n
90 * the number to add.
91 */
92 public void add(long n) {
93 if (count == entries.length)
94 grow();
95 entries[count++] = n;
96 }
97
98 /**
99 * Assign an entry in the list.
100 *
101 * @param index
102 * index to set, must be in the range [0, {@link #size()}).
103 * @param n
104 * value to store at the position.
105 */
106 public void set(int index, long n) {
107 if (count < index)
108 throw new ArrayIndexOutOfBoundsException(index);
109 else if (count == index)
110 add(n);
111 else
112 entries[index] = n;
113 }
114
115 /**
116 * Pad the list with entries.
117 *
118 * @param toIndex
119 * index position to stop filling at. 0 inserts no filler. 1
120 * ensures the list has a size of 1, adding <code>val</code> if
121 * the list is currently empty.
122 * @param val
123 * value to insert into padded positions.
124 */
125 public void fillTo(int toIndex, long val) {
126 while (count < toIndex)
127 add(val);
128 }
129
130 /**
131 * Sort the list of longs according to their natural ordering.
132 */
133 public void sort() {
134 Arrays.sort(entries, 0, count);
135 }
136
137 private void grow() {
138 final long[] n = new long[(entries.length + 16) * 3 / 2];
139 System.arraycopy(entries, 0, n, 0, count);
140 entries = n;
141 }
142
143 /** {@inheritDoc} */
144 @Override
145 public String toString() {
146 final StringBuilder r = new StringBuilder();
147 r.append('[');
148 for (int i = 0; i < count; i++) {
149 if (i > 0)
150 r.append(", "); //$NON-NLS-1$
151 r.append(entries[i]);
152 }
153 r.append(']');
154 return r.toString();
155 }
156 }