]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
working
authorthadh <thadh@THADH-MTV.ad.corp.google.com>
Tue, 10 Feb 2009 07:28:35 +0000 (23:28 -0800)
committerthadh <thadh@THADH-MTV.ad.corp.google.com>
Tue, 10 Feb 2009 07:28:35 +0000 (23:28 -0800)
src/com/hughes/android/dictionary/MemoryIndex.java [new file with mode: 0755]

diff --git a/src/com/hughes/android/dictionary/MemoryIndex.java b/src/com/hughes/android/dictionary/MemoryIndex.java
new file mode 100755 (executable)
index 0000000..c269a65
--- /dev/null
@@ -0,0 +1,63 @@
+package com.hughes.android.dictionary;\r
+\r
+import java.io.DataInputStream;\r
+import java.io.DataOutputStream;\r
+import java.io.IOException;\r
+import java.io.Serializable;\r
+\r
+\r
+public final class MemoryIndex {\r
+  \r
+  private static final long serialVersionUID = 3375180767865334065L;\r
+\r
+  static final class Node implements Serializable {\r
+\r
+    private static final long serialVersionUID = 8824115665859184225L;\r
+\r
+    final String[] chars;\r
+    final Node[] children;\r
+    final int[] offsets;\r
+    \r
+    Node(final int numChildren, final int numOffsets) {\r
+      chars = new String[numChildren];\r
+      children = new Node[numChildren];\r
+      offsets = new int[numOffsets];\r
+    }\r
+    \r
+    int descendantCount() {\r
+      int total = 1;\r
+      for (final Node child : children) {\r
+        total += child.descendantCount();\r
+      }\r
+      return total;\r
+    }\r
+    \r
+    Node(final DataInputStream is) throws IOException {\r
+      final int numChildren = is.readInt();\r
+      chars = new String[numChildren];\r
+      children = new Node[numChildren];\r
+      for (int i = 0; i < numChildren; ++i) {\r
+        chars[i] = is.readUTF().intern();\r
+        children[i] = new Node(is);\r
+      }\r
+      final int numOffsets = is.readInt();\r
+      offsets = new int[numOffsets];\r
+      for (int i = 0; i < numOffsets; ++i) {\r
+        offsets[i] = is.readInt();\r
+      }\r
+    }\r
+    \r
+    void write(final DataOutputStream os) throws IOException {\r
+      os.writeInt(chars.length);\r
+      for (int i = 0; i < chars.length; i++) {\r
+        os.writeUTF(chars[i]);\r
+        children[i].write(os);\r
+      }\r
+      os.writeInt(offsets.length);\r
+      for (int i = 0; i < offsets.length; i++) {\r
+        os.writeInt(offsets[i]);\r
+      }\r
+    }\r
+  }\r
+  \r
+}\r