From: thadh Date: Tue, 10 Feb 2009 07:28:35 +0000 (-0800) Subject: working X-Git-Url: http://gitweb.fperrin.net/?a=commitdiff_plain;h=054bc8fce52809a00e736735d16b024a3fe82be2;p=Dictionary.git working --- diff --git a/src/com/hughes/android/dictionary/MemoryIndex.java b/src/com/hughes/android/dictionary/MemoryIndex.java new file mode 100755 index 0000000..c269a65 --- /dev/null +++ b/src/com/hughes/android/dictionary/MemoryIndex.java @@ -0,0 +1,63 @@ +package com.hughes.android.dictionary; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.Serializable; + + +public final class MemoryIndex { + + private static final long serialVersionUID = 3375180767865334065L; + + static final class Node implements Serializable { + + private static final long serialVersionUID = 8824115665859184225L; + + final String[] chars; + final Node[] children; + final int[] offsets; + + Node(final int numChildren, final int numOffsets) { + chars = new String[numChildren]; + children = new Node[numChildren]; + offsets = new int[numOffsets]; + } + + int descendantCount() { + int total = 1; + for (final Node child : children) { + total += child.descendantCount(); + } + return total; + } + + Node(final DataInputStream is) throws IOException { + final int numChildren = is.readInt(); + chars = new String[numChildren]; + children = new Node[numChildren]; + for (int i = 0; i < numChildren; ++i) { + chars[i] = is.readUTF().intern(); + children[i] = new Node(is); + } + final int numOffsets = is.readInt(); + offsets = new int[numOffsets]; + for (int i = 0; i < numOffsets; ++i) { + offsets[i] = is.readInt(); + } + } + + void write(final DataOutputStream os) throws IOException { + os.writeInt(chars.length); + for (int i = 0; i < chars.length; i++) { + os.writeUTF(chars[i]); + children[i].write(os); + } + os.writeInt(offsets.length); + for (int i = 0; i < offsets.length; i++) { + os.writeInt(offsets[i]); + } + } + } + +}