Newer
Older
/*
* Copyright (C) 2024, UChicago Argonne, LLC
* Licensed under the 3-clause BSD license. See accompanying LICENSE.txt file
* in the top-level directory.
*/
package Octeres.Bitmask

Eric Pershey
committed
import scala.collection.IterableOnce.iterableOnceExtensionMethods
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
import scala.collection.immutable.ArraySeq
import scala.collection.mutable.{ArrayBuffer, ListBuffer}
object BitmaskLite {
case class LiteBitmaskStruct(length: Long, bit_count: Long, bit_idx_first: Long, bit_idx_last: Long,
intervals: ArraySeq[ArraySeq[Long]])
def getLiteBitmaskZeros(length: Long): LiteBitmaskStruct = {
LiteBitmaskStruct(length, 0L, -1L, -1L, ArraySeq())
}
def getLiteBitmaskZeros(length: Int): LiteBitmaskStruct = {
LiteBitmaskStruct(length.toLong, 0L, -1L, -1L, ArraySeq())
}
def getCombinedIntervalsUntilAllConsumed(alst: ArraySeq[ArraySeq[Long]], blst: ArraySeq[ArraySeq[Long]]): Seq[List[Long]] = {
var aidx: Long = 0
var bidx: Long = 0
val exit_list = List[Long](-2, -2)
LazyList.continually {
val amin: Long = if (aidx < alst.length) alst(aidx.toInt).head else -2L
val amax: Long = if (aidx < alst.length) alst(aidx.toInt).last else -2L
val bmin: Long = if (bidx < blst.length) blst(bidx.toInt).head else -2L
val bmax: Long = if (bidx < blst.length) blst(bidx.toInt).last else -2L
(amin, bmin) match {
case (-2L, -2L) =>
exit_list
case (_, -2L) =>
aidx += 1L
List(amin, amax)
case (-2L, _) =>
bidx += 1L
List(bmin, bmax)
case (_, _) if (amin != -2 & amin <= bmin) =>
aidx += 1L
List(amin, amax)
case _ =>
bidx += 1L
List(bmin, bmax)
}
}.takeWhile(_ != exit_list)
}
class BitmaskError(message: String) extends Exception(message) {
def this(message: String, cause: Throwable = null) = {
this(message)
initCause(cause)
}
def this(cause: Throwable) = {
this(Option(cause).map(_.toString).orNull, cause)
}
def this() = {
this(null: String)
}
}
def validateLiteBitmaskSlotsLike(obj: LiteBitmaskStruct): Unit = {
val intervals = obj.intervals
if (obj.bit_count > 0) {
if (obj.bit_idx_last >= obj.length) {
throw new BitmaskError(s"bit_idx_last is greater than length")
}
if (obj.bit_idx_first != intervals.head.head) {
throw new BitmaskError(s"bit_idx_first:${obj.bit_idx_first} != intervals.head.head:${intervals.head.head}")
}
if (obj.bit_idx_last != intervals.last.last) {
throw new BitmaskError(s"bit_idx_last:${obj.bit_idx_last} != intervals.last.last:${intervals.last.last}")
}
} else {
if (obj.bit_idx_first != -1) {
throw new BitmaskError(s"bit_idx_first:${obj.bit_idx_first} != -1")
}
if (obj.bit_idx_last != -1) {
throw new BitmaskError(s"bit_idx_last:${obj.bit_idx_last} != -1")
}
}
var bit_count: Long = 0
for ((alar, i) <- intervals.zipWithIndex) {
val al: Long = alar.head
val ar: Long = alar.last
val sub_bit_count: Long = (ar - al) + 1L
if (sub_bit_count < 1) {
throw new BitmaskError(s"negative interval: [$al, $ar]")
}
bit_count += sub_bit_count
if (al > ar) {
throw new BitmaskError(s"negative interval: [$al, $ar]")
}
val blbr: Option[ArraySeq[Long]] = intervals.lift(i + 1)
blbr match {
case Some(x: ArraySeq[Long]) => {
val bl: Long = x.head
val br: Long = x.last
if ((al == bl) & (ar == br)) {
throw new BitmaskError(s"duplicate interval: [$al, $ar]")
}
if ((ar + 1) == bl) {
throw new BitmaskError(s"interval not merged: ${ar}+1 == ${bl} for $alar->$blbr")
}
if (ar > bl) {
throw new BitmaskError(s"interval out of order or not merged: ${ar} > ${bl} for $alar->$blbr")
}
}
case None => Nil
}
}
if (bit_count != obj.bit_count) {
throw new BitmaskError(s"bit_count:${bit_count} != obj.bit_count:${obj.bit_count}")
}
}
def logical_or_v0(a_bitmask: LiteBitmaskStruct, b_bitmask: LiteBitmaskStruct): LiteBitmaskStruct = {
/* does a logical or of two bit intervals
* From Spark ([8,3,ArraySeq(ArraySeq(0, 2)),0,2],[8,3,ArraySeq(ArraySeq(0, 2)),0,2]) */
val intervalGen: Seq[List[Long]] = getCombinedIntervalsUntilAllConsumed(
a_bitmask.intervals,
b_bitmask.intervals)
val intervals = ArrayBuffer.empty[ArraySeq[Long]]
var bitCount: Long = 0
var bitIdxFirst: Long = -1
var bitIdxLast: Long = -1
var prev_start: Long = -1
var prev_end: Long = -1
val itr = intervalGen.iterator
for (List(next_start, next_end) <- itr) {
(next_start, next_end) match {
case (-2, -2) => // case where there is nothing
bitCount = 0
bitIdxFirst = -1
bitIdxLast = -1
case _ if (prev_start == -1 & prev_end == -1) => // Initial variable, setting previous
prev_start = next_start
prev_end = next_end
intervals += ArraySeq(prev_start, prev_end)
bitCount = prev_end - prev_start + 1
bitIdxFirst = intervals.head.head
bitIdxLast = intervals.last.last
case _ =>
if (next_start <= prev_end + 1) {
val new_end = Math.max(prev_end, next_end)
intervals(intervals.length - 1) = ArraySeq(prev_start, new_end)
bitCount += new_end - prev_end
prev_end = new_end
} else {
intervals += ArraySeq(next_start, next_end)
bitCount += next_end - next_start + 1
prev_start = next_start
prev_end = next_end
}
bitIdxFirst = intervals.head.head
bitIdxLast = intervals.last.last
}
}
val result_intervals: ArraySeq[ArraySeq[Long]] = ArraySeq.from(intervals)
// hopefully this is a view not a copy
// val result_intervals: ArraySeq[ArraySeq[Long]] = ArraySeq.unsafeWrapArray(intervals.toArray)
LiteBitmaskStruct(a_bitmask.length, bitCount, bitIdxFirst, bitIdxLast, result_intervals)
}
def logical_or(a_bitmask: LiteBitmaskStruct, b_bitmask: LiteBitmaskStruct): LiteBitmaskStruct = {
logical_or_v0(a_bitmask, b_bitmask)
}
def intersection_possible(a_bitmask: LiteBitmaskStruct, b_bitmask: LiteBitmaskStruct): Boolean = {
var result = true
if ((a_bitmask.bit_count == 0) | (b_bitmask.bit_count == 0)) {
result = false
} else if (a_bitmask.bit_idx_first > b_bitmask.bit_idx_last) {
result = false
} else if (a_bitmask.bit_idx_last < b_bitmask.bit_idx_first) {
result = false
} else {
result = true
}
result
}
def getIntervalUntilAllConsumed(alst: List[List[Long]], blst: List[List[Long]]): Seq[List[Long]] = {
/* from get_intervals_until_all_consumed */
var aidx: Long = 0
var bidx: Long = 0
val exit_list: List[Long] = List(-2, -2)
LazyList.continually {
val amin: Long = if (aidx < alst.length) alst(aidx.toInt).head else -2
val amax: Long = if (aidx < alst.length) alst(aidx.toInt).last else -2
val bmin: Long = if (bidx < blst.length) blst(bidx.toInt).head else -2
val bmax: Long = if (bidx < blst.length) blst(bidx.toInt).last else -2
// println(s"aidx:${aidx} bidx:${bidx} ${amin},${bmin} amin:${amin} amax:${amax} bmin:${bmin} bmax:${bmax}")
(amin, bmin) match {
case (-2, -2) =>
exit_list
case (_, -2) =>
aidx += 1
List(amin, amax)
case (-2, _) =>
bidx += 1
List(bmin, bmax)
case (_, _) if (amin != -2 & amin <= bmin) =>
aidx += 1
List(amin, amax)
case _ =>
bidx += 1
List(bmin, bmax)
}
}.takeWhile(_ != exit_list)
}
def getIntervalBits(a_bitmask: LiteBitmaskStruct, bit_idx_first: Long = 0, bit_idx_last: Long = -2): List[Long] = {
val bit_lst: ListBuffer[Long] = ListBuffer()
a_bitmask.intervals.foreach(elem => {
for (i <- elem.head to elem.last) { // inclusive
if (i < bit_idx_first){
} else if (bit_idx_last != -2 & (i > bit_idx_last)) {
} else {
bit_lst += i
}
}
})
bit_lst.toList
}
def intersection_any_v0(a_bitmask: LiteBitmaskStruct, b_bitmask: LiteBitmaskStruct): Boolean = {
var result = false
if (!intersection_possible(a_bitmask, b_bitmask)) {
result = false
} else {
val first_a = a_bitmask.bit_idx_first
val last_a = a_bitmask.bit_idx_last
val first_b = b_bitmask.bit_idx_first
val last_b = b_bitmask.bit_idx_last
if ((first_a == first_b) | (last_a == last_b)) {
result = true
} else if ((a_bitmask.bit_idx_last == b_bitmask.bit_idx_first) | (a_bitmask.bit_idx_last == b_bitmask.bit_idx_last)) {
result = true
} else {
val bit_idx_first = if (first_a >= first_b) first_a else first_b
val bit_idx_last = if (last_a < last_b) last_a else last_b
var a_lst = getIntervalBits(a_bitmask, bit_idx_first=bit_idx_first, bit_idx_last=bit_idx_last)
var b_lst = getIntervalBits(b_bitmask, bit_idx_first=bit_idx_first, bit_idx_last=bit_idx_last)
if (a_lst.isEmpty | b_lst.isEmpty){
} else {
var a_idx = a_lst.head
a_lst = a_lst.tail
var b_idx = b_lst.head
b_lst = b_lst.tail
while (a_idx >= 0 & b_idx >= 0) {
if (a_idx == b_idx) {
result = true
a_idx = -2
b_idx = -2
} else if (a_idx > b_idx){
// move b_idx
if (b_lst.nonEmpty) {
b_idx = b_lst.head
b_lst = b_lst.tail
} else {
b_idx = -2
}
} else {
// move a_idx
if (a_lst.nonEmpty){
a_idx = a_lst.head
a_lst = a_lst.tail
} else {
a_idx = -2
}
}
}
}
}
}
result
}

Eric Pershey
committed
def toArrayRangeGen(arraySeq: ArraySeq[ArraySeq[Long]], idx_first: Long = 0, idx_last: Long = -2): Iterator[Long] = new Iterator[Long] {
/*
Given an array of arrays that is a array of ranges [a, b], yield an integer from the range.
Note does not yet support full size longs. See .toInt
FIXME: doesn't use idx_first and idx_last due to the complexities.
*/
private var currOutIdx: Long = if (arraySeq.isEmpty) -1L else 0L
private var currInnVal: Long = if (arraySeq.nonEmpty) arraySeq(currOutIdx.toInt).head else -1L
private var currInnEnd: Long = if (arraySeq.nonEmpty) arraySeq(currOutIdx.toInt).last else -1L

Eric Pershey
committed
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
def hasNext: Boolean = {
if (arraySeq.isEmpty){
false
} else if (currOutIdx <= arraySeq.length){ // don't include the length
if (currInnVal <= currInnEnd){ // include the end
true
} else {
false
}
} else {
false
}
}
def next(): Long = {
val value = currInnVal
currInnVal += 1
if (currInnVal > currInnEnd) {
// at the end of a range
currOutIdx += 1
if (currOutIdx < arraySeq.length) {
currInnVal = arraySeq(currOutIdx.toInt).head
currInnEnd = arraySeq(currOutIdx.toInt).last
}
} else {
// still in the current range
}
value
}
}
def intersection_any_v1(a_bitmask: LiteBitmaskStruct, b_bitmask: LiteBitmaskStruct): Boolean = {
var result = false
if (!intersection_possible(a_bitmask, b_bitmask)) {
result = false
} else {
val first_a = a_bitmask.bit_idx_first
val last_a = a_bitmask.bit_idx_last
val first_b = b_bitmask.bit_idx_first
val last_b = b_bitmask.bit_idx_last
if ((first_a == first_b) | (last_a == last_b)) {
result = true
} else if ((a_bitmask.bit_idx_last == b_bitmask.bit_idx_first) | (a_bitmask.bit_idx_last == b_bitmask.bit_idx_last)) {
result = true
} else {
// does not work yet
// val bit_idx_first = if (first_a >= first_b) first_a else first_b
// val bit_idx_last = if (last_a < last_b) last_a else last_b
val a_ii = toArrayRangeGen(a_bitmask.intervals) //, bit_idx_first, bit_idx_last)
val b_ii = toArrayRangeGen(b_bitmask.intervals) //, bit_idx_first, bit_idx_last)
if (a_ii.isEmpty | b_ii.isEmpty){
} else {
var a_idx = if (a_ii.hasNext) a_ii.next() else -2
var b_idx = if (b_ii.hasNext) b_ii.next() else -2
while (a_idx >= 0 & b_idx >= 0) {
if (a_idx == b_idx) {
result = true
a_idx = -2
b_idx = -2
} else if (a_idx > b_idx){
// move b_idx
if (b_ii.nonEmpty) {
b_idx = if (b_ii.hasNext) b_ii.next() else -2
} else {
b_idx = -2
}
} else {
// move a_idx
if (a_ii.nonEmpty){
a_idx = if (a_ii.hasNext) a_ii.next() else -2
} else {
a_idx = -2
}
}
}
}
}
}
result
}

Eric Pershey
committed
def toArrayRangeLimitGen(arraySeq: ArraySeq[ArraySeq[Long]], idx_first: Long = 0, idx_last: Long = -2): Iterator[Long] = {
/* remove full outer ArraySeq that are not in the idx_first to idx_last range. */

Eric Pershey
committed
if (arraySeq.nonEmpty){
toArrayRangeGen(arraySeq.filter(a => (
((a.head >= idx_first)) | (a.last >= idx_first))
&
(((a.head <= idx_last) | (a.last <= idx_last)) | (idx_last == -2))))
} else {
toArrayRangeGen(arraySeq)
}
}

Eric Pershey
committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
def intersection_any_v2(a_bitmask: LiteBitmaskStruct, b_bitmask: LiteBitmaskStruct): Boolean = {
var result = false
if (!intersection_possible(a_bitmask, b_bitmask)) {
result = false
} else {
val first_a = a_bitmask.bit_idx_first
val last_a = a_bitmask.bit_idx_last
val first_b = b_bitmask.bit_idx_first
val last_b = b_bitmask.bit_idx_last
if ((first_a == first_b) | (last_a == last_b)) {
result = true
} else if ((a_bitmask.bit_idx_last == b_bitmask.bit_idx_first) | (a_bitmask.bit_idx_last == b_bitmask.bit_idx_last)) {
result = true
} else {
val bit_idx_first = if (first_a >= first_b) first_a else first_b
val bit_idx_last = if (last_a < last_b) last_a else last_b
val a_ii = toArrayRangeLimitGen(a_bitmask.intervals, bit_idx_first, bit_idx_last)
val b_ii = toArrayRangeLimitGen(b_bitmask.intervals, bit_idx_first, bit_idx_last)
if (a_ii.isEmpty | b_ii.isEmpty){
} else {
var a_idx = if (a_ii.hasNext) a_ii.next() else -2
var b_idx = if (b_ii.hasNext) b_ii.next() else -2
while (a_idx >= 0 & b_idx >= 0) {
if (a_idx == b_idx) {
result = true
a_idx = -2
b_idx = -2
} else if (a_idx > b_idx){
// move b_idx
if (b_ii.nonEmpty) {
b_idx = if (b_ii.hasNext) b_ii.next() else -2
} else {
b_idx = -2
}
} else {
// move a_idx
if (a_ii.nonEmpty){
a_idx = if (a_ii.hasNext) a_ii.next() else -2
} else {
a_idx = -2
}
}
}
}
}
}
result
}
def intersection_any(a_bitmask: LiteBitmaskStruct, b_bitmask: LiteBitmaskStruct): Boolean = {
/* this directs the data to the current version of the function */
intersection_any_v2(a_bitmask, b_bitmask)
}