1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2023 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.objectlib.tablebases;
|
11
|
|
12
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
13
|
|
14
|
class Tablebase
|
15
|
{
|
16
|
private final byte UNINITIALIZED = (byte)0xff;
|
17
|
private final byte[] mUnpackedTable;
|
18
|
private byte[] mPackedTable;
|
19
|
|
20
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
21
|
|
22
|
Tablebase(int size)
|
23
|
{
|
24
|
mUnpackedTable = new byte[size];
|
25
|
for(int i=0; i<size; i++) mUnpackedTable[i] = UNINITIALIZED;
|
26
|
}
|
27
|
|
28
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
|
30
|
boolean insertUnpacked(int index, byte value)
|
31
|
{
|
32
|
if( mUnpackedTable[index]==UNINITIALIZED )
|
33
|
{
|
34
|
mUnpackedTable[index] = value;
|
35
|
return true;
|
36
|
}
|
37
|
return false;
|
38
|
}
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
|
42
|
byte retrieveUnpacked(int index)
|
43
|
{
|
44
|
return mUnpackedTable[index];
|
45
|
}
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
private void createPacked(int size)
|
50
|
{
|
51
|
mPackedTable = new byte[(size+4)/5];
|
52
|
}
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
private void insertPacked(int index, byte value)
|
57
|
{
|
58
|
int indexToInsert = index/5;
|
59
|
byte actualInserted = (byte)(value%3);
|
60
|
byte val = mPackedTable[indexToInsert];
|
61
|
|
62
|
int b4 = val%3; val/=3;
|
63
|
int b3 = val%3; val/=3;
|
64
|
int b2 = val%3; val/=3;
|
65
|
int b1 = val%3; val/=3;
|
66
|
int b0 = val%3;
|
67
|
|
68
|
switch(index%5)
|
69
|
{
|
70
|
case 0: b0 = actualInserted; break;
|
71
|
case 1: b1 = actualInserted; break;
|
72
|
case 2: b2 = actualInserted; break;
|
73
|
case 3: b3 = actualInserted; break;
|
74
|
case 4: b4 = actualInserted; break;
|
75
|
}
|
76
|
|
77
|
mPackedTable[indexToInsert] = (byte)(b4 + 3*(b3 + 3*(b2 + 3*(b1 + 3*b0))));
|
78
|
}
|
79
|
|
80
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81
|
|
82
|
byte retrievePacked(int index)
|
83
|
{
|
84
|
int indexToRetrieve = index/5;
|
85
|
byte val = mPackedTable[indexToRetrieve];
|
86
|
|
87
|
byte b4 = (byte)(val%3); val/=3;
|
88
|
byte b3 = (byte)(val%3); val/=3;
|
89
|
byte b2 = (byte)(val%3); val/=3;
|
90
|
byte b1 = (byte)(val%3); val/=3;
|
91
|
byte b0 = (byte)(val%3);
|
92
|
|
93
|
switch(index%5)
|
94
|
{
|
95
|
case 0: return b0;
|
96
|
case 1: return b1;
|
97
|
case 2: return b2;
|
98
|
case 3: return b3;
|
99
|
case 4: return b4;
|
100
|
}
|
101
|
|
102
|
return 0;
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
void pack()
|
108
|
{
|
109
|
int size = mUnpackedTable.length;
|
110
|
createPacked(size);
|
111
|
|
112
|
for(int i=0; i<size; i++)
|
113
|
{
|
114
|
byte unpacked = retrieveUnpacked(i);
|
115
|
insertPacked(i,unpacked);
|
116
|
}
|
117
|
}
|
118
|
}
|