Advertisement
agmike

AGMHumpUtils

Sep 9th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.81 KB | None | 0 0
  1. //
  2. // Hump yard marker
  3.  
  4. include "agmhump.gs"
  5.  
  6.  
  7. final static class AGMHumpUtils
  8. {
  9.     public void Sort(int[] a);
  10.     public void Sort(int[] a, int[] data);
  11.     public void Sort(AGMHumpJunction[] a);
  12.     public void Sort(AGMHumpTrack[] a);
  13.     public void Sort(AGMHumpTask[] a, int count);
  14.    
  15.     public int BinaryIndexOf(int[] a, int num);
  16.     public int BinaryIndexOf(AGMHumpJunction[] a, int jnId);
  17.     public int BinaryIndexOf(AGMHumpTrack[] a, int trackNum);
  18.     public int BinaryIndexOf(AGMHumpTask[] a, int vehId, int count);
  19.    
  20.     public int IndexOf(Vehicle[] vehs, Vehicle veh);
  21.    
  22.     public int[] ToIntArray(Soup sp);
  23.     public Soup ToSoup(int[] array);
  24.    
  25.     public float[] ToFloatArray(Soup sp);
  26.     public Soup ToSoup(float[] array);
  27.    
  28.     public int Sign(int value);
  29.    
  30.     public float Log(float value);
  31.    
  32.     public float NormalRand(float mean, float deviation);
  33.    
  34.     public int WeightedRand(float[] weights);
  35.  
  36.    
  37.     // ****************************************************
  38.     //
  39.     //    I M P L E M E N T A T I O N
  40.     //
  41.     // ****************************************************
  42.  
  43.     public void Sort(int[] a)
  44.     {
  45.         int i, length = a.size();
  46.         for (i = 1; i < length; ++i) {
  47.             int j = i;
  48.             for (; j > 0 and a[j - 1] > a[j]; --j) {
  49.                 int tmp = a[j - 1];
  50.                 a[j - 1] = a[j];
  51.                 a[j] = tmp;
  52.             }
  53.         }
  54.     }
  55.  
  56.     public void Sort(int[] a, int[] data)
  57.     {
  58.         int i, length = a.size();
  59.         for (i = 1; i < length; ++i) {
  60.             int j = i;
  61.             for (; j > 0 and a[j - 1] > a[j]; --j) {
  62.                 int tmp = a[j - 1];
  63.                 a[j - 1] = a[j];
  64.                 a[j] = tmp;
  65.                 tmp = data[j - 1];
  66.                 data[j - 1] = data[j];
  67.                 data[j] = tmp;
  68.             }
  69.         }
  70.     }
  71.  
  72.     public void Sort(AGMHumpJunction[] a)
  73.     {
  74.         int i, length = a.size();
  75.         for (i = 1; i < length; ++i) {
  76.             int j = i;
  77.             for (; j > 0 and a[j - 1].JnId > a[j].JnId; --j) {
  78.                 AGMHumpJunction tmp = a[j - 1];
  79.                 a[j - 1] = a[j];
  80.                 a[j] = tmp;
  81.             }
  82.         }
  83.     }
  84.  
  85.     public void Sort(AGMHumpTrack[] a)
  86.     {
  87.         int i, length = a.size();
  88.         for (i = 1; i < length; ++i) {
  89.             int j = i;
  90.             for (; j > 0 and a[j - 1].Number > a[j].Number; --j) {
  91.                 AGMHumpTrack tmp = a[j - 1];
  92.                 a[j - 1] = a[j];
  93.                 a[j] = tmp;
  94.             }
  95.         }
  96.     }
  97.  
  98.     public void Sort(AGMHumpTask[] a, int count)
  99.     {
  100.         int i, length = count;
  101.         for (i = 1; i < length; ++i) {
  102.             int j = i;
  103.             for (; j > 0 and a[j - 1].VehId > a[j].VehId; --j) {
  104.                 AGMHumpTask tmp = a[j - 1];
  105.                 a[j - 1] = a[j];
  106.                 a[j] = tmp;
  107.             }
  108.         }
  109.     }
  110.    
  111.     public int BinaryIndexOf(int[] a, int num)
  112.     {
  113.         int l = 0;
  114.         int r = a.size() - 1;
  115.         while (l < r) {
  116.             int m = (l + r) / 2;
  117.             if (a[m] < num) l = m + 1;
  118.             else r = m;
  119.         }
  120.         if ((l == r) and (a[l] == num))
  121.             return l;
  122.         return -1;
  123.     }
  124.    
  125.     public int BinaryIndexOf(AGMHumpJunction[] a, int jnId)
  126.     {
  127.         int l = 0;
  128.         int r = a.size() - 1;
  129.         while (l < r) {
  130.             int m = (l + r) / 2;
  131.             if (a[m].JnId < jnId) l = m + 1;
  132.             else r = m;
  133.         }
  134.         if ((l == r) and (a[l].JnId == jnId))
  135.             return l;
  136.         return -1;
  137.     }
  138.    
  139.     public int BinaryIndexOf(AGMHumpTrack[] a, int trackNum)
  140.     {
  141.         int l = 0;
  142.         int r = a.size() - 1;
  143.         while (l < r) {
  144.             int m = (l + r) / 2;
  145.             if (a[m].Number < trackNum) l = m + 1;
  146.             else r = m;
  147.         }
  148.         if ((l == r) and (a[l].Number == trackNum))
  149.             return l;
  150.         return -1;
  151.     }
  152.    
  153.     public int BinaryIndexOf(AGMHumpTask[] a, int vehId, int count)
  154.     {
  155.         int l = 0;
  156.         int r = count - 1;
  157.         while (l < r) {
  158.             int m = (l + r) / 2;
  159.             if (a[m].VehId < vehId) l = m + 1;
  160.             else r = m;
  161.         }
  162.         if ((l == r) and (a[l].VehId == vehId))
  163.             return l;
  164.         return -1;
  165.     }
  166.    
  167.     public int IndexOf(Vehicle[] vehs, Vehicle veh)
  168.     {
  169.         int i, count =vehs.size();
  170.         for (i = 0; i < count; ++i)
  171.             if (vehs[i] == veh)
  172.                 return i;
  173.         return -1;
  174.     }
  175.    
  176.     public int[] ToIntArray(Soup sp)
  177.     {
  178.         int[] ret;
  179.         if (!sp)
  180.             return ret;
  181.         int i, count = sp.CountTags();
  182.         ret = new int[count];
  183.         for (i = 0; i < count; ++i)
  184.             ret[i] = sp.GetNamedTagAsInt(i);
  185.         return ret;
  186.     }
  187.    
  188.     public Soup ToSoup(int[] array)
  189.     {
  190.         Soup ret = Constructors.NewSoup();
  191.         if (!array)
  192.             return ret;
  193.         int i, count = array.size();
  194.         for (i = 0; i < count; ++i)
  195.             ret.SetNamedTag(i, array[i]);
  196.         return ret;
  197.     }
  198.    
  199.     public float[] ToFloatArray(Soup sp)
  200.     {
  201.         float[] ret;
  202.         if (!sp)
  203.             return ret;
  204.         int i, count = sp.CountTags();
  205.         ret = new float[count];
  206.         for (i = 0; i < count; ++i)
  207.             ret[i] = sp.GetNamedTagAsFloat(i);
  208.         return ret;
  209.     }
  210.    
  211.     public Soup ToSoup(float[] array)
  212.     {
  213.         Soup ret = Constructors.NewSoup();
  214.         if (!array)
  215.             return ret;
  216.         int i, count = array.size();
  217.         for (i = 0; i < count; ++i)
  218.             ret.SetNamedTag(i, array[i]);
  219.         return ret;
  220.     }
  221.    
  222.     public int Sign(int value)
  223.     {
  224.         if (value > 0) return 1;
  225.         else if (value < 0) return -1;
  226.         return 0;
  227.     }
  228.    
  229.     define float LogError = 0.00000005f;
  230.    
  231.     public float Log(float value)
  232.     {
  233.         if (value <= 0.0f) {
  234.             Interface.Exception("logarithm of negative or zero");
  235.             return 0.0f;
  236.         }
  237.         float nom = (value - 1.0f) / (value + 1.0f);
  238.         float dnm = 1.0f;
  239.         float p = nom * nom;
  240.         float a = nom;
  241.         float log = a;
  242.         while (true) {
  243.             nom = nom * p;
  244.             dnm = dnm + 2.0f;
  245.             float an = nom / dnm;
  246.             log = log + an;
  247.             float delta = an - a;
  248.             if (delta < 0) delta = -delta;
  249.             if (delta <= LogError)
  250.                 break;
  251.             a = an;
  252.         }
  253.         return 2.0f * log;
  254.     }
  255.    
  256.     float nrSpare;
  257.     bool nrSpareReady = false;
  258.      
  259.     public float NormalRand(float mean, float deviation)
  260.     {
  261.         if (nrSpareReady) {
  262.             nrSpareReady = false;
  263.             return nrSpare * deviation + mean;
  264.         }
  265.         float u, v, s;
  266.         while (true) {
  267.             u = Math.Rand(0.0f, 1.0f) * 2.0f - 1.0f;
  268.             v = Math.Rand(0.0f, 1.0f) * 2.0f - 1.0f;
  269.             s = u * u + v * v;
  270.             if (0.0f < s and s < 1.0f)
  271.                 break;
  272.         }
  273.         float mul = Math.Sqrt(-2.0f * AGMHumpUtils.Log(s) / s);
  274.         nrSpare = v * mul;
  275.         nrSpareReady = true;
  276.         return mean + deviation * u * mul;
  277.     }
  278.    
  279.     public int WeightedRand(float[] weights)
  280.     {
  281.         int i, count = weights.size();
  282.         float sum = 0.0f;
  283.         for (i = 0; i < count; ++i)
  284.             sum = sum + weights[i];
  285.         if (sum <= 0.0f)
  286.             return Math.Rand(0, count);
  287.         float value = Math.Rand(0.0f, sum);
  288.         for (i = 0; i < count - 1; ++i) {
  289.             if (weights[i] > 0.0f) {
  290.                 value = value - weights[i];
  291.                 if (value <= 0.0f)
  292.                     break;
  293.             }
  294.         }
  295.         return i;
  296.     }
  297. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement