Skip to content

Commit

Permalink
Conversions from borrowed types into sparse vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
timvisee committed Dec 4, 2023
1 parent c29d693 commit 85c7397
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,18 @@ impl From<Vec<f32>> for Vector {

impl From<Vec<(u32, f32)>> for Vector {
fn from(tuples: Vec<(u32, f32)>) -> Self {
Self::from(tuples.as_slice())
}
}

// Since we construct two new Vec's anyway it's fine to source from a reference
impl From<&[(u32, f32)]> for Vector {
fn from(tuples: &[(u32, f32)]) -> Self {
let mut indices = Vec::with_capacity(tuples.len());
let mut values = Vec::with_capacity(tuples.len());
for (i, w) in tuples {
indices.push(i);
values.push(w);
indices.push(*i);
values.push(*w);
}
Vector {
data: values,
Expand Down Expand Up @@ -277,6 +284,20 @@ impl From<HashMap<String, Vec<(u32, f32)>>> for Vectors {
}
}

// Since we construct two new Vec's anyway it's fine to source from a reference
impl From<HashMap<String, &[(u32, f32)]>> for Vectors {
fn from(named_vectors: HashMap<String, &[(u32, f32)]>) -> Self {
Vectors {
vectors_options: Some(VectorsOptions::Vectors(NamedVectors {
vectors: named_vectors
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect(),
})),
}
}
}

impl From<Vec<f32>> for Vectors {
fn from(vector: Vec<f32>) -> Self {
Vectors {
Expand Down

0 comments on commit 85c7397

Please sign in to comment.