Skip to content

Commit

Permalink
CRoaring 0.9.8
Browse files Browse the repository at this point in the history
  • Loading branch information
SalvatorePreviti committed Feb 21, 2023
1 parent 011f8ae commit 62f2d82
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 22 deletions.
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export function bufferAlignedAlloc(size: number, alignment?: number): Buffer;
*/
export function bufferAlignedAllocUnsafe(size: number, alignment?: number): Buffer;

/**
* Given some kind of buffer or array buffer, returns a nodejs Buffer instance that contains the same data.
* The underlying ArrayBuffer is the same.
*
* @param buffer The source
* @returns A nodejs Buffer instance that contains the same data.
*/
export function asBuffer(buffer: Buffer | ArrayBufferView | TypedArray | ArrayBuffer): Buffer;

export type TypedArray =
| Uint8Array
| Uint16Array
Expand Down
46 changes: 31 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,42 @@ function iterator() {
return new RoaringBitmap32Iterator(this);
}

let isUint8Array;
let isInt8Array;
let isArrayBuffer;
let isArrayBufView;

// eslint-disable-next-line node/no-unsupported-features/node-builtins
const utilTypes = util.types; // util.types is supported only in Node.js 10+
if (utilTypes) {
isUint8Array = utilTypes.isUint8Array;
isInt8Array = utilTypes.isInt8Array;
isArrayBuffer = utilTypes.isArrayBuffer;
isArrayBufView = utilTypes.isArrayBufferView;
} else {
isUint8Array = (v) => v instanceof Uint8Array;
isInt8Array = (v) => v instanceof Int8Array;
isArrayBuffer = (v) => v instanceof ArrayBuffer;
isArrayBufView = (v) => ArrayBuffer.isView(v);
}

