Advertisement
KoMeDiAnT

Second Experiment

Dec 12th, 2016
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Profiling
  8. {
  9.     class Generator
  10.     {
  11.         public static string GenerateDeclarations()
  12.         {
  13.             var structOrClass = new StringBuilder();
  14.             var value = new StringBuilder();
  15.  
  16.             for (int i = 0; i < Constants.FieldCounts.Count; i++)
  17.             {
  18.                 var elementOfField = Constants.FieldCounts.ElementAt(i);
  19.                 for (int j = elementOfField/2; j < elementOfField; j++)
  20.                 {
  21.                     value.Append("byte Value" + j + ';');
  22.                 }
  23.  
  24.                 structOrClass.Append("struct S" + elementOfField + "{" + value + "}");
  25.                 structOrClass.Append("class C" + elementOfField + "{" + value + "}");
  26.             }
  27.             return structOrClass.ToString();
  28.         }
  29.  
  30.         public static void MethodsForClassAndStruct(StringBuilder arrayRuner, int i)
  31.         {
  32.             var elementOfField = Constants.FieldCounts.ElementAt(i);
  33.             arrayRuner.Append("\nvoid PC" + elementOfField + "()\n");
  34.             arrayRuner.Append("{\n\tvar array = new C" + elementOfField + "[Constants.ArraySize];\n");
  35.             arrayRuner.Append("\tfor (int i = 0; i < Constants.ArraySize; i++) array[i] = new C" +
  36.                 elementOfField + "();\n}\n");
  37.             arrayRuner.Append("\nvoid PS" + elementOfField + "()\n");
  38.             arrayRuner.Append("{\n\tvar array = new S" + elementOfField + "[Constants.ArraySize];\n}\n");
  39.         }
  40.  
  41.         public static string GenerateArrayRunner()
  42.         {
  43.             var arrayRuner = new StringBuilder();
  44.             arrayRuner.Append("public class ArrayRunner : IRunner\n{");
  45.  
  46.             for (int i = 0; i < Constants.FieldCounts.Count; i++)
  47.             {
  48.                 MethodsForClassAndStruct(arrayRuner, i);
  49.             }
  50.  
  51.             arrayRuner.Append("public void Call(bool isClass, int size, int count)\n{\n");
  52.  
  53.             for (int i = 0; i < Constants.FieldCounts.Count; i++)
  54.             {
  55.                 var elementOfField = Constants.FieldCounts.ElementAt(i);
  56.                 arrayRuner.Append("if (isClass && size == " + elementOfField + ")\n{\n\tfor (int i = 0; i < count; i++) PC" +
  57.                     elementOfField + "(); \nreturn;}\n");
  58.                 arrayRuner.Append("if (!isClass && size == " + elementOfField + ")\n{\n\tfor (int i = 0; i < count; i++) PS" +
  59.                     elementOfField + "(); \nreturn;}\n");
  60.             }
  61.  
  62.             arrayRuner.Append("throw new ArgumentException();\n}\n}\n");
  63.             return arrayRuner.ToString();
  64.         }
  65.  
  66.         public static void Call(StringBuilder callRunner, int i)
  67.         {
  68.             var elementOfField = Constants.FieldCounts.ElementAt(i);
  69.             callRunner.Append("if (isClass && size == " + elementOfField + ")\n{\n");
  70.             callRunner.Append("var o = new C" + elementOfField + "(); for (int i = 0; i < count; i++) PC" +
  71.                 elementOfField + "(o); \nreturn;\n}\n");
  72.             callRunner.Append("if (!isClass && size == " + elementOfField + ")\n{\n");
  73.             callRunner.Append("var o = new S" + elementOfField + "(); for (int i = 0; i < count; i++) PS" +
  74.                 elementOfField + "(o); \nreturn;\n}\n");
  75.         }
  76.  
  77.         public static string GenerateCallRunner()
  78.         {
  79.             var callRunner = new StringBuilder();
  80.             callRunner.Append("public class CallRunner : IRunner\n{\n");
  81.  
  82.             for (int i = 0; i < Constants.FieldCounts.Count; i++)
  83.             {
  84.                 var elementOfField = Constants.FieldCounts.ElementAt(i);
  85.                 callRunner.Append("void PC" + elementOfField + "(C" + elementOfField + " o) { }\n");
  86.                 callRunner.Append("void PS" + elementOfField + "(S" + elementOfField + " o) { }\n");
  87.             }
  88.  
  89.             callRunner.Append("public void Call(bool isClass, int size, int count)\n{\n");
  90.  
  91.             for (int i = 0; i < Constants.FieldCounts.Count; i++)
  92.             {
  93.                 Call(callRunner, i);
  94.             }
  95.  
  96.             callRunner.Append("throw new ArgumentException();\n}\n}\n");
  97.             return callRunner.ToString();
  98.         }
  99.  
  100.      }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement