probabilistic_machines 0.1.3 → 0.6.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +70 -0
- data/Cargo.lock +22 -3
- data/Cargo.toml +2 -0
- data/README.md +166 -58
- data/ext/probabilistic_machines_native/core/Cargo.toml +10 -5
- data/ext/probabilistic_machines_native/core/src/countmin.rs +117 -99
- data/ext/probabilistic_machines_native/core/src/cuckoo.rs +109 -81
- data/ext/probabilistic_machines_native/core/src/hash.rs +9 -17
- data/ext/probabilistic_machines_native/core/src/hyperloglog.rs +104 -92
- data/ext/probabilistic_machines_native/core/src/lib.rs +144 -22
- data/ext/probabilistic_machines_native/core/src/tdigest.rs +175 -90
- data/ext/probabilistic_machines_native/core/src/topk.rs +253 -126
- data/ext/probabilistic_machines_native/ffi/Cargo.toml +5 -11
- data/ext/probabilistic_machines_native/ffi/src/countmin.rs +111 -70
- data/ext/probabilistic_machines_native/ffi/src/cuckoo.rs +102 -44
- data/ext/probabilistic_machines_native/ffi/src/data.rs +102 -55
- data/ext/probabilistic_machines_native/ffi/src/hyperloglog.rs +92 -49
- data/ext/probabilistic_machines_native/ffi/src/init.rs +15 -0
- data/ext/probabilistic_machines_native/ffi/src/lib.rs +74 -109
- data/ext/probabilistic_machines_native/ffi/src/tdigest.rs +91 -90
- data/ext/probabilistic_machines_native/ffi/src/topk.rs +181 -136
- data/lib/probabilistic_machines/version.rb +1 -1
- data/lib/probabilistic_machines.rb +40 -2
- data/sig/probabilistic_machines.rbs +129 -56
- metadata +2 -1
|
@@ -1,35 +1,62 @@
|
|
|
1
1
|
# Sketches that answer a counting question in memory fixed at construction
|
|
2
|
-
# time. Every class
|
|
2
|
+
# time. Every sketch class is defined by the native extension.
|
|
3
|
+
#
|
|
4
|
+
# Sizes come from a fixed menu (a class constant); anything else raises
|
|
5
|
+
# RangeError, a non-Integer TypeError. A call the core refuses answers the
|
|
6
|
+
# snake_case symbol of its `Refusal` variant, changes nothing and never raises.
|
|
7
|
+
# No answer depends on, or records, a clock.
|
|
3
8
|
module ProbabilisticMachines
|
|
4
9
|
VERSION: String
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
# Every refusal the crate answers.
|
|
12
|
+
type refusal = :truncated | :wrong_structure | :wrong_shape | :malformed | :full
|
|
13
|
+
| :overflow | :zero_count | :not_finite | :invalid_weight
|
|
8
14
|
|
|
9
|
-
#
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
# What `decode` refuses: a frame cut short, another structure's frame, a frame
|
|
16
|
+
# of another shape, or contents no sketch produces.
|
|
17
|
+
type decode_refusal = :truncated | :wrong_structure | :wrong_shape | :malformed
|
|
18
|
+
|
|
19
|
+
# Private native checks behind `Hitter.new` and `Centroid.new`: the members
|
|
20
|
+
# of a valid value (a frozen UTF-8 copy of the key; the centroid as Floats),
|
|
21
|
+
# TypeError/RangeError where the core has no such value.
|
|
22
|
+
private def self.__hitter: (String key, Integer count, Integer overestimate) -> [String, Integer, Integer]
|
|
23
|
+
private def self.__centroid: (Float | Integer mean, Float | Integer weight) -> [Float, Float]
|
|
12
24
|
|
|
13
|
-
#
|
|
14
|
-
# `count
|
|
25
|
+
# The core's `Hitter`. The true count lies in `count - overestimate` through
|
|
26
|
+
# `count`. Raises RangeError unless `overestimate < count` (both in
|
|
27
|
+
# `0...2**64`), TypeError for a non-String key or a non-Integer count.
|
|
15
28
|
class Hitter < ::Data
|
|
16
29
|
attr_reader key: String
|
|
17
30
|
attr_reader count: Integer
|
|
18
31
|
attr_reader overestimate: Integer
|
|
32
|
+
|
|
33
|
+
def self.new: (String key, Integer count, Integer overestimate) -> instance
|
|
34
|
+
| (key: String, count: Integer, overestimate: Integer) -> instance
|
|
19
35
|
end
|
|
20
36
|
|
|
21
|
-
#
|
|
37
|
+
# The core's `Centroid`: one cluster of a digest. Raises RangeError unless
|
|
38
|
+
# the mean is finite and the weight finite and positive, TypeError for a
|
|
39
|
+
# non-number. Stored as Floats.
|
|
22
40
|
class Centroid < ::Data
|
|
23
41
|
attr_reader mean: Float
|
|
24
42
|
attr_reader weight: Float
|
|
43
|
+
|
|
44
|
+
def self.new: (Float | Integer mean, Float | Integer weight) -> instance
|
|
45
|
+
| (mean: Float | Integer, weight: Float | Integer) -> instance
|
|
25
46
|
end
|
|
26
47
|
|
|
27
48
|
# Distinct counts in a fixed register array.
|
|
28
49
|
class HyperLogLog
|
|
29
|
-
|
|
30
|
-
|
|
50
|
+
REGISTERS: Array[Integer]
|
|
51
|
+
|
|
52
|
+
# RangeError outside REGISTERS.
|
|
53
|
+
def self.new: (Integer registers) -> instance
|
|
31
54
|
|
|
32
|
-
|
|
55
|
+
# `:wrong_shape` for a register count this build does not carry.
|
|
56
|
+
def self.decode: (String bytes) -> (instance | decode_refusal)
|
|
57
|
+
|
|
58
|
+
# Reads a frame of this sketch's register count over it.
|
|
59
|
+
def decode_into: (String bytes) -> (:decoded | decode_refusal)
|
|
33
60
|
|
|
34
61
|
def registers: () -> Integer
|
|
35
62
|
def add: (String item) -> nil
|
|
@@ -44,53 +71,74 @@ module ProbabilisticMachines
|
|
|
44
71
|
|
|
45
72
|
def empty?: () -> bool
|
|
46
73
|
|
|
47
|
-
#
|
|
48
|
-
def merge: (HyperLogLog other) ->
|
|
74
|
+
# Exact union. `:wrong_shape` for another register count.
|
|
75
|
+
def merge: (HyperLogLog other) -> (:merged | :wrong_shape)
|
|
49
76
|
|
|
50
|
-
def
|
|
77
|
+
def encoded: () -> String
|
|
51
78
|
def inspect: () -> String
|
|
52
79
|
end
|
|
53
80
|
|
|
54
81
|
# How often each item was seen, in `width * depth` counters.
|
|
55
82
|
class CountMinSketch
|
|
56
|
-
|
|
57
|
-
|
|
83
|
+
WIDTHS: Array[Integer]
|
|
84
|
+
DEPTHS: Array[Integer]
|
|
85
|
+
|
|
86
|
+
# RangeError outside WIDTHS by DEPTHS; every pair is carried.
|
|
87
|
+
def self.new: (Integer width, Integer depth) -> instance
|
|
58
88
|
|
|
59
|
-
def self.
|
|
89
|
+
def self.decode: (String bytes) -> (instance | decode_refusal)
|
|
90
|
+
def decode_into: (String bytes) -> (:decoded | decode_refusal)
|
|
60
91
|
|
|
61
92
|
def width: () -> Integer
|
|
62
93
|
def depth: () -> Integer
|
|
63
|
-
|
|
94
|
+
|
|
95
|
+
# `:overflow` when one of the item's counters would pass `2**32 - 1`.
|
|
96
|
+
def add: (String item) -> (:added | :overflow)
|
|
97
|
+
|
|
98
|
+
# `count` is in `0...2**32` (RangeError otherwise); zero is `:zero_count`.
|
|
99
|
+
def add_count: (String item, Integer count) -> (:added | :zero_count | :overflow)
|
|
100
|
+
|
|
101
|
+
def add_hash_count: (Integer hash, Integer count) -> (:added | :zero_count | :overflow)
|
|
64
102
|
|
|
65
103
|
# Never below the true count, and over it by at most
|
|
66
104
|
# `e * total / width` with probability `1 - e ** -depth`.
|
|
67
|
-
def
|
|
105
|
+
def estimate: (String item) -> Integer
|
|
106
|
+
|
|
107
|
+
def estimate_hash: (Integer hash) -> Integer
|
|
68
108
|
|
|
69
109
|
# Sum of every count added.
|
|
70
110
|
def total: () -> Integer
|
|
71
|
-
def saturated?: () -> bool
|
|
72
111
|
|
|
73
|
-
#
|
|
74
|
-
def merge: (CountMinSketch other) ->
|
|
112
|
+
# Exact sum. `:wrong_shape` for another width or depth.
|
|
113
|
+
def merge: (CountMinSketch other) -> (:merged | :overflow | :wrong_shape)
|
|
75
114
|
|
|
76
|
-
def
|
|
115
|
+
def encoded: () -> String
|
|
77
116
|
def inspect: () -> String
|
|
78
117
|
end
|
|
79
118
|
|
|
80
119
|
# Quantiles of a stream, in clusters instead of samples.
|
|
81
120
|
class TDigest
|
|
82
|
-
|
|
121
|
+
MIN_COMPRESSION: Integer
|
|
83
122
|
|
|
84
|
-
|
|
123
|
+
# RangeError below MIN_COMPRESSION or past 65535.
|
|
124
|
+
def self.new: (Integer compression) -> instance
|
|
125
|
+
|
|
126
|
+
def self.decode: (String bytes) -> (instance | :truncated | :wrong_structure | :malformed)
|
|
85
127
|
|
|
86
128
|
def compression: () -> Integer
|
|
87
|
-
def add: (Float value, ?weight: Float) -> nil
|
|
88
129
|
|
|
89
|
-
|
|
90
|
-
|
|
130
|
+
def add: (Float | Integer value) -> (:added | :not_finite | :overflow)
|
|
131
|
+
|
|
132
|
+
# One value standing for `weight` observations.
|
|
133
|
+
def add_weighted: (Float | Integer value, Float | Integer weight)
|
|
134
|
+
-> (:added | :not_finite | :invalid_weight | :overflow)
|
|
135
|
+
|
|
136
|
+
# Value at quantile `q`, `Float::NAN` when empty. RangeError for `q`
|
|
137
|
+
# outside 0..=1, NaN included.
|
|
138
|
+
def quantile: (Float | Integer q) -> Float
|
|
91
139
|
|
|
92
140
|
# Fraction of the stream at or below `value`, `Float::NAN` when empty.
|
|
93
|
-
def cdf: (Float value) -> Float
|
|
141
|
+
def cdf: (Float | Integer value) -> Float
|
|
94
142
|
|
|
95
143
|
# Total weight added.
|
|
96
144
|
def count: () -> Float
|
|
@@ -106,53 +154,75 @@ module ProbabilisticMachines
|
|
|
106
154
|
|
|
107
155
|
def empty?: () -> bool
|
|
108
156
|
def centroids: () -> Array[Centroid]
|
|
109
|
-
|
|
110
|
-
|
|
157
|
+
|
|
158
|
+
# Collapses adjacent clusters up to the weight their quantile allows.
|
|
159
|
+
def compress: () -> nil
|
|
160
|
+
|
|
161
|
+
# `:wrong_shape` for a digest of another compression.
|
|
162
|
+
def merge: (TDigest other) -> (:merged | :wrong_shape | :overflow)
|
|
163
|
+
|
|
164
|
+
def encoded: () -> String
|
|
111
165
|
def inspect: () -> String
|
|
112
166
|
end
|
|
113
167
|
|
|
114
168
|
# Membership that can forget, in `buckets * 4` fingerprints.
|
|
115
169
|
class CuckooFilter
|
|
116
|
-
|
|
117
|
-
def self.new: (buckets: Integer) -> instance
|
|
170
|
+
BUCKETS: Array[Integer]
|
|
118
171
|
|
|
119
|
-
|
|
172
|
+
# RangeError outside BUCKETS.
|
|
173
|
+
def self.new: (Integer buckets) -> instance
|
|
174
|
+
|
|
175
|
+
def self.decode: (String bytes) -> (instance | :truncated | :wrong_structure | :wrong_shape)
|
|
176
|
+
def decode_into: (String bytes) -> (:decoded | :truncated | :wrong_structure | :wrong_shape)
|
|
120
177
|
|
|
121
178
|
def buckets: () -> Integer
|
|
122
179
|
|
|
123
180
|
# Fingerprint slots in the table, `buckets * 4`.
|
|
124
181
|
def capacity: () -> Integer
|
|
125
182
|
|
|
126
|
-
#
|
|
127
|
-
def insert: (String item) ->
|
|
183
|
+
# Stores one more copy of the item's fingerprint.
|
|
184
|
+
def insert: (String item) -> (:inserted | :full)
|
|
185
|
+
|
|
186
|
+
def insert_hash: (Integer hash) -> (:inserted | :full)
|
|
128
187
|
|
|
129
188
|
# Never false for an item that was inserted; true for one that was not
|
|
130
189
|
# with probability about 3%.
|
|
131
|
-
def
|
|
190
|
+
def contains?: (String item) -> bool
|
|
191
|
+
|
|
192
|
+
def contains_hash?: (Integer hash) -> bool
|
|
132
193
|
|
|
133
|
-
|
|
194
|
+
# `:cleared` clears one copy of the item's fingerprint, which may have
|
|
195
|
+
# been a colliding item's: only remove what you inserted. `:not_held`
|
|
196
|
+
# proves the fingerprint is in neither bucket; nothing changed.
|
|
197
|
+
def remove: (String item) -> (:cleared | :not_held)
|
|
134
198
|
|
|
135
|
-
|
|
136
|
-
# with one takes the other item's fingerprint with it.
|
|
137
|
-
def remove: (String item) -> bool
|
|
199
|
+
def remove_hash: (Integer hash) -> (:cleared | :not_held)
|
|
138
200
|
|
|
139
201
|
def size: () -> Integer
|
|
140
202
|
def empty?: () -> bool
|
|
141
|
-
def
|
|
203
|
+
def encoded: () -> String
|
|
142
204
|
def inspect: () -> String
|
|
143
205
|
end
|
|
144
206
|
|
|
145
|
-
# Which keys dominate a stream, in
|
|
207
|
+
# Which keys dominate a stream, in a fixed number of counters.
|
|
146
208
|
class HeavyHitters
|
|
147
|
-
|
|
148
|
-
def self.new: (k: Integer) -> instance
|
|
209
|
+
COUNTERS: Array[Integer]
|
|
149
210
|
|
|
150
|
-
|
|
211
|
+
# RangeError outside COUNTERS.
|
|
212
|
+
def self.new: (Integer counters) -> instance
|
|
151
213
|
|
|
152
|
-
|
|
214
|
+
# `:malformed` also covers a key table that is cut short or does not name
|
|
215
|
+
# every tracked key.
|
|
216
|
+
def self.decode: (String bytes) -> (instance | decode_refusal)
|
|
217
|
+
def decode_into: (String bytes) -> (:decoded | decode_refusal)
|
|
153
218
|
|
|
154
|
-
|
|
155
|
-
|
|
219
|
+
def counters: () -> Integer
|
|
220
|
+
|
|
221
|
+
# Keys are UTF-8 strings, so the Hitter values can hand them back.
|
|
222
|
+
def add: (String key) -> (:added | :overflow)
|
|
223
|
+
|
|
224
|
+
# `count` is in `0...2**64` (RangeError otherwise); zero is `:zero_count`.
|
|
225
|
+
def add_count: (String key, Integer count) -> (:added | :zero_count | :overflow)
|
|
156
226
|
|
|
157
227
|
# Upper bound on how often `key` occurred, 0 when it is not tracked.
|
|
158
228
|
def count: (String key) -> Integer
|
|
@@ -160,22 +230,25 @@ module ProbabilisticMachines
|
|
|
160
230
|
# Observations fed in.
|
|
161
231
|
def observed: () -> Integer
|
|
162
232
|
|
|
163
|
-
# Keys currently tracked, at most `
|
|
233
|
+
# Keys currently tracked, at most `counters`.
|
|
164
234
|
def size: () -> Integer
|
|
165
235
|
|
|
166
236
|
def empty?: () -> bool
|
|
167
237
|
|
|
168
|
-
# The
|
|
169
|
-
def top: (
|
|
238
|
+
# The most frequent tracked key, nil when nothing is tracked.
|
|
239
|
+
def top: () -> Hitter?
|
|
170
240
|
|
|
171
241
|
# Every tracked key, most frequent first.
|
|
172
242
|
def ranked: () -> Array[Hitter]
|
|
173
243
|
|
|
174
|
-
#
|
|
175
|
-
def
|
|
244
|
+
# Every tracked key, in the table's own order.
|
|
245
|
+
def hitters: () -> Array[Hitter]
|
|
246
|
+
|
|
247
|
+
# `:wrong_shape` for another counter count.
|
|
248
|
+
def merge: (HeavyHitters other) -> (:merged | :overflow | :wrong_shape)
|
|
176
249
|
|
|
177
250
|
# The crate's frame followed by the key table that carries the strings.
|
|
178
|
-
def
|
|
251
|
+
def encoded: () -> String
|
|
179
252
|
|
|
180
253
|
def inspect: () -> String
|
|
181
254
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: probabilistic_machines
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.6.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
@@ -54,6 +54,7 @@ files:
|
|
|
54
54
|
- ext/probabilistic_machines_native/ffi/src/cuckoo.rs
|
|
55
55
|
- ext/probabilistic_machines_native/ffi/src/data.rs
|
|
56
56
|
- ext/probabilistic_machines_native/ffi/src/hyperloglog.rs
|
|
57
|
+
- ext/probabilistic_machines_native/ffi/src/init.rs
|
|
57
58
|
- ext/probabilistic_machines_native/ffi/src/lib.rs
|
|
58
59
|
- ext/probabilistic_machines_native/ffi/src/tdigest.rs
|
|
59
60
|
- ext/probabilistic_machines_native/ffi/src/topk.rs
|