Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / Tablebase.java @ 6d637f71

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
  Tablebase(byte[] data)
31
    {
32
    mUnpackedTable = null;
33
    mPackedTable = data;
34
    }
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

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

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

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

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

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

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

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

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

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  byte[] getPacked()
141
    {
142
    return mPackedTable;
143
    }
144
}
(2-2/9)