Project

General

Profile

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

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

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
    int val = mPackedTable[indexToInsert] & 0xff; // convert to 'unsigned'
61

    
62
    int b0 = val%3; val/=3;
63
    int b1 = val%3; val/=3;
64
    int b2 = val%3; val/=3;
65
    int b3 = val%3; val/=3;
66
    int b4 = 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)(b0 + 3*(b1 + 3*(b2 + 3*(b3 + 3*b4))));
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  byte retrievePacked(int index)
83
    {
84
    int indexToRetrieve = index/5;
85
    int val = mPackedTable[indexToRetrieve] & 0xff; // convert to 'unsigned'
86
    int offset = (index%5);
87

    
88
    if(offset==0) return (byte)(val%3);
89
    val/=3;
90
    if(offset==1) return (byte)(val%3);
91
    val/=3;
92
    if(offset==2) return (byte)(val%3);
93
    val/=3;
94
    if(offset==3) return (byte)(val%3);
95
    val/=3;
96
    return (byte)(val%3);
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  void pack()
102
    {
103
    int size = mUnpackedTable.length;
104
    createPacked(size);
105

    
106
    for(int i=0; i<size; i++)
107
      {
108
      byte unpacked = retrieveUnpacked(i);
109
      insertPacked(i,unpacked);
110
      }
111

    
112
    for(int i=0; i<size; i++)
113
      {
114
      byte unpacked = retrieveUnpacked(i);
115
      byte packed = retrievePacked(i);
116

    
117
      String m = "index="+i+" offset="+(i%5)+" unpacked="+unpacked+" packed="+packed;
118
      if( ((unpacked%3) != packed) ) android.util.Log.e("D", m);
119
      else android.util.Log.d("D", m);
120
      }
121
    }
122
}
(1-1/3)