Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions src/traits/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ pub trait ColumnSelection {
/// # Examples
/// ```ignore
/// table.c("age") // single column by name
/// table.c(&["a", "b"]) // multiple columns by name
/// table.c(["a", "b"]) // multiple columns by name
/// table.c(&["a", "b"]) // multiple columns by name, by reference
/// table.c(0) // single column by index
/// table.c(0..3) // columns by range
/// ```
Expand All @@ -126,7 +127,8 @@ pub trait ColumnSelection {
/// # Examples
/// ```ignore
/// table.col("age") // single column by name
/// table.c(&["a", "b"]) // multiple columns by name
/// table.c(["a", "b"]) // multiple columns by name
/// table.c(&["a", "b"]) // multiple columns by name, by reference
/// table.c(0) // single column by index
/// table.c(0..3) // columns by range
/// ```
Expand Down Expand Up @@ -159,7 +161,8 @@ pub trait RowSelection {
/// # Examples
/// ```ignore
/// table.r(5) // single row
/// table.r(&[1, 3, 5]) // specific rows
/// table.r([1, 3, 5]) // specific rows
/// table.r(&[1, 3, 5]) // specific rows, by reference
/// table.r(0..10) // row range
/// ```
fn r<S: DataSelector>(&self, selection: S) -> Self::View;
Expand All @@ -172,7 +175,8 @@ pub trait RowSelection {
/// # Examples
/// ```ignore
/// table.row(5) // single row
/// table.r(&[1, 3, 5]) // specific rows
/// table.r([1, 3, 5]) // specific rows
/// table.r(&[1, 3, 5]) // specific rows, by reference
/// table.r(0..10) // row range
/// ```
fn row(&self, idx: usize) -> Self::View {
Expand Down Expand Up @@ -249,7 +253,7 @@ impl<T> Selection2D for T where
}

// These allow users to pass names, indices, and ranges when selecting fields.
// For example: table.c("age"), table.c(&["name", "age"]), table.c(0..3)
// For example: table.c("age"), table.c(["name", "age"]), table.c(&["name", "age"]), table.c(0..3)

/// Single field by name
impl FieldSelector for &str {
Expand Down Expand Up @@ -289,6 +293,18 @@ impl<const N: usize> FieldSelector for &[&str; N] {
}
}

/// Multiple fields by names (array)
impl<const N: usize> FieldSelector for [&str; N] {
fn resolve_fields(&self, fields: &[Arc<Field>]) -> Vec<usize> {
self.iter()
.filter_map(|name| fields.iter().position(|f| f.name == *name))
.collect()
}
fn to_owned(&self) -> Box<dyn FieldSelector + Send + Sync> {
Box::new(self.iter().map(|s| s.to_string()).collect::<Vec<String>>())
}
}

/// Multiple fields by names (Vec of borrowed str)
impl FieldSelector for Vec<&str> {
fn resolve_fields(&self, fields: &[Arc<Field>]) -> Vec<usize> {
Expand Down Expand Up @@ -353,6 +369,19 @@ impl<const N: usize> FieldSelector for &[usize; N] {
}
}

/// Multiple fields by indices (array)
impl<const N: usize> FieldSelector for [usize; N] {
fn resolve_fields(&self, fields: &[Arc<Field>]) -> Vec<usize> {
self.iter()
.copied()
.filter(|&idx| idx < fields.len())
.collect()
}
fn to_owned(&self) -> Box<dyn FieldSelector + Send + Sync> {
Box::new(self.to_vec())
}
}

/// Multiple fields by indices (Vec)
impl FieldSelector for Vec<usize> {
fn resolve_fields(&self, fields: &[Arc<Field>]) -> Vec<usize> {
Expand Down Expand Up @@ -431,7 +460,7 @@ impl FieldSelector for Box<dyn FieldSelector + Send + Sync> {
}

// These allow users to pass indices and ranges when selecting data (rows, time, etc.).
// For example: table.r(5), table.r(&[1, 3, 5]), table.r(0..10)
// For example: table.r(5), table.r([1, 3, 5]), table.r(&[1, 3, 5]), table.r(0..10)

/// Single data index
impl DataSelector for usize {
Expand Down Expand Up @@ -538,6 +567,23 @@ impl<const N: usize> DataSelector for &[usize; N] {
}
}

/// Multiple data indices (array)
impl<const N: usize> DataSelector for [usize; N] {
fn resolve_mask(&self, count: usize) -> Bitmask {
let mut mask = Bitmask::new_set_all(count, false);
for &idx in self.iter() {
if idx < count {
mask.set_true(idx);
}
}
mask
}

fn to_owned(&self) -> Box<dyn DataSelector + Send + Sync> {
Box::new(self.to_vec())
}
}

/// Multiple data indices (Vec)
impl DataSelector for Vec<usize> {
fn resolve_mask(&self, count: usize) -> Bitmask {
Expand Down
18 changes: 18 additions & 0 deletions tests/test_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,21 @@ fn test_data_selector_boxed_resolve_axis_forwards() {
let full: Box<dyn DataSelector + Send + Sync> = DataSelector::to_owned(&..);
assert_eq!(full.resolve_axis(5), (0, 5, false));
}

#[test]
fn test_array_selectors_equal_their_reference_forms() {
let table = create_test_table();

let by_names = table.c(["name", "value"]);
let by_names_ref = table.c(&["name", "value"]);
assert_eq!(by_names.col_names(), by_names_ref.col_names());

let by_indices = table.c([0, 2]);
let by_indices_ref = table.c(&[0, 2]);
assert_eq!(by_indices.col_names(), by_indices_ref.col_names());

let rows = table.r([0, 2, 4]);
let rows_ref = table.r(&[0, 2, 4]);
assert_eq!(rows.n_rows(), 3);
assert_eq!(rows.n_rows(), rows_ref.n_rows());
}
Loading