]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/engine/RowBase.java
More compact row list format.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / RowBase.java
index d76eb30bc6c6455a217280f62054a45c40a95cdb..2930fa721f2754c0a1c48f780fd4a10d200c2160 100644 (file)
@@ -18,9 +18,10 @@ import com.hughes.util.IndexedObject;
 import com.hughes.util.raf.RAFListSerializer;
 import com.ibm.icu.text.Transliterator;
 
+import java.io.DataInput;
+import java.io.DataOutput;
 import java.io.IOException;
 import java.io.PrintStream;
-import java.io.RandomAccessFile;
 import java.util.Comparator;
 import java.util.List;
 import java.util.regex.Pattern;
@@ -41,11 +42,11 @@ public abstract class RowBase extends IndexedObject {
      */
     private TokenRow tokenRow = null;
 
-    RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index)
+    RowBase(final DataInput raf, final int thisRowIndex, final Index index, final int extra)
             throws IOException {
         super(thisRowIndex);
         this.index = index;
-        this.referenceIndex = raf.readInt(); // what this points to.
+        this.referenceIndex = extra == -1 ? raf.readInt() : ((extra << 24) + raf.readUnsignedShort()); // what this points to.
     }
 
     public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
@@ -147,33 +148,41 @@ public abstract class RowBase extends IndexedObject {
         }
 
         @Override
-        public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
-            final byte rowType = raf.readByte();
+        public RowBase read(DataInput raf, final int listIndex) throws IOException {
+            int rowType = raf.readUnsignedByte();
+            int extra = -1;
+            if (rowType >= 0x20) {
+                extra = rowType & 0x1f;
+                rowType = (rowType >> 5) - 1;
+            }
             if (rowType == 0) {
-                return new PairEntry.Row(raf, listIndex, index);
+                return new PairEntry.Row(raf, listIndex, index, extra);
             } else if (rowType == 1 || rowType == 3) {
-                return new TokenRow(raf, listIndex, index, /* hasMainEntry */rowType == 1);
+                return new TokenRow(raf, listIndex, index, /* hasMainEntry */rowType == 1, extra);
             } else if (rowType == 2) {
-                return new TextEntry.Row(raf, listIndex, index);
+                return new TextEntry.Row(raf, listIndex, index, extra);
             } else if (rowType == 4) {
-                return new HtmlEntry.Row(raf, listIndex, index);
+                return new HtmlEntry.Row(raf, listIndex, index, extra);
             }
             throw new RuntimeException("Invalid rowType:" + rowType);
         }
 
         @Override
-        public void write(RandomAccessFile raf, RowBase t) throws IOException {
+        public void write(DataOutput raf, RowBase t) throws IOException {
+            int type = 0;
             if (t instanceof PairEntry.Row) {
-                raf.writeByte(0);
+                type = 0;
             } else if (t instanceof TokenRow) {
                 final TokenRow tokenRow = (TokenRow) t;
-                raf.writeByte(tokenRow.hasMainEntry ? 1 : 3);
+                type = tokenRow.hasMainEntry ? 1 : 3;
             } else if (t instanceof TextEntry.Row) {
-                raf.writeByte(2);
+                type = 2;
             } else if (t instanceof HtmlEntry.Row) {
-                raf.writeByte(4);
+                type = 4;
             }
-            raf.writeInt(t.referenceIndex);
+            assert t.referenceIndex < (1 << 21);
+            raf.writeByte(((type + 1) << 5) + (t.referenceIndex >> 24));
+            raf.writeShort(t.referenceIndex);
         }
     }