| 1 | /* |
| 2 | * $Id: TraverseFunctions.java 594 2010-11-19 20:41:04Z PSpeed $ |
| 3 | * |
| 4 | * The Filament BSD license. |
| 5 | * |
| 6 | * Copyright (c) 2009-2010, the original author or authors |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1) Redistributions of source code must retain the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer. |
| 14 | * 2) Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3) Neither the names "Filament", "fgraph.org", "filamentgraph.org", nor the |
| 18 | * names of its contributors may be used to endorse or promote products |
| 19 | * derived from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | * POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | package org.fgraph.steps; |
| 35 | |
| 36 | import java.util.*; |
| 37 | |
| 38 | import com.google.common.base.Function; |
| 39 | import com.google.common.base.Predicate; |
| 40 | import com.google.common.collect.Iterables; |
| 41 | import com.google.common.collect.Iterators; |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * @version $Revision: 594 $ |
| 47 | * @author Paul Speed |
| 48 | */ |
| 49 | public class TraverseFunctions |
| 50 | { |
| 51 | /** |
| 52 | * Creates a function that performs a pre-order depth-first |
| 53 | * traversal starting at an applied node and using the specified |
| 54 | * adjacency function for traversal. |
| 55 | * |
| 56 | * <p>If an optional visitMarker function is provided then the |
| 57 | * traversal is acyclic based on the 'marker' returned from that |
| 58 | * function. For example, a Functions.identity() visit marker |
| 59 | * will make sure a node is only visited once based on its own |
| 60 | * .equals() implementation. A null indicates that no cycle |
| 61 | * detection should be performed.</p> |
| 62 | */ |
| 63 | public static <T> StepFunction<T,T> preOrder( Function<T,? extends Iterable<T>> fstep, |
| 64 | Function<? super T,? extends Object> visitMarker ) |
| 65 | { |
| 66 | return StepFunctions.unwrapSteps( preOrderSteps(fstep, visitMarker) ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates a function that performs a pre-order depth-first |
| 71 | * traversal starting at an applied node and using the specified |
| 72 | * adjacency function for traversal, also providing additional |
| 73 | * control over and information about the ongoing traversal. |
| 74 | * |
| 75 | * <p>This is similar to the non-step based version except that |
| 76 | * the traversed values are returned wrapped in DepthFirstStep |
| 77 | * objects that provide additional information about the state |
| 78 | * of the traversal such as depth. These steps also allow themselves |
| 79 | * to be 'pruned', resulting in any potential descendants being ignored |
| 80 | * during the subsequent traverses.</p> |
| 81 | * |
| 82 | * <p>If an optional visitMarker function is provided then the |
| 83 | * traversal is acyclic based on the 'marker' returned from that |
| 84 | * function. For example, a Functions.identity() visit marker |
| 85 | * will make sure a node is only visited once based on its own |
| 86 | * .equals() implementation. A null indicates that no cycle |
| 87 | * detection should be performed.</p> |
| 88 | */ |
| 89 | public static <T> StepFunction<T,DepthFirstStep<T>> preOrderSteps( |
| 90 | Function<T,? extends Iterable<T>> fstep, |
| 91 | Function<? super T,? extends Object> visitMarker ) |
| 92 | { |
| 93 | StepFunction<T,DepthFirstStep<T>> fTraverse = depthFirstSteps(fstep, visitMarker); |
| 94 | fTraverse = StepFunctions.filter( fTraverse, StepPredicates.<T>descending() ); |
| 95 | return fTraverse; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Creates a function that performs a post-order depth-first |
| 100 | * traversal starting at an applied node and using the specified |
| 101 | * adjacency function for traversal. |
| 102 | * |
| 103 | * <p>If an optional visitMarker function is provided then the |
| 104 | * traversal is acyclic based on the 'marker' returned from that |
| 105 | * function. For example, a Functions.identity() visit marker |
| 106 | * will make sure a node is only visited once based on its own |
| 107 | * .equals() implementation. A null indicates that no cycle |
| 108 | * detection should be performed.</p> |
| 109 | */ |
| 110 | public static <T> StepFunction<T,T> postOrder( Function<T,? extends Iterable<T>> fstep, |
| 111 | Function<? super T,? extends Object> visitMarker ) |
| 112 | { |
| 113 | return StepFunctions.unwrapSteps( postOrderSteps(fstep, visitMarker) ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Creates a function that performs a post-order depth-first |
| 118 | * traversal starting at an applied node and using the specified |
| 119 | * adjacency function for traversal, also providing additional |
| 120 | * control over and information about the ongoing traversal. |
| 121 | * |
| 122 | * <p>This is similar to the non-step based version except that |
| 123 | * the traversed values are returned wrapped in DepthFirstStep |
| 124 | * objects that provide additional information about the state |
| 125 | * of the traversal such as depth. These steps also allow themselves |
| 126 | * to be 'pruned', resulting in any potential descendants being ignored |
| 127 | * during the subsequent traverses.</p> |
| 128 | * |
| 129 | * <p>If an optional visitMarker function is provided then the |
| 130 | * traversal is acyclic based on the 'marker' returned from that |
| 131 | * function. For example, a Functions.identity() visit marker |
| 132 | * will make sure a node is only visited once based on its own |
| 133 | * .equals() implementation. A null indicates that no cycle |
| 134 | * detection should be performed.</p> |
| 135 | */ |
| 136 | public static <T> StepFunction<T,DepthFirstStep<T>> postOrderSteps( |
| 137 | Function<T,? extends Iterable<T>> fstep, |
| 138 | Function<? super T,? extends Object> visitMarker ) |
| 139 | { |
| 140 | StepFunction<T,DepthFirstStep<T>> fTraverse = depthFirstSteps(fstep, visitMarker); |
| 141 | fTraverse = StepFunctions.filter( fTraverse, StepPredicates.<T>ascending() ); |
| 142 | return fTraverse; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Creates a function that performs a full depth-first |
| 147 | * traversal (both pre- and post-) starting at an applied node |
| 148 | * and using the specified adjacency function for traversal. |
| 149 | * Every 'node' will therefore be visited twice, once descending |
| 150 | * and once ascending. Use the step-based depthFirstSteps() |
| 151 | * method to distinguish descending from ascending. |
| 152 | * |
| 153 | * <p>If an optional visitMarker function is provided then the |
| 154 | * traversal is acyclic based on the 'marker' returned from that |
| 155 | * function. For example, a Functions.identity() visit marker |
| 156 | * will make sure a node is only visited once based on its own |
| 157 | * .equals() implementation. A null indicates that no cycle |
| 158 | * detection should be performed.</p> |
| 159 | */ |
| 160 | public static <T> StepFunction<T,T> depthFirst( Function<T,? extends Iterable<T>> fstep, |
| 161 | Function<? super T,? extends Object> visitMarker ) |
| 162 | { |
| 163 | return StepFunctions.unwrapSteps( depthFirstSteps(fstep, visitMarker) ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Creates a function that performs a full depth-first |
| 168 | * traversal (both pre- and post-) starting at an applied node |
| 169 | * and using the specified adjacency function for traversal, |
| 170 | * also providing additional control over and information about |
| 171 | * the ongoing traversal. |
| 172 | * |
| 173 | * <p>This is similar to the non-step based version except that |
| 174 | * the traversed values are returned wrapped in DepthFirstStep |
| 175 | * objects that provide additional information about the state |
| 176 | * of the traversal such as depth, descending/ascending, etc. |
| 177 | * These steps also allow themselves to be 'pruned', resulting |
| 178 | * in any potential descendants being ignored during the subsequent |
| 179 | * traverses.</p> |
| 180 | * |
| 181 | * <p>If an optional visitMarker function is provided then the |
| 182 | * traversal is acyclic based on the 'marker' returned from that |
| 183 | * function. For example, a Functions.identity() visit marker |
| 184 | * will make sure a node is only visited once based on its own |
| 185 | * .equals() implementation. A null indicates that no cycle |
| 186 | * detection should be performed.</p> |
| 187 | */ |
| 188 | public static <T> StepFunction<T,DepthFirstStep<T>> depthFirstSteps( |
| 189 | Function<T,? extends Iterable<T>> fstep, |
| 190 | Function<? super T,? extends Object> visitMarker ) |
| 191 | { |
| 192 | return new DepthFirstFunction<T>( fstep, visitMarker ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Creates a function that performs a breadth-first |
| 197 | * traversal starting at an applied node and using the specified |
| 198 | * adjacency function for traversal. |
| 199 | * |
| 200 | * <p>If an optional visitMarker function is provided then the |
| 201 | * traversal is acyclic based on the 'marker' returned from that |
| 202 | * function. For example, a Functions.identity() visit marker |
| 203 | * will make sure a node is only visited once based on its own |
| 204 | * .equals() implementation. A null indicates that no cycle |
| 205 | * detection should be performed.</p> |
| 206 | */ |
| 207 | public static <T> StepFunction<T,T> breadthFirst( Function<T,? extends Iterable<T>> fstep, |
| 208 | Function<? super T,? extends Object> visitMarker ) |
| 209 | { |
| 210 | return StepFunctions.unwrapSteps( breadthFirstSteps(fstep, visitMarker) ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Creates a function that performs a breadth-first |
| 215 | * traversal starting at an applied node and using the specified |
| 216 | * adjacency function for traversal, also providing additional |
| 217 | * control over and information about the ongoing traversal. |
| 218 | * |
| 219 | * <p>This is similar to the non-step based version except that |
| 220 | * the traversed values are returned wrapped in PrunableStep |
| 221 | * objects that provide additional information about the state |
| 222 | * of the traversal such as depth. These steps also allow themselves |
| 223 | * to be 'pruned', resulting in any potential descendants being ignored |
| 224 | * during the subsequent traverses.</p> |
| 225 | * |
| 226 | * <p>If an optional visitMarker function is provided then the |
| 227 | * traversal is acyclic based on the 'marker' returned from that |
| 228 | * function. For example, a Functions.identity() visit marker |
| 229 | * will make sure a node is only visited once based on its own |
| 230 | * .equals() implementation. A null indicates that no cycle |
| 231 | * detection should be performed.</p> |
| 232 | */ |
| 233 | public static <T> StepFunction<T,PrunableStep<T>> breadthFirstSteps( |
| 234 | Function<T,? extends Iterable<T>> fstep, |
| 235 | Function<? super T,? extends Object> visitMarker ) |
| 236 | { |
| 237 | return new BreadthFirstFunction<T>( fstep, visitMarker ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Creates a function that performs a priority-ordered |
| 242 | * traversal starting at an applied node and using the specified |
| 243 | * adjacency function for traversal. |
| 244 | * |
| 245 | * <p>The priority comparator is used to select the next step/path |
| 246 | * from the current pending set of descendants. For example, a |
| 247 | * comparator could be based to select the smallest/shortest route |
| 248 | * based on the sum of the accumulated weights/distances up to that step.</p> |
| 249 | * |
| 250 | * <p>If an optional visitMarker function is provided then the |
| 251 | * traversal is acyclic based on the 'marker' returned from that |
| 252 | * function. For example, a Functions.identity() visit marker |
| 253 | * will make sure a node is only visited once based on its own |
| 254 | * .equals() implementation. A null indicates that no cycle |
| 255 | * detection should be performed.</p> |
| 256 | */ |
| 257 | public static <T> StepFunction<T,T> priority( Function<T,? extends Iterable<T>> fstep, |
| 258 | Comparator<? super T> priority, |
| 259 | Function<? super T,? extends Object> visitMarker ) |
| 260 | { |
| 261 | return StepFunctions.unwrapSteps( prioritySteps( fstep, priority, visitMarker ) ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Creates a function that performs a priority-ordered |
| 266 | * traversal starting at an applied node and using the specified |
| 267 | * adjacency function for traversal, also providing additional |
| 268 | * control over and information about the ongoing traversal. |
| 269 | * |
| 270 | * <p>The priority comparator is used to select the next step/path |
| 271 | * from the current pending set of descendants. For example, a |
| 272 | * comparator could be based to select the smallest/shortest route |
| 273 | * based on the sum of the accumulated weights/distances up to that step.</p> |
| 274 | * |
| 275 | * <p>This is similar to the non-step based version except that |
| 276 | * the traversed values are returned wrapped in PrunableStep |
| 277 | * objects that provide additional information about the state |
| 278 | * of the traversal such as depth. These steps also allow themselves |
| 279 | * to be 'pruned', resulting in any potential descendants being ignored |
| 280 | * during the subsequent traverses. Note: for traversals that branch |
| 281 | * quickly, pruning a step may be a non-trivial amount of work since |
| 282 | * it must exhaustively search the current sorted pending steps to |
| 283 | * remove any queued descendants.</p> |
| 284 | * |
| 285 | * <p>If an optional visitMarker function is provided then the |
| 286 | * traversal is acyclic based on the 'marker' returned from that |
| 287 | * function. For example, a Functions.identity() visit marker |
| 288 | * will make sure a node is only visited once based on its own |
| 289 | * .equals() implementation. A null indicates that no cycle |
| 290 | * detection should be performed.</p> |
| 291 | */ |
| 292 | public static <T> StepFunction<T,PrunableStep<T>> prioritySteps( |
| 293 | Function<T,? extends Iterable<T>> fstep, |
| 294 | Comparator<? super T> priority, |
| 295 | Function<? super T,? extends Object> visitMarker ) |
| 296 | { |
| 297 | return new PriorityTraverseFunction<T>( fstep, priority, visitMarker ); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Function implementation that creates depth first iterators on demand. |
| 302 | */ |
| 303 | private static class DepthFirstFunction<T> implements StepFunction<T,DepthFirstStep<T>> |
| 304 | { |
| 305 | private Function<T,? extends Iterable<T>> fstep; |
| 306 | private Function<? super T,? extends Object> visitMarker; |
| 307 | |
| 308 | public DepthFirstFunction( Function<T,? extends Iterable<T>> fstep, |
| 309 | Function<? super T,? extends Object> visitMarker ) |
| 310 | { |
| 311 | this.fstep = fstep; |
| 312 | this.visitMarker = visitMarker; |
| 313 | } |
| 314 | |
| 315 | public Iterable<DepthFirstStep<T>> apply( T start ) |
| 316 | { |
| 317 | return Traversers.depthFirstSteps( start, fstep, visitMarker ); |
| 318 | } |
| 319 | |
| 320 | public String toString() |
| 321 | { |
| 322 | return "DepthFirstFunction[delegate=" + fstep + ", visitMarker=" + visitMarker + "]"; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Function implementation that creates breadth first iterators on demand. |
| 328 | */ |
| 329 | private static class BreadthFirstFunction<T> implements StepFunction<T,PrunableStep<T>> |
| 330 | { |
| 331 | private Function<T,? extends Iterable<T>> fstep; |
| 332 | private Function<? super T,? extends Object> visitMarker; |
| 333 | |
| 334 | public BreadthFirstFunction( Function<T,? extends Iterable<T>> fstep, |
| 335 | Function<? super T,? extends Object> visitMarker ) |
| 336 | { |
| 337 | this.fstep = fstep; |
| 338 | this.visitMarker = visitMarker; |
| 339 | } |
| 340 | |
| 341 | public Iterable<PrunableStep<T>> apply( T start ) |
| 342 | { |
| 343 | return Traversers.breadthFirstSteps( start, fstep, visitMarker ); |
| 344 | } |
| 345 | |
| 346 | public String toString() |
| 347 | { |
| 348 | return "BreadthFirstFunction[delegate=" + fstep + ", visitMarker=" + visitMarker + "]"; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Function implementation that creates priority first iterators on demand. |
| 354 | */ |
| 355 | private static class PriorityTraverseFunction<T> implements StepFunction<T,PrunableStep<T>> |
| 356 | { |
| 357 | private Function<T,? extends Iterable<T>> fstep; |
| 358 | private Comparator<? super T> priority; |
| 359 | private Function<? super T,? extends Object> visitMarker; |
| 360 | |
| 361 | public PriorityTraverseFunction( Function<T,? extends Iterable<T>> fstep, |
| 362 | Comparator<? super T> priority, |
| 363 | Function<? super T,? extends Object> visitMarker ) |
| 364 | { |
| 365 | this.fstep = fstep; |
| 366 | this.priority = priority; |
| 367 | this.visitMarker = visitMarker; |
| 368 | } |
| 369 | |
| 370 | public Iterable<PrunableStep<T>> apply( T start ) |
| 371 | { |
| 372 | return Traversers.prioritySteps( start, fstep, priority, visitMarker ); |
| 373 | } |
| 374 | |
| 375 | public String toString() |
| 376 | { |
| 377 | return "PriorityTraverseFunction[delegate=" + fstep + ", priority=" + priority |
| 378 | + ", visitMarker=" + visitMarker + "]"; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | } |
| 383 | |