diff options
Diffstat (limited to '')
-rw-r--r-- | src/datastructures/linkedlists/template | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/src/datastructures/linkedlists/template b/src/datastructures/linkedlists/template index 53a7c4a..b225457 100644 --- a/src/datastructures/linkedlists/template +++ b/src/datastructures/linkedlists/template @@ -120,6 +120,21 @@ public class £{name}<T> } + /** + * Constructor + */ + public £{name}(Class<T> tClass) + { + this.tClass = tClass; +£>if (( with_sentinel )); then +£>new node null + this.edge = node; +£>for var in $refs; do + £($var this.edge) = £(singularity this.edge); +£>done +£>fi + } + £>else /** @@ -153,9 +168,10 @@ public class £{name}<T> /** * Constructor */ - public £{name}() + @SuppressWarnings("unchecked") + public £{name}(Class<T> tClass) { - this(DEFAULT_INITIAL_CAPACITY); + this(tClass, DEFAULT_INITIAL_CAPACITY); } /** @@ -164,15 +180,23 @@ public class £{name}<T> * @param initialCapacity The initial size of the arrays */ @SuppressWarnings("unchecked") - public £{name}(int initialCapacity) + public £{name}(Class<T> tClass, int initialCapacity) { + this.tClass = tClass; initialCapacity = algorithms.bits.Powers.toPowerOf2(initialCapacity); this.capacity = initialCapacity; £>for var in $refs reusable; do this.£{var} = new int[initialCapacity]; £>done - this.values = (T[])(new Object[initialCapacity]); + this.values = (T[])(Array.newInstance(this.tClass, initialCapacity)); +£>if (( with_sentinel )); then +£>new node null + this.edge = node; +£>for var in $refs; do + £($var this.edge) = £(singularity this.edge); +£>done +£>fi } @@ -233,22 +257,27 @@ public class £{name}<T> £>fi £>fi + /** + * Java's generics erasure is kind of silly + * when it comes to arrays. We can either + * create {@code Object[]} and cast it to + * {@code T[]} and let the user of the class + * cast it back to {@code Object[]}, get an + * element and cast it back to {@code T}; or + * we can let the user not only specify + * {@code <T>} when calling the constructor, + * but also add an {@code T.class} argument + * and then store that argument so we can + * at any time use the class {@link Array} + * to create an array. + */ + private Class<T> tClass; + £>if (( with_sentinel )); then /** * The sentinel node in the list */ public £{Node} edge; - - /** - * Initialiser - */ - { -£>new node null - this.edge = node; -£>for var in $refs; do - £($var this.edge) = £(singularity this.edge); -£>done - } £>fi £>if (( with_head )); then @@ -290,7 +319,7 @@ public class £{name}<T> £>(( with_xor )) && int prev = EDGE; @SuppressWarnings("unchecked") - T[] vals = (T[])(new Object[cap]); + T[] vals = (T[])(Array.newInstance(this.tClass, cap)); £>if (( 1 - with_head )); then int head = 0; while ((head < this.end) && (£($forward head) == UNUSED)) @@ -359,7 +388,7 @@ public class £{name}<T> if (this.end == this.capacity) { this.capacity <<= 1; - System.arraycopy(this.values, 0, this.values = (T[])(new Object[this.capacity]), 0, this.end); + System.arraycopy(this.values, 0, this.values = (T[])(Array.newInstance(this.tClass, this.capacity)), 0, this.end); £>for var in $refs reusable; do System.arraycopy(this.£{var}, 0, this.£{var} = new int[this.capacity], 0, this.end); £>done |