Changeset 1282 for trunk/autoquest-core-usageprofiles/src/main/java/de/ugoe/cs/autoquest/usageprofiles/TrieNode.java
- Timestamp:
- 07/30/13 09:38:43 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-core-usageprofiles/src/main/java/de/ugoe/cs/autoquest/usageprofiles/TrieNode.java
r1251 r1282 29 29 * <p> 30 30 * This class implements a node of a trie. Each node is associated with a symbol and has a counter. 31 * The counter marks the number of occur ences of the sequence defined by the path from the root of31 * The counter marks the number of occurrences of the sequence defined by the path from the root of 32 32 * the trie to this node. 33 33 * </p> … … 50 50 /** 51 51 * <p> 52 * Counter for the number of occur ences of the sequence.52 * Counter for the number of occurrences of the sequence. 53 53 * </p> 54 54 */ … … 71 71 /** 72 72 * <p> 73 * Comparator to be used for comparing the symbols with each other74 * </p> 75 */ 76 private Symbol Comparator<T> comparator;73 * Strategy for handling symbols, i.e., for comparing and storing them 74 * </p> 75 */ 76 private SymbolStrategy<T> strategy; 77 77 78 78 /** … … 80 80 * Constructor. Creates a new TrieNode without a symbol associated.<br> 81 81 * <b>This constructor should only be used to create the root node of the trie!</b> 82 * The comparator used by the tree node will be a {@link DefaultSymbolComparator}.82 * The strategy used by the trie node will be a {@link DefaultSymbolStrategy}. 83 83 * </p> 84 84 */ 85 85 TrieNode() { 86 this(new DefaultSymbol Comparator<T>());86 this(new DefaultSymbolStrategy<T>()); 87 87 } 88 88 … … 90 90 * <p> 91 91 * Constructor. Creates a new TrieNode. The symbol must not be null. 92 * The comparator used by the tree node will be a {@link DefaultSymbolComparator}.92 * The strategy used by the trie node will be a {@link DefaultSymbolStrategy}. 93 93 * </p> 94 94 * 95 95 * @param symbol 96 96 * symbol associated with the trie node 97 * 98 * @throws IllegalArgumentException 99 * if the provided symbol is null 97 100 */ 98 101 TrieNode(T symbol) { 99 this(symbol, new DefaultSymbol Comparator<T>());102 this(symbol, new DefaultSymbolStrategy<T>()); 100 103 } 101 104 … … 103 106 * <p> 104 107 * Constructor. Creates a new TrieNode without a symbol associated using the provided 105 * comparator.<br>108 * strategy.<br> 106 109 * <b>This constructor should only be used to create the root node of the trie!</b> 107 * <br>The comparator must not be null. 108 * </p> 109 */ 110 TrieNode(SymbolComparator<T> comparator) { 111 if (comparator == null) { 112 throw new IllegalArgumentException("comparator must not be null!"); 113 } 114 this.comparator = comparator; 110 * <br>The strategy must not be null. 111 * </p> 112 * 113 * @param strategy 114 * the strategy used for comparing and storing symbols 115 * 116 * @throws IllegalArgumentException 117 * if the provided strategy is null 118 */ 119 TrieNode(SymbolStrategy<T> strategy) { 120 if (strategy == null) { 121 throw new IllegalArgumentException("strategy must not be null!"); 122 } 123 this.strategy = strategy; 115 124 this.symbol = null; 116 125 count = 0; 117 children = new SymbolMap<T, TrieNode<T>>(this.comparator);118 } 119 120 /** 121 * <p> 122 * Constructor. Creates a new TrieNode using the provided comparator. The symbol and the123 * comparatormust not be null.126 children = strategy.createSymbolMap(); 127 } 128 129 /** 130 * <p> 131 * Constructor. Creates a new TrieNode using the provided strategy. The symbol and the 132 * strategy must not be null. 124 133 * </p> 125 134 * 126 135 * @param symbol 127 136 * symbol associated with the trie node 128 */ 129 TrieNode(T symbol, SymbolComparator<T> comparator) { 137 * @param strategy 138 * the strategy used for comparing and storing symbols 139 * 140 * @throws IllegalArgumentException 141 * if the provided symbol or strategy is null 142 */ 143 TrieNode(T symbol, SymbolStrategy<T> strategy) { 130 144 if (symbol == null) { 131 145 throw new IllegalArgumentException … … 134 148 this.symbol = symbol; 135 149 136 if ( comparator== null) {137 throw new IllegalArgumentException(" comparatormust not be null!");138 } 139 this. comparator = comparator;150 if (strategy == null) { 151 throw new IllegalArgumentException("strategy must not be null!"); 152 } 153 this.strategy = strategy; 140 154 141 155 count = 0; 142 children = new SymbolMap<T, TrieNode<T>>(this.comparator);156 children = strategy.createSymbolMap(); 143 157 } 144 158 … … 148 162 * </p> 149 163 * 150 * @param other 164 * @param other the trie node to create the copy of 165 * 166 * @throws IllegalArgumentException 167 * if the provided node is null 151 168 */ 152 169 TrieNode(TrieNode<T> other) { … … 156 173 symbol = other.symbol; 157 174 count = other.count; 158 comparator = other.comparator;159 children = new SymbolMap<T, TrieNode<T>>(this.comparator);175 strategy = other.strategy; 176 children = strategy.createSymbolMap(); 160 177 161 178 for (TrieNode<T> child : other.children.getValues()) { … … 175 192 public void add(List<T> subsequence) { 176 193 if (!subsequence.isEmpty()) { 177 if (! comparator.equals(symbol, subsequence.get(0))) { // should be guaranteed by178 //the recursion/TrieRoot!194 if (!strategy.getSymbolComparator().equals(symbol, subsequence.get(0))) { 195 // should be guaranteed by the recursion/TrieRoot! 179 196 throw new AssertionError("Invalid trie operation!"); 180 197 } … … 201 218 /** 202 219 * <p> 203 * Returns the number of occur ences of the sequence represented by the node.204 * </p> 205 * 206 * @return number of occur ences of the sequence represented by the node220 * Returns the number of occurrences of the sequence represented by the node. 221 * </p> 222 * 223 * @return number of occurrences of the sequence represented by the node 207 224 */ 208 225 public int getCount() { … … 224 241 TrieNode<T> node = getChild(symbol); 225 242 if (node == null) { 226 node = new TrieNode<T>(symbol, comparator);243 node = new TrieNode<T>(symbol, strategy); 227 244 children.addSymbol(symbol, node); 228 245 } … … 283 300 /** 284 301 * <p> 285 * Returns a collection of all symbols that follow athis node, i.e., the symbols associated302 * Returns a collection of all symbols that follow this node, i.e., the symbols associated 286 303 * with the children of this node. 287 304 * </p> … … 399 416 /** 400 417 * <p> 401 * Recursive method sthat collects all nodes that are ancestors of leafs and stores them in the418 * Recursive method that collects all nodes that are ancestors of leafs and stores them in the 402 419 * set. 403 420 * </p> … … 457 474 * <p> 458 475 * Two TrieNodes are defined as equal, if their {@link #count}, {@link #symbol}, and 459 * {@link #children} are equal. For the comparison of their symbols the comparator is used. 476 * {@link #children} are equal. For the comparison of their symbols the comparator provided 477 * by the symbol management strategy is used. 460 478 * </p> 461 479 * … … 477 495 return count == otherNode.count && 478 496 symbol.getClass().isInstance(((TrieNode) other).symbol) && 479 comparator.equals(symbol, ((TrieNode<T>) other).symbol) &&497 strategy.getSymbolComparator().equals(symbol, ((TrieNode<T>) other).symbol) && 480 498 children.equals(((TrieNode) other).children); 481 499 }
Note: See TracChangeset
for help on using the changeset viewer.