From 26ac161f5a9c3f7db2ae961cd5a011d27fd369d2 Mon Sep 17 00:00:00 2001 From: Thad Hughes Date: Fri, 23 Jul 2010 17:57:40 -0700 Subject: [PATCH] go --- .../android/dictionary/engine/Dictionary.java | 44 ++++++++++++++++ .../android/dictionary/engine/Entry.java | 12 +++++ .../dictionary/engine/EntrySource.java | 5 ++ .../android/dictionary/engine/PairEntry.java | 29 +++++++++++ .../hughes/android/dictionary/engine/Row.java | 50 +++++++++++++++++++ .../dictionary/engine/RowWithIndex.java | 50 +++++++++++++++++++ .../android/dictionary/engine/TokenRow.java | 27 ++++++++++ 7 files changed, 217 insertions(+) create mode 100644 src/com/hughes/android/dictionary/engine/Dictionary.java create mode 100644 src/com/hughes/android/dictionary/engine/Entry.java create mode 100644 src/com/hughes/android/dictionary/engine/EntrySource.java create mode 100644 src/com/hughes/android/dictionary/engine/PairEntry.java create mode 100644 src/com/hughes/android/dictionary/engine/Row.java create mode 100644 src/com/hughes/android/dictionary/engine/RowWithIndex.java create mode 100644 src/com/hughes/android/dictionary/engine/TokenRow.java diff --git a/src/com/hughes/android/dictionary/engine/Dictionary.java b/src/com/hughes/android/dictionary/engine/Dictionary.java new file mode 100644 index 0000000..8f1db2d --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/Dictionary.java @@ -0,0 +1,44 @@ +package com.hughes.android.dictionary.engine; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.List; + +import com.hughes.util.raf.RAFSerializable; + +public class Dictionary { + + // persisted + List pairEntries; + + // persisted + List sources; + + // -------------------------------------------------------------------------- + + final class Index { + // One big list! + // Various sub-types. + // persisted + List rows; + + // persisted + List sortedIndexEntries; + + Dictionary getDict() { + return Dictionary.this; + } + } + + static final class IndexEntry implements RAFSerializable { + String token; + int startRow; + + public void write(RandomAccessFile raf) throws IOException { + raf.writeUTF(token); + raf.write(startRow); + } + } + + +} \ No newline at end of file diff --git a/src/com/hughes/android/dictionary/engine/Entry.java b/src/com/hughes/android/dictionary/engine/Entry.java new file mode 100644 index 0000000..af279c1 --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/Entry.java @@ -0,0 +1,12 @@ +package com.hughes.android.dictionary.engine; + +import java.util.List; + +public abstract class Entry { + + EntrySource entrySource; + + abstract List getMainTokens(); + abstract List getOtherTokens(); + +} diff --git a/src/com/hughes/android/dictionary/engine/EntrySource.java b/src/com/hughes/android/dictionary/engine/EntrySource.java new file mode 100644 index 0000000..07e825f --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/EntrySource.java @@ -0,0 +1,5 @@ +package com.hughes.android.dictionary.engine; + +public class EntrySource { + +} diff --git a/src/com/hughes/android/dictionary/engine/PairEntry.java b/src/com/hughes/android/dictionary/engine/PairEntry.java new file mode 100644 index 0000000..40282a0 --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/PairEntry.java @@ -0,0 +1,29 @@ +package com.hughes.android.dictionary.engine; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.List; + +public class PairEntry extends Entry { + + @Override + List getMainTokens() { + return null; + } + + @Override + List getOtherTokens() { + return null; + } + + + public static class Row extends RowWithIndex { + Row(final RandomAccessFile raf, final int thisRowIndex, final Dictionary.Index index) throws IOException { + super(raf, thisRowIndex, index); + } + public PairEntry getEntry() { + return index.getDict().pairEntries.get(referenceIndex); + } + } + +} diff --git a/src/com/hughes/android/dictionary/engine/Row.java b/src/com/hughes/android/dictionary/engine/Row.java new file mode 100644 index 0000000..7c2c927 --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/Row.java @@ -0,0 +1,50 @@ +package com.hughes.android.dictionary.engine; + +import java.io.IOException; +import java.io.RandomAccessFile; + +import com.hughes.util.raf.RAFListSerializer; + +public interface Row { + + public void write(RandomAccessFile raf) throws IOException; + + /** + * @return the TokenRow that this row is "filed under". + */ + public TokenRow getTokenRow(final boolean search); + + public void setTokenRow(final TokenRow tokenRow); + + + // Row must manage "disk-based" polymorphism. All other polymorphism is + // dealt with in the normal manner. + static class Serializer implements RAFListSerializer { + + final Dictionary.Index index; + + Serializer(final Dictionary.Index index) { + this.index = index; + } + + @Override + public Row read(RandomAccessFile raf, final int listIndex) throws IOException { + final byte rowType = raf.readByte(); + if (rowType == 0) { + return new PairEntry.Row(raf, listIndex, index); + } else if (rowType == 1) { + return new TokenRow(raf, listIndex, index); + } + throw new RuntimeException("Invalid rowType:" + rowType); + } + + @Override + public void write(RandomAccessFile raf, Row t) throws IOException { + if (t instanceof PairEntry.Row) { + raf.writeByte(0); + } else if (t instanceof TokenRow) { + raf.writeByte(1); + } + } + }; +} diff --git a/src/com/hughes/android/dictionary/engine/RowWithIndex.java b/src/com/hughes/android/dictionary/engine/RowWithIndex.java new file mode 100644 index 0000000..00d7db8 --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/RowWithIndex.java @@ -0,0 +1,50 @@ +package com.hughes.android.dictionary.engine; + +import java.io.IOException; +import java.io.RandomAccessFile; + +public abstract class RowWithIndex implements Row { + final Dictionary.Index index; + int thisRowIndex; + int referenceIndex; + + TokenRow tokenRow = null; + + RowWithIndex(final RandomAccessFile raf, final int thisRowIndex, final Dictionary.Index index) throws IOException { + this.index = index; + this.thisRowIndex = thisRowIndex; + this.referenceIndex = raf.readInt(); + } + + @Override + public void write(RandomAccessFile raf) throws IOException { + raf.writeInt(referenceIndex); + } + + @Override + public TokenRow getTokenRow(final boolean search) { + if (tokenRow == null && search) { + int r = thisRowIndex - 1; + while (r >= 0) { + final Row row = index.rows.get(r); + final TokenRow candidate = row.getTokenRow(false); + if (candidate != null) { + for (++r; r <= thisRowIndex; ++r) { + index.rows.get(r).setTokenRow(candidate); + } + } + } + assert tokenRow != null; + } + return tokenRow; + } + + @Override + public void setTokenRow(TokenRow tokenRow) { + assert this.tokenRow == null; + assert tokenRow != null; + this.tokenRow = tokenRow; + } + + +} diff --git a/src/com/hughes/android/dictionary/engine/TokenRow.java b/src/com/hughes/android/dictionary/engine/TokenRow.java new file mode 100644 index 0000000..61a35d2 --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/TokenRow.java @@ -0,0 +1,27 @@ +package com.hughes.android.dictionary.engine; + +import java.io.IOException; +import java.io.RandomAccessFile; + +public class TokenRow extends RowWithIndex { + + TokenRow(final RandomAccessFile raf, final int thisRowIndex, final Dictionary.Index index) throws IOException { + super(raf, thisRowIndex, index); + } + + @Override + public TokenRow getTokenRow(final boolean search) { + return this; + } + + @Override + public void setTokenRow(TokenRow tokenRow) { + throw new RuntimeException("Shouldn't be setting TokenRow!"); + } + + public String getToken() { + return index.sortedIndexEntries.get(referenceIndex).token; + } + + +} -- 2.43.0