summaryrefslogtreecommitdiff
path: root/examples/scala-js/javalib/src/main/scala/java/util/Arrays.scala
blob: ed9afd1612187e2e724c5f488dce166c978dfda2 (plain) (blame)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package java.util

import scala.annotation.tailrec

object Arrays {
  def sort[T <: Object](array: Array[Object], comparator: Comparator[T]): Unit = {
    scala.util.Sorting.stableSort[Object](array,
        (a: Object, b: Object) =>
          comparator.compare(a.asInstanceOf[T], b.asInstanceOf[T]) < 0)
  }

  def fill(a: Array[Boolean], value: Boolean): Unit =
    fillImpl(a, value)

  def fill(a: Array[Boolean], fromIndex: Int, toIndex: Int, value: Boolean): Unit =
    fillImpl(a, fromIndex, toIndex, value)

  def fill(a: Array[Byte], value: Byte): Unit =
    fillImpl(a, value)

  def fill(a: Array[Byte], fromIndex: Int, toIndex: Int, value: Byte): Unit =
    fillImpl(a, fromIndex, toIndex, value)

  def fill(a: Array[Char], value: Char): Unit =
    fillImpl(a, value)

  def fill(a: Array[Char], fromIndex: Int, toIndex: Int, value: Char): Unit =
    fillImpl(a, fromIndex, toIndex, value)

  def fill(a: Array[Short], value: Short): Unit =
    fillImpl(a, value)

  def fill(a: Array[Short], fromIndex: Int, toIndex: Int, value: Short): Unit =
    fillImpl(a, fromIndex, toIndex, value)

  def fill(a: Array[Int], value: Int): Unit =
    fillImpl(a, value)

  def fill(a: Array[Int], fromIndex: Int, toIndex: Int, value: Int): Unit =
    fillImpl(a, fromIndex, toIndex, value)

  def fill(a: Array[Long], value: Long): Unit =
    fillImpl(a, value)

  def fill(a: Array[Long], fromIndex: Int, toIndex: Int, value: Long): Unit =
    fillImpl(a, fromIndex, toIndex, value)

  def fill(a: Array[Float], value: Float): Unit =
    fillImpl(a, value)

  def fill(a: Array[Float], fromIndex: Int, toIndex: Int, value: Float): Unit =
    fillImpl(a, fromIndex, toIndex, value)

  def fill(a: Array[Double], value: Double): Unit =
    fillImpl(a, value)

  def fill(a: Array[Double], fromIndex: Int, toIndex: Int, value: Double): Unit =
    fillImpl(a, fromIndex, toIndex, value)

  private def fillImpl[@specialized T](a: Array[T], value: T): Unit = {
    var i = 0
    while (i != a.length) {
      a(i) = value
      i += 1
    }
  }

  private def fillImpl[@specialized T](a: Array[T],
      fromIndex: Int, toIndex: Int, value: T): Unit = {
    if (fromIndex > toIndex)
      throw new IllegalArgumentException
    if (fromIndex < 0 || toIndex > a.length)
      throw new ArrayIndexOutOfBoundsException

    var i = fromIndex
    while (i != toIndex) {
      a(i) = value
      i += 1
    }
  }

  def fill(a: Array[AnyRef], value: AnyRef): Unit = {
    var i = 0
    while (i < a.length) {
      a(i) = value
      i += 1
    }
  }

  def fill(a: Array[AnyRef],
      fromIndex: Int, toIndex: Int, value: AnyRef): Unit = {
    if (fromIndex > toIndex)
      throw new IllegalArgumentException
    if (fromIndex < 0 || toIndex > a.length)
      throw new ArrayIndexOutOfBoundsException

    var i = fromIndex
    while (i < toIndex) {
      a(i) = value
      i += 1
    }
  }

  @inline private def checkIndexForBinarySearch(
      length: Int, start: Int, end: Int): Unit = {
    if (start > end)
      throw new IllegalArgumentException("fromIndex(" + start + ") > toIndex(" + end + ")")
    if (start < 0)
      throw new ArrayIndexOutOfBoundsException("Array index out of range: " + start)
    if (end > length)
      throw new ArrayIndexOutOfBoundsException("Array index out of range: " + end)
  }

  def binarySearch(a: Array[Char], key: Char): Int =
    binarySearchImpl[Char](a, 0, a.length, key, _ < _)

  def binarySearch(a: Array[Char],
      startIndex: Int, endIndex: Int, key: Char): Int = {
    checkIndexForBinarySearch(a.length, startIndex, endIndex)
    binarySearchImpl[Char](a, startIndex, endIndex, key, _ < _)
  }

  def binarySearch(a: Array[Short], key: Short): Int =
    binarySearchImpl[Short](a, 0, a.length, key, _ < _)

