1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-06 03:50:19 +02:00

text model optimization (in progress)

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@400 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
Nikolay Pultsin 2007-12-14 04:30:07 +00:00
parent d4c920db65
commit 892ab3350f
9 changed files with 61 additions and 24 deletions

View file

@ -2,7 +2,7 @@ package org.zlibrary.text.model;
import java.util.List; import java.util.List;
public interface ZLTextParagraph { public interface ZLTextParagraph extends Iterable<ZLTextParagraph.Entry> {
interface Entry { interface Entry {
} }
@ -19,6 +19,4 @@ public interface ZLTextParagraph {
Kind getKind(); Kind getKind();
int getEntryNumber(); int getEntryNumber();
int getTextLength(); int getTextLength();
List<Entry> getEntries();
void addEntry(Entry entry);
} }

View file

@ -17,6 +17,7 @@ public class ZLModelFactory {
return new ZLTextTreeModelImpl(); return new ZLTextTreeModelImpl();
} }
//paragraphs //paragraphs
/*
public ZLTextParagraph createParagraph() { public ZLTextParagraph createParagraph() {
return new ZLTextParagraphImpl(); return new ZLTextParagraphImpl();
} }
@ -32,6 +33,7 @@ public class ZLModelFactory {
public ZLTextTreeParagraph createTreeParagraph() { public ZLTextTreeParagraph createTreeParagraph() {
return new ZLTextTreeParagraphImpl(null); return new ZLTextTreeParagraphImpl(null);
} }
*/
//entries //entries
public ZLTextControlEntry createControlEntry(byte kind, boolean isStart) { public ZLTextControlEntry createControlEntry(byte kind, boolean isStart) {

View file

@ -10,6 +10,7 @@ import java.util.Map;
abstract class ZLTextModelImpl implements ZLTextModel { abstract class ZLTextModelImpl implements ZLTextModel {
private final ArrayList<ZLTextParagraph> myParagraphs = new ArrayList<ZLTextParagraph>(); private final ArrayList<ZLTextParagraph> myParagraphs = new ArrayList<ZLTextParagraph>();
protected final ArrayList<ZLTextParagraph.Entry> myEntries = new ArrayList<ZLTextParagraph.Entry>();
private static final int DATA_BLOCK_SIZE = 51200; private static final int DATA_BLOCK_SIZE = 51200;
private int myBlockOffset = 0; private int myBlockOffset = 0;
@ -34,7 +35,7 @@ abstract class ZLTextModelImpl implements ZLTextModel {
private void addEntry(ZLTextParagraph.Entry entry) { private void addEntry(ZLTextParagraph.Entry entry) {
final ArrayList<ZLTextParagraph> paragraphs = myParagraphs; final ArrayList<ZLTextParagraph> paragraphs = myParagraphs;
paragraphs.get(paragraphs.size() - 1).addEntry(entry); ((ZLTextParagraphImpl)paragraphs.get(paragraphs.size() - 1)).addEntry(entry);
} }
public void addControl(byte textKind, boolean isStart) { public void addControl(byte textKind, boolean isStart) {
@ -95,7 +96,7 @@ abstract class ZLTextModelImpl implements ZLTextModel {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (ZLTextParagraph paragraph: myParagraphs) { for (ZLTextParagraph paragraph: myParagraphs) {
sb.append("[PARAGRAPH]\n"); sb.append("[PARAGRAPH]\n");
for (ZLTextParagraph.Entry entry: paragraph.getEntries()) { for (ZLTextParagraph.Entry entry : paragraph) {
if (entry instanceof ZLTextEntryImpl) { if (entry instanceof ZLTextEntryImpl) {
ZLTextEntryImpl textEntry = (ZLTextEntryImpl)entry; ZLTextEntryImpl textEntry = (ZLTextEntryImpl)entry;
sb.append("[TEXT]"); sb.append("[TEXT]");

View file

@ -3,31 +3,61 @@ package org.zlibrary.text.model.impl;
import org.zlibrary.text.model.ZLTextParagraph; import org.zlibrary.text.model.ZLTextParagraph;
import org.zlibrary.text.model.impl.ZLTextEntryImpl; import org.zlibrary.text.model.impl.ZLTextEntryImpl;
import java.util.List; import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
class ZLTextParagraphImpl implements ZLTextParagraph { class ZLTextParagraphImpl implements ZLTextParagraph {
private final ArrayList<ZLTextParagraph.Entry> myEntries = new ArrayList<ZLTextParagraph.Entry>(); private final ArrayList<Entry> myEntries;
private final int myOffset;
private int myLength;
ZLTextParagraphImpl() { ZLTextParagraphImpl(ArrayList<Entry> entries) {
myEntries = entries;
myOffset = entries.size();
myLength = 0;
} }
public List<ZLTextParagraph.Entry> getEntries() { private class EntryIterator implements Iterator<Entry> {
return Collections.unmodifiableList(myEntries); private int myPosition;
EntryIterator() {
myPosition = myOffset;
}
public boolean hasNext() {
return myPosition < myOffset + myLength;
}
public Entry next() {
if (myPosition == myOffset + myLength) {
throw new NoSuchElementException();
}
return myEntries.get(myPosition++);
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public Iterator<Entry> iterator() {
return new EntryIterator();
} }
public Kind getKind() { public Kind getKind() {
return Kind.TEXT_PARAGRAPH; return Kind.TEXT_PARAGRAPH;
} }
public int getEntryNumber() { public final int getEntryNumber() {
return myEntries.size(); return myLength;
} }
public int getTextLength() { public final int getTextLength() {
int size = 0; int size = 0;
for (ZLTextParagraph.Entry entry: myEntries) { final ArrayList<Entry> entries = myEntries;
final int from = myOffset;
final int to = from + myLength;
for (int i = from; i < to; ++i) {
Entry entry = entries.get(i);
if (entry instanceof ZLTextEntryImpl) { if (entry instanceof ZLTextEntryImpl) {
size += ((ZLTextEntryImpl)entry).getDataLength(); size += ((ZLTextEntryImpl)entry).getDataLength();
} }
@ -35,7 +65,8 @@ class ZLTextParagraphImpl implements ZLTextParagraph {
return size; return size;
} }
public void addEntry(ZLTextParagraph.Entry entry) { final void addEntry(Entry entry) {
myEntries.add(entry); myEntries.add(entry);
++myLength;
} }
} }

View file

@ -5,7 +5,9 @@ import org.zlibrary.text.model.ZLTextPlainModel;
class ZLTextPlainModelImpl extends ZLTextModelImpl implements ZLTextPlainModel { class ZLTextPlainModelImpl extends ZLTextModelImpl implements ZLTextPlainModel {
public void createParagraph(ZLTextParagraph.Kind kind) { public void createParagraph(ZLTextParagraph.Kind kind) {
ZLTextParagraph paragraph = (kind == ZLTextParagraph.Kind.TEXT_PARAGRAPH) ? new ZLTextParagraphImpl() : new ZLTextSpecialParagraphImpl(kind); ZLTextParagraph paragraph =
(kind == ZLTextParagraph.Kind.TEXT_PARAGRAPH) ?
new ZLTextParagraphImpl(myEntries) : new ZLTextSpecialParagraphImpl(kind, myEntries);
addParagraphInternal(paragraph); addParagraphInternal(paragraph);
} }
} }

View file

@ -1,9 +1,12 @@
package org.zlibrary.text.model.impl; package org.zlibrary.text.model.impl;
import java.util.ArrayList;
class ZLTextSpecialParagraphImpl extends ZLTextParagraphImpl { class ZLTextSpecialParagraphImpl extends ZLTextParagraphImpl {
private Kind myKind; private Kind myKind;
ZLTextSpecialParagraphImpl(Kind kind) { ZLTextSpecialParagraphImpl(Kind kind, ArrayList<Entry> entries) {
super(entries);
myKind = kind; myKind = kind;
} }

View file

@ -7,7 +7,7 @@ public class ZLTextTreeModelImpl extends ZLTextModelImpl implements ZLTextTreeMo
private final ZLTextTreeParagraph myRoot; private final ZLTextTreeParagraph myRoot;
public ZLTextTreeModelImpl() { public ZLTextTreeModelImpl() {
myRoot = new ZLTextTreeParagraphImpl(null); myRoot = new ZLTextTreeParagraphImpl(null, myEntries);
myRoot.open(true); myRoot.open(true);
} }
@ -15,7 +15,7 @@ public class ZLTextTreeModelImpl extends ZLTextModelImpl implements ZLTextTreeMo
if (parent == null) { if (parent == null) {
parent = myRoot; parent = myRoot;
} }
ZLTextTreeParagraph tp = new ZLTextTreeParagraphImpl(parent); ZLTextTreeParagraph tp = new ZLTextTreeParagraphImpl(parent, myEntries);
addParagraphInternal(tp); addParagraphInternal(tp);
return tp; return tp;
} }

View file

@ -12,7 +12,8 @@ class ZLTextTreeParagraphImpl extends ZLTextParagraphImpl implements ZLTextTreeP
private ZLTextTreeParagraph myParent; private ZLTextTreeParagraph myParent;
private final ArrayList<ZLTextTreeParagraph> myChildren = new ArrayList<ZLTextTreeParagraph>(); private final ArrayList<ZLTextTreeParagraph> myChildren = new ArrayList<ZLTextTreeParagraph>();
ZLTextTreeParagraphImpl(ZLTextTreeParagraph parent) { ZLTextTreeParagraphImpl(ZLTextTreeParagraph parent, ArrayList<Entry> entries) {
super(entries);
myParent = parent; myParent = parent;
if (parent != null) { if (parent != null) {
((ZLTextTreeParagraphImpl)parent).addChild(this); ((ZLTextTreeParagraphImpl)parent).addChild(this);

View file

@ -24,8 +24,7 @@ public abstract class ZLTextParagraphCursor {
/*Why do we need ZLTextParagraph.Entry interface?*/ /*Why do we need ZLTextParagraph.Entry interface?*/
public void fill() { public void fill() {
List <ZLTextParagraph.Entry> entries = myParagraph.getEntries(); for (ZLTextParagraph.Entry entry : myParagraph) {
for (ZLTextParagraph.Entry entry : entries) {
if (entry instanceof ZLTextEntry) { if (entry instanceof ZLTextEntry) {
processTextEntry((ZLTextEntry) entry); processTextEntry((ZLTextEntry) entry);
} else if (entry instanceof ZLTextControlEntry) { } else if (entry instanceof ZLTextControlEntry) {