Nice TWiki > Doc > GeneratorMultiMethodExample ( vs. r1.1) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
 <<O>>  Difference Topic GeneratorMultiMethodExample (r1.1 - 30 Jan 2004 - IsaacGouy)
Added:
>
>

%META:TOPICINFO{author="IsaacGouy" date="1075492349" format="1.0" version="1.1"}% %META:TOPICPARENT{name="BuilderPatternMultiMethodExample"}%

/* Please try the NoviceExamples before the MultiMethodExamples
To compile:
   nicec --sourcepath=.. -a builderpattern.jar builderpattern
To run:
   java -jar builderpattern.jar
*/

package builderpattern;

String outFile(Style s) = "driver" + VISITEES + ".java";


//==== dumpHeader


void dumpHeader(Style s){
   out.println("public class " + "driver" + VISITEES + " { \n");
}


//==== dumpInnerClasses


void dumpInnerClasses(Style s){
   dumpBaseClasses(s);
   for (int i=0;i<VISITEES;i++) dumpVisitee(s, i, HIERARCHY);
   dumpVisitorClasses(s);
}


void dumpBaseClasses(Style s){
   out.println("public interface A {}\n");
}


void dumpVisitee(Style s, int i, Hierarchy h){
   if (h == FLAT || (i ==  0)) 
      dumpVisitee0(s, i);
   else 
      dumpVisiteeI(s, i);
}


void dumpVisitee0(Style s, int i) = 
   out.println("public static class A"+i+" implements A {}\n");


void dumpVisiteeI(Style s, int i) {
   out.println("public static class A" + i +
      " extends A" + (i-1) + " implements A {}\n");
}


void dumpVisitorClasses(Style s){}



//==== dumpLoop


void dumpMainHeader(Style s) = 
   out.println("\n\npublic static void main(String[] args) {");


void dumpMainFooter(Style s) = out.println("}\n\n");


void dumpLoop(Style s){
   out.println("   "+s+"();");
   out.println("   int[] results"+s+" = new int[" + REPEATS + "];");
   out.println("   for (int repeat=0;repeat<"+REPEATS+";repeat++)");
   out.println("      results"+s+"[repeat] = " + s + "();");
   out.println("   long sum"+s+" = 0;\n"+
               "   long sqsum"+s+" = 0;\n"+
               "   long min"+s+" = results"+s+"[0];\n"+
               "   long max"+s+" = results"+s+"[0];\n"+
               "   for (int i=0;i<"+REPEATS+";i++) {\n"+
               "      sum"+s+" += results"+s+"[i];\n"+
               "      sqsum"+s+" += results"+s+"[i] * results"+s+"[i];\n"+
               "      if (results"+s+"[i] > max"+s+")\n"+
               "         max"+s+" = results"+s+"[i];\n"+
               "      if (results"+s+"[i] < min"+s+")\n"+
               "         min"+s+" = results"+s+"[i];\n"+      
               "   }"
      );

   if (GP_OUTPUT) 
      out.println("   System.out.println(\n"+
                  "      \""+s+" "+ VISITEES+ " \" + (sum"+s+"/"+REPEATS+".0) + \n"+
                  "      \"  \" + (Math.sqrt((sqsum"+s+" -\n" +
                  "         (sum"+s+"*sum"+s+"/"+REPEATS+".0))/"+(REPEATS-1)+".0)) +\n"+
                  "      \"  \" + min"+s+" +"+" \"  \" + max"+s+");"
         );
   else
      out.println("   System.out.println(\n"+
                  "      \""+s+": AVG: \" + (sum"+s+"/"+REPEATS+".0) + \n"+
                  "      \" SDV: \" + (Math.sqrt((sqsum"+s+" -\n" + 
                  "         (sum"+s+"*sum"+s+ "/"+REPEATS+".0))/"+(REPEATS-1)+".0)) +\n"+
                  "      \" MIN: \" + min"+s+" +" +" \" MAX: \" + max"+s+");"
         );
}



//==== dumpFun


void createVisitees(Style s) {
   out.println("   final A[] a = new A[" + VISITEES+"];\n");
   for (int i=0;i<VISITEES;i++)
      out.println("   a["+i+ "] = new A"+i+"();");
   out.print("\n");
}


void startTimer(Style s) =
   out.println("   long start = System.currentTimeMillis();");


void stopTimer(Style s) =
   out.println("   long end = System.currentTimeMillis();\n" +
               "   return (int)(end - start);");


void dumpFun(Style s){}


//==== dumpFooter


void dumpFooter(Style s) = out.println("\n\n} // end Driver\n\n");
-- IsaacGouy - 30 Jan 2004

Topic GeneratorMultiMethodExample . { View | Diffs | r1.1 | More }
Revision -
Revision r1.1 - 30 Jan 2004 - 19:52 GMT - IsaacGouy
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.