Project

General

Profile

Download (4.51 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / Tablebase.java @ 5e30b196

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
  private int mSize;
20

    
21
///////////////////////////////////////////////////////////////////////////////////////////////////
22

    
23
  Tablebase(int size)
24
    {
25
    mSize = size;
26
    mUnpackedTable = new byte[size];
27
    for(int i=0; i<size; i++) mUnpackedTable[i] = UNINITIALIZED;
28
    }
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
  Tablebase(byte[] data)
33
    {
34
    mUnpackedTable = null;
35
    mPackedTable = data;
36
    }
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
  boolean insertUnpacked(int index, byte value)
41
    {
42
    if( mUnpackedTable[index]==UNINITIALIZED )
43
      {
44
      mUnpackedTable[index] = value;
45
      return true;
46
      }
47
    return false;
48
    }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  byte retrieveUnpacked(int index)
53
    {
54
    return mUnpackedTable[index];
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  private void createPacked(int size)
60
    {
61
    mPackedTable = new byte[(size+4)/5];
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  private void insertPacked(int index, byte value)
67
    {
68
    int indexToInsert = index/5;
69
    byte actualInserted = (byte)(value%3);
70
    int val = mPackedTable[indexToInsert] & 0xff; // convert to 'unsigned'
71

    
72
    int b0 = val%3; val/=3;
73
    int b1 = val%3; val/=3;
74
    int b2 = val%3; val/=3;
75
    int b3 = val%3; val/=3;
76
    int b4 = val%3;
77

    
78
    switch(index%5)
79
      {
80
      case 0: b0 = actualInserted; break;
81
      case 1: b1 = actualInserted; break;
82
      case 2: b2 = actualInserted; break;
83
      case 3: b3 = actualInserted; break;
84
      case 4: b4 = actualInserted; break;
85
      }
86

    
87
    mPackedTable[indexToInsert] = (byte)(b0 + 3*(b1 + 3*(b2 + 3*(b3 + 3*b4))));
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  byte retrievePacked(int index)
93
    {
94
    int indexToRetrieve = index/5;
95
    int val = mPackedTable[indexToRetrieve] & 0xff; // convert to 'unsigned'
96
    int offset = (index%5);
97

    
98
    if(offset==0) return (byte)(val%3);
99
    val/=3;
100
    if(offset==1) return (byte)(val%3);
101
    val/=3;
102
    if(offset==2) return (byte)(val%3);
103
    val/=3;
104
    if(offset==3) return (byte)(val%3);
105
    val/=3;
106
    return (byte)(val%3);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  void pack()
112
    {
113
    int size = mUnpackedTable.length;
114
    createPacked(size);
115

    
116
    for(int i=0; i<size; i++)
117
      {
118
      byte unpacked = retrieveUnpacked(i);
119

    
120
      if( unpacked==UNINITIALIZED )
121
        {
122
        android.util.Log.e("D", "ERROR packing: index "+i+" still uninitialized!!");
123
        }
124

    
125
      insertPacked(i,unpacked);
126
      }
127
/*
128
    for(int i=0; i<size; i++)
129
      {
130
      byte unpacked = retrieveUnpacked(i);
131
      byte packed = retrievePacked(i);
132

    
133
      String m = "index="+i+" offset="+(i%5)+" unpacked="+unpacked+" packed="+packed;
134
      if( ((unpacked%3) != packed) ) android.util.Log.e("D", m);
135
      else android.util.Log.d("D", m);
136
      }
137
*/
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  int getSize()
143
    {
144
    return mSize;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  byte[] getPacked()
150
    {
151
    return mPackedTable;
152
    }
153
}
(3-3/11)