-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAbstractMutableIntArray.java
More file actions
190 lines (163 loc) · 4.39 KB
/
AbstractMutableIntArray.java
File metadata and controls
190 lines (163 loc) · 4.39 KB
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
188
189
190
package javasabr.rlib.collections.array.impl;
import java.util.Arrays;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import javasabr.rlib.collections.array.IntArray;
import javasabr.rlib.collections.array.UnsafeMutableIntArray;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
@FieldDefaults(level = AccessLevel.PROTECTED)
public abstract class AbstractMutableIntArray extends AbstractIntArray implements UnsafeMutableIntArray {
@Override
public boolean add(int value) {
prepareForSize(size() + 1);
unsafeAdd(value);
return true;
}
@Override
public boolean addAll(IntArray array) {
if (array.isEmpty()) {
return false;
}
int size = size();
int elementsToAdd = array.size();
prepareForSize(size + elementsToAdd);
processAdd(array, size, elementsToAdd);
return true;
}
@Override
public boolean addAll(int[] array) {
if (array.length < 1) {
return false;
}
int size = size();
int elementsToAdd = array.length;
prepareForSize(size + elementsToAdd);
processAdd(array, size, elementsToAdd);
return true;
}
@Override
public UnsafeMutableIntArray unsafeAdd(int value) {
wrapped()[getAndIncrementSize()] = value;
return this;
}
@Override
public void replace(int index, int value) {
checkIndex(index);
unsafeSet(index, value);
}
@Override
public UnsafeMutableIntArray unsafeSet(int index, int value) {
wrapped()[index] = value;
return this;
}
@Override
public int removeByIndex(int index) {
checkIndex(index);
return unsafeRemoveByIndex(index);
}
@Override
public boolean remove(int value) {
int index = indexOf(value);
if (index < 0) {
return false;
}
unsafeRemoveByIndex(index);
return true;
}
@Override
public boolean removeAll(IntArray array) {
if (array.isEmpty()) {
return false;
}
int removed = 0;
for (int value : array) {
if (remove(value)) {
removed++;
}
}
return removed > 0;
}
@Override
public boolean removeAll(int[] array) {
if (array.length < 1) {
return false;
}
int removed = 0;
for (int value : array) {
if (remove(value)) {
removed++;
}
}
return removed > 0;
}
@Override
public int unsafeRemoveByIndex(int index) {
int numMoved = size() - index - 1;
int[] wrapped = wrapped();
int value = wrapped[index];
if (numMoved > 0) {
System.arraycopy(wrapped, index + 1, wrapped, index, numMoved);
}
wrapped[decrementAnGetSize()] = 0;
return value;
}
@Override
public void clear() {
int size = size();
if (size > 0) {
Arrays.fill(wrapped(), 0, size, 0);
size(0);
}
}
@Override
public IntStream stream() {
return StreamSupport.intStream(spliterator(), false);
}
@Override
public Spliterator.OfInt spliterator() {
return Spliterators.spliterator(wrapped(), 0, size(), Spliterator.NONNULL);
}
protected void processAdd(IntArray array, int currentSize, int elementsToAdd) {
System.arraycopy(array.asUnsafe().wrapped(), 0, wrapped(), currentSize, elementsToAdd);
size(currentSize + elementsToAdd);
}
protected void processAdd(int[] array, int currentSize, int elementsToAdd) {
System.arraycopy(array, 0, wrapped(), currentSize, elementsToAdd);
size(currentSize + elementsToAdd);
}
protected abstract void size(int size);
protected abstract int getAndIncrementSize();
protected abstract int decrementAnGetSize();
protected abstract void wrapped(int[] wrapped);
@Override
public UnsafeMutableIntArray prepareForSize(int expectedSize) {
int[] wrapped = wrapped();
if (expectedSize > wrapped.length) {
int newLength = Math.max((wrapped.length * 3) / 2, expectedSize);
wrapped(Arrays.copyOf(wrapped, newLength));
}
return this;
}
@Override
public UnsafeMutableIntArray trimToSize() {
int[] wrapped = wrapped();
int size = size();
if (size == wrapped.length) {
return this;
}
wrapped(Arrays.copyOfRange(wrapped, 0, size));
return this;
}
@Override
public UnsafeMutableIntArray asUnsafe() {
return this;
}
protected static void validateCapacity(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("Capacity cannot be negative");
}
}
}