  def binarySearch(a: Array[Short],
      startIndex: Int, endIndex: Int, key: Short): Int = {
    checkIndexForBinarySearch(a.length, startIndex, endIndex)
    binarySearchImpl[Short](a, startIndex, endIndex, key, _ < _)
  }

  def binarySearch(a: Array[Int], key: Int): Int =
    binarySearchImpl[Int](a, 0, a.length, key, _ < _)

  def binarySearch(a: Array[Int],
      startIndex: Int, endIndex: Int, key: Int): Int = {
    checkIndexForBinarySearch(a.length, startIndex, endIndex)
    binarySearchImpl[Int](a, startIndex, endIndex, key, _ < _)
  }

  def binarySearch(a: Array[Long], key: Long): Int =
    binarySearchImpl[Long](a, 0, a.length, key, _ < _)

  def binarySearch(a: Array[Long],
      startIndex: Int, endIndex: Int, key: Long): Int = {
    checkIndexForBinarySearch(a.length, startIndex, endIndex)
    binarySearchImpl[Long](a, startIndex, endIndex, key, _ < _)
  }

  def binarySearch(a: Array[Float], key: Float): Int =
    binarySearchImpl[Float](a, 0, a.length, key, _ < _)

  def binarySearch(a: Array[Float],
      startIndex: Int, endIndex: Int, key: Float): Int = {
    checkIndexForBinarySearch(a.length, startIndex, endIndex)
    binarySearchImpl[Float](a, startIndex, endIndex, key, _ < _)
  }

  def binarySearch(a: Array[Double], key: Double): Int =
    binarySearchImpl[Double](a, 0, a.length, key, _ < _)

  def binarySearch(a: Array[Double],
      startIndex: Int, endIndex: Int, key: Double): Int = {
    checkIndexForBinarySearch(a.length, startIndex, endIndex)
    binarySearchImpl[Double](a, startIndex, endIndex, key, _ < _)
  }

  @inline
  @tailrec
  private def binarySearchImpl[@specialized T](a: Array[T],
      startIndex: Int, endIndex: Int, key: T, lt: (T, T) => Boolean): Int = {
    if (startIndex == endIndex) {
      // Not found
      -startIndex - 1
    } else {
      // Indices are unsigned 31-bit integer, so this does not overflow
      val mid = (startIndex + endIndex) >>> 1
      val elem = a(mid)
      if (lt(key, elem)) {
        binarySearchImpl(a, startIndex, mid, key, lt)
      } else if (key == elem) {
        // Found
        mid
      } else {
        binarySearchImpl(a, mid + 1, endIndex, key, lt)
      }
    }
  }

  def binarySearch(a: Array[AnyRef], key: AnyRef): Int =
    binarySearchImplRef(a, 0, a.length, key)

  def binarySearch(a: Array[AnyRef],
      startIndex: Int, endIndex: Int, key: AnyRef): Int = {
    checkIndexForBinarySearch(a.length, startIndex, endIndex)
    binarySearchImplRef(a, startIndex, endIndex, key)
  }

  @inline
  @tailrec
  def binarySearchImplRef(a: Array[AnyRef],
      startIndex: Int, endIndex: Int, key: AnyRef): Int = {
    if (startIndex == endIndex) {
      // Not found
      -startIndex - 1
    } else {
      // Indices are unsigned 31-bit integer, so this does not overflow
      val mid = (startIndex + endIndex) >>> 1
      val cmp = key.asInstanceOf[Comparable[AnyRef]].compareTo(a(mid))
      if (cmp < 0) {
        binarySearchImplRef(a, startIndex, mid, key)
      } else if (cmp == 0) {
        // Found
        mid
      } else {
        binarySearchImplRef(a, mid + 1, endIndex, key)
      }
    }
  }

  def copyOf(original: Array[Boolean], newLength: Int): Array[Boolean] =
    copyOfImpl(original, newLength, new Array(_))

  def copyOfRange(original: Array[Boolean], start: Int, end: Int): Array[Boolean] =
    copyOfRangeImpl(original, start, end, new Array(_))

  def copyOf(original: Array[Char], newLength: Int): Array[Char] =
    copyOfImpl(original, newLength, new Array(_))

  def copyOfRange(original: Array[Char], start: Int, end: Int): Array[Char] =
    copyOfRangeImpl(original, start, end, new Array(_))

  def copyOf(original: Array[Byte], newLength: Int): Array[Byte] =
    copyOfImpl(original, newLength, new Array(_))

  def copyOfRange(original: Array[Byte], start: Int, end: Int): Array[Byte] =
    copyOfRangeImpl(original, start, end, new Array(_))

  def copyOf(original: Array[Short], newLength: Int): Array[Short] =
    copyOfImpl(original, newLength, new Array(_))

  def copyOfRange(original: Array[Short], start: Int, end: Int): Array[Short] =
    copyOfRangeImpl(original, start, end, new Array(_))

  def copyOf(original: Array[Int], newLength: Int): Array[Int] =
    copyOfImpl(original, newLength, new Array(_))

  def copyOfRange(original: Array[Int], start: Int, end: Int): Array[Int] =
    copyOfRangeImpl(original, start, end, new Array(_))

  def copyOf(original: Array[Long], newLength: Int): Array[Long] =
    copyOfImpl(original, newLength, new Array(_))

  def copyOfRange(original: Array[Long], start: Int, end: Int): Array[Long] =
    copyOfRangeImpl(original, start, end, new Array(_))

  def copyOf(original: Array[Float], newLength: Int): Array[Float] =
    copyOfImpl(original, newLength, new Array(_))

  def copyOfRange(original: Array[Float], start: Int, end: Int): Array[Float] =
    copyOfRangeImpl(original, start, end, new Array(_))

  def copyOf(original: Array[Double], newLength: Int): Array[Double] =
    copyOfImpl(original, newLength, new Array(_))

  def copyOfRange(original: Array[Double], start: Int, end: Int): Array[Double] =
    copyOfRangeImpl(original, start, end, new Array(_))

  @inline private def copyOfImpl[@specialized T](original: Array[T],
      newLength: Int, newArray: Int => Array[T]): Array[T] = {
    checkArrayLength(newLength)
    val copyLength = Math.min(newLength, original.length)
    val ret = newArray(newLength)
    System.arraycopy(original, 0, ret, 0, copyLength)
    ret
  }

  @inline private def copyOfRangeImpl[@specialized T](original: Array[T],
      start: Int, end: Int, newArray: Int => Array[T]): Array[T] = {
    checkIndicesForCopyOfRange(original.length, start, end)
    val retLength = end - start
    val copyLength = Math.min(retLength, original.length - start)
    val ret = newArray(retLength)
    System.arraycopy(original, start, ret, 0, copyLength)
    ret
  }

  def copyOf(original: Array[AnyRef], newLength: Int): Array[AnyRef] = {
    checkArrayLength(newLength)
    val copyLength = Math.min(newLength, original.length)
    val ret = java.lang.reflect.Array.newInstance(
        original.getClass.getComponentType, newLength).asInstanceOf[Array[AnyRef]]
    System.arraycopy(original, 0, ret, 0, copyLength)
    ret
  }

  def copyOfRange(original: Array[AnyRef], start: Int, end: Int): Array[AnyRef] = {
    checkIndicesForCopyOfRange(original.length, start, end)
    val retLength = end - start
    val copyLength = Math.min(retLength, original.length - start)
    val ret = java.lang.reflect.Array.newInstance(
        original.getClass.getComponentType, retLength).asInstanceOf[Array[AnyRef]]
    System.arraycopy(original, start, ret, 0, copyLength)
    ret
  }

  @inline private def checkArrayLength(len: Int): Unit = {
    if (len < 0)
      throw new NegativeArraySizeException
  }

  @inline private def checkIndicesForCopyOfRange(
      len: Int, start: Int, end: Int): Unit = {
    if (start > end)
      throw new IllegalArgumentException(start + " > " + end)
    if (start < 0 || start > len)
      throw new ArrayIndexOutOfBoundsException
  }

  def hashCode(a: Array[Boolean]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + new java.lang.Boolean(x).hashCode)
  }

  def hashCode(a: Array[Char]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + new java.lang.Character(x).hashCode)
  }

  def hashCode(a: Array[Byte]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + new java.lang.Byte(x).hashCode)
  }

  def hashCode(a: Array[Short]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + new java.lang.Short(x).hashCode)
  }

  def hashCode(a: Array[Int]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + new java.lang.Integer(x).hashCode)
  }

  def hashCode(a: Array[Long]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + new java.lang.Long(x).hashCode)
  }

  def hashCode(a: Array[Float]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + new java.lang.Float(x).hashCode)
  }

  def hashCode(a: Array[Double]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + new java.lang.Double(x).hashCode)
  }

  def hashCode(a: Array[AnyRef]): Int = {
    if (a == null) 0
    else a.foldLeft(1)((acc, x) => 31*acc + (if (x == null) 0 else x.hashCode))
  }

  def equals(a: Array[Boolean], b: Array[Boolean]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

  def equals(a: Array[Char], b: Array[Char]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

  def equals(a: Array[Byte], b: Array[Byte]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

  def equals(a: Array[Short], b: Array[Short]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

  def equals(a: Array[Int], b: Array[Int]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

  def equals(a: Array[Long], b: Array[Long]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

  def equals(a: Array[Float], b: Array[Float]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

  def equals(a: Array[Double], b: Array[Double]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

  def equals(a: Array[AnyRef], b: Array[AnyRef]): Boolean =
    (a eq b) || (a != null && b != null && a.length == b.length &&
        (0 until a.size).forall(i => a(i) == b(i)))

}