Skip to content

Commit a6a3ee0

Browse files
committed
Move SparseVec from usize to u64 backing storage.
By using variable sized storage, we couldn't serialise a SparseVec on a 64-bit machine and deserialise on a 32-bit machine (i.e. we couldn't cross-compile grmtools to WASM). This relatively simple change makes that possible (since the backing is now always `u64` no matter the machine). This is technically an API break (since we've gone from requiring a `T` compatible with `usize` to a `T` compatible with `u64`), but I doubt anyone using this library is using a `T` which isn't compatible both ways.
1 parent 56c0ca6 commit a6a3ee0

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ license = "Apache-2.0/MIT"
1010
categories = ["data-structures"]
1111

1212
[dependencies]
13-
vob = { version="3.0", features=["serde"] }
13+
vob = { version=">=3.0.2", features=["serde"] }
1414
packedvec = { version="1.0", features=["serde"] }
1515
serde = { version="1.0", features=["derive"], optional=true }
1616
num-traits = "0.2"

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ pub struct SparseVec<T> {
6565
displacement: Vec<usize>, // Displacement vector
6666
row_length: usize, // Row length of the input matrix
6767
empty_val: T, // Value considered "empty"
68-
empties: Vob, // Mapping of "empty" cells
69-
data: PackedVec<T>, // Compressed matrix
68+
empties: Vob<u64>, // Mapping of "empty" cells
69+
data: PackedVec<T, u64>, // Compressed matrix
7070
}
7171

7272
impl<T: Clone + Copy + PartialEq> SparseVec<T>
7373
where
74-
T: AsPrimitive<usize> + FromPrimitive + Ord + PrimInt + ToPrimitive + Unsigned,
75-
usize: AsPrimitive<T>,
74+
T: AsPrimitive<u64> + FromPrimitive + Ord + PrimInt + ToPrimitive + Unsigned,
75+
u64: AsPrimitive<T>,
7676
{
7777
/// Constructs a new SparseVec from a `Vec` of unsigned integers where `empty_val` describes
7878
/// the values to be compressed and `row_length` the element size per row in the original
@@ -91,8 +91,8 @@ where
9191
displacement: Vec::new(),
9292
row_length: 0,
9393
empty_val,
94-
empties: Vob::new(),
95-
data: PackedVec::new(v.to_vec()),
94+
empties: Vob::<u64>::new_with_storage_type(0),
95+
data: PackedVec::<T, u64>::new_with_storaget(v.to_vec()),
9696
};
9797
}
9898

@@ -101,7 +101,7 @@ where
101101
let s = sort(v, empty_val, row_length);
102102
let (c, d) = compress(v, &s, empty_val, row_length);
103103
let e = calc_empties(v, empty_val);
104-
let pv = PackedVec::new(c);
104+
let pv = PackedVec::<T, u64>::new_with_storaget(c);
105105
SparseVec {
106106
displacement: d,
107107
row_length,
@@ -155,8 +155,8 @@ where
155155
}
156156
}
157157

158-
fn calc_empties<T: PartialEq>(vec: &[T], empty_val: T) -> Vob {
159-
let mut vob = Vob::from_elem(false, vec.len());
158+
fn calc_empties<T: PartialEq>(vec: &[T], empty_val: T) -> Vob<u64> {
159+
let mut vob = Vob::<u64>::from_elem_with_storage_type(false, vec.len());
160160
for (i, v) in vec.iter().enumerate() {
161161
if *v == empty_val {
162162
vob.set(i, true);

0 commit comments

Comments
 (0)