if (!roaring.PackageVersion) {
function asBuffer(buffer) {
const result = asBufferUnsafe(buffer);
return Buffer.isBuffer(result) ? result : Buffer.from(result);
}

function asBufferUnsafe(buffer) {
if (Buffer.isBuffer(buffer)) {
return buffer;
}
if (isArrayBuffer(buffer)) {
return Buffer.from(buffer);
}
if (isArrayBufView(buffer)) {
return Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
}
return buffer;
}

const initializedSym = Symbol.for("roaring-node");

if (!roaring[initializedSym]) {
Reflect.defineProperty(roaring, initializedSym, { value: true });

const roaringBitmap32_proto = RoaringBitmap32.prototype;
roaringBitmap32_proto[Symbol.iterator] = iterator;
roaringBitmap32_proto.iterator = iterator;
Expand All @@ -169,7 +188,7 @@ if (!roaring.PackageVersion) {
};

roaringBitmap32_proto.map = function (fn, self, output = []) {
let index = 0;
let index = output.length;
if (self === undefined) {
for (const v of this) {
output.push(fn(v, index++, this));
Expand Down Expand Up @@ -243,14 +262,9 @@ if (!roaring.PackageVersion) {
if (typeof alignment !== "number" || alignment < 0 || alignment > 1024 || !Number.isInteger(alignment)) {
throw new TypeError("ensureBufferAligned alignment must be an integer number between 0 and 1024");
}
buffer = asBufferUnsafe(buffer);
if (!Buffer.isBuffer(buffer)) {
if (buffer instanceof ArrayBuffer) {
buffer = Buffer.from(buffer);
} else if (isUint8Array(buffer) || isInt8Array(buffer) || isInt8Array(buffer) || isArrayBuffer(buffer)) {
buffer = Buffer.from(buffer.buffer);
} else {
throw new TypeError("ensureBufferAligned expects a Buffer instance");
}
throw new TypeError("ensureBufferAligned expects a Buffer instance");
}
if (!roaring.isBufferAligned(buffer, alignment)) {
const aligned = roaring.bufferAlignedAlloc(buffer.length, alignment);
Expand All @@ -262,5 +276,7 @@ if (!roaring.PackageVersion) {

RoaringBitmap32.getRoaringUsedMemory = roaring.getRoaringUsedMemory;

roaring.asBuffer = asBuffer;

roaring._initTypes({ Uint32Array, Buffer_from: Buffer.from });
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "roaring",
"version": "1.4.3",
"version": "1.4.4",
"private": false,
"description": "CRoaring official port for NodeJS",
"keywords": [
Expand Down Expand Up @@ -93,5 +93,5 @@
"typescript": "^4.9.5"
},
"gypfile": true,
"roaring_version": "0.9.6"
"roaring_version": "0.9.8"
}
3 changes: 1 addition & 2 deletions src/cpp/CRoaringUnityBuild/roaring.c
Original file line number Diff line number Diff line change
Expand Up @@ -11097,8 +11097,7 @@ bool roaring_bitmap_contains_range(const roaring_bitmap_t *r, uint64_t range_sta
}
int32_t is = ra_get_index(&r->high_low_container, hb_rs);
int32_t ie = ra_get_index(&r->high_low_container, hb_re);
ie = (ie < 0 ? -ie - 1 : ie);
if ((is < 0) || ((ie - is) != span) || ie >= hlc_sz) {
if ((ie < 0) || (is < 0) || ((ie - is) != span) || ie >= hlc_sz) {
return false;
}
const uint32_t lb_rs = range_start & 0xFFFF;
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/CRoaringUnityBuild/roaring.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
// /include/roaring/roaring_version.h automatically generated by release.py, do not change by hand
#ifndef ROARING_INCLUDE_ROARING_VERSION
#define ROARING_INCLUDE_ROARING_VERSION
#define ROARING_VERSION "0.9.6"
#define ROARING_VERSION "0.9.8"
enum {
ROARING_VERSION_MAJOR = 0,
ROARING_VERSION_MINOR = 9,
ROARING_VERSION_REVISION = 6
ROARING_VERSION_REVISION = 8
};
#endif // ROARING_INCLUDE_ROARING_VERSION
/* end file include/roaring/roaring_version.h */
Expand Down
2 changes: 1 addition & 1 deletion submodules/CRoaring
58 changes: 58 additions & 0 deletions test/aligned-buffers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
asBuffer,
RoaringBitmap32,
bufferAlignedAlloc,
bufferAlignedAllocUnsafe,
Expand All @@ -7,6 +8,63 @@ import {
} from "../";
import { expect } from "chai";

describe("asBuffer", () => {
it("returns a buffer", () => {
const input = Buffer.alloc(3);
expect(input).to.eq(input);
});

it("wraps an array buffer", () => {
const input = new ArrayBuffer(3);
const output = asBuffer(input);
expect(output).to.be.instanceOf(Buffer);
expect(output.buffer).to.eq(input);
});

it("wraps an arraybuffer view", () => {
for (const ctor of [
Uint8Array,
Uint16Array,
Uint32Array,
Int8Array,
Int16Array,
Int32Array,
Float32Array,
Float64Array,
Uint8ClampedArray,
]) {
const input = new ctor(3);
const output = asBuffer(input);
expect(output).to.be.instanceOf(Buffer);
expect(output.buffer).to.eq(input.buffer);
expect(output.byteOffset).to.eq(0);
expect(output.byteLength).to.eq(input.byteLength);
}
});

it("wraps an arraybuffer view with offset", () => {
for (const ctor of [
Uint8Array,
Uint16Array,
Uint32Array,
Int8Array,
Int16Array,
Int32Array,
Float32Array,
Float64Array,
Uint8ClampedArray,
]) {
const sourceArrayBuffer = new ArrayBuffer(20 * ctor.BYTES_PER_ELEMENT);
const input = new ctor(sourceArrayBuffer, 8, 8);
const output = asBuffer(input);
expect(output).to.be.instanceOf(Buffer);
expect(output.buffer).to.eq(input.buffer);
expect(output.byteOffset).to.eq(input.byteOffset);
expect(output.byteLength).to.eq(input.byteLength);
}
});
});

describe("bufferAlignedAlloc", () => {
it("exposes the bufferAlignedAlloc function", () => {
expect(bufferAlignedAlloc).to.be.a("function");
Expand Down

0 comments on commit 62f2d82

Please sign in to comment.