| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.util.Iterator; |
| 9 |
|
| 10 |
/** |
| 11 |
* An immutable array. |
| 12 |
* |
| 13 |
* @param <K> the type |
| 14 |
*/ |
| 15 |
public abstract class ImmutableArray3<K> implements Iterable<K> { |
| 16 |
|
| 17 |
private static final int MAX_LEVEL = 4; |
| 18 |
|
| 19 |
private static final ImmutableArray3<?> EMPTY = new Plain<>(new Object[0]); |
| 20 |
|
| 21 |
/** |
| 22 |
* Get the length. |
| 23 |
* |
| 24 |
* @return the length |
| 25 |
*/ |
| 26 |
public abstract int length(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Get the entry at this index. |
| 30 |
* |
| 31 |
* @param index the index |
| 32 |
* @return the entry |
| 33 |
*/ |
| 34 |
public abstract K get(int index); |
| 35 |
|
| 36 |
/** |
| 37 |
* Set the entry at this index. |
| 38 |
* |
| 39 |
* @param index the index |
| 40 |
* @param obj the object |
| 41 |
* @return the new immutable array |
| 42 |
*/ |
| 43 |
public abstract ImmutableArray3<K> set(int index, K obj); |
| 44 |
|
| 45 |
/** |
| 46 |
* Insert an entry at this index. |
| 47 |
* |
| 48 |
* @param index the index |
| 49 |
* @param obj the object |
| 50 |
* @return the new immutable array |
| 51 |
*/ |
| 52 |
public abstract ImmutableArray3<K> insert(int index, K obj); |
| 53 |
|
| 54 |
/** |
| 55 |
* Remove the entry at this index. |
| 56 |
* |
| 57 |
* @param index the index |
| 58 |
* @return the new immutable array |
| 59 |
*/ |
| 60 |
public abstract ImmutableArray3<K> remove(int index); |
| 61 |
|
| 62 |
/** |
| 63 |
* Get a sub-array. |
| 64 |
* |
| 65 |
* @param fromIndex the index of the first entry |
| 66 |
* @param toIndex the end index, plus one |
| 67 |
* @return the new immutable array |
| 68 |
*/ |
| 69 |
public ImmutableArray3<K> subArray(int fromIndex, int toIndex) { |
| 70 |
int len = toIndex - fromIndex; |
| 71 |
@SuppressWarnings("unchecked") |
| 72 |
K[] array = (K[]) new Object[len]; |
| 73 |
for (int i = 0; i < len; i++) { |
| 74 |
array[i] = get(fromIndex + i); |
| 75 |
} |
| 76 |
return new Plain<>(array); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Create an immutable array. |
| 81 |
* |
| 82 |
* @param array the data |
| 83 |
* @return the new immutable array |
| 84 |
*/ |
| 85 |
@SafeVarargs |
| 86 |
public static <K> ImmutableArray3<K> create(K... array) { |
| 87 |
return new Plain<>(array); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get the data. |
| 92 |
* |
| 93 |
* @return the data |
| 94 |
*/ |
| 95 |
public K[] array() { |
| 96 |
int len = length(); |
| 97 |
@SuppressWarnings("unchecked") |
| 98 |
K[] array = (K[]) new Object[len]; |
| 99 |
for (int i = 0; i < len; i++) { |
| 100 |
array[i] = get(i); |
| 101 |
} |
| 102 |
return array; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get the level of "abstraction". |
| 107 |
* |
| 108 |
* @return the level |
| 109 |
*/ |
| 110 |
abstract int level(); |
| 111 |
|
| 112 |
@Override |
| 113 |
public String toString() { |
| 114 |
StringBuilder buff = new StringBuilder(); |
| 115 |
for (K obj : this) { |
| 116 |
buff.append(' ').append(obj); |
| 117 |
} |
| 118 |
return buff.toString(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get an empty immutable array. |
| 123 |
* |
| 124 |
* @param <K> the key type |
| 125 |
* @return the array |
| 126 |
*/ |
| 127 |
@SuppressWarnings("unchecked") |
| 128 |
public static <K> ImmutableArray3<K> empty() { |
| 129 |
return (ImmutableArray3<K>) EMPTY; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get an iterator over all entries. |
| 134 |
* |
| 135 |
* @return the iterator |
| 136 |
*/ |
| 137 |
@Override |
| 138 |
public Iterator<K> iterator() { |
| 139 |
return new Iterator<K>() { |
| 140 |
|
| 141 |
ImmutableArray3<K> a = ImmutableArray3.this; |
| 142 |
int index; |
| 143 |
|
| 144 |
@Override |
| 145 |
public boolean hasNext() { |
| 146 |
return index < a.length(); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public K next() { |
| 151 |
return a.get(index++); |
| 152 |
} |
| 153 |
|
| 154 |
}; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* An immutable array backed by an array. |
| 160 |
* |
| 161 |
* @param <K> the type |
| 162 |
*/ |
| 163 |
static class Plain<K> extends ImmutableArray3<K> { |
| 164 |
|
| 165 |
/** |
| 166 |
* The array. |
| 167 |
*/ |
| 168 |
private final K[] array; |
| 169 |
|
| 170 |
public Plain(K[] array) { |
| 171 |
this.array = array; |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public K get(int index) { |
| 176 |
return array[index]; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public int length() { |
| 181 |
return array.length; |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public ImmutableArray3<K> set(int index, K obj) { |
| 186 |
return new Set<>(this, index, obj); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public ImmutableArray3<K> insert(int index, K obj) { |
| 191 |
return new Insert<>(this, index, obj); |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public ImmutableArray3<K> remove(int index) { |
| 196 |
return new Remove<>(this, index); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Get a plain array with the given entry updated. |
| 201 |
* |
| 202 |
* @param <K> the type |
| 203 |
* @param base the base type |
| 204 |
* @param index the index |
| 205 |
* @param obj the object |
| 206 |
* @return the immutable array |
| 207 |
*/ |
| 208 |
static <K> ImmutableArray3<K> set(ImmutableArray3<K> base, int index, K obj) { |
| 209 |
int len = base.length(); |
| 210 |
@SuppressWarnings("unchecked") |
| 211 |
K[] array = (K[]) new Object[len]; |
| 212 |
for (int i = 0; i < len; i++) { |
| 213 |
array[i] = i == index ? obj : base.get(i); |
| 214 |
} |
| 215 |
return new Plain<>(array); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Get a plain array with the given entry inserted. |
| 220 |
* |
| 221 |
* @param <K> the type |
| 222 |
* @param base the base type |
| 223 |
* @param index the index |
| 224 |
* @param obj the object |
| 225 |
* @return the immutable array |
| 226 |
*/ |
| 227 |
static <K> ImmutableArray3<K> insert(ImmutableArray3<K> base, int index, K obj) { |
| 228 |
int len = base.length() + 1; |
| 229 |
@SuppressWarnings("unchecked") |
| 230 |
K[] array = (K[]) new Object[len]; |
| 231 |
for (int i = 0; i < len; i++) { |
| 232 |
array[i] = i == index ? obj : i < index ? base.get(i) : base.get(i - 1); |
| 233 |
} |
| 234 |
return new Plain<>(array); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Get a plain array with the given entry removed. |
| 239 |
* |
| 240 |
* @param <K> the type |
| 241 |
* @param base the base type |
| 242 |
* @param index the index |
| 243 |
* @return the immutable array |
| 244 |
*/ |
| 245 |
static <K> ImmutableArray3<K> remove(ImmutableArray3<K> base, int index) { |
| 246 |
int len = base.length() - 1; |
| 247 |
@SuppressWarnings("unchecked") |
| 248 |
K[] array = (K[]) new Object[len]; |
| 249 |
for (int i = 0; i < len; i++) { |
| 250 |
array[i] = i < index ? base.get(i) : base.get(i + 1); |
| 251 |
} |
| 252 |
return new Plain<>(array); |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
int level() { |
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* An immutable array backed by another immutable array, with one element |
| 264 |
* changed. |
| 265 |
* |
| 266 |
* @param <K> the type |
| 267 |
*/ |
| 268 |
static class Set<K> extends ImmutableArray3<K> { |
| 269 |
|
| 270 |
private final int index; |
| 271 |
private final ImmutableArray3<K> base; |
| 272 |
private final K obj; |
| 273 |
|
| 274 |
Set(ImmutableArray3<K> base, int index, K obj) { |
| 275 |
this.base = base; |
| 276 |
this.index = index; |
| 277 |
this.obj = obj; |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public int length() { |
| 282 |
return base.length(); |
| 283 |
} |
| 284 |
|
| 285 |
@Override |
| 286 |
public K get(int index) { |
| 287 |
return this.index == index ? obj : base.get(index); |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public ImmutableArray3<K> set(int index, K obj) { |
| 292 |
if (index == this.index) { |
| 293 |
return new Set<>(base, index, obj); |
| 294 |
} else if (level() < MAX_LEVEL) { |
| 295 |
return new Set<>(this, index, obj); |
| 296 |
} |
| 297 |
return Plain.set(this, index, obj); |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public ImmutableArray3<K> insert(int index, K obj) { |
| 302 |
if (level() < MAX_LEVEL) { |
| 303 |
return new Insert<>(this, index, obj); |
| 304 |
} |
| 305 |
return Plain.insert(this, index, obj); |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public ImmutableArray3<K> remove(int index) { |
| 310 |
if (level() < MAX_LEVEL) { |
| 311 |
return new Remove<>(this, index); |
| 312 |
} |
| 313 |
return Plain.remove(this, index); |
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
int level() { |
| 318 |
return base.level() + 1; |
| 319 |
} |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* An immutable array backed by another immutable array, with one element |
| 325 |
* added. |
| 326 |
* |
| 327 |
* @param <K> the type |
| 328 |
*/ |
| 329 |
static class Insert<K> extends ImmutableArray3<K> { |
| 330 |
|
| 331 |
private final int index; |
| 332 |
private final ImmutableArray3<K> base; |
| 333 |
private final K obj; |
| 334 |
|
| 335 |
Insert(ImmutableArray3<K> base, int index, K obj) { |
| 336 |
this.base = base; |
| 337 |
this.index = index; |
| 338 |
this.obj = obj; |
| 339 |
} |
| 340 |
|
| 341 |
@Override |
| 342 |
public ImmutableArray3<K> set(int index, K obj) { |
| 343 |
if (level() < MAX_LEVEL) { |
| 344 |
return new Set<>(this, index, obj); |
| 345 |
} |
| 346 |
return Plain.set(this, index, obj); |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public ImmutableArray3<K> insert(int index, K obj) { |
| 351 |
if (level() < MAX_LEVEL) { |
| 352 |
return new Insert<>(this, index, obj); |
| 353 |
} |
| 354 |
return Plain.insert(this, index, obj); |
| 355 |
} |
| 356 |
|
| 357 |
@Override |
| 358 |
public ImmutableArray3<K> remove(int index) { |
| 359 |
if (index == this.index) { |
| 360 |
return base; |
| 361 |
} else if (level() < MAX_LEVEL) { |
| 362 |
return new Remove<>(this, index); |
| 363 |
} |
| 364 |
return Plain.remove(this, index); |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public int length() { |
| 369 |
return base.length() + 1; |
| 370 |
} |
| 371 |
|
| 372 |
@Override |
| 373 |
public K get(int index) { |
| 374 |
if (index == this.index) { |
| 375 |
return obj; |
| 376 |
} else if (index < this.index) { |
| 377 |
return base.get(index); |
| 378 |
} |
| 379 |
return base.get(index - 1); |
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
int level() { |
| 384 |
return base.level() + 1; |
| 385 |
} |
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* An immutable array backed by another immutable array, with one element |
| 391 |
* removed. |
| 392 |
* |
| 393 |
* @param <K> the type |
| 394 |
*/ |
| 395 |
static class Remove<K> extends ImmutableArray3<K> { |
| 396 |
|
| 397 |
private final int index; |
| 398 |
private final ImmutableArray3<K> base; |
| 399 |
|
| 400 |
Remove(ImmutableArray3<K> base, int index) { |
| 401 |
this.base = base; |
| 402 |
this.index = index; |
| 403 |
} |
| 404 |
|
| 405 |
@Override |
| 406 |
public ImmutableArray3<K> set(int index, K obj) { |
| 407 |
if (level() < MAX_LEVEL) { |
| 408 |
return new Set<>(this, index, obj); |
| 409 |
} |
| 410 |
return Plain.set(this, index, obj); |
| 411 |
} |
| 412 |
|
| 413 |
@Override |
| 414 |
public ImmutableArray3<K> insert(int index, K obj) { |
| 415 |
if (index == this.index) { |
| 416 |
return base.set(index, obj); |
| 417 |
} else if (level() < MAX_LEVEL) { |
| 418 |
return new Insert<>(this, index, obj); |
| 419 |
} |
| 420 |
return Plain.insert(this, index, obj); |
| 421 |
} |
| 422 |
|
| 423 |
@Override |
| 424 |
public ImmutableArray3<K> remove(int index) { |
| 425 |
if (level() < MAX_LEVEL) { |
| 426 |
return new Remove<>(this, index); |
| 427 |
} |
| 428 |
return Plain.remove(this, index); |
| 429 |
} |
| 430 |
|
| 431 |
@Override |
| 432 |
public int length() { |
| 433 |
return base.length() - 1; |
| 434 |
} |
| 435 |
|
| 436 |
@Override |
| 437 |
public K get(int index) { |
| 438 |
if (index < this.index) { |
| 439 |
return base.get(index); |
| 440 |
} |
| 441 |
return base.get(index + 1); |
| 442 |
} |
| 443 |
|
| 444 |
@Override |
| 445 |
int level() { |
| 446 |
return base.level() + 1; |
| 447 |
} |
| 448 |
|
| 449 |
} |
| 450 |
|
| 451 |
} |