Trait polars_core::chunked_array::ops::TakeRandom
source · pub trait TakeRandom {
type Item;
fn get(&self, index: usize) -> Option<Self::Item>;
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item>
where
Self: Sized,
{ ... }
}
Expand description
Random access
Required Associated Types§
Required Methods§
Provided Methods§
sourceunsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item>where
Self: Sized,
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item>where
Self: Sized,
Examples found in repository?
More examples
src/chunked_array/ops/compare_inner.rs (line 89)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
unsafe fn eq_element_unchecked(&self, idx_a: usize, idx_b: usize) -> bool {
// no nulls so we can do unchecked
self.get_unchecked(idx_a) == self.get_unchecked(idx_b)
}
}
/// Create a type that implements PartialEqInner
pub(crate) trait IntoPartialEqInner<'a> {
/// Create a type that implements `TakeRandom`.
fn into_partial_eq_inner(self) -> Box<dyn PartialEqInner + 'a>;
}
/// We use a trait object because we want to call this from Series and cannot use a typed enum.
impl<'a, T> IntoPartialEqInner<'a> for &'a ChunkedArray<T>
where
T: PolarsNumericType,
{
fn into_partial_eq_inner(self) -> Box<dyn PartialEqInner + 'a> {
let mut chunks = self.downcast_iter();
if self.chunks.len() == 1 {
let arr = chunks.next().unwrap();
if !self.has_validity() {
let t = NumTakeRandomCont {
slice: arr.values(),
};
Box::new(t)
} else {
let t = NumTakeRandomSingleChunk::<'_, T::Native>::new(arr);
Box::new(t)
}
} else {
let t = NumTakeRandomChunked::<'_, T::Native> {
chunks: chunks.collect(),
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
Box::new(t)
}
}
}
impl<'a> IntoPartialEqInner<'a> for &'a Utf8Chunked {
fn into_partial_eq_inner(self) -> Box<dyn PartialEqInner + 'a> {
match self.chunks.len() {
1 => {
let arr = self.downcast_iter().next().unwrap();
let t = Utf8TakeRandomSingleChunk { arr };
Box::new(t)
}
_ => {
let chunks = self.downcast_chunks();
let t = Utf8TakeRandom {
chunks,
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
Box::new(t)
}
}
}
}
#[cfg(feature = "dtype-binary")]
impl<'a> IntoPartialEqInner<'a> for &'a BinaryChunked {
fn into_partial_eq_inner(self) -> Box<dyn PartialEqInner + 'a> {
match self.chunks.len() {
1 => {
let arr = self.downcast_iter().next().unwrap();
let t = BinaryTakeRandomSingleChunk { arr };
Box::new(t)
}
_ => {
let chunks = self.downcast_chunks();
let t = BinaryTakeRandom {
chunks,
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
Box::new(t)
}
}
}
}
impl<'a> IntoPartialEqInner<'a> for &'a BooleanChunked {
fn into_partial_eq_inner(self) -> Box<dyn PartialEqInner + 'a> {
match self.chunks.len() {
1 => {
let arr = self.downcast_iter().next().unwrap();
let t = BoolTakeRandomSingleChunk { arr };
Box::new(t)
}
_ => {
let chunks = self.downcast_chunks();
let t = BoolTakeRandom {
chunks,
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
Box::new(t)
}
}
}
}
// Partial ordering implementations
fn fallback<T: PartialEq>(a: T) -> Ordering {
// nan != nan
// this is a simple way to check if it is nan
// without convincing the compiler we deal with floats
#[allow(clippy::eq_op)]
if a != a {
Ordering::Less
} else {
Ordering::Greater
}
}
impl<T> PartialOrdInner for NumTakeRandomCont<'_, T>
where
T: Copy + PartialOrd + Sync,
{
unsafe fn cmp_element_unchecked(&self, idx_a: usize, idx_b: usize) -> Ordering {
// no nulls so we can do unchecked
let a = self.get_unchecked(idx_a);
let b = self.get_unchecked(idx_b);
a.partial_cmp(&b).unwrap_or_else(|| fallback(a))
}
src/chunked_array/ops/take/take_random.rs (line 99)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item> {
match self {
Self::SingleNoNull(s) => s.get_unchecked(index),
Self::Single(s) => s.get_unchecked(index),
Self::Multi(m) => m.get_unchecked(index),
}
}
}
pub enum TakeRandBranch2<S, M> {
Single(S),
Multi(M),
}
impl<S, M, I> TakeRandom for TakeRandBranch2<S, M>
where
S: TakeRandom<Item = I>,
M: TakeRandom<Item = I>,
{
type Item = I;
fn get(&self, index: usize) -> Option<Self::Item> {
match self {
Self::Single(s) => s.get(index),
Self::Multi(m) => m.get(index),
}
}
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item> {
match self {
Self::Single(s) => s.get_unchecked(index),
Self::Multi(m) => m.get_unchecked(index),
}
}
}
#[allow(clippy::type_complexity)]
impl<'a, T> IntoTakeRandom<'a> for &'a ChunkedArray<T>
where
T: PolarsNumericType,
{
type Item = T::Native;
type TakeRandom = TakeRandBranch3<
NumTakeRandomCont<'a, T::Native>,
NumTakeRandomSingleChunk<'a, T::Native>,
NumTakeRandomChunked<'a, T::Native>,
>;
#[inline]
fn take_rand(&self) -> Self::TakeRandom {
let mut chunks = self.downcast_iter();
if self.chunks.len() == 1 {
let arr = chunks.next().unwrap();
if !self.has_validity() {
let t = NumTakeRandomCont {
slice: arr.values(),
};
TakeRandBranch3::SingleNoNull(t)
} else {
let t = NumTakeRandomSingleChunk::new(arr);
TakeRandBranch3::Single(t)
}
} else {
let t = NumTakeRandomChunked {
chunks: chunks.collect(),
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
TakeRandBranch3::Multi(t)
}
}
}
pub struct Utf8TakeRandom<'a> {
pub(crate) chunks: Chunks<'a, Utf8Array<i64>>,
pub(crate) chunk_lens: Vec<IdxSize>,
}
impl<'a> TakeRandom for Utf8TakeRandom<'a> {
type Item = &'a str;
#[inline]
fn get(&self, index: usize) -> Option<Self::Item> {
take_random_get!(self, index)
}
#[inline]
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item> {
take_random_get_unchecked!(self, index)
}
}
pub struct Utf8TakeRandomSingleChunk<'a> {
pub(crate) arr: &'a Utf8Array<i64>,
}
impl<'a> TakeRandom for Utf8TakeRandomSingleChunk<'a> {
type Item = &'a str;
#[inline]
fn get(&self, index: usize) -> Option<Self::Item> {
take_random_get_single!(self, index)
}
#[inline]
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item> {
if self.arr.is_valid_unchecked(index) {
Some(self.arr.value_unchecked(index))
} else {
None
}
}
}
impl<'a> IntoTakeRandom<'a> for &'a Utf8Chunked {
type Item = &'a str;
type TakeRandom = TakeRandBranch2<Utf8TakeRandomSingleChunk<'a>, Utf8TakeRandom<'a>>;
fn take_rand(&self) -> Self::TakeRandom {
match self.chunks.len() {
1 => {
let arr = self.downcast_iter().next().unwrap();
let t = Utf8TakeRandomSingleChunk { arr };
TakeRandBranch2::Single(t)
}
_ => {
let chunks = self.downcast_chunks();
let t = Utf8TakeRandom {
chunks,
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
TakeRandBranch2::Multi(t)
}
}
}
}
#[cfg(feature = "dtype-binary")]
pub struct BinaryTakeRandom<'a> {
pub(crate) chunks: Chunks<'a, BinaryArray<i64>>,
pub(crate) chunk_lens: Vec<IdxSize>,
}
#[cfg(feature = "dtype-binary")]
impl<'a> TakeRandom for BinaryTakeRandom<'a> {
type Item = &'a [u8];
#[inline]
fn get(&self, index: usize) -> Option<Self::Item> {
take_random_get!(self, index)
}
#[inline]
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item> {
take_random_get_unchecked!(self, index)
}
}
#[cfg(feature = "dtype-binary")]
pub struct BinaryTakeRandomSingleChunk<'a> {
pub(crate) arr: &'a BinaryArray<i64>,
}
#[cfg(feature = "dtype-binary")]
impl<'a> TakeRandom for BinaryTakeRandomSingleChunk<'a> {
type Item = &'a [u8];
#[inline]
fn get(&self, index: usize) -> Option<Self::Item> {
take_random_get_single!(self, index)
}
#[inline]
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item> {
if self.arr.is_valid_unchecked(index) {
Some(self.arr.value_unchecked(index))
} else {
None
}
}
}
#[cfg(feature = "dtype-binary")]
impl<'a> IntoTakeRandom<'a> for &'a BinaryChunked {
type Item = &'a [u8];
type TakeRandom = TakeRandBranch2<BinaryTakeRandomSingleChunk<'a>, BinaryTakeRandom<'a>>;
fn take_rand(&self) -> Self::TakeRandom {
match self.chunks.len() {
1 => {
let arr = self.downcast_iter().next().unwrap();
let t = BinaryTakeRandomSingleChunk { arr };
TakeRandBranch2::Single(t)
}
_ => {
let chunks = self.downcast_chunks();
let t = BinaryTakeRandom {
chunks,
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
TakeRandBranch2::Multi(t)
}
}
}
}
impl<'a> IntoTakeRandom<'a> for &'a BooleanChunked {
type Item = bool;
type TakeRandom = TakeRandBranch2<BoolTakeRandomSingleChunk<'a>, BoolTakeRandom<'a>>;
fn take_rand(&self) -> Self::TakeRandom {
match self.chunks.len() {
1 => {
let arr = self.downcast_iter().next().unwrap();
let t = BoolTakeRandomSingleChunk { arr };
TakeRandBranch2::Single(t)
}
_ => {
let chunks = self.downcast_chunks();
let t = BoolTakeRandom {
chunks,
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
TakeRandBranch2::Multi(t)
}
}
}
}
impl<'a> IntoTakeRandom<'a> for &'a ListChunked {
type Item = Series;
type TakeRandom = TakeRandBranch2<ListTakeRandomSingleChunk<'a>, ListTakeRandom<'a>>;
fn take_rand(&self) -> Self::TakeRandom {
let mut chunks = self.downcast_iter();
if self.chunks.len() == 1 {
let t = ListTakeRandomSingleChunk {
arr: chunks.next().unwrap(),
name: self.name(),
};
TakeRandBranch2::Single(t)
} else {
let t = ListTakeRandom {
ca: self,
chunks: chunks.collect(),
chunk_lens: self.chunks.iter().map(|a| a.len() as IdxSize).collect(),
};
TakeRandBranch2::Multi(t)
}
}
}
pub struct NumTakeRandomChunked<'a, T>
where
T: NumericNative,
{
pub(crate) chunks: Vec<&'a PrimitiveArray<T>>,
pub(crate) chunk_lens: Vec<IdxSize>,
}
impl<'a, T> TakeRandom for NumTakeRandomChunked<'a, T>
where
T: NumericNative,
{
type Item = T;
#[inline]
fn get(&self, index: usize) -> Option<Self::Item> {
take_random_get!(self, index)
}
#[inline]
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item> {
take_random_get_unchecked!(self, index)
}
}
pub struct NumTakeRandomCont<'a, T> {
pub(crate) slice: &'a [T],
}
impl<'a, T> TakeRandom for NumTakeRandomCont<'a, T>
where
T: Copy,
{
type Item = T;
#[inline]
fn get(&self, index: usize) -> Option<Self::Item> {
self.slice.get(index).copied()
}
#[inline]
unsafe fn get_unchecked(&self, index: usize) -> Option<Self::Item> {
Some(*self.slice.get_unchecked(index))
}
}
pub struct TakeRandomBitmap<'a> {
bytes: &'a [u8],
offset: usize,
}
impl<'a> TakeRandomBitmap<'a> {
pub(crate) fn new(bitmap: &'a Bitmap) -> Self {
let (bytes, offset, _) = bitmap.as_slice();
Self { bytes, offset }
}
unsafe fn get_unchecked(&self, index: usize) -> bool {
get_bit_unchecked(self.bytes, self.offset + index)
}
}
pub struct NumTakeRandomSingleChunk<'a, T>
where
T: NumericNative,
{
pub(crate) vals: &'a [T],
pub(crate) validity: TakeRandomBitmap<'a>,
}
impl<'a, T: NumericNative> NumTakeRandomSingleChunk<'a, T> {
pub(crate) fn new(arr: &'a PrimitiveArray<T>) -> Self {
let validity = TakeRandomBitmap::new(arr.validity().unwrap());
let vals = arr.values();
NumTakeRandomSingleChunk { vals, validity }
}
}
impl<'a, T> TakeRandom for NumTakeRandomSingleChunk<'a, T>
where
T: NumericNative,
{
type Item = T;
#[inline]
fn get(&self, index: usize) -> Option<Self::Item> {
if index < self.vals.len() {
unsafe { self.get_unchecked(index) }
} else {
None
}
}
src/chunked_array/logical/categorical/ops/take_random.rs (line 39)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
unsafe fn cmp_element_unchecked(&self, idx_a: usize, idx_b: usize) -> Ordering {
let a = self
.cats
.get_unchecked(idx_a)
.map(|cat| self.rev_map.value_unchecked(cat as usize));
let b = self
.cats
.get_unchecked(idx_b)
.map(|cat| self.rev_map.value_unchecked(cat as usize));
a.partial_cmp(&b).unwrap()
}
}
pub(crate) struct CategoricalTakeRandomGlobal<'a> {
rev_map_part_1: &'a PlHashMap<u32, u32>,
rev_map_part_2: &'a Utf8Array<i64>,
cats: TakeCats<'a>,
}
impl<'a> CategoricalTakeRandomGlobal<'a> {
pub(crate) fn new(ca: &'a CategoricalChunked) -> Self {
// should be rechunked upstream
assert_eq!(ca.logical.chunks.len(), 1, "implementation error");
if let RevMapping::Global(rev_map_part_1, rev_map_part_2, _) = &**ca.get_rev_map() {
let cats = ca.logical().take_rand();
Self {
rev_map_part_1,
rev_map_part_2,
cats,
}
} else {
unreachable!()
}
}
}
impl PartialOrdInner for CategoricalTakeRandomGlobal<'_> {
unsafe fn cmp_element_unchecked(&self, idx_a: usize, idx_b: usize) -> Ordering {
let a = self.cats.get_unchecked(idx_a).map(|cat| {
let idx = self.rev_map_part_1.get(&cat).unwrap();
self.rev_map_part_2.value_unchecked(*idx as usize)
});
let b = self.cats.get_unchecked(idx_b).map(|cat| {
let idx = self.rev_map_part_1.get(&cat).unwrap();
self.rev_map_part_2.value_unchecked(*idx as usize)
});
a.partial_cmp(&b).unwrap()
}
src/frame/hash_join/mod.rs (line 217)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
fn zip_outer_join_column(
&self,
right_column: &Series,
opt_join_tuples: &[(Option<IdxSize>, Option<IdxSize>)],
) -> Series {
let right_ca = self.unpack_series_matching_type(right_column).unwrap();
let left_rand_access = self.take_rand();
let right_rand_access = right_ca.take_rand();
opt_join_tuples
.iter()
.map(|(opt_left_idx, opt_right_idx)| {
if let Some(left_idx) = opt_left_idx {
unsafe { left_rand_access.get_unchecked(*left_idx as usize) }
} else {
unsafe {
let right_idx = opt_right_idx.unwrap_unchecked();
right_rand_access.get_unchecked(right_idx as usize)
}
}
})
.collect_trusted::<ChunkedArray<T>>()
.into_series()
}
Additional examples can be found in: