commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example Quicksort.java
Date Mon, 24 Feb 2003 11:58:27 GMT
rwaldhoff    2003/02/24 03:58:27

  Modified:    functor/src/test/org/apache/commons/functor/example
                        Quicksort.java
  Log:
  some additional comments
  
  Revision  Changes    Path
  1.4       +38 -27    jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/Quicksort.java
  
  Index: Quicksort.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/Quicksort.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Quicksort.java	24 Feb 2003 11:38:07 -0000	1.3
  +++ Quicksort.java	24 Feb 2003 11:58:27 -0000	1.4
  @@ -77,17 +77,6 @@
   import org.apache.commons.functor.core.comparator.IsLessThan;
   import org.apache.commons.functor.core.composite.ConditionalUnaryFunction;
   
  -
  -/**
  - * An example of implementing the quicksort sorting algorithm 
  - * using commons-functor.
  - * <p> 
  - * See the extensive in line comments for details.
  - * 
  - * @version $Revision$ $Date$
  - * @author Rodney Waldhoff
  - */
  -
   /*
    * ----------------------------------------------------------------------------
    * INTRODUCTION:
  @@ -102,16 +91,24 @@
    * c-style, multi-line comments (slash-star to star-slash).
    * 
    * For convenience, and to make sure this example stays up to date, 
  - * we'll implement our quicksort example as a JUnit TestCase...
  + * we'll implement our quicksort example as a JUnit TestCase.
  + */
  +
  +/**
  + * An example of implementing the quicksort sorting algorithm 
  + * using commons-functor.
  + * <p> 
  + * See the extensive in line comments for details.
    * 
  + * @version $Revision$ $Date$
  + * @author Rodney Waldhoff
    */
   public class Quicksort extends TestCase {
   
   /*
  - * ...and declare the relevant constructor and suite() methods
  + * Let's declare the constructor and suite() methods we need
    * to ensure this test suite can be executed along with all the
    * others:
  - *
    */
   
       public Quicksort(String testName) {
  @@ -290,12 +287,9 @@
   
   /*
    * Finally, while this quicksort implementation is intended to 
  - * illustrate the use of Commons Functor, let's output some
  - * timings, just to demonstrate that the performance is adequate.
  - * 
  - * Let's declare some constants (we'll use them again below) to
  - * determine how big of a list and how many lists to sort for
  - * timing purposes.
  + * illustrate the use of Commons Functor, not for performance,
  + * let's output some timings, just to demonstrate that the 
  + * performance is adequate.
    */
    
       private static final int SIZE = 1000;
  @@ -376,7 +370,7 @@
    * Let's save ourselves some casting and error checking by defining
    * functor subclasses that deal with java.util.List.
    *
  - * Let ListFunction by a UnaryFunction that maps Lists to Lists:
  + * Let ListFunction be a UnaryFunction that operates on Lists :
    */ 
    
       public abstract class ListFunction implements UnaryFunction {
  @@ -419,7 +413,9 @@
    */
   
   /* 
  - * ...
  + * Let's define functors for the operations we'll need.
  + * 
  + * Given a List, we want to be able to break it into its head:
    */
   
       private UnaryFunction head = new ListFunction() {
  @@ -428,12 +424,19 @@
           }        
       };
   
  +/* 
  + * and its tail:
  + */
       private UnaryFunction tail = new ListFunction() {
           public Object evaluate(List list) {
               return list.size() < 2 ? Collections.EMPTY_LIST : list.subList(1,list.size());
           }        
       };
               
  +/* 
  + * Given a List in head/tail form, we should be able to find
  + * the List of elements in the tail less than the head:
  + */
       private BinaryFunction lesserTail = new BinaryFunction() {
           public Object evaluate(Object head, Object tail) {
               return CollectionAlgorithms.select(
  @@ -444,6 +447,10 @@
           }
       };
   
  +/* 
  + * and we should be able to find the List of elements in 
  + * the tail greater than the head:
  + */
       private BinaryFunction greaterTail = new BinaryFunction() {
           public Object evaluate(Object head, Object tail) {
               return CollectionAlgorithms.select(
  @@ -454,10 +461,14 @@
           }
       };
   
  -    private UnaryFunction quicksort = new ConditionalUnaryFunction(
  -        IsEmpty.getIsEmpty(),
  -        new ConstantFunction(Collections.EMPTY_LIST),
  -        new ListFunction() {
  +/* 
  + * With these building blocks, our quicksort function is a 
  + * straightfoward application of the description above:
  + */
  +    private UnaryFunction quicksort = new ConditionalUnaryFunction(        
  +        IsEmpty.getIsEmpty(),                         /* If the list is empty, */
  +        new ConstantFunction(Collections.EMPTY_LIST), /*  then return an empty list, */
  +        new ListFunction() {                          /*  else, split and recurse*/
               public Object evaluate(List list) {
                   List result = new ArrayList(list.size());
                   Object h = head.evaluate(list);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message