Project

General

Profile

« Previous | Next » 

Revision 884b702b

Added by Leszek Koltunski about 1 year ago

progress with PruningTable.

View differences:

src/main/java/org/distorted/objectlib/tablebases/TablebasesMITM.java
9 9

  
10 10
package org.distorted.objectlib.tablebases;
11 11

  
12
///////////////////////////////////////////////////////////////////////////////////////////////////
12
import android.content.res.Resources;
13 13

  
14
public abstract class TablebasesMITM extends TablebasesAbstract
15
{
16
  private PruningTable mTable;
14
import java.io.ByteArrayOutputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17 17

  
18 18
///////////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20
  public TablebasesMITM()
21
    {
22
    super();
23
    }
20
public abstract class TablebasesMITM extends TablebasesAbstract
21
{
22
  private PruningTable[] mTables;
23
  private boolean mInitialized;
24
  private int[] mLevels;
24 25

  
25 26
///////////////////////////////////////////////////////////////////////////////////////////////////
26 27

  
27
  private String createAdjusted(int num)
28
  private void createPruningTable(Resources res, int id, int index)
28 29
    {
29
    if( num<10  ) return "   "+num;
30
    if( num<100 ) return  "  "+num;
31
    if( num<1000) return   " "+num;
32

  
33
    return Integer.toString(num);
34
    }
35

  
36
///////////////////////////////////////////////////////////////////////////////////////////////////
30
    InputStream stream = res.openRawResource(id);
31
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
37 32

  
38
  private int numBytesForIndices(int total)
39
    {
40
    int bytes = 0;
33
    int nRead;
34
    byte[] tmp = new byte[16384];
41 35

  
42
    while(total!=0)
36
    try
43 37
      {
44
      total>>=8;
45
      bytes++;
46
      }
47

  
48
    return bytes;
49
    }
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

  
53
  public void probe(int level, int numBits)
54
    {
55
    /*
56
    int numPos = 0;
57
    int numsInBucket = (1<<numBits);
58
    int numBuckets = (mSize+numsInBucket-1)/numsInBucket;
59
    int[] bucket = new int[numBuckets];
60

  
61
    for(int i=0; i<mSize; i++)
62
      if( mTablebase.retrieveUnpacked(i)==level )
38
      while ((nRead = stream.read(tmp, 0, tmp.length)) != -1)
63 39
        {
64
        numPos++;
65
        int currBucket = i/numsInBucket;
66
        bucket[currBucket]++;
40
        buffer.write(tmp, 0, nRead);
67 41
        }
68

  
69
    int totalBytesForTable = 0;
70
    int maxObjectsInBucket = 0;
71

  
72
    for(int i=0; i<numBuckets; i++)
42
      stream.close();
43
      byte[] data = buffer.toByteArray();
44
      buffer.close();
45
      mTables[index] = new PruningTable(data);
46
      mLevels[index] = mTables[index].getLevel();
47
      }
48
    catch(IOException ex)
73 49
      {
74
      int numBytes = (bucket[i]*numBits+7)/8;
75
      totalBytesForTable+=numBytes;
76
      if( bucket[i]>maxObjectsInBucket ) maxObjectsInBucket = bucket[i];
50
      mInitialized = false;
77 51
      }
52
    }
78 53

  
79
    int totalBytesForBuckets = numBuckets*numBytesForIndices(totalBytesForTable);
80
    int constantModeBytes = (maxObjectsInBucket*numBits+7)/8;
54
///////////////////////////////////////////////////////////////////////////////////////////////////
81 55

  
82
    android.util.Log.e("D","bits: "+numBits+" header: 4 buckets: "+totalBytesForBuckets+" table: "+totalBytesForTable+
83
                            " total: "+(4+totalBytesForBuckets+totalBytesForTable)+
84
                            " max : "+maxObjectsInBucket+" total constant: "+(numBuckets*constantModeBytes+4));
85
    */
56
  public TablebasesMITM()
57
    {
58
    super();
86 59
    }
87 60

  
88 61
///////////////////////////////////////////////////////////////////////////////////////////////////
89 62

  
90
  public void pack(int level, int numBits)
63
  public TablebasesMITM(Resources res, int[] resourceIDs)
91 64
    {
92
    //mTable = new PruningTable(mTablebase,level,numBits);
65
    super();
66

  
67
    int numOfIDs = resourceIDs.length;
68
    mTables = new PruningTable[numOfIDs];
69
    mLevels = new int[numOfIDs];
70
    mInitialized = true;
71

  
72
    for(int i=0; i<numOfIDs; i++) createPruningTable(res,resourceIDs[i],i);
93 73
    }
94 74

  
95 75
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff