This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b950b9
commit cf2ea2e
Showing
2 changed files
with
22 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,20 @@ | ||
// This example demos how to operate on arrays in-place. | ||
use arrow2::{ | ||
array::{Array, PrimitiveArray}, | ||
types::NativeType, | ||
}; | ||
|
||
// this function will clone-on-write the array and apply `f` to its values | ||
fn cow_apply<T: NativeType, F: Fn(&mut [T])>(array: &mut dyn Array, f: F) { | ||
// 1. downcast the array to its concrete type | ||
let array = array | ||
.as_any_mut() | ||
.downcast_mut::<PrimitiveArray<T>>() | ||
.unwrap(); | ||
|
||
// 2. empty the mut reference and create a new array on the stack with its contents | ||
let new_array = array.take(); | ||
|
||
// 3. deconstruct the array into its parts | ||
let (dt, values, validity) = new_array.into_inner(); | ||
|
||
// 4. clone-on-write the values | ||
let mut values = values.make_mut(); | ||
|
||
// 5. apply the function over the values | ||
f(&mut values); | ||
|
||
// 6. assign the new values to the array | ||
array.try_assign(dt, values.into(), validity).unwrap(); | ||
} | ||
use arrow2::array::{Array, PrimitiveArray}; | ||
|
||
fn main() { | ||
// say we have have received an array | ||
let mut array = PrimitiveArray::from_vec(vec![1i32, 2]).boxed(); | ||
let mut array: Box<dyn Array> = PrimitiveArray::from_vec(vec![1i32, 2]).boxed(); | ||
|
||
// we can apply a transformation to its values without allocating a new array as follows: | ||
cow_apply(array.as_mut(), |values: &mut [i32]| { | ||
values.iter_mut().for_each(|x| *x *= 10) | ||
}); | ||
// 1. downcast it to the correct type (known via `array.data_type().to_physical_type()`) | ||
let array = array | ||
.as_any_mut() | ||
.downcast_mut::<PrimitiveArray<i32>>() | ||
.unwrap(); | ||
|
||
// 2. call `apply_values` with the function to apply over the values | ||
array.apply_values(|x| x.iter_mut().for_each(|x| *x *= 10)); | ||
|
||
// confirm that it gives the right result :) | ||
assert_eq!(array.as_ref(), PrimitiveArray::from_vec(vec![10i32, 20])); | ||
assert_eq!(array, &PrimitiveArray::from_vec(vec![10i32, 20])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters