forked from scala/scala-java8-compat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleAccumulator.scala
353 lines (316 loc) · 11 KB
/
DoubleAccumulator.scala
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
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc. dba Akka
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.compat.java8.collectionImpl
import scala.language.higherKinds
/** A `DoubleAccumulator` is a low-level collection specialized for gathering
* elements in parallel and then joining them in order by merging them.
* This is a manually specialized variant of `Accumulator` with no actual
* subclassing relationship with `Accumulator`.
*/
final class DoubleAccumulator extends AccumulatorLike[Double, DoubleAccumulator] { self =>
private[java8] var current: Array[Double] = DoubleAccumulator.emptyDoubleArray
private[java8] var history: Array[Array[Double]] = DoubleAccumulator.emptyDoubleArrayArray
private[java8] def cumulative(i: Int) = { val x = history(i); x(x.length-1).toLong }
private def expand(): Unit = {
if (index > 0) {
current(current.length-1) = (if (hIndex > 0) { val x = history(hIndex-1); x(x.length-1) } else 0) + index
if (hIndex >= history.length) hExpand()
history(hIndex) = current
hIndex += 1
}
current = new Array[Double](nextBlockSize+1)
index = 0
}
private def hExpand(): Unit = {
if (hIndex == 0) history = new Array[Array[Double]](4)
else history = java.util.Arrays.copyOf(history, history.length << 1)
}
/** Appends an element to this `DoubleAccumulator`. */
final def +=(a: Double): Unit = {
totalSize += 1
if (index+1 >= current.length) expand()
current(index) = a
index += 1
}
/** Removes all elements from `that` and appends them to this `DoubleAccumulator`. */
final def drain(that: DoubleAccumulator): Unit = {
var h = 0
var prev = 0L
var more = true
while (more && h < that.hIndex) {
val cuml = that.cumulative(h)
val n = (cuml - prev).toInt
if (current.length - index - 1 >= n) {
System.arraycopy(that.history(h), 0, current, index, n)
prev = cuml
index += n
h += 1
}
else more = false
}
if (h >= that.hIndex && current.length - index - 1>= that.index) {
if (that.index > 0) System.arraycopy(that.current, 0, current, index, that.index)
index += that.index
}
else {
val slots = (if (index > 0) 1 else 0) + that.hIndex - h
if (hIndex + slots > history.length) {
val n = math.max(4, 1 << (32 - java.lang.Integer.numberOfLeadingZeros(1 + hIndex + slots)))
history = java.util.Arrays.copyOf(history, n)
}
var pv = (if (hIndex > 0) cumulative(hIndex-1) else 0L)
if (index > 0) {
val x =
if (index < (current.length >>> 3) && current.length - 1 > 32) {
val ans = java.util.Arrays.copyOf(current, index + 1)
ans(ans.length - 1) = current(current.length - 1)
ans
}
else current
pv = pv + index
x(x.length - 1) = pv
history(hIndex) = x
hIndex += 1
}
while (h < that.hIndex) {
val cuml = that.cumulative(h)
pv = pv + cuml - prev
prev = cuml
val x = that.history(h)
x(x.length - 1) = pv
history(hIndex) = x
h += 1
hIndex += 1
}
index = that.index
current = that.current
}
totalSize += that.totalSize
that.clear
}
override def clear(): Unit = {
super.clear()
current = DoubleAccumulator.emptyDoubleArray
history = DoubleAccumulator.emptyDoubleArrayArray
}
/** Retrieves the `ix`th element. */
final def apply(ix: Long): Double = {
if (totalSize - ix <= index || hIndex == 0) current((ix - (totalSize - index)).toInt)
else {
val w = seekSlot(ix)
history((w >>> 32).toInt)((w & 0xFFFFFFFFL).toInt)
}
}
/** Retrieves the `ix`th element, using an `Int` index. */
final def apply(i: Int): Double = apply(i.toLong)
/** Returns a `DoubleStepper` over the contents of this `DoubleAccumulator`. */
final def stepper: DoubleStepper = new DoubleAccumulatorStepper(this)
/** Returns an `Iterator` over the contents of this `DoubleAccumulator`. The `Iterator` is not specialized. */
final def iterator = stepper.iterator
/** Returns a `java.util.Spliterator.OfDouble` over the contents of this `DoubleAccumulator`*/
final def spliterator: java.util.Spliterator.OfDouble = stepper
/** Produces a sequential Java 8 `DoubleStream` over the elements of this `DoubleAccumulator`*/
final def seqStream: java.util.stream.DoubleStream = java.util.stream.StreamSupport.doubleStream(spliterator, false)
/** Produces a parallel Java 8 `DoubleStream` over the elements of this `DoubleAccumulator`*/
final def parStream: java.util.stream.DoubleStream = java.util.stream.StreamSupport.doubleStream(spliterator, true)
/** Copies the elements in this `DoubleAccumulator` into an `Array[Double]` */
final def toArray = {
if (totalSize > Int.MaxValue) throw new IllegalArgumentException("Too many elements accumulated for an array: "+totalSize.toString)
val a = new Array[Double](totalSize.toInt)
var j = 0
var h = 0
var pv = 0L
while (h < hIndex) {
val x = history(h)
val cuml = x(x.length-1).toLong
val n = (cuml - pv).toInt
pv = cuml
System.arraycopy(x, 0, a, j, n)
j += n
h += 1
}
System.arraycopy(current, 0, a, j, index)
j += index
a
}
/** Copies the elements in this `DoubleAccumulator` to a `List` */
final def toList: List[Double] = {
var ans: List[Double] = Nil
var i = index - 1
while (i >= 0) {
ans = current(i) :: ans
i -= 1
}
var h = hIndex - 1
while (h >= 0) {
val a = history(h)
i = (cumulative(h) - (if (h == 0) 0L else cumulative(h-1))).toInt - 1
while (i >= 0) {
ans = a(i) :: ans
i -= 1
}
h -= 1
}
ans
}
/** Copies the elements in this `DoubleAccumulator` to a specified collection.
* Note that the target collection is not specialized.
* Usage example: `acc.to[Vector]`
*/
final def to[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Double, Coll[Double]]): Coll[Double] = {
if (totalSize > Int.MaxValue) throw new IllegalArgumentException("Too many elements accumulated for a Scala collection: "+totalSize.toString)
val b = cbf()
b.sizeHint(totalSize.toInt)
var h = 0
var pv = 0L
while (h < hIndex) {
val x = history(h)
val n = cumulative(h) - pv
pv = cumulative(h)
var i = 0
while (i < n) {
b += x(i)
i += 1
}
h += 1
}
var i = 0
while (i < index) {
b += current(i)
i += 1
}
b.result
}
}
object DoubleAccumulator {
private val emptyDoubleArray = new Array[Double](0)
private val emptyDoubleArrayArray = new Array[Array[Double]](0)
/** A `Supplier` of `DoubleAccumulator`s, suitable for use with `java.util.stream.DoubleStream`'s `collect` method. Suitable for `Stream[Double]` also. */
def supplier = new java.util.function.Supplier[DoubleAccumulator]{ def get: DoubleAccumulator = new DoubleAccumulator }
/** A `BiConsumer` that adds an element to an `Accumulator`, suitable for use with `java.util.stream.DoubleStream`'s `collect` method. */
def adder = new java.util.function.ObjDoubleConsumer[DoubleAccumulator]{ def accept(ac: DoubleAccumulator, a: Double): Unit = { ac += a } }
/** A `BiConsumer` that adds a boxed `Double` to an `DoubleAccumulator`, suitable for use with `java.util.stream.Stream`'s `collect` method. */
def boxedAdder = new java.util.function.BiConsumer[DoubleAccumulator, Double]{ def accept(ac: DoubleAccumulator, a: Double): Unit = { ac += a } }
/** A `BiConsumer` that merges `DoubleAccumulator`s, suitable for use with `java.util.stream.DoubleStream`'s `collect` method. Suitable for `Stream[Double]` also. */
def merger = new java.util.function.BiConsumer[DoubleAccumulator, DoubleAccumulator]{ def accept(a1: DoubleAccumulator, a2: DoubleAccumulator): Unit = { a1 drain a2 } }
/** Builds a `DoubleAccumulator` from any `Double`-valued `TraversableOnce` */
def from[A](source: TraversableOnce[Double]) = {
val a = new DoubleAccumulator
source.foreach(a += _)
a
}
}
private[java8] class DoubleAccumulatorStepper(private val acc: DoubleAccumulator) extends DoubleStepper {
import java.util.Spliterator._
private var h = 0
private var i = 0
private var a = if (acc.hIndex > 0) acc.history(0) else acc.current
private var n = if (acc.hIndex > 0) acc.cumulative(0) else acc.index
private var N = acc.totalSize
private def duplicateSelf(limit: Long): DoubleAccumulatorStepper = {
val ans = new DoubleAccumulatorStepper(acc)
ans.h = h
ans.i = i
ans.a = a
ans.n = n
ans.N = limit
ans
}
private def loadMore(): Unit = {
h += 1
if (h < acc.hIndex) { a = acc.history(h); n = acc.cumulative(h) - acc.cumulative(h-1) }
else { a = acc.current; n = acc.index }
i = 0
}
def characteristics() = ORDERED | SIZED | SUBSIZED | NONNULL
def estimateSize = N
def hasNext = N > 0
def nextDouble: Double =
if (n <= 0) throw new NoSuchElementException("next on empty Stepper")
else {
if (i >= n) loadMore()
val ans = a(i)
i += 1
N -= 1
ans
}
// Overridden for efficiency
override def tryStep(f: Double => Unit): Boolean =
if (N <= 0) false
else {
if (i >= n) loadMore()
f(a(i))
i += 1
N -= 1
true
}
// Overridden for efficiency
override def tryAdvance(f: java.util.function.DoubleConsumer): Boolean =
if (N <= 0) false
else {
if (i >= n) loadMore()
f.accept(a(i))
i += 1
N -= 1
true
}
// Overridden for efficiency
override def foreach(f: Double => Unit): Unit = {
while (N > 0) {
if (i >= n) loadMore()
val i0 = i
if ((n-i) > N) n = i + N.toInt
while (i < n) {
f(a(i))
i += 1
}
N -= (n - i0)
}
}
// Overridden for efficiency
override def forEachRemaining(f: java.util.function.DoubleConsumer): Unit = {
while (N > 0) {
if (i >= n) loadMore()
val i0 = i
if ((n-i) > N) n = i + N.toInt
while (i < n) {
f.accept(a(i))
i += 1
}
N -= (n - i0)
}
}
def substep(): DoubleStepper =
if (N <= 1) null
else {
val half = (N >> 1)
val M = (if (h <= 0) 0L else acc.cumulative(h-1)) + i
val R = M + half
val ans = duplicateSelf(half)
if (h < acc.hIndex) {
val w = acc.seekSlot(R)
h = (w >>> 32).toInt
if (h < acc.hIndex) {
a = acc.history(h)
n = acc.cumulative(h) - (if (h > 0) acc.cumulative(h-1) else 0)
}
else {
a = acc.current
n = acc.index
}
i = (w & 0xFFFFFFFFL).toInt
}
else i += half.toInt
N -= half
ans
}
}