You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TernaryTreeAnalysis.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.hyphenation;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * This class provides some useful methods to print the structure of a TernaryTree object
  23. */
  24. public class TernaryTreeAnalysis {
  25. /**
  26. * The TernaryTree object to analyse
  27. */
  28. protected TernaryTree tt;
  29. /**
  30. * @param tt the TernaryTree object
  31. */
  32. public TernaryTreeAnalysis(TernaryTree tt) {
  33. this.tt = tt;
  34. }
  35. /**
  36. * Class representing a string of nodes in the tree representation of a TernaryTree
  37. */
  38. public static class NodeString {
  39. /**
  40. * The node string being constructed
  41. */
  42. public StringBuffer string = new StringBuffer();
  43. /**
  44. * The indent of the node string
  45. */
  46. public int indent;
  47. /**
  48. * The list of branchpoints into the high direction
  49. */
  50. public List high = new ArrayList();
  51. /**
  52. * The list of branchpoints into the low direction
  53. */
  54. public List low = new ArrayList();
  55. /**
  56. * @param indent the indent of the nodestring
  57. */
  58. public NodeString(int indent) {
  59. this.indent = indent;
  60. string.append("+");
  61. }
  62. }
  63. /**
  64. * Class representing a node of the TernaryTree object
  65. */
  66. protected class Node {
  67. /**
  68. * The index of the node
  69. */
  70. protected int index = 0;
  71. /**
  72. * The index of the high node
  73. */
  74. protected int high = 0;
  75. /**
  76. * The index of the high node
  77. */
  78. protected int low = 0;
  79. /**
  80. * The index of the equal node
  81. */
  82. protected int equal = 0;
  83. /**
  84. * The key following the node
  85. */
  86. protected String key = null;
  87. /**
  88. * True if this is a leaf node
  89. */
  90. protected boolean isLeafNode = false;
  91. /**
  92. * True if this is a packed node
  93. */
  94. protected boolean isPacked = false;
  95. /**
  96. * @param index the index of the node
  97. */
  98. protected Node(int index) {
  99. this.index = index;
  100. if (tt.sc[index] == 0) {
  101. isLeafNode = true;
  102. } else if (tt.sc[index] == 0xFFFF) {
  103. isLeafNode = true;
  104. isPacked = true;
  105. key = readKey().toString();
  106. } else {
  107. key = new String(tt.sc, index, 1);
  108. high = tt.hi[index];
  109. low = tt.lo[index];
  110. equal = tt.eq[index];
  111. }
  112. }
  113. private StringBuffer readKey() {
  114. StringBuffer s = new StringBuffer();
  115. int i = (int) tt.lo[index];
  116. char c = tt.kv.get(i);
  117. for (; c != 0; c = tt.kv.get(++i)) {
  118. s.append(c);
  119. }
  120. return s;
  121. }
  122. /**
  123. * Construct the string representation of the node
  124. * @return the string representing the node
  125. */
  126. public String toNodeString() {
  127. StringBuffer s = new StringBuffer();
  128. if (isLeafNode) {
  129. s.append("-" + index);
  130. if (isPacked) {
  131. s.append(",=>'" + key + "'");
  132. }
  133. s.append(",leaf");
  134. } else {
  135. s.append("-" + index + "--" + key + "-");
  136. }
  137. return s.toString();
  138. }
  139. /**
  140. * Construct the compact string representation of the node
  141. * @return the string representing the node
  142. */
  143. public String toCompactString() {
  144. StringBuffer s = new StringBuffer();
  145. if (isLeafNode) {
  146. s.append("-" + index);
  147. if (isPacked) {
  148. s.append(",=>'" + key + "'");
  149. }
  150. s.append(",leaf\n");
  151. } else {
  152. if (high != 0) {
  153. s.append("(+-" + high + ")\n |\n");
  154. }
  155. s.append("-" + index + "- " + key + " (-" + equal + ")\n");
  156. if (low != 0) {
  157. s.append(" |\n(+-" + low + ")\n");
  158. }
  159. }
  160. return s.toString();
  161. }
  162. /* (non-Javadoc)
  163. * @see java.lang.Object#toString()
  164. */
  165. public String toString() {
  166. StringBuffer s = new StringBuffer();
  167. s.append("Node " + index + ":\n");
  168. if (isLeafNode) {
  169. if (isPacked) {
  170. s.append("key: " + key + "\n");
  171. }
  172. } else {
  173. s.append("high: " + (high == 0 ? "-" : String.valueOf(high))
  174. + ", equal: " + equal
  175. + ", low: " + (low == 0 ? "-" : String.valueOf(low))
  176. + "\n");
  177. s.append("key: " + key + "\n");
  178. }
  179. return s.toString();
  180. }
  181. }
  182. /**
  183. * Construct the compact node representation of the TernaryTree object
  184. * @return the string representing the tree
  185. */
  186. public String toCompactNodes() {
  187. StringBuffer s = new StringBuffer();
  188. for (int i = 1; i < tt.sc.length; ++i) {
  189. if (i != 1) {
  190. s.append("\n");
  191. }
  192. s.append((new Node(i)).toCompactString());
  193. }
  194. return s.toString();
  195. }
  196. /**
  197. * Construct the node representation of the TernaryTree object
  198. * @return the string representing the tree
  199. */
  200. public String toNodes() {
  201. StringBuffer s = new StringBuffer();
  202. for (int i = 1; i < tt.sc.length; ++i) {
  203. if (i != 1) {
  204. s.append("\n");
  205. }
  206. s.append((new Node(i)).toString());
  207. }
  208. return s.toString();
  209. }
  210. private static StringBuffer toString(char[] c) {
  211. StringBuffer s = new StringBuffer();
  212. for (int i = 0; i < c.length; ++i) {
  213. s.append((int) c[i]);
  214. s.append(",");
  215. }
  216. return s;
  217. }
  218. /* (non-Javadoc)
  219. * @see java.lang.Object#toString()
  220. */
  221. public String toString() {
  222. StringBuffer s = new StringBuffer();
  223. s.append("hi: ");
  224. s.append(toString(tt.hi));
  225. s.append("\n");
  226. s.append("eq: ");
  227. s.append(toString(tt.eq));
  228. s.append("\n");
  229. s.append("lo: ");
  230. s.append(toString(tt.lo));
  231. s.append("\n");
  232. s.append("sc: ");
  233. for (int i = 0; i < tt.sc.length; ++i) {
  234. if (tt.sc[i] == 0) {
  235. s.append("-");
  236. } else if (tt.sc[i] == 0xFFFF) {
  237. s.append("^");
  238. } else {
  239. s.append(tt.sc[i]);
  240. }
  241. }
  242. s.append("\n");
  243. s.append("kv: ");
  244. for (int i = 0; i < tt.kv.length(); ++i) {
  245. if (tt.kv.get(i) == 0) {
  246. s.append("-");
  247. } else {
  248. s.append(tt.kv.get(i));
  249. }
  250. }
  251. s.append("\n");
  252. s.append("freenode: ");
  253. s.append((int) tt.freenode);
  254. s.append("\n");
  255. s.append("root: ");
  256. s.append((int) tt.root);
  257. s.append("\n");
  258. return s.toString();
  259. }
  260. }