bekovski

ppbs_manager

Aug 8th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.23 KB | None | 0 0
  1. package pivotHelper;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import algoanim.animalscript.AnimalRectGenerator;
  9. import algoanim.animalscript.AnimalTextGenerator;
  10. import algoanim.primitives.ArrayMarker;
  11. import algoanim.primitives.IntArray;
  12. import algoanim.primitives.Rect;
  13. import algoanim.primitives.SourceCode;
  14. import algoanim.primitives.Text;
  15. import algoanim.primitives.generators.Language;
  16. import algoanim.properties.AnimationPropertiesKeys;
  17. import algoanim.properties.RectProperties;
  18. import algoanim.properties.TextProperties;
  19. import algoanim.util.Coordinates;
  20. import translator.TranslatableGUIElement;
  21. import translator.Translator;
  22.  
  23. public class Manager {
  24.    
  25.     public static Language lang;
  26.    
  27.     private static List<Object> clearList = new ArrayList<>();
  28.    
  29.     private static int upLeftX = Integer.MAX_VALUE;
  30.     private static int upLeftY = Integer.MAX_VALUE;
  31.    
  32.     private static Text pivotText;
  33.     private static Text resultText;
  34.     private static Text iterationText;
  35.    
  36.     private static final int offsetX = 10;
  37.    
  38.     private static Text m1Text;
  39.     private static final int m1YOffset = 10;
  40.    
  41.     private static Text m2Text;
  42.     private static final int m2YOffset = 25;
  43.    
  44.     private static Text i1Text;
  45.     private static final int i1YOffset = 40;
  46.    
  47.     private static Text i2Text;
  48.     private static final int i2YOffset = 55;
  49.    
  50.     private static Text i3Text;
  51.     private static final int i3YOffset = 70;
  52.    
  53.    
  54.     // highlight array cells
  55.     public static void highlightCell(IntArray array, int idx, Color highlightColor) {
  56.         array.setHighlightFillColor(idx, highlightColor, null, null);
  57.         array.highlightCell(idx, null, null);
  58.     }
  59.    
  60.     public static void hideArrayMarker(ArrayMarker marker) {
  61.         marker.hide();
  62.     }
  63.    
  64.    
  65.     public static void addToClearList(Object o) {
  66.         clearList.add(o);
  67.     }
  68.    
  69.     public static void clear() {
  70.         for(Object o : clearList) {
  71.             if(o instanceof Text) {
  72.                 ((Text)o).hide();
  73.             }
  74.             else if(o instanceof SourceCode) {
  75.                 ((SourceCode)o).hide();
  76.             }
  77.             else if(o instanceof IntArray) {
  78.                 ((IntArray)o).hide();
  79.             }
  80.             else if(o instanceof ArrayMarker) {
  81.                 ((ArrayMarker)o).hide();
  82.             }
  83.             else if(o instanceof Rect) {
  84.                 ((Rect)o).hide();
  85.             }
  86.         }
  87.        
  88.         clearList.clear();
  89.     }
  90.    
  91.    
  92.     // ====================== makeXXX ===========================
  93.    
  94.    
  95.     public static Text makeText(String text, int x, int y, TextProperties textProps) {
  96.         return new Text(new AnimalTextGenerator(lang), new Coordinates(x, y), text, "text", null, textProps);
  97.     }
  98.    
  99.     public static Rect makeBox(int upperLeftX, int upperLeftY, int lowerRightX, int lowerRightY, RectProperties rectProps) {
  100.         upLeftX = upperLeftX;
  101.         upLeftY = upperLeftY;
  102.         Rect box = new Rect(new AnimalRectGenerator(lang), new Coordinates(upperLeftX, upperLeftY), new Coordinates(lowerRightX, lowerRightY), "rect", null, rectProps);
  103.         addToClearList(box);
  104.         return box;
  105.     }
  106.    
  107.     public static void makeTitle() {
  108.         RectProperties titleBoxProps = new RectProperties();
  109.         titleBoxProps.set(AnimationPropertiesKeys.COLOR_PROPERTY, Color.BLACK);
  110.         titleBoxProps.set(AnimationPropertiesKeys.FILLED_PROPERTY, true);
  111.         titleBoxProps.set(AnimationPropertiesKeys.FILL_PROPERTY, Color.LIGHT_GRAY);
  112.         new Rect(new AnimalRectGenerator(lang), new Coordinates(20, 50), new Coordinates(300, 90), "titleBox", null, titleBoxProps);
  113.        
  114.         TextProperties titleProps = new TextProperties();
  115.         titleProps.set(AnimationPropertiesKeys.FONT_PROPERTY, new Font("SansSerif", Font.BOLD, 16));
  116.         new Text(new AnimalTextGenerator(lang), new Coordinates(40, 60), "Pivot Partitioning by Scanning", "title", null, titleProps);
  117.     }
  118.    
  119.     // pivot and iteration and result texts
  120.    
  121.     public static void makePivotText(int pivot, int x, int y, TextProperties pivotTextProps) {
  122.         pivotText = makeText("pivot = " + pivot, x, y, pivotTextProps);
  123.         addToClearList(pivotText);
  124.     }
  125.    
  126.     public static void makeIterationText(int iteration, int x, int y, TextProperties iterationTextProps) {
  127.         if(iterationText != null)
  128.             iterationText.hide();
  129.         iterationText = makeText("# iterations = " + iteration, x, y, iterationTextProps);
  130.         addToClearList(iterationText);
  131.     }
  132.    
  133.     public static void makeResultText(int[] result, int x, int y, TextProperties resultTextProps) {
  134.         resultText = makeText("result = [m1, m2] = [" + result[0] + ", " + result[1] + "]", x, y, resultTextProps);
  135.         addToClearList(resultText);
  136.     }
  137.    
  138.     // write values of indices into the box
  139.    
  140.     public static void makeM1Text(int m1, TextProperties textProps) {
  141.         if(m1Text != null)
  142.             m1Text.hide();
  143.        
  144.         m1Text = makeBoxText(m1Text, "m1 = " + m1, m1YOffset, textProps);
  145.         addToClearList(m1Text);
  146.     }
  147.    
  148.     public static void makeM2Text(int m2, TextProperties textProps) {
  149.         if(m2Text != null)
  150.             m2Text.hide();
  151.        
  152.         m2Text = makeBoxText(m2Text, "m2 = " + m2, m2YOffset, textProps);
  153.         addToClearList(m2Text);
  154.     }
  155.    
  156.     public static void makeI1Text(int i1, TextProperties textProps) {
  157.         if(i1Text != null)
  158.             i1Text.hide();
  159.        
  160.         i1Text = makeBoxText(i1Text, "i1 = " + i1, i1YOffset, textProps);
  161.         addToClearList(i1Text);
  162.     }
  163.    
  164.     public static void makeI2Text(int i2, TextProperties textProps) {
  165.         if(i2Text != null)
  166.             i2Text.hide();
  167.        
  168.         i2Text = makeBoxText(i2Text, "i2 = " + i2, i2YOffset, textProps);
  169.         addToClearList(i2Text);
  170.     }
  171.    
  172.     public static void makeI3Text(int i3, TextProperties textProps) {
  173.         if(i3Text != null)
  174.             i3Text.hide();
  175.        
  176.         i3Text = makeBoxText(i3Text, "i3 = " + i3, i3YOffset, textProps);
  177.         addToClearList(i3Text);
  178.     }
  179.    
  180.     private static Text makeBoxText(Text text, String txt, int offsetY, TextProperties textProps) {
  181.         if(text != null)
  182.             text.hide();
  183.        
  184.         int x = upLeftX + offsetX;
  185.         int y = upLeftY + offsetY;
  186.         return new Text(new AnimalTextGenerator(lang), new Coordinates(x, y), txt, "boxText", null, textProps);
  187.     }
  188.    
  189.    
  190.     // intro and outro texts
  191.    
  192.     public static void showIntroText2(TextProperties textProps, Translator translator) {
  193.         TranslatableGUIElement builder = translator.getGenerator();
  194.        
  195.         Text introText = makeText("Introduction", 50, 140, textProps);
  196.         introText.changeColor("", Color.BLUE, null, null);
  197.         introText.setFont(new Font("Monospaced", Font.BOLD, 20), null, null);
  198.         addToClearList(introText);
  199.        
  200.         String t1 = "Pivot Partitioning by Scanning (PPbS) rearranges the elements in an array A around a given pivot element in place,";
  201.         String t2 = "much like the partitioning procedure used in the Quicksort algorithm, e.g. the one shown in the famous CLRS book.";
  202.         String t3 = "There are some differences though: whereas the algorithm from CLRS partitions the array into two parts (elements <= pivot and elements > pivot), ";
  203.         String t4 = "given a pivot element which always is the last element in the array, PPbS partitions the array into three parts (<, ==, and > than the pivot)";
  204.         String t5 = "with a freely chosable pivot. PPbS, however, suffers from nested loops, so its time complexity is worse.";
  205.        
  206.         String t6 = "PPbS returns two pointers, m1 and m2, that satisfy the following conditions:";
  207.         String t7 = "\t 1. There are m1 many elements that have a value < pivot";
  208.         String t8 = "\t 2. There are (m2 - m1) many elements that have a value == pivot";
  209.         String t9 = "\t 3. There are (n - m2) many elements that have a value > pivot, where n == A.length.";
  210.        
  211.         addToClearList(makeText(builder.generateJLabel("in1").getText(), 50, 170, textProps));
  212.         addToClearList(makeText(builder.generateJLabel("in2").getText(), 50, 190, textProps));
  213.         addToClearList(makeText(builder.generateJLabel("in3").getText(), 50, 210, textProps));
  214.         addToClearList(makeText(builder.generateJLabel("in4").getText(), 50, 230, textProps));
  215.         addToClearList(makeText(builder.generateJLabel("in5").getText(), 50, 250, textProps));
  216.         addToClearList(makeText(builder.generateJLabel("in6").getText(), 50, 300, textProps));
  217.         addToClearList(makeText(builder.generateJLabel("in7").getText(), 50, 320, textProps));
  218.         addToClearList(makeText(builder.generateJLabel("in8").getText(), 50, 340, textProps));
  219.         addToClearList(makeText(builder.generateJLabel("in9").getText(), 50, 360, textProps));
  220.     }
  221.    
  222.     public static void showIntroText(TextProperties textProps) {
  223.         Text introText = makeText("Introduction", 50, 140, textProps);
  224.         introText.changeColor("", Color.BLUE, null, null);
  225.         introText.setFont(new Font("Monospaced", Font.BOLD, 20), null, null);
  226.         addToClearList(introText);
  227.        
  228.         String t1 = "Pivot Partitioning by Scanning (PPbS) rearranges the elements in an array A around a given pivot element in place,";
  229.         String t2 = "much like the partitioning procedure used in the Quicksort algorithm, e.g. the one shown in the famous CLRS book.";
  230.         String t3 = "There are some differences though: whereas the algorithm from CLRS partitions the array into two parts (elements <= pivot and elements > pivot), ";
  231.         String t4 = "given a pivot element which always is the last element in the array, PPbS partitions the array into three parts (<, ==, and > than the pivot)";
  232.         String t5 = "with a freely chosable pivot. PPbS, however, suffers from nested loops, so its time complexity is worse.";
  233.        
  234.         String t6 = "PPbS returns two pointers, m1 and m2, that satisfy the following conditions:";
  235.         String t7 = "\t 1. There are m1 many elements that have a value < pivot";
  236.         String t8 = "\t 2. There are (m2 - m1) many elements that have a value == pivot";
  237.         String t9 = "\t 3. There are (n - m2) many elements that have a value > pivot, where n == A.length.";
  238.        
  239.         addToClearList(makeText(t1, 50, 170, textProps));
  240.         addToClearList(makeText(t2, 50, 190, textProps));
  241.         addToClearList(makeText(t3, 50, 210, textProps));
  242.         addToClearList(makeText(t4, 50, 230, textProps));
  243.         addToClearList(makeText(t5, 50, 250, textProps));
  244.         addToClearList(makeText(t6, 50, 300, textProps));
  245.         addToClearList(makeText(t7, 50, 320, textProps));
  246.         addToClearList(makeText(t8, 50, 340, textProps));
  247.         addToClearList(makeText(t9, 50, 360, textProps));
  248.     }
  249.    
  250.     public static void showOutroText(TextProperties textProps) {
  251.         Text outroText = makeText("Final Words", 50, 140, textProps);
  252.         outroText.changeColor("", Color.BLUE, null, null);
  253.         outroText.setFont(new Font("Monospaced", Font.BOLD, 20), null, null);
  254.         addToClearList(outroText);
  255.        
  256.         String t1 = "For more information regarding PPbS (e.g. loop invariant, variant, etc.), visit";
  257.         String t2 = "    https://wiki.algo.informatik.tu-darmstadt.de/Pivot_partitioning_by_scanning";
  258.        
  259.         String t3 = "Nabla offers a problem generator for PPbS, which you could solve with this animation.";
  260.         String t4 = "    https://nabla.algo.informatik.tu-darmstadt.de/";
  261.        
  262.         String t5 = "Beware, though, that both sources mentioned above use a different indexing (starting from 1 instead of 0), so you'll have to adjust.";
  263.        
  264.         addToClearList(makeText(t1, 50, 170, textProps));
  265.         Text algoLink = makeText(t2, 50, 200, textProps);
  266.         Color c = (Color)algoLink.getProperties().get(AnimationPropertiesKeys.COLOR_PROPERTY);
  267.         algoLink.changeColor("", c == Color.BLUE ? Color.ORANGE : Color.BLUE, null, null);
  268.         addToClearList(algoLink);
  269.        
  270.         addToClearList(makeText(t3, 50, 250, textProps));
  271.         Text nablaLink = makeText(t4, 50, 280, textProps);
  272.         nablaLink.changeColor("", c == Color.BLUE ? Color.ORANGE : Color.BLUE, null, null);
  273.         addToClearList(nablaLink);
  274.        
  275.        
  276.         addToClearList(makeText(t5, 50, 350, textProps));
  277.     }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment