13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.runtime.core.array;
|
|
11
|
|
12 import java.util.*;
|
|
13
|
|
14 import com.oracle.truffle.api.*;
|
|
15 import com.oracle.truffle.api.nodes.*;
|
|
16 import com.oracle.truffle.ruby.runtime.*;
|
|
17
|
|
18 /**
|
|
19 * A store for an array of Fixnums.
|
|
20 */
|
|
21 public final class FixnumArrayStore extends BaseArrayStore {
|
|
22
|
|
23 private int[] values;
|
|
24
|
|
25 public FixnumArrayStore() {
|
|
26 this(new int[]{});
|
|
27 }
|
|
28
|
|
29 public FixnumArrayStore(int[] values) {
|
|
30 this.values = values;
|
|
31 size = values.length;
|
|
32 capacity = values.length;
|
|
33 }
|
|
34
|
|
35 @Override
|
|
36 public Object get(int normalisedIndex) {
|
|
37 try {
|
|
38 return getFixnum(normalisedIndex);
|
|
39 } catch (UnexpectedResultException e) {
|
|
40 return e.getResult();
|
|
41 }
|
|
42 }
|
|
43
|
|
44 public int getFixnum(int normalisedIndex) throws UnexpectedResultException {
|
|
45 if (normalisedIndex >= size) {
|
|
46 throw new UnexpectedResultException(NilPlaceholder.INSTANCE);
|
|
47 }
|
|
48
|
|
49 return values[normalisedIndex];
|
|
50 }
|
|
51
|
|
52 @Override
|
|
53 public ArrayStore getRange(int normalisedBegin, int truncatedNormalisedExclusiveEnd) {
|
|
54 if (normalisedBegin >= size) {
|
|
55 return null; // Represents Nil
|
|
56 }
|
|
57
|
|
58 return new FixnumArrayStore(Arrays.copyOfRange(values, normalisedBegin, truncatedNormalisedExclusiveEnd));
|
|
59 }
|
|
60
|
|
61 @Override
|
|
62 public void set(int normalisedIndex, Object value) throws GeneraliseArrayStoreException {
|
|
63 if (value instanceof Integer) {
|
|
64 setFixnum(normalisedIndex, (int) value);
|
|
65 } else {
|
|
66 throw new GeneraliseArrayStoreException();
|
|
67 }
|
|
68 }
|
|
69
|
|
70 public void setFixnum(int normalisedIndex, int value) throws GeneraliseArrayStoreException {
|
|
71 if (normalisedIndex > size) {
|
|
72 throw new GeneraliseArrayStoreException();
|
|
73 }
|
|
74
|
|
75 if (normalisedIndex == size) {
|
|
76 push(value);
|
|
77 } else {
|
|
78 values[normalisedIndex] = value;
|
|
79 }
|
|
80 }
|
|
81
|
|
82 @Override
|
|
83 public void setRangeSingle(int normalisedBegin, int truncatedNormalisedExclusiveEnd, Object value) throws GeneraliseArrayStoreException {
|
|
84 if (value instanceof Integer) {
|
|
85 setRangeSingleFixnum(normalisedBegin, truncatedNormalisedExclusiveEnd, (int) value);
|
|
86 } else {
|
|
87 throw new GeneraliseArrayStoreException();
|
|
88 }
|
|
89 }
|
|
90
|
|
91 public void setRangeSingleFixnum(int normalisedBegin, int truncatedNormalisedExclusiveEnd, int value) {
|
|
92 // Is the range the whole array?
|
|
93
|
|
94 if (normalisedBegin == 0 && truncatedNormalisedExclusiveEnd == size) {
|
|
95 // Reset length and set the value.
|
|
96 size = 1;
|
|
97 values[0] = value;
|
|
98 } else {
|
|
99 // Delete the range, except for the first value.
|
|
100 deleteSpace(normalisedBegin + 1, truncatedNormalisedExclusiveEnd - normalisedBegin - 1);
|
|
101
|
|
102 // Set the value we left in.
|
|
103 values[normalisedBegin] = value;
|
|
104 }
|
|
105 }
|
|
106
|
|
107 @Override
|
|
108 public void setRangeArray(int normalisedBegin, int normalisedExclusiveEnd, ArrayStore other) throws GeneraliseArrayStoreException {
|
|
109 if (other instanceof FixnumArrayStore) {
|
|
110 setRangeArrayFixnum(normalisedBegin, normalisedExclusiveEnd, (FixnumArrayStore) other);
|
|
111 } else {
|
|
112 throw new GeneraliseArrayStoreException();
|
|
113 }
|
|
114 }
|
|
115
|
|
116 public void setRangeArrayFixnum(int normalisedBegin, int normalisedExclusiveEnd, FixnumArrayStore other) {
|
|
117 setRangeArrayMatchingTypes(normalisedBegin, normalisedExclusiveEnd, other.values, other.size);
|
|
118 }
|
|
119
|
|
120 @Override
|
|
121 public void insert(int normalisedIndex, Object value) throws GeneraliseArrayStoreException {
|
|
122 if (value instanceof Integer) {
|
|
123 insertFixnum(normalisedIndex, (int) value);
|
|
124 } else {
|
|
125 throw new GeneraliseArrayStoreException();
|
|
126 }
|
|
127 }
|
|
128
|
|
129 public void insertFixnum(int normalisedIndex, int value) throws GeneraliseArrayStoreException {
|
|
130 if (normalisedIndex > size) {
|
|
131 throw new GeneraliseArrayStoreException();
|
|
132 }
|
|
133
|
|
134 createSpace(normalisedIndex, 1);
|
|
135 values[normalisedIndex] = value;
|
|
136 }
|
|
137
|
|
138 @Override
|
|
139 public void push(Object value) throws GeneraliseArrayStoreException {
|
|
140 if (value instanceof Integer) {
|
|
141 pushFixnum((int) value);
|
|
142 } else {
|
|
143 throw new GeneraliseArrayStoreException();
|
|
144 }
|
|
145 }
|
|
146
|
|
147 public void pushFixnum(int value) {
|
|
148 createSpaceAtEnd(1);
|
|
149 values[size - 1] = value;
|
|
150 }
|
|
151
|
|
152 @Override
|
|
153 public Object deleteAt(int normalisedIndex) {
|
|
154 try {
|
|
155 return deleteAtFixnum(normalisedIndex);
|
|
156 } catch (UnexpectedResultException e) {
|
|
157 return e.getResult();
|
|
158 }
|
|
159 }
|
|
160
|
|
161 public int deleteAtFixnum(int normalisedIndex) throws UnexpectedResultException {
|
|
162 if (normalisedIndex >= size) {
|
|
163 CompilerDirectives.transferToInterpreter();
|
|
164 throw new UnexpectedResultException(NilPlaceholder.INSTANCE);
|
|
165 }
|
|
166
|
|
167 final int value = values[normalisedIndex];
|
|
168
|
|
169 deleteSpace(normalisedIndex, 1);
|
|
170
|
|
171 return value;
|
|
172 }
|
|
173
|
|
174 @Override
|
|
175 public ArrayStore dup() {
|
|
176 return new FixnumArrayStore(Arrays.copyOf(values, size));
|
|
177 }
|
|
178
|
|
179 @Override
|
|
180 public boolean contains(Object value) {
|
|
181 if (!(value instanceof Integer)) {
|
|
182 return false;
|
|
183 }
|
|
184
|
|
185 final int intValue = (int) value;
|
|
186
|
|
187 for (int n = 0; n < size; n++) {
|
|
188 if (values[n] == intValue) {
|
|
189 return true;
|
|
190 }
|
|
191 }
|
|
192
|
|
193 return false;
|
|
194 }
|
|
195
|
|
196 @Override
|
|
197 public ArrayStore generalizeFor(Object type) {
|
|
198 return new ObjectArrayStore(toObjectArray());
|
|
199 }
|
|
200
|
|
201 @Override
|
|
202 public Object getIndicativeValue() {
|
|
203 return 0;
|
|
204 }
|
|
205
|
|
206 @Override
|
|
207 protected void setCapacityByCopying(int newCapacity) {
|
|
208 values = Arrays.copyOf(values, newCapacity);
|
|
209 capacity = values.length;
|
|
210 }
|
|
211
|
|
212 @Override
|
|
213 protected void setCapacityWithNewArray(int newCapacity) {
|
|
214 values = new int[newCapacity];
|
|
215 capacity = values.length;
|
|
216 }
|
|
217
|
|
218 @Override
|
|
219 protected Object getValuesArrayObject() {
|
|
220 return values;
|
|
221 }
|
|
222
|
|
223 @Override
|
|
224 public Object[] toObjectArray() {
|
|
225 final Object[] objectValues = new Object[size];
|
|
226
|
|
227 // System.arraycopy will not box.
|
|
228
|
|
229 for (int n = 0; n < size; n++) {
|
|
230 objectValues[n] = values[n];
|
|
231 }
|
|
232
|
|
233 return objectValues;
|
|
234 }
|
|
235
|
|
236 @Override
|
|
237 public boolean equals(ArrayStore other) {
|
|
238 if (other instanceof FixnumArrayStore) {
|
|
239 return equals((FixnumArrayStore) other);
|
|
240 } else {
|
|
241 return super.equals(other);
|
|
242 }
|
|
243 }
|
|
244
|
|
245 public boolean equals(FixnumArrayStore other) {
|
|
246 if (other == null) {
|
|
247 return false;
|
|
248 } else if (other == this) {
|
|
249 return true;
|
|
250 } else if (other.size != size) {
|
|
251 return false;
|
|
252 } else if (other.capacity == capacity) {
|
|
253 return Arrays.equals(other.values, values);
|
|
254 } else {
|
|
255 for (int n = 0; n < size; n++) {
|
|
256 if (other.values[n] != values[n]) {
|
|
257 return false;
|
|
258 }
|
|
259 }
|
|
260
|
|
261 return true;
|
|
262 }
|
|
263 }
|
|
264 }
|