|
fop 2.3 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.fop.hyphenation.TernaryTree
public class TernaryTree
A ternary search tree is a hibrid between a binary tree and a digital search tree (trie). Keys are limited to strings. A data value of type char is stored in each leaf node. It can be used as an index (or pointer) to the data. Branches that only contain one key are compressed to one node by storing a pointer to the trailer substring of the key. This class is intended to serve as base class or helper class to implement Dictionary collections or the like. Ternary trees have some nice properties as the following: the tree can be traversed in sorted order, partial matches (wildcard) can be implemented, retrieval of all keys within a given distance from the target, etc. The storage requirements are higher than a binary tree but a lot less than a trie. Performance is comparable with a hash table, sometimes it outperforms a hash function (most of the time can determine a miss faster than a hash).
The main purpose of this java port is to serve as a base for implementing TeX's hyphenation algorithm (see The TeXBook, appendix H). Each language requires from 5000 to 15000 hyphenation patterns which will be keys in this tree. The strings patterns are usually small (from 2 to 5 characters), but each char in the tree is stored in a node. Thus memory usage is the main concern. We will sacrify 'elegance' to keep memory requirements to the minimum. Using java's char type as pointer (yes, I know pointer it is a forbidden word in java) we can keep the size of the node to be just 8 bytes (3 pointers and the data char). This gives room for about 65000 nodes. In my tests the english patterns took 7694 nodes and the german patterns 10055 nodes, so I think we are safe.
All said, this is a map with strings as keys and char as value. Pretty limited!. It can be extended to a general map by using the string representation of an object and using the char value as an index to an array that contains the object values.
This work was authored by Carlos Villegas ([email protected]).
Nested Class Summary | |
---|---|
class |
TernaryTree.Iterator
an iterator |
Field Summary | |
---|---|
protected static int |
BLOCK_SIZE
allocation size for arrays |
protected char[] |
eq
Pointer to equal branch and to data when this node is a string terminator. |
protected char |
freenode
free node |
protected char[] |
hi
Pointer to high branch. |
protected CharVector |
kv
This vector holds the trailing of the keys when the branch is compressed. |
protected int |
length
number of items in tree |
protected char[] |
lo
Pointer to low branch and to rest of the key when it is stored directly in this node, we don't have unions in java! |
protected char |
root
root |
protected char[] |
sc
The character stored in this node: splitchar. |
Method Summary | |
---|---|
void |
balance()
Balance the tree for best search performance |
java.lang.Object |
clone()
|
int |
find(char[] key,
int start)
Find key. |
int |
find(java.lang.String key)
Find key. |
protected void |
init()
initialize |
void |
insert(char[] key,
int start,
char val)
Insert key. |
void |
insert(java.lang.String key,
char val)
Branches are initially compressed, needing one node per key plus the size of the string key. |
protected void |
insertBalanced(java.lang.String[] k,
char[] v,
int offset,
int n)
Recursively insert the median first and then the median of the lower and upper halves, and so on in order to get a balanced tree. |
java.util.Enumeration |
keys()
|
boolean |
knows(java.lang.String key)
|
static void |
main(java.lang.String[] args)
Main entry point for testing. |
void |
printStats()
Print stats (for testing). |
int |
size()
|
static int |
strcmp(char[] a,
int startA,
char[] b,
int startB)
Compares 2 null terminated char arrays |
static int |
strcmp(java.lang.String str,
char[] a,
int start)
Compares a string with null terminated char array |
static void |
strcpy(char[] dst,
int di,
char[] src,
int si)
|
static int |
strlen(char[] a)
|
static int |
strlen(char[] a,
int start)
|
void |
trimToSize()
Each node stores a character (splitchar) which is part of some key(s). |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected char[] lo
protected char[] hi
protected char[] eq
protected char[] sc
The character stored in this node: splitchar. Two special values are reserved:
This shouldn't be a problem if we give the usual semantics to strings since 0xFFFF is garanteed not to be an Unicode character.
protected CharVector kv
protected char root
protected char freenode
protected int length
protected static final int BLOCK_SIZE
Method Detail |
---|
protected void init()
public void insert(java.lang.String key, char val)
key
- the keyval
- a valuepublic void insert(char[] key, int start, char val)
key
- the keystart
- offset into key arrayval
- a valuepublic static int strcmp(char[] a, int startA, char[] b, int startB)
a
- a character arraystartA
- an index into character arrayb
- a character arraystartB
- an index into character array
public static int strcmp(java.lang.String str, char[] a, int start)
str
- a stringa
- a character arraystart
- an index into character array
public static void strcpy(char[] dst, int di, char[] src, int si)
dst
- a character arraydi
- an index into character arraysrc
- a character arraysi
- an index into character arraypublic static int strlen(char[] a, int start)
a
- a character arraystart
- an index into character array
public static int strlen(char[] a)
a
- a character array
public int find(java.lang.String key)
key
- the key
public int find(char[] key, int start)
key
- the keystart
- offset into key array
public boolean knows(java.lang.String key)
key
- a key
public int size()
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone
in class java.lang.Object
java.lang.CloneNotSupportedException
protected void insertBalanced(java.lang.String[] k, char[] v, int offset, int n)
k
- array of keysv
- array of valuesoffset
- where to insertn
- count to insertpublic void balance()
public void trimToSize()
public java.util.Enumeration keys()
public void printStats()
public static void main(java.lang.String[] args) throws java.lang.Exception
args
- not used
java.lang.Exception
- if not caught
|
fop 2.3 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |