/* This file is part of Dynamic Surroundings, licensed under the MIT License (MIT).
 *
 * Copyright (c) OreCruncher
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.blockartistry.lib;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;

import org.blockartistry.lib.random.XorShiftRandom;

public class WeightTable<T> {

	protected final List<Item<T>> items = new ArrayList<Item<T>>(16);
	protected int totalWeight = 0;

	public static class Item<T> {

		public final int itemWeight;
		public final T item;

		public Item(@Nonnull final T item, final int weight) {
			this.itemWeight = weight;
			this.item = item;
		}
	}

	public WeightTable() {
	}
	
	@SafeVarargs
	public WeightTable(@Nonnull final IEntrySource<T>... src) {
		for (int i = 0; i < src.length; i++)
			if (src[i].matches())
				this.add(src[i].getItem(), src[i].getWeight());
	}

	public WeightTable<T> add(@Nonnull final T entry, final int itemWeight) {
		assert itemWeight > 0;
		assert entry != null;

		this.totalWeight += itemWeight;
		this.items.add(new Item<T>(entry, itemWeight));
		return this;
	}

	public WeightTable<T> add(@Nonnull final Item<T> entry) {
		assert entry != null;
		assert entry.itemWeight > 0;
		assert entry.item != null;

		this.totalWeight += entry.itemWeight;
		this.items.add(entry);
		return this;
	}

	@Nonnull
	public T next() {
		if(this.totalWeight <= 0)
			return null;
		
		int targetWeight = XorShiftRandom.current().nextInt(this.totalWeight);

		int i = -1;
		do {
			targetWeight -= this.items.get(++i).itemWeight;
		} while (targetWeight >= 0);

		return this.items.get(i).item;
	}

	public static interface IEntrySource<T> {
		int getWeight();

		T getItem();

		boolean matches();
	}
}

@@ -1242,41 +1242,16 @@
 til.
-ArrayList;%0Aimport java.util.List;
+Random;%0A
 %0Aimp
@@ -1314,127 +1314,214 @@
 lib.
-random.XorShiftRandom;%0A%0Apublic class WeightTable%3CT%3E %7B%0A%0A%09protected final List%3CItem%3CT%3E%3E items = new ArrayList%3CItem%3CT%3E%3E(16
+collections.ObjectArray;%0Aimport org.blockartistry.lib.random.XorShiftRandom;%0A%0Apublic class WeightTable%3CT%3E extends ObjectArray%3CWeightTable.Item%3CT%3E%3E %7B%0A%0A%09protected final Random RANDOM = XorShiftRandom.current(
 );%0A%09
@@ -1781,17 +1781,16 @@
 () %7B%0A%09%7D%0A
-%09
 %0A%09@SafeV
@@ -1934,16 +1934,28 @@
 his.add(
+new Item%3CT%3E(
 src%5Bi%5D.g
@@ -1983,16 +1983,17 @@
 eight())
+)
 ;%0A%09%7D%0A%0A%09p
@@ -1994,38 +1994,31 @@
 %09%7D%0A%0A%09public 
-WeightTable%3CT%3E
+boolean
  add(@Nonnul
@@ -2111,134 +2111,89 @@
 ll;%0A
-%0A
 %09%09
-this.totalWeight += itemWeight;%0A%09%09this.items.add(new Item%3CT%3E(entry, itemWeight));%0A%09%09return this;%0A%09%7D%0A%0A%09public WeightTable%3CT%3E
+return this.add(new Item%3CT%3E(entry, itemWeight));%0A%09%7D%0A%0A%09@Override%0A%09public boolean
  add
@@ -2309,17 +2309,16 @@
 = null;%0A
-%0A
 %09%09this.t
@@ -2351,26 +2351,28 @@
 ight;%0A%09%09
-this.items
+return super
 .add(ent
@@ -2381,25 +2381,42 @@
 );%0A%09
-%09return this;%0A%09%7D%0A
+%7D%0A%0A%09@SuppressWarnings(%22unchecked%22)
 %0A%09@N
@@ -2445,16 +2445,17 @@
 ) %7B%0A%09%09if
+ 
 (this.to
@@ -2486,18 +2486,16 @@
 n null;%0A
-%09%09
 %0A%09%09int t
@@ -2512,32 +2512,19 @@
 t = 
-XorShiftRandom.current()
+this.RANDOM
 .nex
@@ -2554,65 +2554,145 @@
 %0A%0A%09%09
-int i = -1;%0A%09%09do %7B%0A%09%09%09targetWeight -= this.items.get(++i)
+WeightTable.Item%3CT%3E selected = null;%0A%09%09int i = -1;%0A%09%09do %7B%0A%09%09%09selected = (WeightTable.Item%3CT%3E) this.data%5B++i%5D;%0A%09%09%09targetWeight -= selected
 .ite
@@ -2745,25 +2745,16 @@
 urn 
-this.items.get(i)
+selected
 .ite

Streamline WeightTable