Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / TablebaseHelpers.java @ 4d63b066

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
public class TablebaseHelpers
15
{
16
  private static int swaps(int val, int[] buffer, int len)
17
    {
18
    int ret = 0;
19

    
20
    for(int i=0; i<len; i++)
21
      {
22
           if( buffer[i] >val ) ret++;
23
      else if( buffer[i]==val ) return ret;
24
      }
25

    
26
    return -1;
27
    }
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
  public static boolean permutationIsEven(int[] buffer)
32
    {
33
    int len = buffer.length;
34
    int numOfSwaps = 0;
35
    for(int i=0; i<len-1; i++) numOfSwaps += swaps(i,buffer,len);
36
    return (numOfSwaps%2==0);
37
    }
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
  public static int computeEvenPermutationNum(int[] permutation)
42
    {
43
    int len = permutation.length-2;
44
    int ret = 0;
45
    int mul = 4;
46

    
47
    for(int i=0; i<len; i++)
48
      {
49
      ret += swaps(len-1-i,permutation,len+2);
50

    
51
      if( i<len-1 )
52
        {
53
        ret *= mul;
54
        mul++;
55
        }
56
      }
57

    
58
    return ret;
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  public static void getEvenPermutationFromNum(int[] buffer,int permSize, int permNum)
64
    {
65
    int index = permSize;
66
    int len = permSize-2;
67
    int totalSwaps = 0;
68

    
69
    for(int i=0; i<permSize; i++) buffer[i]=-1;
70

    
71
    for(int i=0; i<len; i++)
72
      {
73
      int swaps = (permNum%index);
74
      permNum /= index;
75
      index--;
76
      totalSwaps += swaps;
77

    
78
      for(int j=0; j<permSize; j++)
79
        {
80
        if( buffer[j]==-1 )
81
          {
82
          if( swaps==0 )
83
            {
84
            buffer[j] = i;
85
            break;
86
            }
87
          swaps--;
88
          }
89
        }
90
      }
91

    
92
    int lower, upper;
93

    
94
    if( (totalSwaps%2)==0 )
95
      {
96
      lower = permSize-2;
97
      upper = permSize-1;
98
      }
99
    else
100
      {
101
      lower = permSize-1;
102
      upper = permSize-2;
103
      }
104

    
105
    boolean first=true;
106

    
107
    for(int i=0; i<permSize; i++)
108
      if( buffer[i]==-1 )
109
        {
110
        if( first )
111
          {
112
          buffer[i] = lower;
113
          first=false;
114
          }
115
        else
116
          {
117
          buffer[i] = upper;
118
          break;
119
          }
120
        }
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  public static int computePermutationNum(int[] permutation)
126
    {
127
    int len = permutation.length-1;
128
    int ret = 0;
129
    int mul = 3;
130

    
131
    for(int i=0; i<len; i++)
132
      {
133
      ret += swaps(len-1-i,permutation,len+1);
134

    
135
      if( i<len-1 )
136
        {
137
        ret *= mul;
138
        mul++;
139
        }
140
      }
141

    
142
    return ret;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  public static void getPermutationFromNum(int[] buffer,int permSize, int permNum)
148
    {
149
    int index = permSize;
150
    int len = permSize-1;
151

    
152
    for(int i=0; i<permSize; i++) buffer[i]=-1;
153

    
154
    for(int i=0; i<len; i++)
155
      {
156
      int swaps = (permNum%index);
157
      permNum /= index;
158
      index--;
159

    
160
      for(int j=0; j<permSize; j++)
161
        {
162
        if( buffer[j]==-1 )
163
          {
164
          if( swaps==0 )
165
            {
166
            buffer[j] = i;
167
            break;
168
            }
169
          swaps--;
170
          }
171
        }
172
      }
173

    
174
    for(int i=0; i<permSize; i++)
175
      if( buffer[i]==-1 )
176
        {
177
        buffer[i] = permSize-1;
178
        break;
179
        }
180
    }
181
}
(3-3/6)