Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Hump yard marker
- include "agmhump.gs"
- final static class AGMHumpUtils
- {
- public void Sort(int[] a);
- public void Sort(int[] a, int[] data);
- public void Sort(AGMHumpJunction[] a);
- public void Sort(AGMHumpTrack[] a);
- public void Sort(AGMHumpTask[] a, int count);
- public int BinaryIndexOf(int[] a, int num);
- public int BinaryIndexOf(AGMHumpJunction[] a, int jnId);
- public int BinaryIndexOf(AGMHumpTrack[] a, int trackNum);
- public int BinaryIndexOf(AGMHumpTask[] a, int vehId, int count);
- public int IndexOf(Vehicle[] vehs, Vehicle veh);
- public int[] ToIntArray(Soup sp);
- public Soup ToSoup(int[] array);
- public float[] ToFloatArray(Soup sp);
- public Soup ToSoup(float[] array);
- public int Sign(int value);
- public float Log(float value);
- public float NormalRand(float mean, float deviation);
- public int WeightedRand(float[] weights);
- // ****************************************************
- //
- // I M P L E M E N T A T I O N
- //
- // ****************************************************
- public void Sort(int[] a)
- {
- int i, length = a.size();
- for (i = 1; i < length; ++i) {
- int j = i;
- for (; j > 0 and a[j - 1] > a[j]; --j) {
- int tmp = a[j - 1];
- a[j - 1] = a[j];
- a[j] = tmp;
- }
- }
- }
- public void Sort(int[] a, int[] data)
- {
- int i, length = a.size();
- for (i = 1; i < length; ++i) {
- int j = i;
- for (; j > 0 and a[j - 1] > a[j]; --j) {
- int tmp = a[j - 1];
- a[j - 1] = a[j];
- a[j] = tmp;
- tmp = data[j - 1];
- data[j - 1] = data[j];
- data[j] = tmp;
- }
- }
- }
- public void Sort(AGMHumpJunction[] a)
- {
- int i, length = a.size();
- for (i = 1; i < length; ++i) {
- int j = i;
- for (; j > 0 and a[j - 1].JnId > a[j].JnId; --j) {
- AGMHumpJunction tmp = a[j - 1];
- a[j - 1] = a[j];
- a[j] = tmp;
- }
- }
- }
- public void Sort(AGMHumpTrack[] a)
- {
- int i, length = a.size();
- for (i = 1; i < length; ++i) {
- int j = i;
- for (; j > 0 and a[j - 1].Number > a[j].Number; --j) {
- AGMHumpTrack tmp = a[j - 1];
- a[j - 1] = a[j];
- a[j] = tmp;
- }
- }
- }
- public void Sort(AGMHumpTask[] a, int count)
- {
- int i, length = count;
- for (i = 1; i < length; ++i) {
- int j = i;
- for (; j > 0 and a[j - 1].VehId > a[j].VehId; --j) {
- AGMHumpTask tmp = a[j - 1];
- a[j - 1] = a[j];
- a[j] = tmp;
- }
- }
- }
- public int BinaryIndexOf(int[] a, int num)
- {
- int l = 0;
- int r = a.size() - 1;
- while (l < r) {
- int m = (l + r) / 2;
- if (a[m] < num) l = m + 1;
- else r = m;
- }
- if ((l == r) and (a[l] == num))
- return l;
- return -1;
- }
- public int BinaryIndexOf(AGMHumpJunction[] a, int jnId)
- {
- int l = 0;
- int r = a.size() - 1;
- while (l < r) {
- int m = (l + r) / 2;
- if (a[m].JnId < jnId) l = m + 1;
- else r = m;
- }
- if ((l == r) and (a[l].JnId == jnId))
- return l;
- return -1;
- }
- public int BinaryIndexOf(AGMHumpTrack[] a, int trackNum)
- {
- int l = 0;
- int r = a.size() - 1;
- while (l < r) {
- int m = (l + r) / 2;
- if (a[m].Number < trackNum) l = m + 1;
- else r = m;
- }
- if ((l == r) and (a[l].Number == trackNum))
- return l;
- return -1;
- }
- public int BinaryIndexOf(AGMHumpTask[] a, int vehId, int count)
- {
- int l = 0;
- int r = count - 1;
- while (l < r) {
- int m = (l + r) / 2;
- if (a[m].VehId < vehId) l = m + 1;
- else r = m;
- }
- if ((l == r) and (a[l].VehId == vehId))
- return l;
- return -1;
- }
- public int IndexOf(Vehicle[] vehs, Vehicle veh)
- {
- int i, count =vehs.size();
- for (i = 0; i < count; ++i)
- if (vehs[i] == veh)
- return i;
- return -1;
- }
- public int[] ToIntArray(Soup sp)
- {
- int[] ret;
- if (!sp)
- return ret;
- int i, count = sp.CountTags();
- ret = new int[count];
- for (i = 0; i < count; ++i)
- ret[i] = sp.GetNamedTagAsInt(i);
- return ret;
- }
- public Soup ToSoup(int[] array)
- {
- Soup ret = Constructors.NewSoup();
- if (!array)
- return ret;
- int i, count = array.size();
- for (i = 0; i < count; ++i)
- ret.SetNamedTag(i, array[i]);
- return ret;
- }
- public float[] ToFloatArray(Soup sp)
- {
- float[] ret;
- if (!sp)
- return ret;
- int i, count = sp.CountTags();
- ret = new float[count];
- for (i = 0; i < count; ++i)
- ret[i] = sp.GetNamedTagAsFloat(i);
- return ret;
- }
- public Soup ToSoup(float[] array)
- {
- Soup ret = Constructors.NewSoup();
- if (!array)
- return ret;
- int i, count = array.size();
- for (i = 0; i < count; ++i)
- ret.SetNamedTag(i, array[i]);
- return ret;
- }
- public int Sign(int value)
- {
- if (value > 0) return 1;
- else if (value < 0) return -1;
- return 0;
- }
- define float LogError = 0.00000005f;
- public float Log(float value)
- {
- if (value <= 0.0f) {
- Interface.Exception("logarithm of negative or zero");
- return 0.0f;
- }
- float nom = (value - 1.0f) / (value + 1.0f);
- float dnm = 1.0f;
- float p = nom * nom;
- float a = nom;
- float log = a;
- while (true) {
- nom = nom * p;
- dnm = dnm + 2.0f;
- float an = nom / dnm;
- log = log + an;
- float delta = an - a;
- if (delta < 0) delta = -delta;
- if (delta <= LogError)
- break;
- a = an;
- }
- return 2.0f * log;
- }
- float nrSpare;
- bool nrSpareReady = false;
- public float NormalRand(float mean, float deviation)
- {
- if (nrSpareReady) {
- nrSpareReady = false;
- return nrSpare * deviation + mean;
- }
- float u, v, s;
- while (true) {
- u = Math.Rand(0.0f, 1.0f) * 2.0f - 1.0f;
- v = Math.Rand(0.0f, 1.0f) * 2.0f - 1.0f;
- s = u * u + v * v;
- if (0.0f < s and s < 1.0f)
- break;
- }
- float mul = Math.Sqrt(-2.0f * AGMHumpUtils.Log(s) / s);
- nrSpare = v * mul;
- nrSpareReady = true;
- return mean + deviation * u * mul;
- }
- public int WeightedRand(float[] weights)
- {
- int i, count = weights.size();
- float sum = 0.0f;
- for (i = 0; i < count; ++i)
- sum = sum + weights[i];
- if (sum <= 0.0f)
- return Math.Rand(0, count);
- float value = Math.Rand(0.0f, sum);
- for (i = 0; i < count - 1; ++i) {
- if (weights[i] > 0.0f) {
- value = value - weights[i];
- if (value <= 0.0f)
- break;
- }
- }
- return i;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement