polars_core/chunked_array/iterator/par/
string.rs

1use rayon::prelude::*;
2
3use crate::prelude::*;
4
5#[inline]
6unsafe fn idx_to_str(idx: usize, arr: &Utf8ViewArray) -> Option<&str> {
7    if arr.is_valid(idx) {
8        Some(arr.value_unchecked(idx))
9    } else {
10        None
11    }
12}
13
14impl StringChunked {
15    pub fn par_iter_indexed(&self) -> impl IndexedParallelIterator<Item = Option<&str>> {
16        assert_eq!(self.chunks.len(), 1);
17        let arr = &*self.chunks[0];
18
19        // SAFETY:
20        // guarded by the type system
21        let arr = unsafe { &*(arr as *const dyn Array as *const Utf8ViewArray) };
22        (0..arr.len())
23            .into_par_iter()
24            .map(move |idx| unsafe { idx_to_str(idx, arr) })
25    }
26
27    pub fn par_iter(&self) -> impl ParallelIterator<Item = Option<&str>> + '_ {
28        self.chunks.par_iter().flat_map(move |arr| {
29            // SAFETY:
30            // guarded by the type system
31            let arr = &**arr;
32            let arr = unsafe { &*(arr as *const dyn Array as *const Utf8ViewArray) };
33            (0..arr.len())
34                .into_par_iter()
35                .map(move |idx| unsafe { idx_to_str(idx, arr) })
36        })
37    }
38}