ZenScript main repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package stdlib;
  2. import java.lang.reflect.Array;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.function.Consumer;
  8. import java.util.function.Function;
  9. import java.util.function.Predicate;
  10. import zsynthetic.FunctionUSizeTToBool;
  11. import zsynthetic.FunctionUSizeTToU;
  12. import zsynthetic.FunctionUSizeTToVoid;
  13. public final class Arrays {
  14. private Arrays() {}
  15. public static <T> T getFirst(Class<T> typeOfT, T[] self) {
  16. return self.length == 0 ? null : self[0];
  17. }
  18. public static <T> T getLast(Class<T> typeOfT, T[] self) {
  19. return self.length == 0 ? null : self[self.length - 1];
  20. }
  21. public static <T> void reverse(Class<T> typeOfT, T[] self) {
  22. int limitForI = self.length / 2;
  23. for (int i = 0; i < limitForI; i++) {
  24. T temp = self[i];
  25. self[i] = self[self.length - i - 1];
  26. self[self.length - i - 1] = temp;
  27. }
  28. }
  29. public static <U, T> U[] map(Class<T> typeOfT, T[] self, Class<U> typeOfU, Function<T, U> projection) {
  30. U[] temp1 = (U[])(Array.newInstance(typeOfU, self.length));
  31. for (int temp2 = 0; temp2 < temp1.length; temp2++)
  32. temp1[temp2] = projection.apply(self[temp2]);
  33. return temp1;
  34. }
  35. public static <U, T> U[] map(Class<T> typeOfT, T[] self, Class<U> typeOfU, FunctionUSizeTToU<T, U> projection) {
  36. U[] temp1 = (U[])(Array.newInstance(typeOfU, self.length));
  37. for (int temp2 = 0; temp2 < temp1.length; temp2++)
  38. temp1[temp2] = projection.invoke(temp2, self[temp2]);
  39. return temp1;
  40. }
  41. public static <T> T[] filter(Class<T> typeOfT, T[] self, Predicate<T> predicate) {
  42. List<T> values = new ArrayList<T>();
  43. for (T value : self)
  44. if (predicate.test(value))
  45. values.add(value);
  46. return values.toArray((T[])(Array.newInstance(typeOfT, values.size())));
  47. }
  48. public static <T> T[] filter(Class<T> typeOfT, T[] self, FunctionUSizeTToBool<T> predicate) {
  49. List<T> values = new ArrayList<T>();
  50. for (int i = 0; i < self.length; i++) {
  51. T value = self[i];
  52. if (predicate.invoke(i, value))
  53. values.add(value);
  54. }
  55. return values.toArray((T[])(Array.newInstance(typeOfT, values.size())));
  56. }
  57. public static <T> void each(Class<T> typeOfT, T[] self, Consumer<T> consumer) {
  58. for (T value : self)
  59. consumer.accept(value);
  60. }
  61. public static <T> void each(Class<T> typeOfT, T[] self, FunctionUSizeTToVoid<T> consumer) {
  62. for (int i = 0; i < self.length; i++) {
  63. T value = self[i];
  64. consumer.invoke(i, value);
  65. }
  66. }
  67. public static <T> boolean contains(Class<T> typeOfT, T[] self, Predicate<T> predicate) {
  68. for (T value : self)
  69. if (predicate.test(value))
  70. return true;
  71. return false;
  72. }
  73. public static <T> boolean contains(Class<T> typeOfT, T[] self, FunctionUSizeTToBool<T> predicate) {
  74. for (int i = 0; i < self.length; i++) {
  75. T value = self[i];
  76. if (predicate.invoke(i, value))
  77. return true;
  78. }
  79. return false;
  80. }
  81. public static <T> boolean all(Class<T> typeOfT, T[] self, Predicate<T> predicate) {
  82. for (T value : self)
  83. if (!predicate.test(value))
  84. return false;
  85. return true;
  86. }
  87. public static <T> boolean all(Class<T> typeOfT, T[] self, FunctionUSizeTToBool<T> predicate) {
  88. for (int i = 0; i < self.length; i++) {
  89. T value = self[i];
  90. if (!predicate.invoke(i, value))
  91. return false;
  92. }
  93. return true;
  94. }
  95. public static <T> T first(Class<T> typeOfT, T[] self, Predicate<T> predicate) {
  96. for (T value : self)
  97. if (predicate.test(value))
  98. return value;
  99. return null;
  100. }
  101. public static <T> T first(Class<T> typeOfT, T[] self, FunctionUSizeTToBool<T> predicate) {
  102. for (int i = 0; i < self.length; i++) {
  103. T value = self[i];
  104. if (predicate.invoke(i, value))
  105. return value;
  106. }
  107. return null;
  108. }
  109. public static <T> T last(Class<T> typeOfT, T[] self, Predicate<T> predicate) {
  110. int i = self.length;
  111. while (i > 0) {
  112. i--;
  113. if (predicate.test(self[i]))
  114. return self[i];
  115. }
  116. return null;
  117. }
  118. public static <T> T last(Class<T> typeOfT, T[] self, FunctionUSizeTToBool<T> predicate) {
  119. int i = self.length;
  120. while (i > 0) {
  121. i--;
  122. if (predicate.invoke(i, self[i]))
  123. return self[i];
  124. }
  125. return null;
  126. }
  127. public static <T> int count(Class<T> typeOfT, T[] self, Predicate<T> predicate) {
  128. int result = 0;
  129. for (T value : self)
  130. if (predicate.test(value))
  131. result++;
  132. return result;
  133. }
  134. public static <T> int count(Class<T> typeOfT, T[] self, FunctionUSizeTToBool<T> predicate) {
  135. int result = 0;
  136. for (int i = 0; i < self.length; i++) {
  137. T value = self[i];
  138. if (predicate.invoke(i, value))
  139. result++;
  140. }
  141. return result;
  142. }
  143. public static <K, T> Map<K, T> index(Class<T> typeOfT, T[] self, Class<K> typeOfK, Function<T, K> key) {
  144. Map<K, T> result = new HashMap<>();
  145. for (T value : self)
  146. result.put(key.apply(value), value);
  147. return result;
  148. }
  149. }