Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

69
LINES

< > BotCompany Repo | #1035234 // BlockOptimizedIntMatrix - Matrix<Int> stored in blocks of a fixed size (very compact if an entire block is one value, gracefully degrades otherwise)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (8681L/49K).

1  
// Note: Optimizes only when you call optimize or optimizeBlock
2  
persistable sclass BlockOptimizedIntMatrix extends AbstractMatrix<Int> {
3  
  // Each block is square with side length being a power of two
4  
  // (i.e. blockSize = 1 << blockShift)
5  
  int blockShift, blockSize;
6  
  
7  
  int bw, bh; // number of block cols/block rows
8  
  
9  
  int[][] fullBlocks; // if entry != null, it's a detailed block
10  
  int[] simpleBlocks; // if fullBlocks entry is null, find block value here
11  
  
12  
  *(int *w, int *h, int *blockShift) {
13  
    blockSize = 1 << blockShift;
14  
    bw = rshift_ceil(w, blockShift);
15  
    bh = rshift_ceil(h, blockShift);
16  
    simpleBlocks = new int[bw*bh];
17  
    fullBlocks = new int[bw*bh][];
18  
  }
19  
  
20  
  public Int get(int x, int y) {
21  
    int bx = x >> blockShift, by = y >> blockShift;
22  
    int iBlock = by*bw+bx;
23  
    int[] block = fullBlocks[iBlock];
24  
    if (block != null)
25  
      ret block[x & (blockSize-1) | ((y & (blockSize-1)) << blockShift)];
26  
    ret simpleBlocks[iBlock];
27  
  }
28  
  
29  
  public void set(int x, int y, Int a) {
30  
    int bx = x >> blockShift, by = y >> blockShift;
31  
    int iBlock = by*bw+bx;
32  
    
33  
    int[] block = fullBlocks[iBlock];
34  
    if (block == null) {
35  
      int val = simpleBlocks[iBlock];
36  
      if (val == a) ret; // no change
37  
      
38  
      // convert to full block
39  
      block = new int[1 << (blockShift*2)];
40  
      fullBlocks[iBlock] = block;
41  
      fillArray(block, val);
42  
      
43  
      simpleBlocks[iBlock] = 0; // set irrelevant array entry to 0 for beauty reasons
44  
    }
45  
    
46  
    block[x & (blockSize-1) | ((y & (blockSize-1)) << blockShift)] = a;
47  
  }
48  
  
49  
  void optimizeBlock(int iBlock) {
50  
    int[] block = fullBlocks[iBlock];
51  
    if (block == null) ret; // already optimized
52  
    
53  
    int val = block[0];
54  
    for (int i = 1; i < block.length; i++)
55  
      if (block[i] != val)
56  
        ret; // Can't optimize
57  
        
58  
    // Can optimize!
59  
    simpleBlocks[iBlock] = val;
60  
    fullBlocks[iBlock] = null;
61  
  }
62  
  
63  
  // optimize all blocks
64  
  void optimize {
65  
    int n = simpleBlocks.length;
66  
    for i to n:
67  
      optimizeBlock(i);
68  
  }
69  
}

Author comment

Began life as a copy of #1033112

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035234
Snippet name: BlockOptimizedIntMatrix - Matrix<Int> stored in blocks of a fixed size (very compact if an entire block is one value, gracefully degrades otherwise)
Eternal ID of this version: #1035234/14
Text MD5: 4c1fdbf19bffd3977b4ad546058b8db5
Transpilation MD5: 61c0caaecbcc7c194e9be38ed5e41a6f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-20 20:55:40
Source code size: 2109 bytes / 69 lines
Pitched / IR pitched: No / No
Views / Downloads: 186 / 278
Version history: 13 change(s)
Referenced in: [show references]