Skip to content

Commit

Permalink
minor changes; previous to index refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 5, 2020
1 parent b229760 commit 3854002
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions polars/src/chunked_array/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,13 @@ mod test {
builder.append_opt_series(Some(&s1));
builder.append_opt_series(Some(&s2));
let ls = builder.finish();
if let AnyType::LargeList(s) = ls.get(0) {
if let AnyType::LargeList(s) = ls.get_any(0) {
// many chunks are aggregated to one in the ListArray
assert_eq!(s.len(), 6)
} else {
assert!(false)
}
if let AnyType::LargeList(s) = ls.get(1) {
if let AnyType::LargeList(s) = ls.get_any(1) {
assert_eq!(s.len(), 3)
} else {
assert!(false)
Expand Down
6 changes: 3 additions & 3 deletions polars/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ where
}

/// Get a single value. Beware this is slow.
pub fn get(&self, index: usize) -> AnyType {
pub fn get_any(&self, index: usize) -> AnyType {
let (chunk_idx, idx) = self.index_to_chunked_index(index);
let arr = &self.chunks[chunk_idx];

Expand Down Expand Up @@ -1095,10 +1095,10 @@ pub(crate) mod test {
#[test]
fn get() {
let mut a = get_chunked_array();
assert_eq!(AnyType::Int32(2), a.get(1));
assert_eq!(AnyType::Int32(2), a.get_any(1));
// check if chunks indexes are properly determined
a.append_array(a.chunks[0].clone()).unwrap();
assert_eq!(AnyType::Int32(1), a.get(3));
assert_eq!(AnyType::Int32(1), a.get_any(3));
}

#[test]
Expand Down
26 changes: 16 additions & 10 deletions polars/src/chunked_array/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,23 @@ impl AsTakeIndex for [u32] {
}
}

/// Fast indexing in a `ChunkedArray` by doing only a one time downcast up front.
pub trait TakeRandom {
type Item;

/// Get a nullable value by index.
fn get(&self, index: usize) -> Option<Self::Item>;

/// Get a value by index and ignore the null bit.
unsafe fn get_unchecked(&self, index: usize) -> Self::Item;
}

/// Create a type that implements `TakeRandom`.
pub trait IntoTakeRandom<'a> {
type Item;
type IntoTR;
fn take_rand(&self) -> Self::IntoTR;
type TakeRandom;
/// Create a type that implements `TakeRandom`.
fn take_rand(&self) -> Self::TakeRandom;
}

/// Choose the Struct for multiple chunks or the struct for a single chunk.
Expand Down Expand Up @@ -394,9 +400,9 @@ where
T: PolarsNumericType,
{
type Item = T::Native;
type IntoTR = NumTakeRandomDispatch<'a, T>;
type TakeRandom = NumTakeRandomDispatch<'a, T>;

fn take_rand(&self) -> Self::IntoTR {
fn take_rand(&self) -> Self::TakeRandom {
match self.cont_slice() {
Ok(slice) => NumTakeRandomDispatch::Cont(NumTakeRandomCont { slice }),
_ => {
Expand All @@ -416,27 +422,27 @@ where

impl<'a> IntoTakeRandom<'a> for &'a Utf8Chunked {
type Item = &'a str;
type IntoTR = Box<dyn TakeRandom<Item = Self::Item> + 'a>;
type TakeRandom = Box<dyn TakeRandom<Item = Self::Item> + 'a>;

fn take_rand(&self) -> Self::IntoTR {
fn take_rand(&self) -> Self::TakeRandom {
many_or_single!(self, Utf8TakeRandomSingleChunk, Utf8TakeRandom)
}
}

impl<'a> IntoTakeRandom<'a> for &'a BooleanChunked {
type Item = bool;
type IntoTR = Box<dyn TakeRandom<Item = Self::Item> + 'a>;
type TakeRandom = Box<dyn TakeRandom<Item = Self::Item> + 'a>;

fn take_rand(&self) -> Self::IntoTR {
fn take_rand(&self) -> Self::TakeRandom {
many_or_single!(self, BoolTakeRandomSingleChunk, BoolTakeRandom)
}
}

impl<'a> IntoTakeRandom<'a> for &'a LargeListChunked {
type Item = Series;
type IntoTR = Box<dyn TakeRandom<Item = Self::Item> + 'a>;
type TakeRandom = Box<dyn TakeRandom<Item = Self::Item> + 'a>;

fn take_rand(&self) -> Self::IntoTR {
fn take_rand(&self) -> Self::TakeRandom {
let chunks = self.downcast_chunks();
if chunks.len() == 1 {
Box::new(ListTakeRandomSingleChunk {
Expand Down
2 changes: 1 addition & 1 deletion polars/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ impl Series {
/// Get a single value by index. Don't use this operation for loops as a runtime cast is
/// needed for every iteration.
pub fn get(&self, index: usize) -> AnyType {
apply_method_all_series!(self, get, index)
apply_method_all_series!(self, get_any, index)
}

/// Sort in place.
Expand Down

0 comments on commit 3854002

Please sign in to comment.