@peerbit/riblt 0.0.1-5cf61cb
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 +21 -0
- package/README.md +101 -0
- package/package.json +58 -0
- package/src/encoding.rs +397 -0
- package/src/lib.rs +7 -0
- package/src/sketch.rs +101 -0
- package/src/testing.rs +50 -0
- package/src/tests.rs +2343 -0
- package/src/wasm.rs +173 -0
package/src/wasm.rs
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
use crate::encoding::*;
|
|
2
|
+
use js_sys::Array;
|
|
3
|
+
use wasm_bindgen::prelude::*;
|
|
4
|
+
/*
|
|
5
|
+
#[wasm_bindgen]
|
|
6
|
+
#[derive(Clone, Copy, PartialEq)]
|
|
7
|
+
pub struct MyU64(u64);
|
|
8
|
+
|
|
9
|
+
impl From<u64> for MyU64 {
|
|
10
|
+
fn from(value: u64) -> Self {
|
|
11
|
+
MyU64(value)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
impl Into<u64> for MyU64 {
|
|
16
|
+
fn into(self) -> u64 {
|
|
17
|
+
self.0
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
impl Symbol for MyU64 {
|
|
22
|
+
fn zero() -> MyU64 {
|
|
23
|
+
MyU64(0)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fn xor(&self, other: &MyU64) -> MyU64 {
|
|
27
|
+
MyU64(self.0 ^ other.0)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
fn hash(&self) -> u64 {
|
|
31
|
+
let mut hasher = DefaultHasher::new();
|
|
32
|
+
hasher.write_u64(self.0);
|
|
33
|
+
hasher.finish()
|
|
34
|
+
}
|
|
35
|
+
} */
|
|
36
|
+
|
|
37
|
+
pub type IdentityU64 = u64;
|
|
38
|
+
|
|
39
|
+
#[wasm_bindgen]
|
|
40
|
+
pub struct EncoderWrapper {
|
|
41
|
+
encoder: Encoder<IdentityU64>,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#[wasm_bindgen]
|
|
45
|
+
impl EncoderWrapper {
|
|
46
|
+
#[wasm_bindgen(constructor)]
|
|
47
|
+
pub fn new() -> EncoderWrapper {
|
|
48
|
+
EncoderWrapper {
|
|
49
|
+
encoder: Encoder::<IdentityU64>::new(),
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
pub fn add_symbol(&mut self, symbol: u64) {
|
|
54
|
+
let my_symbol: IdentityU64 = symbol;
|
|
55
|
+
self.encoder.add_symbol(&my_symbol);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
pub fn produce_next_coded_symbol(&mut self) -> JsValue {
|
|
59
|
+
let coded_symbol = self.encoder.produce_next_coded_symbol();
|
|
60
|
+
let symbol_u64 = coded_symbol.symbol;
|
|
61
|
+
let hash_u64 = coded_symbol.hash;
|
|
62
|
+
let count_i64 = coded_symbol.count;
|
|
63
|
+
|
|
64
|
+
// Create a JavaScript object to hold the coded symbol
|
|
65
|
+
let obj = js_sys::Object::new();
|
|
66
|
+
|
|
67
|
+
js_sys::Reflect::set(
|
|
68
|
+
&obj,
|
|
69
|
+
&JsValue::from_str("symbol"),
|
|
70
|
+
&JsValue::from(symbol_u64),
|
|
71
|
+
)
|
|
72
|
+
.unwrap();
|
|
73
|
+
js_sys::Reflect::set(&obj, &JsValue::from_str("hash"), &JsValue::from(hash_u64)).unwrap();
|
|
74
|
+
js_sys::Reflect::set(&obj, &JsValue::from_str("count"), &JsValue::from(count_i64)).unwrap();
|
|
75
|
+
|
|
76
|
+
JsValue::from(obj)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
pub fn reset(&mut self) {
|
|
80
|
+
self.encoder.reset();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
pub fn to_decoder(&self) -> DecoderWrapper {
|
|
84
|
+
DecoderWrapper {
|
|
85
|
+
decoder: self.encoder.to_decoder(),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
pub fn clone(&self) -> EncoderWrapper {
|
|
90
|
+
EncoderWrapper {
|
|
91
|
+
encoder: self.encoder.clone(),
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#[wasm_bindgen]
|
|
97
|
+
pub struct DecoderWrapper {
|
|
98
|
+
decoder: Decoder<IdentityU64>,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#[wasm_bindgen]
|
|
102
|
+
impl DecoderWrapper {
|
|
103
|
+
#[wasm_bindgen(constructor)]
|
|
104
|
+
pub fn new() -> DecoderWrapper {
|
|
105
|
+
DecoderWrapper {
|
|
106
|
+
decoder: Decoder::<IdentityU64>::new(),
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
pub fn add_symbol(&mut self, symbol: u64) {
|
|
111
|
+
let my_symbol: IdentityU64 = symbol;
|
|
112
|
+
self.decoder.add_symbol(&my_symbol);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
pub fn add_coded_symbol(&mut self, coded_symbol_js: &JsValue) {
|
|
116
|
+
// Extract symbol, hash, and count from JsValue
|
|
117
|
+
let symbol_js =
|
|
118
|
+
js_sys::Reflect::get(coded_symbol_js, &JsValue::from_str("symbol")).unwrap();
|
|
119
|
+
let hash_js = js_sys::Reflect::get(coded_symbol_js, &JsValue::from_str("hash")).unwrap();
|
|
120
|
+
let count_js = js_sys::Reflect::get(coded_symbol_js, &JsValue::from_str("count")).unwrap();
|
|
121
|
+
|
|
122
|
+
let symbol_u64: u64 = symbol_js.try_into().unwrap();
|
|
123
|
+
let hash_u64: u64 = hash_js.try_into().unwrap();
|
|
124
|
+
let count_i64: i64 = count_js.try_into().unwrap();
|
|
125
|
+
let coded_symbol = CodedSymbol {
|
|
126
|
+
symbol: symbol_u64,
|
|
127
|
+
hash: hash_u64,
|
|
128
|
+
count: count_i64,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
self.decoder.add_coded_symbol(&coded_symbol);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
pub fn try_decode(&mut self) -> Result<(), JsValue> {
|
|
135
|
+
match self.decoder.try_decode() {
|
|
136
|
+
Ok(_) => Ok(()),
|
|
137
|
+
// error is a enum with number of variants
|
|
138
|
+
Err(e) => {
|
|
139
|
+
return match e {
|
|
140
|
+
Error::InvalidDegree => Err(JsValue::from_str("Invalid degree")),
|
|
141
|
+
Error::InvalidSize => Err(JsValue::from_str("Invalid size")),
|
|
142
|
+
Error::DecodeFailed => Err(JsValue::from_str("Decode failed")),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
pub fn decoded(&self) -> bool {
|
|
149
|
+
self.decoder.decoded()
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
pub fn get_remote_symbols(&self) -> Array {
|
|
153
|
+
let symbols = self.decoder.get_remote_symbols();
|
|
154
|
+
let array = Array::new();
|
|
155
|
+
for sym in symbols {
|
|
156
|
+
array.push(&JsValue::from(sym.symbol));
|
|
157
|
+
}
|
|
158
|
+
array
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
pub fn get_local_symbols(&self) -> Array {
|
|
162
|
+
let symbols = self.decoder.get_local_symbols();
|
|
163
|
+
let array = Array::new();
|
|
164
|
+
for sym in symbols {
|
|
165
|
+
array.push(&JsValue::from(sym.symbol));
|
|
166
|
+
}
|
|
167
|
+
array
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
pub fn reset(&mut self) {
|
|
171
|
+
self.decoder.reset();
|
|
172
|
+
}
|
|
173
|
+
}
|