Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.ruby.runtime/src/com/oracle/truffle/ruby/runtime/core/array/ObjectArrayStore.java @ 13514:0fbee3eb71f0
Ruby: import project.
author | Chris Seaton <chris.seaton@oracle.com> |
---|---|
date | Mon, 06 Jan 2014 17:12:09 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
13513:64a23ce736a0 | 13514:0fbee3eb71f0 |
---|---|
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.ruby.runtime.*; | |
15 | |
16 /** | |
17 * A store for an array of any objects. | |
18 */ | |
19 public final class ObjectArrayStore extends BaseArrayStore { | |
20 | |
21 private Object[] values; | |
22 | |
23 public ObjectArrayStore() { | |
24 this(new Object[]{}); | |
25 } | |
26 | |
27 public ObjectArrayStore(Object[] values) { | |
28 this.values = values; | |
29 size = values.length; | |
30 capacity = values.length; | |
31 } | |
32 | |
33 @Override | |
34 public Object get(int normalisedIndex) { | |
35 if (normalisedIndex >= size) { | |
36 return NilPlaceholder.INSTANCE; | |
37 } | |
38 | |
39 return values[normalisedIndex]; | |
40 } | |
41 | |
42 @Override | |
43 public ArrayStore getRange(int normalisedBegin, int normalisedExclusiveEnd) { | |
44 if (normalisedBegin >= size) { | |
45 return null; // Represents Nil | |
46 } | |
47 | |
48 return new ObjectArrayStore(Arrays.copyOfRange(values, normalisedBegin, normalisedExclusiveEnd)); | |
49 } | |
50 | |
51 @Override | |
52 public void set(int normalisedIndex, Object value) throws GeneraliseArrayStoreException { | |
53 if (normalisedIndex > size) { | |
54 final int originalLength = size; | |
55 createSpace(size, normalisedIndex - size + 1); | |
56 Arrays.fill(values, originalLength, normalisedIndex, NilPlaceholder.INSTANCE); | |
57 values[normalisedIndex] = value; | |
58 } else if (normalisedIndex == size) { | |
59 push(value); | |
60 } else { | |
61 values[normalisedIndex] = value; | |
62 } | |
63 } | |
64 | |
65 @Override | |
66 public void setRangeSingle(int normalisedBegin, int normalisedExclusiveEnd, Object value) throws GeneraliseArrayStoreException { | |
67 // Is the range the whole array? | |
68 | |
69 if (normalisedBegin == 0 && normalisedExclusiveEnd == size) { | |
70 // Reset length and set the value. | |
71 size = 1; | |
72 values[0] = value; | |
73 } else { | |
74 // Delete the range, except for the first value. | |
75 deleteSpace(normalisedBegin + 1, normalisedExclusiveEnd - normalisedBegin - 1); | |
76 | |
77 // Set the value we left in. | |
78 System.err.println(normalisedBegin + " in " + size + " with " + values.length); | |
79 values[normalisedBegin] = value; | |
80 } | |
81 } | |
82 | |
83 @Override | |
84 public void setRangeArray(int normalisedBegin, int normalisedExclusiveEnd, ArrayStore other) throws GeneraliseArrayStoreException { | |
85 setRangeArray(normalisedBegin, normalisedExclusiveEnd, (ObjectArrayStore) other.generalizeFor(null)); | |
86 } | |
87 | |
88 public void setRangeArray(int normalisedBegin, int normalisedExclusiveEnd, ObjectArrayStore other) { | |
89 setRangeArrayMatchingTypes(normalisedBegin, normalisedExclusiveEnd, other.values, other.size); | |
90 } | |
91 | |
92 @Override | |
93 public void insert(int normalisedIndex, Object value) throws GeneraliseArrayStoreException { | |
94 if (normalisedIndex > size) { | |
95 final int originalLength = size; | |
96 createSpaceAtEnd(normalisedIndex - size + 1); | |
97 Arrays.fill(values, originalLength, normalisedIndex, NilPlaceholder.INSTANCE); | |
98 values[normalisedIndex] = value; | |
99 } else { | |
100 createSpace(normalisedIndex, 1); | |
101 values[normalisedIndex] = value; | |
102 } | |
103 } | |
104 | |
105 @Override | |
106 public void push(Object value) throws GeneraliseArrayStoreException { | |
107 createSpaceAtEnd(1); | |
108 values[size - 1] = value; | |
109 } | |
110 | |
111 @Override | |
112 public Object deleteAt(int normalisedIndex) { | |
113 if (normalisedIndex >= size) { | |
114 return NilPlaceholder.INSTANCE; | |
115 } | |
116 | |
117 final Object value = values[normalisedIndex]; | |
118 | |
119 deleteSpace(normalisedIndex, 1); | |
120 | |
121 return value; | |
122 } | |
123 | |
124 @Override | |
125 public ArrayStore dup() { | |
126 return new ObjectArrayStore(Arrays.copyOf(values, size)); | |
127 } | |
128 | |
129 @Override | |
130 public boolean contains(Object value) { | |
131 for (int n = 0; n < size; n++) { | |
132 if (values[n].equals(value)) { | |
133 return true; | |
134 } | |
135 } | |
136 | |
137 return false; | |
138 } | |
139 | |
140 @Override | |
141 public ObjectArrayStore generalizeFor(Object type) { | |
142 return this; | |
143 } | |
144 | |
145 @Override | |
146 public Object getIndicativeValue() { | |
147 return null; | |
148 } | |
149 | |
150 @Override | |
151 protected void setCapacityByCopying(int newCapacity) { | |
152 values = Arrays.copyOf(values, newCapacity); | |
153 capacity = values.length; | |
154 } | |
155 | |
156 @Override | |
157 protected void setCapacityWithNewArray(int newCapacity) { | |
158 values = new Object[newCapacity]; | |
159 capacity = values.length; | |
160 } | |
161 | |
162 @Override | |
163 protected Object getValuesArrayObject() { | |
164 return values; | |
165 } | |
166 | |
167 public Object[] getValues() { | |
168 return values; | |
169 } | |
170 | |
171 @Override | |
172 public Object[] toObjectArray() { | |
173 if (values.length == size) { | |
174 return values; | |
175 } else { | |
176 return Arrays.copyOf(values, size); | |
177 } | |
178 } | |
179 | |
180 @Override | |
181 public boolean equals(ArrayStore other) { | |
182 if (other instanceof ObjectArrayStore) { | |
183 return equals((ObjectArrayStore) other); | |
184 } else { | |
185 return super.equals(other); | |
186 } | |
187 } | |
188 | |
189 public boolean equals(ObjectArrayStore other) { | |
190 if (other == null) { | |
191 return false; | |
192 } else if (other == this) { | |
193 return true; | |
194 } else if (other.size != size) { | |
195 return false; | |
196 } else if (other.capacity == capacity) { | |
197 return Arrays.equals(other.values, values); | |
198 } else { | |
199 for (int n = 0; n < size; n++) { | |
200 if (!other.values[n].equals(values[n])) { | |
201 return false; | |
202 } | |
203 } | |
204 | |
205 return true; | |
206 } | |
207 } | |
208 } |