Skip to content

Deduplicate records by numeric id

A Roaring bitset records which ids you have seen in about two bytes per id, or one bit per possible id once ids are dense.

import {
function roaringBitset(options?: BitsetOptions): Bitset

A bitset of unsigned 32-bit ids over 65,536-id chunks, each a sorted id array or an 8 KB bitmap.

This is the recommended layout: memory follows the ids actually stored, so a caller that does not yet know how far its ids range does not pay for the span. Chunked containers also make isDisjointFrom and intersectionSize stop at the first chunk that decides them.

@param ― options - Optional items, added as if by add.

@throws ― {BitsetInvalidInputError} With code INVALID_ID when an item is not in 0 … BITSET_MAX_ID.

@example

const seen = roaringBitset({ items: [3, 65_536] })
seen.has(3) // true
seen.size // 2

roaringBitset
} from 'knotkit/bitset'
const
const rows: {
id: number;
name: string;
}[]
rows
= [
{
id: number
id
: 7,
name: string
name
: 'ada' },
{
id: number
id
: 4,
name: string
name
: 'grace' },
{
id: number
id
: 7,
name: string
name
: 'ada' },
]
const
const seen: Bitset
seen
=
function roaringBitset(options?: BitsetOptions): Bitset

A bitset of unsigned 32-bit ids over 65,536-id chunks, each a sorted id array or an 8 KB bitmap.

This is the recommended layout: memory follows the ids actually stored, so a caller that does not yet know how far its ids range does not pay for the span. Chunked containers also make isDisjointFrom and intersectionSize stop at the first chunk that decides them.

@param ― options - Optional items, added as if by add.

@throws ― {BitsetInvalidInputError} With code INVALID_ID when an item is not in 0 … BITSET_MAX_ID.

@example

const seen = roaringBitset({ items: [3, 65_536] })
seen.has(3) // true
seen.size // 2

roaringBitset
()
const
const unique: {
id: number;
name: string;
}[]
unique
=
const rows: {
id: number;
name: string;
}[]
rows
.
Array<{ id: number; name: string; }>.filter(predicate: (value: {
id: number;
name: string;
}, index: number, array: {
id: number;
name: string;
}[]) => unknown, thisArg?: any): {
id: number;
name: string;
}[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@param ― predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@param ― thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

filter
((
row: {
id: number;
name: string;
}
row
) => {
if (
const seen: Bitset
seen
.
Bitset.has: (id: number) => boolean

Whether id is stored. Total: returns false for any value that is not an id (-1, 1.5, NaN, a string, 2 ** 32), as Set.prototype.has does for a value it does not hold. O(1) for dense; O(log c + log n) for a roaring array container and O(log c) for a bitmap container, where c is the number of occupied chunks and n a container's cardinality.

has
(
row: {
id: number;
name: string;
}
row
.
id: number
id
)) {
return false
}
const seen: Bitset
seen
.
Bitset.add: (id: number) => boolean

Add id. O(1) amortized for dense; O(log c + n) for a roaring array container, which shifts its tail.

@returns ― Whether the id was absent.

@throws ― {BitsetInvalidInputError} With code INVALID_ID when id is not in 0 … BITSET_MAX_ID.

@throws ― {BitsetInvalidInputError} With code MAX_CAPACITY_EXCEEDED when a dense bitset cannot grow its bitmap.

add
(
row: {
id: number;
name: string;
}
row
.
id: number
id
)
return true
})
const unique: {
id: number;
name: string;
}[]
unique
.
Array<{ id: number; name: string; }>.map<string>(callbackfn: (value: {
id: number;
name: string;
}, index: number, array: {
id: number;
name: string;
}[]) => string, thisArg?: any): string[]

Calls a defined callback function on each element of an array, and returns an array that contains the results.

@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

map
((
row: {
id: number;
name: string;
}
row
) =>
row: {
id: number;
name: string;
}
row
.
name: string
name
) // ['ada', 'grace']
const seen: Bitset
seen
.
Bitset.size: number

Number of stored ids. O(1). The bitset is empty when this is 0.

size
// 2

Ids must be integers from 0 to 2³² − 1. For other keys, a Set is the right tool.

Modules: knotkit/bitset · API reference