1 package org.simantics.scl.compiler.parser.generator.compression;
3 import java.util.Arrays;
5 public class CompressTable {
7 private static class Row implements Comparable<Row> {
16 public int compareTo(Row o) {
17 return o.density < density ? 1 : o.density > density ? -1 : 0;
21 public static CompressedTable compress(int[][] table_) {
23 Row[] table = new Row[table_.length];
24 for(int i=0;i<table.length;++i) {
31 for(int j=0;j<row.data.length;++j) {
40 row.density = density;
48 int width = table_.length == 0 ? 0 : table_[0].length;
49 int capacity = 2*table_.length*width;
50 int[] rowIds = new int[capacity];
51 Arrays.fill(rowIds, -1);
52 int[] values = new int[capacity];
53 for(Row row : table) {
54 if(row.density == 0) {
55 row.displacement = capacity/2;
58 tryAgain: for(int dId=0;dId<capacity;++dId) {
59 int d = capacity/2 + ((dId&1)==0 ? dId/2 : -1-dId/2);
60 for(int p = row.minPos;p<=row.maxPos;++p) {
61 int newValue = row.data[p];
70 for(int p = row.minPos;p<=row.maxPos;++p) {
71 int val = row.data[p];
82 // Produce final tables
85 for(Row row : table) {
86 int d = row.displacement;
92 int[] displacement = new int[table.length];
93 for(int i=0;i<table.length;++i)
94 displacement[table[i].id] = table[i].displacement-minDis;
95 rowIds = Arrays.copyOfRange(rowIds, minDis, maxDis+width);
96 values = Arrays.copyOfRange(values, minDis, maxDis+width);
98 return new CompressedTable(rowIds, values, displacement);