@ac-kit/data 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +75 -0
- package/dist/bit/bit-set.d.ts +84 -0
- package/dist/bit/bit-set.d.ts.map +1 -0
- package/dist/bit/bit-set.js +206 -0
- package/dist/bit/bit-set.js.map +1 -0
- package/dist/bit/bit-vector.d.ts +69 -0
- package/dist/bit/bit-vector.d.ts.map +1 -0
- package/dist/bit/bit-vector.js +172 -0
- package/dist/bit/bit-vector.js.map +1 -0
- package/dist/cache/_lru-store.d.ts +37 -0
- package/dist/cache/_lru-store.d.ts.map +1 -0
- package/dist/cache/_lru-store.js +137 -0
- package/dist/cache/_lru-store.js.map +1 -0
- package/dist/cache/lru-cache.d.ts +90 -0
- package/dist/cache/lru-cache.d.ts.map +1 -0
- package/dist/cache/lru-cache.js +143 -0
- package/dist/cache/lru-cache.js.map +1 -0
- package/dist/cache/lru-map.d.ts +72 -0
- package/dist/cache/lru-map.d.ts.map +1 -0
- package/dist/cache/lru-map.js +100 -0
- package/dist/cache/lru-map.js.map +1 -0
- package/dist/collection/_bounded.d.ts +27 -0
- package/dist/collection/_bounded.d.ts.map +1 -0
- package/dist/collection/_bounded.js +48 -0
- package/dist/collection/_bounded.js.map +1 -0
- package/dist/collection/_capacity-semaphore.d.ts +31 -0
- package/dist/collection/_capacity-semaphore.d.ts.map +1 -0
- package/dist/collection/_capacity-semaphore.js +94 -0
- package/dist/collection/_capacity-semaphore.js.map +1 -0
- package/dist/collection/iasync-collection.d.ts +16 -0
- package/dist/collection/iasync-collection.d.ts.map +1 -0
- package/dist/collection/iasync-collection.js +13 -0
- package/dist/collection/iasync-collection.js.map +1 -0
- package/dist/collection/iasync-searchable.d.ts +9 -0
- package/dist/collection/iasync-searchable.d.ts.map +1 -0
- package/dist/collection/iasync-searchable.js +2 -0
- package/dist/collection/iasync-searchable.js.map +1 -0
- package/dist/collection/ibounded.d.ts +36 -0
- package/dist/collection/ibounded.d.ts.map +1 -0
- package/dist/collection/ibounded.js +8 -0
- package/dist/collection/ibounded.js.map +1 -0
- package/dist/collection/icollection.d.ts +24 -0
- package/dist/collection/icollection.d.ts.map +1 -0
- package/dist/collection/icollection.js +2 -0
- package/dist/collection/icollection.js.map +1 -0
- package/dist/collection/ilossy.d.ts +20 -0
- package/dist/collection/ilossy.d.ts.map +1 -0
- package/dist/collection/ilossy.js +2 -0
- package/dist/collection/ilossy.js.map +1 -0
- package/dist/collection/ireplacing.d.ts +25 -0
- package/dist/collection/ireplacing.d.ts.map +1 -0
- package/dist/collection/ireplacing.js +2 -0
- package/dist/collection/ireplacing.js.map +1 -0
- package/dist/collection/isearchable.d.ts +36 -0
- package/dist/collection/isearchable.d.ts.map +1 -0
- package/dist/collection/isearchable.js +2 -0
- package/dist/collection/isearchable.js.map +1 -0
- package/dist/cursor/ibidirectional-cursor-sequence.d.ts +30 -0
- package/dist/cursor/ibidirectional-cursor-sequence.d.ts.map +1 -0
- package/dist/cursor/ibidirectional-cursor-sequence.js +2 -0
- package/dist/cursor/ibidirectional-cursor-sequence.js.map +1 -0
- package/dist/cursor/ibidirectional-cursor.d.ts +13 -0
- package/dist/cursor/ibidirectional-cursor.d.ts.map +1 -0
- package/dist/cursor/ibidirectional-cursor.js +2 -0
- package/dist/cursor/ibidirectional-cursor.js.map +1 -0
- package/dist/cursor/icursor-sequence.d.ts +48 -0
- package/dist/cursor/icursor-sequence.d.ts.map +1 -0
- package/dist/cursor/icursor-sequence.js +2 -0
- package/dist/cursor/icursor-sequence.js.map +1 -0
- package/dist/cursor/icursor.d.ts +27 -0
- package/dist/cursor/icursor.d.ts.map +1 -0
- package/dist/cursor/icursor.js +2 -0
- package/dist/cursor/icursor.js.map +1 -0
- package/dist/cursor/index-cursor-sequence.d.ts +57 -0
- package/dist/cursor/index-cursor-sequence.d.ts.map +1 -0
- package/dist/cursor/index-cursor-sequence.js +181 -0
- package/dist/cursor/index-cursor-sequence.js.map +1 -0
- package/dist/cursor/irandom-access-cursor.d.ts +16 -0
- package/dist/cursor/irandom-access-cursor.d.ts.map +1 -0
- package/dist/cursor/irandom-access-cursor.js +2 -0
- package/dist/cursor/irandom-access-cursor.js.map +1 -0
- package/dist/deque/blocking-deque.d.ts +36 -0
- package/dist/deque/blocking-deque.d.ts.map +1 -0
- package/dist/deque/blocking-deque.js +101 -0
- package/dist/deque/blocking-deque.js.map +1 -0
- package/dist/deque/bounded-deque.d.ts +58 -0
- package/dist/deque/bounded-deque.d.ts.map +1 -0
- package/dist/deque/bounded-deque.js +103 -0
- package/dist/deque/bounded-deque.js.map +1 -0
- package/dist/deque/deque.d.ts +35 -0
- package/dist/deque/deque.d.ts.map +1 -0
- package/dist/deque/deque.js +62 -0
- package/dist/deque/deque.js.map +1 -0
- package/dist/deque/iasync-deque.d.ts +19 -0
- package/dist/deque/iasync-deque.d.ts.map +1 -0
- package/dist/deque/iasync-deque.js +2 -0
- package/dist/deque/iasync-deque.js.map +1 -0
- package/dist/deque/iasync-lossy-deque.d.ts +11 -0
- package/dist/deque/iasync-lossy-deque.d.ts.map +1 -0
- package/dist/deque/iasync-lossy-deque.js +2 -0
- package/dist/deque/iasync-lossy-deque.js.map +1 -0
- package/dist/deque/ideque-storage.d.ts +6 -0
- package/dist/deque/ideque-storage.d.ts.map +1 -0
- package/dist/deque/ideque-storage.js +2 -0
- package/dist/deque/ideque-storage.js.map +1 -0
- package/dist/deque/ideque.d.ts +72 -0
- package/dist/deque/ideque.d.ts.map +1 -0
- package/dist/deque/ideque.js +2 -0
- package/dist/deque/ideque.js.map +1 -0
- package/dist/deque/ilossy-deque.d.ts +42 -0
- package/dist/deque/ilossy-deque.d.ts.map +1 -0
- package/dist/deque/ilossy-deque.js +2 -0
- package/dist/deque/ilossy-deque.js.map +1 -0
- package/dist/deque/iwaitable-deque.d.ts +42 -0
- package/dist/deque/iwaitable-deque.d.ts.map +1 -0
- package/dist/deque/iwaitable-deque.js +2 -0
- package/dist/deque/iwaitable-deque.js.map +1 -0
- package/dist/disjoint-set/iasync-disjoint-set.d.ts +11 -0
- package/dist/disjoint-set/iasync-disjoint-set.d.ts.map +1 -0
- package/dist/disjoint-set/iasync-disjoint-set.js +2 -0
- package/dist/disjoint-set/iasync-disjoint-set.js.map +1 -0
- package/dist/disjoint-set/idisjoint-set.d.ts +11 -0
- package/dist/disjoint-set/idisjoint-set.d.ts.map +1 -0
- package/dist/disjoint-set/idisjoint-set.js +2 -0
- package/dist/disjoint-set/idisjoint-set.js.map +1 -0
- package/dist/disjoint-set/static-disjoint-set.d.ts +67 -0
- package/dist/disjoint-set/static-disjoint-set.d.ts.map +1 -0
- package/dist/disjoint-set/static-disjoint-set.js +146 -0
- package/dist/disjoint-set/static-disjoint-set.js.map +1 -0
- package/dist/fuzzy/fuzzy-map.d.ts +62 -0
- package/dist/fuzzy/fuzzy-map.d.ts.map +1 -0
- package/dist/fuzzy/fuzzy-map.js +91 -0
- package/dist/fuzzy/fuzzy-map.js.map +1 -0
- package/dist/fuzzy/fuzzy-multi-map.d.ts +62 -0
- package/dist/fuzzy/fuzzy-multi-map.d.ts.map +1 -0
- package/dist/fuzzy/fuzzy-multi-map.js +143 -0
- package/dist/fuzzy/fuzzy-multi-map.js.map +1 -0
- package/dist/graph/directed-graph.d.ts +78 -0
- package/dist/graph/directed-graph.d.ts.map +1 -0
- package/dist/graph/directed-graph.js +165 -0
- package/dist/graph/directed-graph.js.map +1 -0
- package/dist/graph/graph.d.ts +73 -0
- package/dist/graph/graph.d.ts.map +1 -0
- package/dist/graph/graph.js +145 -0
- package/dist/graph/graph.js.map +1 -0
- package/dist/graph/iasync-directed-graph.d.ts +10 -0
- package/dist/graph/iasync-directed-graph.d.ts.map +1 -0
- package/dist/graph/iasync-directed-graph.js +2 -0
- package/dist/graph/iasync-directed-graph.js.map +1 -0
- package/dist/graph/iasync-graph.d.ts +19 -0
- package/dist/graph/iasync-graph.d.ts.map +1 -0
- package/dist/graph/iasync-graph.js +2 -0
- package/dist/graph/iasync-graph.js.map +1 -0
- package/dist/graph/idirected-graph.d.ts +10 -0
- package/dist/graph/idirected-graph.d.ts.map +1 -0
- package/dist/graph/idirected-graph.js +2 -0
- package/dist/graph/idirected-graph.js.map +1 -0
- package/dist/graph/igraph.d.ts +25 -0
- package/dist/graph/igraph.d.ts.map +1 -0
- package/dist/graph/igraph.js +2 -0
- package/dist/graph/igraph.js.map +1 -0
- package/dist/heap/_array-heap.d.ts +12 -0
- package/dist/heap/_array-heap.d.ts.map +1 -0
- package/dist/heap/_array-heap.js +74 -0
- package/dist/heap/_array-heap.js.map +1 -0
- package/dist/heap/binary-heap.d.ts +29 -0
- package/dist/heap/binary-heap.d.ts.map +1 -0
- package/dist/heap/binary-heap.js +83 -0
- package/dist/heap/binary-heap.js.map +1 -0
- package/dist/heap/blocking-binary-heap.d.ts +34 -0
- package/dist/heap/blocking-binary-heap.d.ts.map +1 -0
- package/dist/heap/blocking-binary-heap.js +84 -0
- package/dist/heap/blocking-binary-heap.js.map +1 -0
- package/dist/heap/bounded-binary-heap.d.ts +42 -0
- package/dist/heap/bounded-binary-heap.d.ts.map +1 -0
- package/dist/heap/bounded-binary-heap.js +71 -0
- package/dist/heap/bounded-binary-heap.js.map +1 -0
- package/dist/heap/iasync-heap.d.ts +19 -0
- package/dist/heap/iasync-heap.d.ts.map +1 -0
- package/dist/heap/iasync-heap.js +2 -0
- package/dist/heap/iasync-heap.js.map +1 -0
- package/dist/heap/iasync-lossy-heap.d.ts +9 -0
- package/dist/heap/iasync-lossy-heap.d.ts.map +1 -0
- package/dist/heap/iasync-lossy-heap.js +2 -0
- package/dist/heap/iasync-lossy-heap.js.map +1 -0
- package/dist/heap/iheap-storage.d.ts +5 -0
- package/dist/heap/iheap-storage.d.ts.map +1 -0
- package/dist/heap/iheap-storage.js +2 -0
- package/dist/heap/iheap-storage.js.map +1 -0
- package/dist/heap/iheap.d.ts +51 -0
- package/dist/heap/iheap.d.ts.map +1 -0
- package/dist/heap/iheap.js +2 -0
- package/dist/heap/iheap.js.map +1 -0
- package/dist/heap/ilossy-heap.d.ts +28 -0
- package/dist/heap/ilossy-heap.d.ts.map +1 -0
- package/dist/heap/ilossy-heap.js +2 -0
- package/dist/heap/ilossy-heap.js.map +1 -0
- package/dist/heap/iwaitable-heap.d.ts +27 -0
- package/dist/heap/iwaitable-heap.d.ts.map +1 -0
- package/dist/heap/iwaitable-heap.js +2 -0
- package/dist/heap/iwaitable-heap.js.map +1 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +144 -0
- package/dist/index.js.map +1 -0
- package/dist/interval/iasync-interval-index.d.ts +8 -0
- package/dist/interval/iasync-interval-index.d.ts.map +1 -0
- package/dist/interval/iasync-interval-index.js +2 -0
- package/dist/interval/iasync-interval-index.js.map +1 -0
- package/dist/interval/iinterval-index.d.ts +8 -0
- package/dist/interval/iinterval-index.d.ts.map +1 -0
- package/dist/interval/iinterval-index.js +2 -0
- package/dist/interval/iinterval-index.js.map +1 -0
- package/dist/interval/static-interval-tree.d.ts +65 -0
- package/dist/interval/static-interval-tree.d.ts.map +1 -0
- package/dist/interval/static-interval-tree.js +170 -0
- package/dist/interval/static-interval-tree.js.map +1 -0
- package/dist/list/_bounded-list.d.ts +14 -0
- package/dist/list/_bounded-list.d.ts.map +1 -0
- package/dist/list/_bounded-list.js +23 -0
- package/dist/list/_bounded-list.js.map +1 -0
- package/dist/list/array-list.d.ts +56 -0
- package/dist/list/array-list.d.ts.map +1 -0
- package/dist/list/array-list.js +198 -0
- package/dist/list/array-list.js.map +1 -0
- package/dist/list/blocking-array-list.d.ts +55 -0
- package/dist/list/blocking-array-list.d.ts.map +1 -0
- package/dist/list/blocking-array-list.js +190 -0
- package/dist/list/blocking-array-list.js.map +1 -0
- package/dist/list/blocking-circular-array-list.d.ts +61 -0
- package/dist/list/blocking-circular-array-list.d.ts.map +1 -0
- package/dist/list/blocking-circular-array-list.js +211 -0
- package/dist/list/blocking-circular-array-list.js.map +1 -0
- package/dist/list/blocking-doubly-linked-list.d.ts +59 -0
- package/dist/list/blocking-doubly-linked-list.d.ts.map +1 -0
- package/dist/list/blocking-doubly-linked-list.js +211 -0
- package/dist/list/blocking-doubly-linked-list.js.map +1 -0
- package/dist/list/blocking-linked-list.d.ts +50 -0
- package/dist/list/blocking-linked-list.d.ts.map +1 -0
- package/dist/list/blocking-linked-list.js +172 -0
- package/dist/list/blocking-linked-list.js.map +1 -0
- package/dist/list/bounded-array-list.d.ts +47 -0
- package/dist/list/bounded-array-list.d.ts.map +1 -0
- package/dist/list/bounded-array-list.js +108 -0
- package/dist/list/bounded-array-list.js.map +1 -0
- package/dist/list/bounded-circular-array-list.d.ts +54 -0
- package/dist/list/bounded-circular-array-list.d.ts.map +1 -0
- package/dist/list/bounded-circular-array-list.js +123 -0
- package/dist/list/bounded-circular-array-list.js.map +1 -0
- package/dist/list/bounded-doubly-linked-list.d.ts +52 -0
- package/dist/list/bounded-doubly-linked-list.d.ts.map +1 -0
- package/dist/list/bounded-doubly-linked-list.js +123 -0
- package/dist/list/bounded-doubly-linked-list.js.map +1 -0
- package/dist/list/bounded-linked-list.d.ts +42 -0
- package/dist/list/bounded-linked-list.d.ts.map +1 -0
- package/dist/list/bounded-linked-list.js +97 -0
- package/dist/list/bounded-linked-list.js.map +1 -0
- package/dist/list/circular-array-list.d.ts +77 -0
- package/dist/list/circular-array-list.d.ts.map +1 -0
- package/dist/list/circular-array-list.js +322 -0
- package/dist/list/circular-array-list.js.map +1 -0
- package/dist/list/doubly-linked-list.d.ts +113 -0
- package/dist/list/doubly-linked-list.d.ts.map +1 -0
- package/dist/list/doubly-linked-list.js +453 -0
- package/dist/list/doubly-linked-list.js.map +1 -0
- package/dist/list/iasync-list.d.ts +17 -0
- package/dist/list/iasync-list.d.ts.map +1 -0
- package/dist/list/iasync-list.js +2 -0
- package/dist/list/iasync-list.js.map +1 -0
- package/dist/list/ilist-storage.d.ts +9 -0
- package/dist/list/ilist-storage.d.ts.map +1 -0
- package/dist/list/ilist-storage.js +2 -0
- package/dist/list/ilist-storage.js.map +1 -0
- package/dist/list/ilist.d.ts +118 -0
- package/dist/list/ilist.d.ts.map +1 -0
- package/dist/list/ilist.js +8 -0
- package/dist/list/ilist.js.map +1 -0
- package/dist/list/iwaitable-list.d.ts +47 -0
- package/dist/list/iwaitable-list.d.ts.map +1 -0
- package/dist/list/iwaitable-list.js +2 -0
- package/dist/list/iwaitable-list.js.map +1 -0
- package/dist/list/linked-list.d.ts +102 -0
- package/dist/list/linked-list.d.ts.map +1 -0
- package/dist/list/linked-list.js +377 -0
- package/dist/list/linked-list.js.map +1 -0
- package/dist/map/iasync-map.d.ts +13 -0
- package/dist/map/iasync-map.d.ts.map +1 -0
- package/dist/map/iasync-map.js +2 -0
- package/dist/map/iasync-map.js.map +1 -0
- package/dist/map/iasync-replacing-map.d.ts +8 -0
- package/dist/map/iasync-replacing-map.d.ts.map +1 -0
- package/dist/map/iasync-replacing-map.js +2 -0
- package/dist/map/iasync-replacing-map.js.map +1 -0
- package/dist/map/iasync-sorted-map.d.ts +12 -0
- package/dist/map/iasync-sorted-map.d.ts.map +1 -0
- package/dist/map/iasync-sorted-map.js +2 -0
- package/dist/map/iasync-sorted-map.js.map +1 -0
- package/dist/map/imap.d.ts +20 -0
- package/dist/map/imap.d.ts.map +1 -0
- package/dist/map/imap.js +2 -0
- package/dist/map/imap.js.map +1 -0
- package/dist/map/ireplacing-map.d.ts +19 -0
- package/dist/map/ireplacing-map.d.ts.map +1 -0
- package/dist/map/ireplacing-map.js +2 -0
- package/dist/map/ireplacing-map.js.map +1 -0
- package/dist/map/isorted-map.d.ts +23 -0
- package/dist/map/isorted-map.d.ts.map +1 -0
- package/dist/map/isorted-map.js +2 -0
- package/dist/map/isorted-map.js.map +1 -0
- package/dist/multimap/iasync-multimap.d.ts +16 -0
- package/dist/multimap/iasync-multimap.d.ts.map +1 -0
- package/dist/multimap/iasync-multimap.js +2 -0
- package/dist/multimap/iasync-multimap.js.map +1 -0
- package/dist/multimap/imultimap.d.ts +28 -0
- package/dist/multimap/imultimap.d.ts.map +1 -0
- package/dist/multimap/imultimap.js +2 -0
- package/dist/multimap/imultimap.js.map +1 -0
- package/dist/multimap/inverted-index.d.ts +90 -0
- package/dist/multimap/inverted-index.d.ts.map +1 -0
- package/dist/multimap/inverted-index.js +234 -0
- package/dist/multimap/inverted-index.js.map +1 -0
- package/dist/multimap/multi-map.d.ts +63 -0
- package/dist/multimap/multi-map.d.ts.map +1 -0
- package/dist/multimap/multi-map.js +146 -0
- package/dist/multimap/multi-map.js.map +1 -0
- package/dist/multiset/iasync-multiset.d.ts +13 -0
- package/dist/multiset/iasync-multiset.d.ts.map +1 -0
- package/dist/multiset/iasync-multiset.js +2 -0
- package/dist/multiset/iasync-multiset.js.map +1 -0
- package/dist/multiset/imultiset.d.ts +22 -0
- package/dist/multiset/imultiset.d.ts.map +1 -0
- package/dist/multiset/imultiset.js +2 -0
- package/dist/multiset/imultiset.js.map +1 -0
- package/dist/multiset/multi-set.d.ts +66 -0
- package/dist/multiset/multi-set.d.ts.map +1 -0
- package/dist/multiset/multi-set.js +124 -0
- package/dist/multiset/multi-set.js.map +1 -0
- package/dist/ordered-range/iasync-ordered-range.d.ts +15 -0
- package/dist/ordered-range/iasync-ordered-range.d.ts.map +1 -0
- package/dist/ordered-range/iasync-ordered-range.js +2 -0
- package/dist/ordered-range/iasync-ordered-range.js.map +1 -0
- package/dist/ordered-range/iordered-range.d.ts +28 -0
- package/dist/ordered-range/iordered-range.d.ts.map +1 -0
- package/dist/ordered-range/iordered-range.js +2 -0
- package/dist/ordered-range/iordered-range.js.map +1 -0
- package/dist/priority-queue/blocking-priority-queue.d.ts +37 -0
- package/dist/priority-queue/blocking-priority-queue.d.ts.map +1 -0
- package/dist/priority-queue/blocking-priority-queue.js +87 -0
- package/dist/priority-queue/blocking-priority-queue.js.map +1 -0
- package/dist/priority-queue/bounded-priority-queue.d.ts +45 -0
- package/dist/priority-queue/bounded-priority-queue.d.ts.map +1 -0
- package/dist/priority-queue/bounded-priority-queue.js +71 -0
- package/dist/priority-queue/bounded-priority-queue.js.map +1 -0
- package/dist/priority-queue/iasync-lossy-priority-queue.d.ts +9 -0
- package/dist/priority-queue/iasync-lossy-priority-queue.d.ts.map +1 -0
- package/dist/priority-queue/iasync-lossy-priority-queue.js +2 -0
- package/dist/priority-queue/iasync-lossy-priority-queue.js.map +1 -0
- package/dist/priority-queue/iasync-priority-queue.d.ts +21 -0
- package/dist/priority-queue/iasync-priority-queue.d.ts.map +1 -0
- package/dist/priority-queue/iasync-priority-queue.js +2 -0
- package/dist/priority-queue/iasync-priority-queue.js.map +1 -0
- package/dist/priority-queue/ilossy-priority-queue.d.ts +29 -0
- package/dist/priority-queue/ilossy-priority-queue.d.ts.map +1 -0
- package/dist/priority-queue/ilossy-priority-queue.js +2 -0
- package/dist/priority-queue/ilossy-priority-queue.js.map +1 -0
- package/dist/priority-queue/ipriority-queue.d.ts +71 -0
- package/dist/priority-queue/ipriority-queue.d.ts.map +1 -0
- package/dist/priority-queue/ipriority-queue.js +2 -0
- package/dist/priority-queue/ipriority-queue.js.map +1 -0
- package/dist/priority-queue/iwaitable-priority-queue.d.ts +27 -0
- package/dist/priority-queue/iwaitable-priority-queue.d.ts.map +1 -0
- package/dist/priority-queue/iwaitable-priority-queue.js +2 -0
- package/dist/priority-queue/iwaitable-priority-queue.js.map +1 -0
- package/dist/priority-queue/priority-queue.d.ts +49 -0
- package/dist/priority-queue/priority-queue.d.ts.map +1 -0
- package/dist/priority-queue/priority-queue.js +134 -0
- package/dist/priority-queue/priority-queue.js.map +1 -0
- package/dist/probabilistic/bloom-filter.d.ts +92 -0
- package/dist/probabilistic/bloom-filter.d.ts.map +1 -0
- package/dist/probabilistic/bloom-filter.js +156 -0
- package/dist/probabilistic/bloom-filter.js.map +1 -0
- package/dist/probabilistic/count-min-sketch.d.ts +76 -0
- package/dist/probabilistic/count-min-sketch.d.ts.map +1 -0
- package/dist/probabilistic/count-min-sketch.js +119 -0
- package/dist/probabilistic/count-min-sketch.js.map +1 -0
- package/dist/probabilistic/cuckoo-filter.d.ts +106 -0
- package/dist/probabilistic/cuckoo-filter.d.ts.map +1 -0
- package/dist/probabilistic/cuckoo-filter.js +214 -0
- package/dist/probabilistic/cuckoo-filter.js.map +1 -0
- package/dist/probabilistic/hash32.d.ts +28 -0
- package/dist/probabilistic/hash32.d.ts.map +1 -0
- package/dist/probabilistic/hash32.js +2 -0
- package/dist/probabilistic/hash32.js.map +1 -0
- package/dist/probabilistic/hyper-log-log.d.ts +74 -0
- package/dist/probabilistic/hyper-log-log.d.ts.map +1 -0
- package/dist/probabilistic/hyper-log-log.js +134 -0
- package/dist/probabilistic/hyper-log-log.js.map +1 -0
- package/dist/probabilistic/top-k.d.ts +62 -0
- package/dist/probabilistic/top-k.d.ts.map +1 -0
- package/dist/probabilistic/top-k.js +104 -0
- package/dist/probabilistic/top-k.js.map +1 -0
- package/dist/queue/blocking-queue.d.ts +32 -0
- package/dist/queue/blocking-queue.d.ts.map +1 -0
- package/dist/queue/blocking-queue.js +70 -0
- package/dist/queue/blocking-queue.js.map +1 -0
- package/dist/queue/bounded-queue.d.ts +47 -0
- package/dist/queue/bounded-queue.d.ts.map +1 -0
- package/dist/queue/bounded-queue.js +64 -0
- package/dist/queue/bounded-queue.js.map +1 -0
- package/dist/queue/iasync-lossy-queue.d.ts +15 -0
- package/dist/queue/iasync-lossy-queue.d.ts.map +1 -0
- package/dist/queue/iasync-lossy-queue.js +2 -0
- package/dist/queue/iasync-lossy-queue.js.map +1 -0
- package/dist/queue/iasync-queue.d.ts +17 -0
- package/dist/queue/iasync-queue.d.ts.map +1 -0
- package/dist/queue/iasync-queue.js +2 -0
- package/dist/queue/iasync-queue.js.map +1 -0
- package/dist/queue/ilossy-queue.d.ts +28 -0
- package/dist/queue/ilossy-queue.d.ts.map +1 -0
- package/dist/queue/ilossy-queue.js +2 -0
- package/dist/queue/ilossy-queue.js.map +1 -0
- package/dist/queue/iqueue-storage.d.ts +5 -0
- package/dist/queue/iqueue-storage.d.ts.map +1 -0
- package/dist/queue/iqueue-storage.js +2 -0
- package/dist/queue/iqueue-storage.js.map +1 -0
- package/dist/queue/iqueue.d.ts +41 -0
- package/dist/queue/iqueue.d.ts.map +1 -0
- package/dist/queue/iqueue.js +2 -0
- package/dist/queue/iqueue.js.map +1 -0
- package/dist/queue/iwaitable-queue.d.ts +27 -0
- package/dist/queue/iwaitable-queue.d.ts.map +1 -0
- package/dist/queue/iwaitable-queue.js +2 -0
- package/dist/queue/iwaitable-queue.js.map +1 -0
- package/dist/queue/lossy-queue.d.ts +33 -0
- package/dist/queue/lossy-queue.d.ts.map +1 -0
- package/dist/queue/lossy-queue.js +75 -0
- package/dist/queue/lossy-queue.js.map +1 -0
- package/dist/queue/queue.d.ts +27 -0
- package/dist/queue/queue.d.ts.map +1 -0
- package/dist/queue/queue.js +43 -0
- package/dist/queue/queue.js.map +1 -0
- package/dist/set/enhanced-set.d.ts +68 -0
- package/dist/set/enhanced-set.d.ts.map +1 -0
- package/dist/set/enhanced-set.js +169 -0
- package/dist/set/enhanced-set.js.map +1 -0
- package/dist/set/iasync-replacing-set.d.ts +8 -0
- package/dist/set/iasync-replacing-set.d.ts.map +1 -0
- package/dist/set/iasync-replacing-set.js +2 -0
- package/dist/set/iasync-replacing-set.js.map +1 -0
- package/dist/set/iasync-set.d.ts +16 -0
- package/dist/set/iasync-set.d.ts.map +1 -0
- package/dist/set/iasync-set.js +2 -0
- package/dist/set/iasync-set.js.map +1 -0
- package/dist/set/iasync-sorted-set.d.ts +7 -0
- package/dist/set/iasync-sorted-set.d.ts.map +1 -0
- package/dist/set/iasync-sorted-set.js +2 -0
- package/dist/set/iasync-sorted-set.js.map +1 -0
- package/dist/set/ireplacing-set.d.ts +24 -0
- package/dist/set/ireplacing-set.d.ts.map +1 -0
- package/dist/set/ireplacing-set.js +2 -0
- package/dist/set/ireplacing-set.js.map +1 -0
- package/dist/set/iset.d.ts +30 -0
- package/dist/set/iset.d.ts.map +1 -0
- package/dist/set/iset.js +2 -0
- package/dist/set/iset.js.map +1 -0
- package/dist/set/isorted-set.d.ts +7 -0
- package/dist/set/isorted-set.d.ts.map +1 -0
- package/dist/set/isorted-set.js +2 -0
- package/dist/set/isorted-set.js.map +1 -0
- package/dist/similarity/iasync-similarity-index.d.ts +8 -0
- package/dist/similarity/iasync-similarity-index.d.ts.map +1 -0
- package/dist/similarity/iasync-similarity-index.js +2 -0
- package/dist/similarity/iasync-similarity-index.js.map +1 -0
- package/dist/similarity/isimilarity-index.d.ts +8 -0
- package/dist/similarity/isimilarity-index.d.ts.map +1 -0
- package/dist/similarity/isimilarity-index.js +2 -0
- package/dist/similarity/isimilarity-index.js.map +1 -0
- package/dist/similarity/vp-tree.d.ts +71 -0
- package/dist/similarity/vp-tree.d.ts.map +1 -0
- package/dist/similarity/vp-tree.js +172 -0
- package/dist/similarity/vp-tree.js.map +1 -0
- package/dist/stack/blocking-stack.d.ts +30 -0
- package/dist/stack/blocking-stack.d.ts.map +1 -0
- package/dist/stack/blocking-stack.js +68 -0
- package/dist/stack/blocking-stack.js.map +1 -0
- package/dist/stack/bounded-stack.d.ts +38 -0
- package/dist/stack/bounded-stack.d.ts.map +1 -0
- package/dist/stack/bounded-stack.js +55 -0
- package/dist/stack/bounded-stack.js.map +1 -0
- package/dist/stack/iasync-lossy-stack.d.ts +9 -0
- package/dist/stack/iasync-lossy-stack.d.ts.map +1 -0
- package/dist/stack/iasync-lossy-stack.js +2 -0
- package/dist/stack/iasync-lossy-stack.js.map +1 -0
- package/dist/stack/iasync-stack.d.ts +15 -0
- package/dist/stack/iasync-stack.d.ts.map +1 -0
- package/dist/stack/iasync-stack.js +2 -0
- package/dist/stack/iasync-stack.js.map +1 -0
- package/dist/stack/ilossy-stack.d.ts +28 -0
- package/dist/stack/ilossy-stack.d.ts.map +1 -0
- package/dist/stack/ilossy-stack.js +2 -0
- package/dist/stack/ilossy-stack.js.map +1 -0
- package/dist/stack/istack-storage.d.ts +5 -0
- package/dist/stack/istack-storage.d.ts.map +1 -0
- package/dist/stack/istack-storage.js +2 -0
- package/dist/stack/istack-storage.js.map +1 -0
- package/dist/stack/istack.d.ts +41 -0
- package/dist/stack/istack.d.ts.map +1 -0
- package/dist/stack/istack.js +2 -0
- package/dist/stack/istack.js.map +1 -0
- package/dist/stack/iwaitable-stack.d.ts +26 -0
- package/dist/stack/iwaitable-stack.d.ts.map +1 -0
- package/dist/stack/iwaitable-stack.js +2 -0
- package/dist/stack/iwaitable-stack.js.map +1 -0
- package/dist/stack/stack.d.ts +26 -0
- package/dist/stack/stack.d.ts.map +1 -0
- package/dist/stack/stack.js +42 -0
- package/dist/stack/stack.js.map +1 -0
- package/dist/storage/iindexed-storage.d.ts +8 -0
- package/dist/storage/iindexed-storage.d.ts.map +1 -0
- package/dist/storage/iindexed-storage.js +2 -0
- package/dist/storage/iindexed-storage.js.map +1 -0
- package/dist/storage/ipop-back-storage.d.ts +8 -0
- package/dist/storage/ipop-back-storage.d.ts.map +1 -0
- package/dist/storage/ipop-back-storage.js +2 -0
- package/dist/storage/ipop-back-storage.js.map +1 -0
- package/dist/storage/ipop-front-storage.d.ts +8 -0
- package/dist/storage/ipop-front-storage.d.ts.map +1 -0
- package/dist/storage/ipop-front-storage.js +2 -0
- package/dist/storage/ipop-front-storage.js.map +1 -0
- package/dist/storage/ipush-back-storage.d.ts +7 -0
- package/dist/storage/ipush-back-storage.d.ts.map +1 -0
- package/dist/storage/ipush-back-storage.js +2 -0
- package/dist/storage/ipush-back-storage.js.map +1 -0
- package/dist/storage/ipush-front-storage.d.ts +7 -0
- package/dist/storage/ipush-front-storage.d.ts.map +1 -0
- package/dist/storage/ipush-front-storage.js +2 -0
- package/dist/storage/ipush-front-storage.js.map +1 -0
- package/dist/storage/istorage.d.ts +13 -0
- package/dist/storage/istorage.d.ts.map +1 -0
- package/dist/storage/istorage.js +2 -0
- package/dist/storage/istorage.js.map +1 -0
- package/dist/storage/ring-vector.d.ts +59 -0
- package/dist/storage/ring-vector.d.ts.map +1 -0
- package/dist/storage/ring-vector.js +192 -0
- package/dist/storage/ring-vector.js.map +1 -0
- package/dist/storage/segmented-ring-vector.d.ts +42 -0
- package/dist/storage/segmented-ring-vector.d.ts.map +1 -0
- package/dist/storage/segmented-ring-vector.js +142 -0
- package/dist/storage/segmented-ring-vector.js.map +1 -0
- package/dist/storage/typed-vector.d.ts +67 -0
- package/dist/storage/typed-vector.d.ts.map +1 -0
- package/dist/storage/typed-vector.js +123 -0
- package/dist/storage/typed-vector.js.map +1 -0
- package/dist/substring/generalized-suffix-array.d.ts +43 -0
- package/dist/substring/generalized-suffix-array.d.ts.map +1 -0
- package/dist/substring/generalized-suffix-array.js +100 -0
- package/dist/substring/generalized-suffix-array.js.map +1 -0
- package/dist/substring/iasync-substring-index.d.ts +6 -0
- package/dist/substring/iasync-substring-index.d.ts.map +1 -0
- package/dist/substring/iasync-substring-index.js +2 -0
- package/dist/substring/iasync-substring-index.js.map +1 -0
- package/dist/substring/isubstring-index.d.ts +6 -0
- package/dist/substring/isubstring-index.d.ts.map +1 -0
- package/dist/substring/isubstring-index.js +2 -0
- package/dist/substring/isubstring-index.js.map +1 -0
- package/dist/substring/suffix-array.d.ts +45 -0
- package/dist/substring/suffix-array.d.ts.map +1 -0
- package/dist/substring/suffix-array.js +164 -0
- package/dist/substring/suffix-array.js.map +1 -0
- package/dist/tree/avl-tree.d.ts +43 -0
- package/dist/tree/avl-tree.d.ts.map +1 -0
- package/dist/tree/avl-tree.js +98 -0
- package/dist/tree/avl-tree.js.map +1 -0
- package/dist/tree/binary-search-tree.d.ts +94 -0
- package/dist/tree/binary-search-tree.d.ts.map +1 -0
- package/dist/tree/binary-search-tree.js +303 -0
- package/dist/tree/binary-search-tree.js.map +1 -0
- package/dist/tree/iavl-tree-node.d.ts +6 -0
- package/dist/tree/iavl-tree-node.d.ts.map +1 -0
- package/dist/tree/iavl-tree-node.js +2 -0
- package/dist/tree/iavl-tree-node.js.map +1 -0
- package/dist/tree/isearch-tree.d.ts +14 -0
- package/dist/tree/isearch-tree.d.ts.map +1 -0
- package/dist/tree/isearch-tree.js +2 -0
- package/dist/tree/isearch-tree.js.map +1 -0
- package/dist/trie/iasync-trie.d.ts +9 -0
- package/dist/trie/iasync-trie.d.ts.map +1 -0
- package/dist/trie/iasync-trie.js +2 -0
- package/dist/trie/iasync-trie.js.map +1 -0
- package/dist/trie/itrie.d.ts +9 -0
- package/dist/trie/itrie.d.ts.map +1 -0
- package/dist/trie/itrie.js +2 -0
- package/dist/trie/itrie.js.map +1 -0
- package/dist/trie/trie.d.ts +93 -0
- package/dist/trie/trie.d.ts.map +1 -0
- package/dist/trie/trie.js +190 -0
- package/dist/trie/trie.js.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/** Zero marks an empty slot, so a fingerprint never is. */
|
|
2
|
+
const EMPTY = 0;
|
|
3
|
+
/**
|
|
4
|
+
* Approximate set membership **with deletion**, the thing a Bloom filter cannot
|
|
5
|
+
* do.
|
|
6
|
+
*
|
|
7
|
+
* Stores a short fingerprint of each item in one of two candidate buckets. The
|
|
8
|
+
* second bucket is derived from the first by XOR with the fingerprint's hash,
|
|
9
|
+
* which is what makes the relation symmetric: from either bucket the other can
|
|
10
|
+
* be computed without the item, so a fingerprint can be relocated during
|
|
11
|
+
* insertion and found again during lookup.
|
|
12
|
+
*
|
|
13
|
+
* Deleting removes one copy of a fingerprint. **Only delete items actually
|
|
14
|
+
* inserted**: removing a fingerprint that a different item happens to share
|
|
15
|
+
* would introduce a false negative, which this structure otherwise never has.
|
|
16
|
+
*
|
|
17
|
+
* `add` can fail. When both candidate buckets are full, it evicts and relocates
|
|
18
|
+
* up to `maxKicks` times; past that the filter is too loaded and `add` returns
|
|
19
|
+
* `false`. A failed insert is **rolled back**, so the filter still holds
|
|
20
|
+
* exactly what it held before — no previously inserted item is ever displaced
|
|
21
|
+
* into nothing.
|
|
22
|
+
*
|
|
23
|
+
* Time complexity: O(1) for `has` and `delete` — exactly two bucket probes.
|
|
24
|
+
* `add` is O(1) amortised, degrading as the load factor approaches capacity.
|
|
25
|
+
*
|
|
26
|
+
* @template T The item type.
|
|
27
|
+
*/
|
|
28
|
+
export class CuckooFilter {
|
|
29
|
+
fingerprints;
|
|
30
|
+
hash;
|
|
31
|
+
bucketCount;
|
|
32
|
+
maxKicks;
|
|
33
|
+
/** Fingerprints per bucket. */
|
|
34
|
+
bucketSize;
|
|
35
|
+
size = 0;
|
|
36
|
+
/** Deterministic eviction choice, so a failure reproduces. */
|
|
37
|
+
evictionSeed = 0x9e3779b9;
|
|
38
|
+
constructor(iterable, options) {
|
|
39
|
+
const { expectedItems } = options;
|
|
40
|
+
const bucketSize = options.bucketSize ?? 4;
|
|
41
|
+
if (!Number.isInteger(expectedItems) || expectedItems < 1) {
|
|
42
|
+
throw new RangeError(`Expected items must be a positive integer, got ${expectedItems}`);
|
|
43
|
+
}
|
|
44
|
+
if (!Number.isInteger(bucketSize) || bucketSize < 1 || bucketSize > 8) {
|
|
45
|
+
throw new RangeError(`Bucket size must be an integer in [1, 8], got ${bucketSize}`);
|
|
46
|
+
}
|
|
47
|
+
this.hash = options.hash;
|
|
48
|
+
this.bucketSize = bucketSize;
|
|
49
|
+
this.maxKicks = options.maxKicks ?? 500;
|
|
50
|
+
// A power of two, so the XOR that pairs the two buckets stays in range.
|
|
51
|
+
const needed = Math.ceil(expectedItems / bucketSize / 0.95);
|
|
52
|
+
this.bucketCount = 2 ** Math.max(1, Math.ceil(Math.log2(needed)));
|
|
53
|
+
this.fingerprints = new Uint8Array(this.bucketCount * bucketSize);
|
|
54
|
+
if (iterable) {
|
|
55
|
+
this.addAll(iterable);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** How many fingerprints are stored. */
|
|
59
|
+
count() {
|
|
60
|
+
return this.size;
|
|
61
|
+
}
|
|
62
|
+
/** The fraction of slots in use. */
|
|
63
|
+
loadFactor() {
|
|
64
|
+
return this.size / this.fingerprints.length;
|
|
65
|
+
}
|
|
66
|
+
clear() {
|
|
67
|
+
this.fingerprints.fill(EMPTY);
|
|
68
|
+
this.size = 0;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* O(1) amortised.
|
|
72
|
+
*
|
|
73
|
+
* @returns `false` if the filter was too full to place the item, after
|
|
74
|
+
* `maxKicks` relocations. The filter is rolled back to exactly its prior
|
|
75
|
+
* contents, so nothing inserted earlier is lost.
|
|
76
|
+
*/
|
|
77
|
+
add(item) {
|
|
78
|
+
const fingerprint = this.fingerprintOf(item);
|
|
79
|
+
const first = this.bucketOf(item);
|
|
80
|
+
const second = this.alternateBucket(first, fingerprint);
|
|
81
|
+
if (this.placeIn(first, fingerprint) || this.placeIn(second, fingerprint)) {
|
|
82
|
+
this.size++;
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
let bucket = (this.evictionSeed & 1) === 0 ? first : second;
|
|
86
|
+
let carried = fingerprint;
|
|
87
|
+
// Every slot overwritten, so a failed insert can be rolled back. Without
|
|
88
|
+
// this the last displaced fingerprint has nowhere to go and is dropped,
|
|
89
|
+
// which would turn a full filter into a false negative for an item that was
|
|
90
|
+
// legitimately inserted earlier.
|
|
91
|
+
const trail = [];
|
|
92
|
+
for (let kick = 0; kick < this.maxKicks; kick++) {
|
|
93
|
+
const slot = bucket * this.bucketSize + this.nextEvictionSlot();
|
|
94
|
+
const displaced = this.fingerprints[slot];
|
|
95
|
+
trail.push({ slot, previous: displaced });
|
|
96
|
+
this.fingerprints[slot] = carried;
|
|
97
|
+
carried = displaced;
|
|
98
|
+
bucket = this.alternateBucket(bucket, carried);
|
|
99
|
+
if (this.placeIn(bucket, carried)) {
|
|
100
|
+
this.size++;
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (let index = trail.length - 1; index >= 0; index--) {
|
|
105
|
+
const { slot, previous } = trail[index];
|
|
106
|
+
this.fingerprints[slot] = previous;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @returns How many items were added. A shortfall means the filter filled up
|
|
112
|
+
* — see `add`.
|
|
113
|
+
*/
|
|
114
|
+
addAll(items) {
|
|
115
|
+
let added = 0;
|
|
116
|
+
for (const item of items) {
|
|
117
|
+
if (this.add(item)) {
|
|
118
|
+
added++;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return added;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* O(1), two bucket probes.
|
|
125
|
+
*
|
|
126
|
+
* @returns `false` means the item is **definitely** absent, provided only
|
|
127
|
+
* inserted items were ever deleted. `true` means probably present.
|
|
128
|
+
*/
|
|
129
|
+
has(item) {
|
|
130
|
+
const fingerprint = this.fingerprintOf(item);
|
|
131
|
+
const first = this.bucketOf(item);
|
|
132
|
+
return (this.bucketHolds(first, fingerprint) ||
|
|
133
|
+
this.bucketHolds(this.alternateBucket(first, fingerprint), fingerprint));
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Removes one copy of the item's fingerprint. O(1).
|
|
137
|
+
*
|
|
138
|
+
* @returns `true` if a matching fingerprint was removed. Deleting an item
|
|
139
|
+
* never inserted can remove a colliding item's fingerprint instead, which
|
|
140
|
+
* is the one way this filter can produce a false negative.
|
|
141
|
+
*/
|
|
142
|
+
delete(item) {
|
|
143
|
+
const fingerprint = this.fingerprintOf(item);
|
|
144
|
+
const first = this.bucketOf(item);
|
|
145
|
+
if (this.removeFrom(first, fingerprint)) {
|
|
146
|
+
this.size--;
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
if (this.removeFrom(this.alternateBucket(first, fingerprint), fingerprint)) {
|
|
150
|
+
this.size--;
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
/** A non-zero byte: zero is reserved for "empty slot". */
|
|
156
|
+
fingerprintOf(item) {
|
|
157
|
+
return ((this.hash(item, 1) >>> 0) % 255) + 1;
|
|
158
|
+
}
|
|
159
|
+
bucketOf(item) {
|
|
160
|
+
return (this.hash(item, 0) >>> 0) & (this.bucketCount - 1);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The partner of `bucket` for this fingerprint.
|
|
164
|
+
*
|
|
165
|
+
* Must be an involution — applying it twice returns the original — or a
|
|
166
|
+
* relocated fingerprint becomes unfindable. That is why `bucketCount` is a
|
|
167
|
+
* power of two and the hash is **masked** rather than reduced modulo: XOR is
|
|
168
|
+
* self-inverse within a fixed bit width, but `(b ^ h) % n` is not, because
|
|
169
|
+
* the reduction discards the high bits the second XOR would need.
|
|
170
|
+
*/
|
|
171
|
+
alternateBucket(bucket, fingerprint) {
|
|
172
|
+
const mask = this.bucketCount - 1;
|
|
173
|
+
return (bucket ^ (Math.imul(fingerprint, 0x5bd1e995) & mask)) & mask;
|
|
174
|
+
}
|
|
175
|
+
placeIn(bucket, fingerprint) {
|
|
176
|
+
const base = bucket * this.bucketSize;
|
|
177
|
+
for (let slot = 0; slot < this.bucketSize; slot++) {
|
|
178
|
+
if (this.fingerprints[base + slot] === EMPTY) {
|
|
179
|
+
this.fingerprints[base + slot] = fingerprint;
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
bucketHolds(bucket, fingerprint) {
|
|
186
|
+
const base = bucket * this.bucketSize;
|
|
187
|
+
for (let slot = 0; slot < this.bucketSize; slot++) {
|
|
188
|
+
if (this.fingerprints[base + slot] === fingerprint) {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
removeFrom(bucket, fingerprint) {
|
|
195
|
+
const base = bucket * this.bucketSize;
|
|
196
|
+
for (let slot = 0; slot < this.bucketSize; slot++) {
|
|
197
|
+
if (this.fingerprints[base + slot] === fingerprint) {
|
|
198
|
+
this.fingerprints[base + slot] = EMPTY;
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
/** Xorshift32, so eviction is spread but reproducible. */
|
|
205
|
+
nextEvictionSlot() {
|
|
206
|
+
let state = this.evictionSeed;
|
|
207
|
+
state ^= state << 13;
|
|
208
|
+
state ^= state >>> 17;
|
|
209
|
+
state ^= state << 5;
|
|
210
|
+
this.evictionSeed = state | 0;
|
|
211
|
+
return Math.abs(state) % this.bucketSize;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=cuckoo-filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cuckoo-filter.js","sourceRoot":"","sources":["../../src/probabilistic/cuckoo-filter.ts"],"names":[],"mappings":"AAmBA,2DAA2D;AAC3D,MAAM,KAAK,GAAG,CAAC,CAAC;AAEhB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAO,YAAY;IACP,YAAY,CAAa;IACzB,IAAI,CAAY;IAChB,WAAW,CAAS;IACpB,QAAQ,CAAS;IAElC,+BAA+B;IACtB,UAAU,CAAS;IAEpB,IAAI,GAAG,CAAC,CAAC;IACjB,8DAA8D;IACtD,YAAY,GAAG,UAAU,CAAC;IAElC,YACC,QAAiC,EACjC,OAA+B;QAE/B,MAAM,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC;QAClC,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,CAAC,CAAC;QAE3C,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,IAAI,aAAa,GAAG,CAAC,EAAE,CAAC;YAC3D,MAAM,IAAI,UAAU,CACnB,kDAAkD,aAAa,EAAE,CACjE,CAAC;QACH,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,UAAU,GAAG,CAAC,IAAI,UAAU,GAA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3
|
+
* Hashes an item to a 32-bit unsigned integer.
|
|
4
|
+
*
|
|
5
|
+
* **Required, never defaulted**: hashes — `murmur3_32`, `xxhash_32`, `fnv_32`,
|
|
6
|
+
* `djb2` — all take a `Uint8Array`, and there is no canonical way to turn an
|
|
7
|
+
* arbitrary `T` into bytes. Only the caller knows how its items should be
|
|
8
|
+
* encoded, so only the caller can close that gap:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { murmur3_32 } from "@ac-kit/noncrypto-hash";
|
|
12
|
+
*
|
|
13
|
+
* const encoder = new TextEncoder();
|
|
14
|
+
* const hash: Hash32<string> = (item, seed) =>
|
|
15
|
+
* murmur3_32(encoder.encode(item), seed);
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* The quality of the estimate is the quality of this function. It must
|
|
19
|
+
* distribute uniformly and avalanche — a weak hash does not merely degrade the
|
|
20
|
+
* false-positive rate, it invalidates the arithmetic these structures are
|
|
21
|
+
* derived from.
|
|
22
|
+
*
|
|
23
|
+
* @param item The item to hash.
|
|
24
|
+
* @param seed A per-call seed. The same `(item, seed)` pair must always give
|
|
25
|
+
* the same result.
|
|
26
|
+
*/
|
|
27
|
+
export type Hash32<T> = Callable<[item: T, seed: number], number>;
|
|
28
|
+
//# sourceMappingURL=hash32.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash32.d.ts","sourceRoot":"","sources":["../../src/probabilistic/hash32.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash32.js","sourceRoot":"","sources":["../../src/probabilistic/hash32.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Hash32 } from "./hash32.js";
|
|
2
|
+
export type HyperLogLogOptions<T> = {
|
|
3
|
+
/** See {@link Hash32}: required, and why it cannot be defaulted. */
|
|
4
|
+
hash: Hash32<T>;
|
|
5
|
+
/**
|
|
6
|
+
* Register-index bits, `4 .. 16`. Defaults to `12`.
|
|
7
|
+
*
|
|
8
|
+
* The estimator uses `2 ** precision` one-byte registers, and its relative
|
|
9
|
+
* error is about `1.04 / sqrt(2 ** precision)` — so `12` costs 4 KiB for
|
|
10
|
+
* roughly 1.6% error, and each extra bit halves the error while doubling the
|
|
11
|
+
* memory.
|
|
12
|
+
*/
|
|
13
|
+
precision?: number;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Estimates how many **distinct** items a stream contained, in memory that does
|
|
17
|
+
* not grow with the answer.
|
|
18
|
+
*
|
|
19
|
+
* A `Set` answers exactly and costs O(distinct); this answers approximately and
|
|
20
|
+
* costs `2 ** precision` bytes whether the stream held a thousand items or a
|
|
21
|
+
* billion. Use it when the cardinality is large, approximate is acceptable, and
|
|
22
|
+
* the exact members are not needed afterwards — they cannot be recovered. The
|
|
23
|
+
* estimate is never exact, not even for a handful of items.
|
|
24
|
+
*
|
|
25
|
+
* The estimator is the standard one: hash each item, take the low `precision`
|
|
26
|
+
* bits as a register index, count the leading zeros of the rest, and keep the
|
|
27
|
+
* maximum per register. The harmonic mean of `2 ** register` across registers
|
|
28
|
+
* is proportional to the cardinality; the small-range correction (linear
|
|
29
|
+
* counting over the empty registers) covers the regime where the raw estimate
|
|
30
|
+
* is biased.
|
|
31
|
+
*
|
|
32
|
+
* Time complexity: O(1) per `add`, O(2 ** precision) per `count`. The estimate
|
|
33
|
+
* is stable — adding the same item twice changes nothing.
|
|
34
|
+
*
|
|
35
|
+
* @template T The item type.
|
|
36
|
+
*/
|
|
37
|
+
export declare class HyperLogLog<T> {
|
|
38
|
+
/** One byte per register: a 32-bit hash cannot produce a rank above 32. */
|
|
39
|
+
private readonly registers;
|
|
40
|
+
private readonly hash;
|
|
41
|
+
private readonly indexBits;
|
|
42
|
+
private readonly alpha;
|
|
43
|
+
/** The number of registers, `2 ** precision`. */
|
|
44
|
+
readonly registerCount: number;
|
|
45
|
+
constructor(iterable: Iterable<T> | undefined, options: HyperLogLogOptions<T>);
|
|
46
|
+
/** The relative error to expect, as a fraction: `1.04 / sqrt(registers)`. */
|
|
47
|
+
get relativeError(): number;
|
|
48
|
+
clear(): void;
|
|
49
|
+
/** O(1). Adding an item already seen leaves the estimate unchanged. */
|
|
50
|
+
add(item: T): void;
|
|
51
|
+
/** O(m) in the number of items. */
|
|
52
|
+
addAll(items: Iterable<T>): void;
|
|
53
|
+
/**
|
|
54
|
+
* The estimated number of distinct items. O(registerCount).
|
|
55
|
+
*
|
|
56
|
+
* Never exact, at any cardinality. Below ~2.5·registers linear counting takes
|
|
57
|
+
* over and is very close — but two items hashing to the same register are
|
|
58
|
+
* indistinguishable there too, so a handful of items can still estimate one
|
|
59
|
+
* or two low. Above that the harmonic-mean estimator lands within
|
|
60
|
+
* `relativeError` about 65% of the time and twice that about 95% of the
|
|
61
|
+
* time.
|
|
62
|
+
*/
|
|
63
|
+
count(): number;
|
|
64
|
+
/**
|
|
65
|
+
* Folds `other` in, so this estimator covers the union of both streams.
|
|
66
|
+
*
|
|
67
|
+
* Merging is lossless — the union's registers are the element-wise maxima —
|
|
68
|
+
* which is what lets partitions be counted independently and combined.
|
|
69
|
+
*
|
|
70
|
+
* @throws {RangeError} If the two were built with different precisions.
|
|
71
|
+
*/
|
|
72
|
+
merge(other: HyperLogLog<T>): void;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=hyper-log-log.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hyper-log-log.d.ts","sourceRoot":"","sources":["../../src/probabilistic/hyper-log-log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI;IACnC,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IAChB;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,WAAW,CAAC,CAAC;IACzB,2EAA2E;IAC3E,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAY;IACjC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAE/B,iDAAiD;IACjD,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAE/B,YACC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS,EACjC,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAmB9B;IAED,6EAA6E;IAC7E,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED,KAAK,IAAI,IAAI,CAEZ;IAED,uEAAuE;IACvE,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,CAiBjB;IAED,mCAAmC;IACnC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAI/B;IAED;;;;;;;;;OASG;IACH,KAAK,IAAI,MAAM,CA2Bd;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAYjC;CACD"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Estimates how many **distinct** items a stream contained, in memory that does
|
|
3
|
+
* not grow with the answer.
|
|
4
|
+
*
|
|
5
|
+
* A `Set` answers exactly and costs O(distinct); this answers approximately and
|
|
6
|
+
* costs `2 ** precision` bytes whether the stream held a thousand items or a
|
|
7
|
+
* billion. Use it when the cardinality is large, approximate is acceptable, and
|
|
8
|
+
* the exact members are not needed afterwards — they cannot be recovered. The
|
|
9
|
+
* estimate is never exact, not even for a handful of items.
|
|
10
|
+
*
|
|
11
|
+
* The estimator is the standard one: hash each item, take the low `precision`
|
|
12
|
+
* bits as a register index, count the leading zeros of the rest, and keep the
|
|
13
|
+
* maximum per register. The harmonic mean of `2 ** register` across registers
|
|
14
|
+
* is proportional to the cardinality; the small-range correction (linear
|
|
15
|
+
* counting over the empty registers) covers the regime where the raw estimate
|
|
16
|
+
* is biased.
|
|
17
|
+
*
|
|
18
|
+
* Time complexity: O(1) per `add`, O(2 ** precision) per `count`. The estimate
|
|
19
|
+
* is stable — adding the same item twice changes nothing.
|
|
20
|
+
*
|
|
21
|
+
* @template T The item type.
|
|
22
|
+
*/
|
|
23
|
+
export class HyperLogLog {
|
|
24
|
+
/** One byte per register: a 32-bit hash cannot produce a rank above 32. */
|
|
25
|
+
registers;
|
|
26
|
+
hash;
|
|
27
|
+
indexBits;
|
|
28
|
+
alpha;
|
|
29
|
+
/** The number of registers, `2 ** precision`. */
|
|
30
|
+
registerCount;
|
|
31
|
+
constructor(iterable, options) {
|
|
32
|
+
const precision = options.precision ?? 12;
|
|
33
|
+
if (!Number.isInteger(precision) || precision < 4 || precision > 16) {
|
|
34
|
+
throw new RangeError(`Precision must be an integer in [4, 16], got ${precision}`);
|
|
35
|
+
}
|
|
36
|
+
this.hash = options.hash;
|
|
37
|
+
this.indexBits = precision;
|
|
38
|
+
this.registerCount = 2 ** precision;
|
|
39
|
+
this.registers = new Uint8Array(this.registerCount);
|
|
40
|
+
this.alpha = biasCorrection(this.registerCount);
|
|
41
|
+
if (iterable) {
|
|
42
|
+
this.addAll(iterable);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** The relative error to expect, as a fraction: `1.04 / sqrt(registers)`. */
|
|
46
|
+
get relativeError() {
|
|
47
|
+
return 1.04 / Math.sqrt(this.registerCount);
|
|
48
|
+
}
|
|
49
|
+
clear() {
|
|
50
|
+
this.registers.fill(0);
|
|
51
|
+
}
|
|
52
|
+
/** O(1). Adding an item already seen leaves the estimate unchanged. */
|
|
53
|
+
add(item) {
|
|
54
|
+
const hashed = this.hash(item, 0) >>> 0;
|
|
55
|
+
const index = hashed & (this.registerCount - 1);
|
|
56
|
+
// The remaining bits decide the rank; shifting the index out first is what
|
|
57
|
+
// keeps the two uses of the hash independent.
|
|
58
|
+
const remaining = hashed >>> this.indexBits;
|
|
59
|
+
const width = 32 - this.indexBits;
|
|
60
|
+
// `clz32` of 0 is 32, which would over-count: cap the rank at the number
|
|
61
|
+
// of bits actually examined, plus one for the leading 1.
|
|
62
|
+
const rank = remaining === 0 ? width + 1 : Math.clz32(remaining) - this.indexBits + 1;
|
|
63
|
+
if (rank > this.registers[index]) {
|
|
64
|
+
this.registers[index] = rank;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** O(m) in the number of items. */
|
|
68
|
+
addAll(items) {
|
|
69
|
+
for (const item of items) {
|
|
70
|
+
this.add(item);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The estimated number of distinct items. O(registerCount).
|
|
75
|
+
*
|
|
76
|
+
* Never exact, at any cardinality. Below ~2.5·registers linear counting takes
|
|
77
|
+
* over and is very close — but two items hashing to the same register are
|
|
78
|
+
* indistinguishable there too, so a handful of items can still estimate one
|
|
79
|
+
* or two low. Above that the harmonic-mean estimator lands within
|
|
80
|
+
* `relativeError` about 65% of the time and twice that about 95% of the
|
|
81
|
+
* time.
|
|
82
|
+
*/
|
|
83
|
+
count() {
|
|
84
|
+
const { registers, registerCount } = this;
|
|
85
|
+
let harmonicSum = 0;
|
|
86
|
+
let emptyRegisters = 0;
|
|
87
|
+
for (let index = 0; index < registerCount; index++) {
|
|
88
|
+
const register = registers[index];
|
|
89
|
+
harmonicSum += 2 ** -register;
|
|
90
|
+
if (register === 0) {
|
|
91
|
+
emptyRegisters++;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const estimate = (this.alpha * registerCount ** 2) / harmonicSum;
|
|
95
|
+
// Below ~2.5·m the raw estimator is biased; linear counting over the
|
|
96
|
+
// still-empty registers is exact in that regime.
|
|
97
|
+
if (estimate <= 2.5 * registerCount && emptyRegisters > 0) {
|
|
98
|
+
return Math.round(registerCount * Math.log(registerCount / emptyRegisters));
|
|
99
|
+
}
|
|
100
|
+
return Math.round(estimate);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Folds `other` in, so this estimator covers the union of both streams.
|
|
104
|
+
*
|
|
105
|
+
* Merging is lossless — the union's registers are the element-wise maxima —
|
|
106
|
+
* which is what lets partitions be counted independently and combined.
|
|
107
|
+
*
|
|
108
|
+
* @throws {RangeError} If the two were built with different precisions.
|
|
109
|
+
*/
|
|
110
|
+
merge(other) {
|
|
111
|
+
if (other.registerCount !== this.registerCount) {
|
|
112
|
+
throw new RangeError("Cannot merge HyperLogLog estimators with different precisions");
|
|
113
|
+
}
|
|
114
|
+
for (let index = 0; index < this.registerCount; index++) {
|
|
115
|
+
if (other.registers[index] > this.registers[index]) {
|
|
116
|
+
this.registers[index] = other.registers[index];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** The constant that debiases the harmonic mean, per Flajolet et al. */
|
|
122
|
+
function biasCorrection(registerCount) {
|
|
123
|
+
switch (registerCount) {
|
|
124
|
+
case 16:
|
|
125
|
+
return 0.673;
|
|
126
|
+
case 32:
|
|
127
|
+
return 0.697;
|
|
128
|
+
case 64:
|
|
129
|
+
return 0.709;
|
|
130
|
+
default:
|
|
131
|
+
return 0.7213 / (1 + 1.079 / registerCount);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=hyper-log-log.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hyper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 @@
|
|
|
1
|
+
import { type CountMinSketchOptions } from "./count-min-sketch.js";
|
|
2
|
+
import type { Hash32 } from "./hash32.js";
|
|
3
|
+
export type TopKOptions<T> = Omit<CountMinSketchOptions<T>, "hash"> & {
|
|
4
|
+
/** See {@link Hash32}: required, and why it cannot be defaulted. */
|
|
5
|
+
hash: Hash32<T>;
|
|
6
|
+
/** How many heavy hitters to track. */
|
|
7
|
+
k: number;
|
|
8
|
+
};
|
|
9
|
+
/** A tracked item and the frequency estimated for it. */
|
|
10
|
+
export type HeavyHitter<T> = {
|
|
11
|
+
readonly item: T;
|
|
12
|
+
readonly count: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Approximate heavy hitters: the `k` most frequent items in a stream.
|
|
16
|
+
*
|
|
17
|
+
* A {@link CountMinSketch} estimates every item's frequency in fixed space; on
|
|
18
|
+
* its own it cannot say _which_ items are frequent, because it never stores
|
|
19
|
+
* them. This pairs it with a bounded set of candidates — the `k` items with the
|
|
20
|
+
* highest estimates seen so far — which is what turns "how often was X seen"
|
|
21
|
+
* into "what is seen most".
|
|
22
|
+
*
|
|
23
|
+
* Both approximations compound: an item is reported with the sketch's
|
|
24
|
+
* over-estimate, and a genuinely heavy item can be missed if it was evicted
|
|
25
|
+
* from the candidate set early, before its count grew. Raising `k` above the
|
|
26
|
+
* number actually wanted makes that far less likely.
|
|
27
|
+
*
|
|
28
|
+
* Time complexity: O(depth + k) per `add` — the sketch update plus a scan of
|
|
29
|
+
* the candidates. `heavyHitters` is O(k log k) to sort.
|
|
30
|
+
*
|
|
31
|
+
* @template T The item type.
|
|
32
|
+
*/
|
|
33
|
+
export declare class TopK<T> {
|
|
34
|
+
private readonly sketch;
|
|
35
|
+
/** Candidates by item, holding the estimate at the time of last update. */
|
|
36
|
+
private readonly candidates;
|
|
37
|
+
/** How many heavy hitters are tracked. */
|
|
38
|
+
readonly k: number;
|
|
39
|
+
constructor(options: TopKOptions<T>);
|
|
40
|
+
/** The total of every count added, exact. */
|
|
41
|
+
total(): number;
|
|
42
|
+
/** How many candidates are currently tracked, at most `k`. */
|
|
43
|
+
count(): number;
|
|
44
|
+
clear(): void;
|
|
45
|
+
/** O(depth + k). */
|
|
46
|
+
add(item: T, times?: number): void;
|
|
47
|
+
/** O(m·(depth + k)) in the number of items. */
|
|
48
|
+
addAll(items: Iterable<T>, times?: number): void;
|
|
49
|
+
/** The sketch's estimate for `item`, tracked or not. O(depth). */
|
|
50
|
+
estimate(item: T): number;
|
|
51
|
+
/** Whether `item` is currently one of the tracked candidates. O(1). */
|
|
52
|
+
isTracked(item: T): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The tracked heavy hitters, most frequent first. O(k log k).
|
|
55
|
+
*
|
|
56
|
+
* Counts are re-read from the sketch, so they are current rather than
|
|
57
|
+
* whatever they were when the candidate was admitted.
|
|
58
|
+
*/
|
|
59
|
+
heavyHitters(): HeavyHitter<T>[];
|
|
60
|
+
private weakestCandidate;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=top-k.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"top-k.d.ts","sourceRoot":"","sources":["../../src/probabilistic/top-k.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,qBAAqB,EAC1B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,IAAI,CAAC,qBAAqB,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG;IACrE,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IAChB,uCAAuC;IACvC,CAAC,EAAE,MAAM,CAAC;CACV,CAAC;AAEF,yDAAyD;AACzD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC5B,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IACjB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,IAAI,CAAC,CAAC;IAClB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;IAC3C,2EAA2E;IAC3E,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAwB;IAEnD,0CAA0C;IAC1C,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IAEnB,YAAY,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,EAOlC;IAED,6CAA6C;IAC7C,KAAK,IAAI,MAAM,CAEd;IAED,8DAA8D;IAC9D,KAAK,IAAI,MAAM,CAEd;IAED,KAAK,IAAI,IAAI,CAGZ;IAED,oBAAoB;IACpB,GAAG,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,SAAI,GAAG,IAAI,CAuB5B;IAED,+CAA+C;IAC/C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,SAAI,GAAG,IAAI,CAI1C;IAED,kEAAkE;IAClE,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,CAExB;IAED,uEAAuE;IACvE,SAAS,CAAC,IAAI,EAAE,CAAC,GAAG,OAAO,CAE1B;IAED;;;;;OAKG;IACH,YAAY,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE,CAU/B;IAED,OAAO,CAAC,gBAAgB;CAWxB"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { CountMinSketch, } from "./count-min-sketch.js";
|
|
2
|
+
/**
|
|
3
|
+
* Approximate heavy hitters: the `k` most frequent items in a stream.
|
|
4
|
+
*
|
|
5
|
+
* A {@link CountMinSketch} estimates every item's frequency in fixed space; on
|
|
6
|
+
* its own it cannot say _which_ items are frequent, because it never stores
|
|
7
|
+
* them. This pairs it with a bounded set of candidates — the `k` items with the
|
|
8
|
+
* highest estimates seen so far — which is what turns "how often was X seen"
|
|
9
|
+
* into "what is seen most".
|
|
10
|
+
*
|
|
11
|
+
* Both approximations compound: an item is reported with the sketch's
|
|
12
|
+
* over-estimate, and a genuinely heavy item can be missed if it was evicted
|
|
13
|
+
* from the candidate set early, before its count grew. Raising `k` above the
|
|
14
|
+
* number actually wanted makes that far less likely.
|
|
15
|
+
*
|
|
16
|
+
* Time complexity: O(depth + k) per `add` — the sketch update plus a scan of
|
|
17
|
+
* the candidates. `heavyHitters` is O(k log k) to sort.
|
|
18
|
+
*
|
|
19
|
+
* @template T The item type.
|
|
20
|
+
*/
|
|
21
|
+
export class TopK {
|
|
22
|
+
sketch;
|
|
23
|
+
/** Candidates by item, holding the estimate at the time of last update. */
|
|
24
|
+
candidates = new Map();
|
|
25
|
+
/** How many heavy hitters are tracked. */
|
|
26
|
+
k;
|
|
27
|
+
constructor(options) {
|
|
28
|
+
if (!Number.isInteger(options.k) || options.k < 1) {
|
|
29
|
+
throw new RangeError(`k must be a positive integer, got ${options.k}`);
|
|
30
|
+
}
|
|
31
|
+
this.k = options.k;
|
|
32
|
+
this.sketch = new CountMinSketch(options);
|
|
33
|
+
}
|
|
34
|
+
/** The total of every count added, exact. */
|
|
35
|
+
total() {
|
|
36
|
+
return this.sketch.total();
|
|
37
|
+
}
|
|
38
|
+
/** How many candidates are currently tracked, at most `k`. */
|
|
39
|
+
count() {
|
|
40
|
+
return this.candidates.size;
|
|
41
|
+
}
|
|
42
|
+
clear() {
|
|
43
|
+
this.sketch.clear();
|
|
44
|
+
this.candidates.clear();
|
|
45
|
+
}
|
|
46
|
+
/** O(depth + k). */
|
|
47
|
+
add(item, times = 1) {
|
|
48
|
+
this.sketch.add(item, times);
|
|
49
|
+
const estimate = this.sketch.estimate(item);
|
|
50
|
+
if (this.candidates.has(item)) {
|
|
51
|
+
this.candidates.set(item, estimate);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (this.candidates.size < this.k) {
|
|
55
|
+
this.candidates.set(item, estimate);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const weakest = this.weakestCandidate();
|
|
59
|
+
// Only displace a candidate this item actually beats; ties keep the
|
|
60
|
+
// incumbent, which has been heavy for longer.
|
|
61
|
+
if (weakest !== undefined && estimate > weakest[1]) {
|
|
62
|
+
this.candidates.delete(weakest[0]);
|
|
63
|
+
this.candidates.set(item, estimate);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** O(m·(depth + k)) in the number of items. */
|
|
67
|
+
addAll(items, times = 1) {
|
|
68
|
+
for (const item of items) {
|
|
69
|
+
this.add(item, times);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** The sketch's estimate for `item`, tracked or not. O(depth). */
|
|
73
|
+
estimate(item) {
|
|
74
|
+
return this.sketch.estimate(item);
|
|
75
|
+
}
|
|
76
|
+
/** Whether `item` is currently one of the tracked candidates. O(1). */
|
|
77
|
+
isTracked(item) {
|
|
78
|
+
return this.candidates.has(item);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The tracked heavy hitters, most frequent first. O(k log k).
|
|
82
|
+
*
|
|
83
|
+
* Counts are re-read from the sketch, so they are current rather than
|
|
84
|
+
* whatever they were when the candidate was admitted.
|
|
85
|
+
*/
|
|
86
|
+
heavyHitters() {
|
|
87
|
+
const found = [];
|
|
88
|
+
for (const item of this.candidates.keys()) {
|
|
89
|
+
found.push({ item, count: this.sketch.estimate(item) });
|
|
90
|
+
}
|
|
91
|
+
found.sort((left, right) => right.count - left.count);
|
|
92
|
+
return found;
|
|
93
|
+
}
|
|
94
|
+
weakestCandidate() {
|
|
95
|
+
let weakest;
|
|
96
|
+
for (const [item, count] of this.candidates) {
|
|
97
|
+
if (weakest === undefined || count < weakest[1]) {
|
|
98
|
+
weakest = [item, count];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return weakest;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=top-k.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"top-k.js","sourceRoot":"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