/* * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (https://h2database.com/html/license.html). * Initial Developer: H2 Group */ package org.h2.dev.util; import java.util.Iterator; /** * An immutable array. * * @param the type */ public abstract class ImmutableArray3 implements Iterable { private static final int MAX_LEVEL = 4; private static final ImmutableArray3 EMPTY = new Plain<>(new Object[0]); /** * Get the length. * * @return the length */ public abstract int length(); /** * Get the entry at this index. * * @param index the index * @return the entry */ public abstract K get(int index); /** * Set the entry at this index. * * @param index the index * @param obj the object * @return the new immutable array */ public abstract ImmutableArray3 set(int index, K obj); /** * Insert an entry at this index. * * @param index the index * @param obj the object * @return the new immutable array */ public abstract ImmutableArray3 insert(int index, K obj); /** * Remove the entry at this index. * * @param index the index * @return the new immutable array */ public abstract ImmutableArray3 remove(int index); /** * Get a sub-array. * * @param fromIndex the index of the first entry * @param toIndex the end index, plus one * @return the new immutable array */ public ImmutableArray3 subArray(int fromIndex, int toIndex) { int len = toIndex - fromIndex; @SuppressWarnings("unchecked") K[] array = (K[]) new Object[len]; for (int i = 0; i < len; i++) { array[i] = get(fromIndex + i); } return new Plain<>(array); } /** * Create an immutable array. * * @param array the data * @return the new immutable array */ @SafeVarargs public static ImmutableArray3 create(K... array) { return new Plain<>(array); } /** * Get the data. * * @return the data */ public K[] array() { int len = length(); @SuppressWarnings("unchecked") K[] array = (K[]) new Object[len]; for (int i = 0; i < len; i++) { array[i] = get(i); } return array; } /** * Get the level of "abstraction". * * @return the level */ abstract int level(); @Override public String toString() { StringBuilder buff = new StringBuilder(); for (K obj : this) { buff.append(' ').append(obj); } return buff.toString(); } /** * Get an empty immutable array. * * @param the key type * @return the array */ @SuppressWarnings("unchecked") public static ImmutableArray3 empty() { return (ImmutableArray3) EMPTY; } /** * Get an iterator over all entries. * * @return the iterator */ @Override public Iterator iterator() { return new Iterator() { ImmutableArray3 a = ImmutableArray3.this; int index; @Override public boolean hasNext() { return index < a.length(); } @Override public K next() { return a.get(index++); } }; } /** * An immutable array backed by an array. * * @param the type */ static class Plain extends ImmutableArray3 { /** * The array. */ private final K[] array; public Plain(K[] array) { this.array = array; } @Override public K get(int index) { return array[index]; } @Override public int length() { return array.length; } @Override public ImmutableArray3 set(int index, K obj) { return new Set<>(this, index, obj); } @Override public ImmutableArray3 insert(int index, K obj) { return new Insert<>(this, index, obj); } @Override public ImmutableArray3 remove(int index) { return new Remove<>(this, index); } /** * Get a plain array with the given entry updated. * * @param the type * @param base the base type * @param index the index * @param obj the object * @return the immutable array */ static ImmutableArray3 set(ImmutableArray3 base, int index, K obj) { int len = base.length(); @SuppressWarnings("unchecked") K[] array = (K[]) new Object[len]; for (int i = 0; i < len; i++) { array[i] = i == index ? obj : base.get(i); } return new Plain<>(array); } /** * Get a plain array with the given entry inserted. * * @param the type * @param base the base type * @param index the index * @param obj the object * @return the immutable array */ static ImmutableArray3 insert(ImmutableArray3 base, int index, K obj) { int len = base.length() + 1; @SuppressWarnings("unchecked") K[] array = (K[]) new Object[len]; for (int i = 0; i < len; i++) { array[i] = i == index ? obj : i < index ? base.get(i) : base.get(i - 1); } return new Plain<>(array); } /** * Get a plain array with the given entry removed. * * @param the type * @param base the base type * @param index the index * @return the immutable array */ static ImmutableArray3 remove(ImmutableArray3 base, int index) { int len = base.length() - 1; @SuppressWarnings("unchecked") K[] array = (K[]) new Object[len]; for (int i = 0; i < len; i++) { array[i] = i < index ? base.get(i) : base.get(i + 1); } return new Plain<>(array); } @Override int level() { return 0; } } /** * An immutable array backed by another immutable array, with one element * changed. * * @param the type */ static class Set extends ImmutableArray3 { private final int index; private final ImmutableArray3 base; private final K obj; Set(ImmutableArray3 base, int index, K obj) { this.base = base; this.index = index; this.obj = obj; } @Override public int length() { return base.length(); } @Override public K get(int index) { return this.index == index ? obj : base.get(index); } @Override public ImmutableArray3 set(int index, K obj) { if (index == this.index) { return new Set<>(base, index, obj); } else if (level() < MAX_LEVEL) { return new Set<>(this, index, obj); } return Plain.set(this, index, obj); } @Override public ImmutableArray3 insert(int index, K obj) { if (level() < MAX_LEVEL) { return new Insert<>(this, index, obj); } return Plain.insert(this, index, obj); } @Override public ImmutableArray3 remove(int index) { if (level() < MAX_LEVEL) { return new Remove<>(this, index); } return Plain.remove(this, index); } @Override int level() { return base.level() + 1; } } /** * An immutable array backed by another immutable array, with one element * added. * * @param the type */ static class Insert extends ImmutableArray3 { private final int index; private final ImmutableArray3 base; private final K obj; Insert(ImmutableArray3 base, int index, K obj) { this.base = base; this.index = index; this.obj = obj; } @Override public ImmutableArray3 set(int index, K obj) { if (level() < MAX_LEVEL) { return new Set<>(this, index, obj); } return Plain.set(this, index, obj); } @Override public ImmutableArray3 insert(int index, K obj) { if (level() < MAX_LEVEL) { return new Insert<>(this, index, obj); } return Plain.insert(this, index, obj); } @Override public ImmutableArray3 remove(int index) { if (index == this.index) { return base; } else if (level() < MAX_LEVEL) { return new Remove<>(this, index); } return Plain.remove(this, index); } @Override public int length() { return base.length() + 1; } @Override public K get(int index) { if (index == this.index) { return obj; } else if (index < this.index) { return base.get(index); } return base.get(index - 1); } @Override int level() { return base.level() + 1; } } /** * An immutable array backed by another immutable array, with one element * removed. * * @param the type */ static class Remove extends ImmutableArray3 { private final int index; private final ImmutableArray3 base; Remove(ImmutableArray3 base, int index) { this.base = base; this.index = index; } @Override public ImmutableArray3 set(int index, K obj) { if (level() < MAX_LEVEL) { return new Set<>(this, index, obj); } return Plain.set(this, index, obj); } @Override public ImmutableArray3 insert(int index, K obj) { if (index == this.index) { return base.set(index, obj); } else if (level() < MAX_LEVEL) { return new Insert<>(this, index, obj); } return Plain.insert(this, index, obj); } @Override public ImmutableArray3 remove(int index) { if (level() < MAX_LEVEL) { return new Remove<>(this, index); } return Plain.remove(this, index); } @Override public int length() { return base.length() - 1; } @Override public K get(int index) { if (index < this.index) { return base.get(index); } return base.get(index + 1); } @Override int level() { return base.level() + 1; } } }