From b4489e38bff16c4f6ce9ef186f2feed80644fa32 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 12 Oct 2014 03:39:31 +0200 Subject: fix errors and start on a performance test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/datastructures/linkedlists/template | 63 ++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 17 deletions(-) (limited to 'src/datastructures/linkedlists/template') 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} } + /** + * Constructor + */ + public £{name}(Class 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} /** * Constructor */ - public £{name}() + @SuppressWarnings("unchecked") + public £{name}(Class tClass) { - this(DEFAULT_INITIAL_CAPACITY); + this(tClass, DEFAULT_INITIAL_CAPACITY); } /** @@ -164,15 +180,23 @@ public class £{name} * @param initialCapacity The initial size of the arrays */ @SuppressWarnings("unchecked") - public £{name}(int initialCapacity) + public £{name}(Class 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} £>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 } 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 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} £>(( 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} 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 -- cgit v1.2.3-70-g09d2