forked from SalvatorePreviti/roaring-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (118 loc) · 3.31 KB
/
index.js
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
'use strict'
const roaring = require('./build/Release/roaring.node')
const { version: packageVersion } = require('./package.json')
module.exports = roaring
const { defineProperty } = Reflect
defineProperty(roaring, '__esModule', { value: true })
const { RoaringBitmap32, RoaringBitmap32BufferedIterator } = roaring
class RoaringBitmap32IteratorResult {
constructor() {
this.value = undefined
this.done = true
}
}
const _iteratorBufferPoolMax = 32
const _iteratorBufferPoolDefaultLen = 512
const _iteratorBufferPool = []
class RoaringBitmap32Iterator {
constructor(bitmap, buffer = _iteratorBufferPoolDefaultLen) {
const r = new RoaringBitmap32IteratorResult()
let t
let i = 0
let n = 0
if (!(bitmap instanceof RoaringBitmap32)) {
if (bitmap === undefined || bitmap === null) {
t = null
} else {
throw new TypeError('RoaringBitmap32Iterator constructor expects a RoaringBitmap32 instance')
}
}
const init = () => {
if (typeof buffer === 'number') {
buffer = (buffer === _iteratorBufferPoolDefaultLen && _iteratorBufferPool.pop()) || new Uint32Array(buffer)
}
t = new RoaringBitmap32BufferedIterator(bitmap, buffer)
n = t.n
r.done = false
}
const m = () => {
if (t !== null) {
if (t === undefined) {
init()
} else {
n = t.fill()
}
if (n === 0) {
t = null
if (
buffer &&
_iteratorBufferPool.length < _iteratorBufferPoolMax &&
buffer.length === _iteratorBufferPoolDefaultLen
) {
_iteratorBufferPool.push(buffer)
}
buffer = undefined
bitmap = undefined
r.value = undefined
r.done = true
} else {
i = 1
r.value = buffer[0]
}
}
}
this.next = () => {
if (i < n) {
r.value = buffer[i++]
} else {
m()
}
return r
}
}
[Symbol.iterator]() {
return this
}
}
const roaringBitmap32IteratorProp = {
value: RoaringBitmap32Iterator,
writable: false,
configurable: false,
enumerable: false
}
defineProperty(RoaringBitmap32Iterator, 'default', roaringBitmap32IteratorProp)
defineProperty(RoaringBitmap32Iterator, 'RoaringBitmap32Iterator', roaringBitmap32IteratorProp)
function iterator() {
return new RoaringBitmap32Iterator(this)
}
if (!roaring.PackageVersion) {
const roaringBitmap32Proto = RoaringBitmap32.prototype
roaringBitmap32Proto[Symbol.iterator] = iterator
roaringBitmap32Proto.iterator = iterator
roaringBitmap32Proto.keys = iterator
roaringBitmap32Proto.values = iterator
roaringBitmap32Proto.entries = function* entries() {
for (const v of this) {
yield [v, v]
}
}
roaringBitmap32Proto.forEach = function (fn, self) {
if (self !== undefined) {
for (const v of this) {
fn(v, v, this)
}
} else {
for (const v of this) {
fn.call(self, v, v, this)
}
}
}
roaringBitmap32Proto.PackageVersion = packageVersion
RoaringBitmap32.PackageVersion = packageVersion
RoaringBitmap32.RoaringBitmap32Iterator = RoaringBitmap32Iterator
roaring.PackageVersion = packageVersion
roaring.RoaringBitmap32Iterator = RoaringBitmap32Iterator
roaring._initTypes({
Uint32Array
})
}