/*	GenChess, a genesis chess engine
	Copyright (C) 2012, Justin Madru (justin.jdm64@gmail.com)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.chess.genesis.engine;

class TransTable
{
	protected final TransItem[] table;
	protected final int size;

	public TransTable(final Board board, final int num_MB)
	{
		final Rand64 rad = new Rand64();
		final long[] hashBox = board.getHashBox();

		for (int i = 0; i < hashBox.length; i++)
			hashBox[i] = rad.next();
		board.setStartHash(rad.next());

		size = (num_MB * 1048576) / 288;
		table = new TransItem[size];
		for (int i = 0; i < size; i++)
			table[i] = new TransItem(board.moveGenerator());
	}

	public void clear()
	{
		for (int i = 0; i < size; i++)
			table[i].hash = 0;
	}

	public boolean getItem(final long hash, final TransItem item)
	{
		item.set(table[(int) Math.abs(hash % size)]);
		return (item.hash == hash);
	}

	public void setItem(final long hash, final int score, final Move move, final int depth, final int type)
	{
		final int index = (int) Math.abs(hash % size);
		final TransItem item = table[index];

		item.hash = hash;
		item.score = score;
		item.depth = depth;
		item.type = type;
		item.move.set(move);
	}
}

@@ -750,16 +750,50 @@
 ngine;%0A%0A
+import com.chess.genesis.util.*;%0A%0A
 class Tr
@@ -802,31 +802,29 @@
 sTable%0A%7B%0A%09pr
-otec
+iva
 te
-d
  final Trans
@@ -844,15 +844,56 @@
 %0A%09pr
-otec
+ivate final NewInstance%3CMove%3E moveType;%0A%09priva
 te
-d
  fin
@@ -1224,172 +1224,40 @@
 ;%0A%09%09
-for (int i = 0; i %3C size; i++)%0A%09%09%09table%5Bi%5D = new TransItem(board.moveGenerator());%0A%09%7D%0A%0A%09public void clear()%0A%09%7B%0A%09%09for (int i = 0; i %3C size; i++)%0A%09%09%09table%5Bi%5D.hash = 0
+moveType = board.moveGenerator()
 ;%0A%09%7D
@@ -1330,17 +1330,30 @@
 %7B%0A%09%09
-item.set(
+final TransItem tmp = 
 tabl
@@ -1382,16 +1382,68 @@
 %25 size)%5D
+;%0A%09%09if (tmp == null)%0A%09%09%09return false;%0A%09%09item.set(tmp
 );%0A%09%09ret
@@ -1446,17 +1446,16 @@
 %09return 
-(
 item.has
@@ -1463,17 +1463,16 @@
  == hash
-)
 ;%0A%09%7D%0A%0A%09p
@@ -1650,16 +1650,84 @@
 m item =
+ table%5Bindex%5D == null?%0A%09%09%09(table%5Bindex%5D = new TransItem(moveType)) :
  table%5Bi

create TransItem objects on demand to save memory