Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: theagentd (posted 2014-04-30 01:19:53, viewed 134 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263  
package drone.util.sort;

import java.util.Comparator;

import net.mokyu.threading.GameExecutor;
import net.mokyu.threading.SplitTask;
import net.mokyu.threading.TaskTree;
import net.mokyu.threading.TaskTreeBuilder;

public class ParallelInsertionSort<T> {
   
   private int numChunks;
   private int numOverlaps;
   
   private TaskTree taskTree;

   private Chunk<T>[] chunks;
   private Overlap[] overlaps;
   
   private T[] array;
   private int sortStart, sortEnd;
   private Comparator<T> comparator;
   
   @SuppressWarnings("unchecked")
   public ParallelInsertionSort(int numChunks) {
      
      if(numChunks < 2){
         throw new IllegalArgumentException("Minimum number of chunks is 2");
      }
      
      this.numChunks = numChunks;
      this.numOverlaps = numChunks - 1; //Potential number of overlaps is numChunks minus 1.
      
      //Initialize numChunks chunks.
      chunks = new Chunk[numChunks];
      for(int i = 0; i < numChunks; i++){
         chunks[i] = new Chunk<T>();
      }
      
      
      overlaps = new Overlap[numOverlaps];
      for(int i = 0; i < numOverlaps; i++){
         overlaps[i] = new Overlap();
      }
      
      buildTaskTree();
   }
   
   private void buildTaskTree() {
      TaskTreeBuilder builder = new TaskTreeBuilder();
      
      //Create tasks.
      SplitTask sortChunksTask = new SortChunksTask(numChunks);
      SplitTask sortOverlapsTask = new SortOverlapsTask(numOverlaps);
      
      //Set up task dependencies
      sortOverlapsTask.addRequiredTask(sortChunksTask); //Sort overlaps AFTER sorting chunks.
      
      
      //Add to builder and build the task tree.
      builder.addTask(sortChunksTask);
      builder.addTask(sortOverlapsTask);
      taskTree = builder.build();
   }
   
   

   public void sort(T[] array, int start, int end, Comparator<T> comparator, GameExecutor executor){
      
      //Store important variables
      this.array = array;
      this.sortStart = start;
      this.sortEnd = end;
      this.comparator = comparator;
      
      //Initialize chunk objects with the bounds of each chunk.
      int length = end - start;
      for(int i = 0; i < numChunks; i++){
         Chunk<T> c = chunks[i];
         c.start = start + (length/numChunks) * i;
         c.end = start + (length/numChunks) * (i+1);
      }
      chunks[numChunks - 1].end = end; //Ensure we don't miss the last few elements due to rounding
      
      
      
      /*Run the precomputed task tree for the sorting algorithm.
      This is the equivalent of a multithreaded version of the following code:
      
         for(int i = 0; i < numChunks; i++){
            sortChunk(i);
         }
         updateOverlaps();
         for(int i = 0; i < numOverlaps; i++){
            sortOverlaps(i);
         }
      
      */
      executor.run(taskTree);
      
      
      //DEBUGGING: Make sure the array is properly sorted
      /*for(int i = sortStart + 1; i < sortEnd; i++){
         if(comparator.compare(array[i-1], array[i]) > 0){
            System.err.println("error at index " + i + ": " + array[i-1] + " vs " + array[i]);
            break;
         }
      }*/
   }
   
   private void sortChunk(int chunkID) {
      Chunk<T> c = chunks[chunkID];
      InsertionSort.sort(array, c.start, c.end, comparator);
      c.first = array[c.start];
      c.last = array[c.end-1];
   }
   
   private void updateOverlaps(){
      //int totalOverlappingElements = 0; //Debugging value
      
      int overlapCount = 0;
      
      for(int i = 0; i < numChunks; ){
         
         Chunk<T> current = chunks[i];
         T highest = current.last; //Used to detect overlapping chunks
         
         int overlapIndex = i;
         
         for(int j = i+1; j < numChunks; j++){
            Chunk<T> c = chunks[j];
            if(comparator.compare(highest, c.first) > 0){
               
               //Update the highest value to include all previous chunks
               //and increase overlapIndex to j.
               for(; overlapIndex < j; overlapIndex++){
                  Chunk<T> overlapChunk = chunks[overlapIndex];
                  if(comparator.compare(highest, overlapChunk.last) < 0){
                     highest = overlapChunk.last;
                  }
               }
               
            }
         }
         
         if(overlapIndex == i){
            //No overlap occured. Skip this chunk.
            i++;
            continue;
         }
         
         //Initialize a Overlap object for this overlap and increase the overlap count
         Overlap overlap = overlaps[overlapCount++];
         overlap.enabled = true;
         overlap.firstChunkID = i;
         overlap.lastChunkID = overlapIndex;
         
         //Skip all chunks affected by this overlap
         i = overlapIndex;
      }
      
      //Disable all unused Overlap objects:
      for(int i = overlapCount; i < numOverlaps; i++){
         overlaps[i].enabled = false;
      }
   }

   private void sortOverlap(int overlapID) {

      Overlap overlap = overlaps[overlapID];
      
      //Quickly return if this overlap is disabled.
      if(!overlap.enabled){
         return;
      }
      
      //Extract some data from the Overlap object
      int firstChunkID = overlap.firstChunkID;
      int lastChunkID = overlap.lastChunkID;
      Chunk<T> firstChunk = chunks[firstChunkID];
      Chunk<T> lastChunk = chunks[lastChunkID];
      
      

      //Compute value of upper and lower bounds for the overlap.
      T highest = firstChunk.last;
      T lowest = firstChunk.last;
      for(int j = firstChunkID+1; j <= lastChunkID; j++){
         Chunk<T> c = chunks[j];
         if(j < lastChunkID && comparator.compare(highest, c.last) < 0){
            highest = c.last;
         }
         if(comparator.compare(lowest, c.first) > 0){
            lowest = c.first;
         }
      }
      
      
      //Find array indices of upper and lower bounds.
      int start = firstChunk.end - 1;
      int end = lastChunk.start;
      
      //TODO: replace with binary search in firstChunk instead of linear search.
      while(start > sortStart && comparator.compare(array[start], lowest) > 0){
         start--;
      }
      //TODO: replace with binary search in lastChunk instead of linear search.
      while(end < sortEnd && comparator.compare(array[end], highest) < 0){
         end++;
      }
      
      //And finally, sort the overlap. Note that there is no need to sort
      //the first few elements that are in the first chunk, since these
      //are already sorted. The last argument is a skip argument which
      //allows us to simply skip sorting those elements but still allow
      //the insertion sort to shuffle in elements into that part of the
      //array.
      InsertionSort.sort(array, start, end, comparator, firstChunk.end - 1 - start);
   }
   
   private static class Chunk<T>{
      private int start, end;
      private T first, last;
   }
   
   private static class Overlap{
      private boolean enabled;
      private int firstChunkID, lastChunkID;
   }
   
   private class SortChunksTask extends SplitTask{

      public SortChunksTask(int numChunks) {
         super(0, 0, numChunks);
      }

      @Override
      protected void runSubtask(int subtask) {
         sortChunk(subtask);
      }

      @Override
      public void finish() {
         updateOverlaps();
      }
   }
   
   private class SortOverlapsTask extends SplitTask{

      public SortOverlapsTask(int numOverlaps) {
         super(1, 0, numOverlaps);
      }

      @Override
      protected void runSubtask(int subtask) {
         sortOverlap(subtask);
      }

      @Override
      public void finish() {
      }
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!