1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections.primitives;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23
24
25
26
27
28
29
30
31
32
33 public class ArrayDoubleList extends RandomAccessDoubleList implements DoubleList, Serializable {
34
35
36
37
38
39
40
41
42 public ArrayDoubleList() {
43 this(8);
44 }
45
46
47
48
49
50
51 public ArrayDoubleList(int initialCapacity) {
52 if(initialCapacity < 0) {
53 throw new IllegalArgumentException("capacity " + initialCapacity);
54 }
55 _data = new double[initialCapacity];
56 _size = 0;
57 }
58
59
60
61
62
63
64
65
66
67
68 public ArrayDoubleList(DoubleCollection that) {
69 this(that.size());
70 addAll(that);
71 }
72
73
74
75
76
77
78
79 public ArrayDoubleList(double[] array) {
80 this(array.length);
81 System.arraycopy(array, 0, _data, 0, array.length);
82 _size = array.length;
83 }
84
85
86
87
88 public double get(int index) {
89 checkRange(index);
90 return _data[index];
91 }
92
93 public int size() {
94 return _size;
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public double removeElementAt(int index) {
111 checkRange(index);
112 incrModCount();
113 double oldval = _data[index];
114 int numtomove = _size - index - 1;
115 if(numtomove > 0) {
116 System.arraycopy(_data,index+1,_data,index,numtomove);
117 }
118 _size--;
119 return oldval;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public double set(int index, double element) {
136 checkRange(index);
137 incrModCount();
138 double oldval = _data[index];
139 _data[index] = element;
140 return oldval;
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public void add(int index, double element) {
159 checkRangeIncludingEndpoint(index);
160 incrModCount();
161 ensureCapacity(_size+1);
162 int numtomove = _size-index;
163 System.arraycopy(_data,index,_data,index+1,numtomove);
164 _data[index] = element;
165 _size++;
166 }
167
168 public void clear() {
169 incrModCount();
170 _size = 0;
171 }
172
173 public boolean addAll(DoubleCollection collection) {
174 return addAll(size(), collection);
175 }
176
177 public boolean addAll(int index, DoubleCollection collection) {
178 if (collection.size() == 0) {
179 return false;
180 }
181 checkRangeIncludingEndpoint(index);
182 incrModCount();
183 ensureCapacity(_size + collection.size());
184 if (index != _size) {
185
186 System.arraycopy(_data, index, _data, index + collection.size(), _size - index);
187 }
188 for (DoubleIterator it = collection.iterator(); it.hasNext();) {
189 _data[index] = it.next();
190 index++;
191 }
192 _size += collection.size();
193 return true;
194 }
195
196
197
198
199
200
201
202
203
204 public void ensureCapacity(int mincap) {
205 incrModCount();
206 if(mincap > _data.length) {
207 int newcap = (_data.length * 3)/2 + 1;
208 double[] olddata = _data;
209 _data = new double[newcap < mincap ? mincap : newcap];
210 System.arraycopy(olddata,0,_data,0,_size);
211 }
212 }
213
214
215
216
217
218 public void trimToSize() {
219 incrModCount();
220 if(_size < _data.length) {
221 double[] olddata = _data;
222 _data = new double[_size];
223 System.arraycopy(olddata,0,_data,0,_size);
224 }
225 }
226
227
228
229
230 private void writeObject(ObjectOutputStream out) throws IOException{
231 out.defaultWriteObject();
232 out.writeInt(_data.length);
233 for(int i=0;i<_size;i++) {
234 out.writeDouble(_data[i]);
235 }
236 }
237
238 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
239 in.defaultReadObject();
240 _data = new double[in.readInt()];
241 for(int i=0;i<_size;i++) {
242 _data[i] = in.readDouble();
243 }
244 }
245
246 private final void checkRange(int index) {
247 if(index < 0 || index >= _size) {
248 throw new IndexOutOfBoundsException("Should be at least 0 and less than " + _size + ", found " + index);
249 }
250 }
251
252 private final void checkRangeIncludingEndpoint(int index) {
253 if(index < 0 || index > _size) {
254 throw new IndexOutOfBoundsException("Should be at least 0 and at most " + _size + ", found " + index);
255 }
256 }
257
258
259
260
261 private transient double[] _data = null;
262 private int _size = 0;
263
264 }