• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16992335779

15 Aug 2025 02:38PM UTC coverage: 87.202% (-0.5%) from 87.72%
16992335779

Pull #2456

github

web-flow
Merge 39c262e2c into 4a23f65b3
Pull Request #2456: feat: basic BoolBuffer / BoolBufferMut

476 of 1231 new or added lines in 107 files covered. (38.67%)

74 existing lines in 19 files now uncovered.

56525 of 64821 relevant lines covered (87.2%)

623742.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.81
/vortex-array/src/patches.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use std::cmp::Ordering;
5
use std::fmt::Debug;
6
use std::hash::Hash;
7

8
use arrow_buffer::BooleanBuffer;
9
use itertools::Itertools as _;
10
use num_traits::{NumCast, ToPrimitive};
11
use serde::{Deserialize, Serialize};
12
use vortex_buffer::BufferMut;
13
use vortex_dtype::Nullability::NonNullable;
14
use vortex_dtype::{
15
    DType, NativePType, PType, match_each_integer_ptype, match_each_unsigned_integer_ptype,
16
};
17
use vortex_error::{
18
    VortexError, VortexExpect, VortexResult, VortexUnwrap, vortex_bail, vortex_err,
19
};
20
use vortex_mask::{AllOr, Mask};
21
use vortex_scalar::{PValue, Scalar};
22
use vortex_utils::aliases::hash_map::HashMap;
23

24
use crate::arrays::PrimitiveArray;
25
use crate::compute::{cast, filter, take};
26
use crate::search_sorted::{SearchResult, SearchSorted, SearchSortedSide};
27
use crate::vtable::ValidityHelper;
28
use crate::{Array, ArrayRef, IntoArray, ToCanonical};
29

30
#[derive(Copy, Clone, Serialize, Deserialize, prost::Message)]
31
pub struct PatchesMetadata {
32
    #[prost(uint64, tag = "1")]
33
    len: u64,
34
    #[prost(uint64, tag = "2")]
35
    offset: u64,
36
    #[prost(enumeration = "PType", tag = "3")]
37
    indices_ptype: i32,
38
}
39

40
impl PatchesMetadata {
41
    pub fn new(len: usize, offset: usize, indices_ptype: PType) -> Self {
78✔
42
        Self {
78✔
43
            len: len as u64,
78✔
44
            offset: offset as u64,
78✔
45
            indices_ptype: indices_ptype as i32,
78✔
46
        }
78✔
47
    }
78✔
48

49
    #[inline]
4✔
50
    pub fn len(&self) -> usize {
1,972✔
51
        usize::try_from(self.len).vortex_expect("len is a valid usize")
1,972✔
52
    }
1,968✔
53

54
    #[inline]
55
    pub fn is_empty(&self) -> bool {
×
56
        self.len == 0
×
57
    }
58

59
    #[inline]
2✔
60
    pub fn offset(&self) -> usize {
986✔
61
        usize::try_from(self.offset).vortex_expect("offset is a valid usize")
986✔
62
    }
984✔
63

64
    #[inline]
2✔
65
    pub fn indices_dtype(&self) -> DType {
986✔
66
        assert!(
986✔
67
            self.indices_ptype().is_unsigned_int(),
984✔
68
            "Patch indices must be unsigned integers"
69
        );
2✔
70
        DType::Primitive(self.indices_ptype(), NonNullable)
986✔
71
    }
984✔
72
}
73

74
/// A helper for working with patched arrays.
75
#[derive(Debug, Clone)]
76
pub struct Patches {
77
    array_len: usize,
78
    offset: usize,
79
    indices: ArrayRef,
80
    values: ArrayRef,
81
}
82

83
impl Patches {
10✔
84
    pub fn new(array_len: usize, offset: usize, indices: ArrayRef, values: ArrayRef) -> Self {
42,164✔
85
        assert_eq!(
42,164✔
86
            indices.len(),
42,164✔
87
            values.len(),
42,154✔
88
            "Patch indices and values must have the same length"
89
        );
10✔
90
        assert!(
42,164✔
91
            indices.dtype().is_unsigned_int() && !indices.dtype().is_nullable(),
42,154✔
92
            "Patch indices must be non-nullable unsigned integers, got {:?}",
1✔
93
            indices.dtype()
1✔
94
        );
10✔
95
        assert!(
42,163✔
96
            indices.len() <= array_len,
42,153✔
97
            "Patch indices must be shorter than the array length"
98
        );
10✔
99
        assert!(!indices.is_empty(), "Patch indices must not be empty");
42,163✔
100
        let max = usize::try_from(
42,163✔
101
            &indices
42,163✔
102
                .scalar_at(indices.len() - 1)
42,163✔
103
                .vortex_expect("indices are not empty"),
42,153✔
104
        )
10✔
105
        .vortex_expect("indices must be a number");
42,163✔
106
        assert!(
42,163✔
107
            max - offset < array_len,
42,153✔
108
            "Patch indices {max:?}, offset {offset} are longer than the array length {array_len}"
109
        );
10✔
110
        Self::new_unchecked(array_len, offset, indices, values)
42,163✔
111
    }
42,153✔
112

113
    /// Construct new patches without validating any of the arguments
114
    ///
115
    /// # Safety
116
    ///
117
    /// Users have to assert that
118
    /// * Indices and values have the same length
119
    /// * Indices is an unsigned integer type
120
    /// * Indices must be sorted
121
    /// * Last value in indices is smaller than array_len
10✔
122
    pub fn new_unchecked(
50,322✔
123
        array_len: usize,
50,322✔
124
        offset: usize,
50,322✔
125
        indices: ArrayRef,
50,322✔
126
        values: ArrayRef,
50,322✔
127
    ) -> Self {
50,322✔
128
        Self {
50,322✔
129
            array_len,
50,322✔
130
            offset,
50,322✔
131
            indices,
50,322✔
132
            values,
50,322✔
133
        }
50,322✔
134
    }
50,312✔
135

4✔
136
    pub fn array_len(&self) -> usize {
2,170,632✔
137
        self.array_len
2,170,632✔
138
    }
2,170,628✔
139

140
    pub fn num_patches(&self) -> usize {
14,548✔
141
        self.indices.len()
14,548✔
142
    }
14,548✔
143

144
    pub fn dtype(&self) -> &DType {
8,847✔
145
        self.values.dtype()
8,847✔
146
    }
8,847✔
147

16✔
148
    pub fn indices(&self) -> &ArrayRef {
62,531✔
149
        &self.indices
62,531✔
150
    }
62,515✔
151

152
    pub fn into_indices(self) -> ArrayRef {
×
153
        self.indices
×
154
    }
155

156
    pub fn indices_mut(&mut self) -> &mut ArrayRef {
×
157
        &mut self.indices
×
158
    }
159

16✔
160
    pub fn values(&self) -> &ArrayRef {
271,977✔
161
        &self.values
271,977✔
162
    }
271,961✔
163

164
    pub fn into_values(self) -> ArrayRef {
702✔
165
        self.values
702✔
166
    }
702✔
167

168
    pub fn values_mut(&mut self) -> &mut ArrayRef {
×
169
        &mut self.values
×
170
    }
171

4✔
172
    pub fn offset(&self) -> usize {
131,654✔
173
        self.offset
131,654✔
174
    }
131,650✔
175

176
    pub fn indices_ptype(&self) -> PType {
×
177
        PType::try_from(self.indices.dtype()).vortex_expect("primitive indices")
×
178
    }
179

4✔
180
    pub fn to_metadata(&self, len: usize, dtype: &DType) -> VortexResult<PatchesMetadata> {
274✔
181
        if self.indices.len() > len {
270✔
182
            vortex_bail!(
×
183
                "Patch indices {} are longer than the array length {}",
×
184
                self.indices.len(),
185
                len
186
            );
4✔
187
        }
274✔
188
        if self.values.dtype() != dtype {
270✔
189
            vortex_bail!(
×
190
                "Patch values dtype {} does not match array dtype {}",
×
191
                self.values.dtype(),
192
                dtype
193
            );
4✔
194
        }
274✔
195
        Ok(PatchesMetadata {
274✔
196
            len: self.indices.len() as u64,
274✔
197
            offset: self.offset as u64,
274✔
198
            indices_ptype: PType::try_from(self.indices.dtype()).vortex_expect("primitive indices")
274✔
199
                as i32,
274✔
200
        })
274✔
201
    }
270✔
202

203
    pub fn cast_values(self, values_dtype: &DType) -> VortexResult<Self> {
7,995✔
204
        Ok(Self::new_unchecked(
7,995✔
205
            self.array_len,
7,995✔
206
            self.offset,
7,995✔
207
            self.indices,
7,995✔
208
            cast(&self.values, values_dtype)?,
7,995✔
209
        ))
210
    }
7,995✔
211

212
    /// Get the patched value at a given index if it exists.
213
    pub fn get_patched(&self, index: usize) -> VortexResult<Option<Scalar>> {
2,767,242✔
214
        if let Some(patch_idx) = self.search_index(index)?.to_found() {
2,767,242✔
215
            self.values().scalar_at(patch_idx).map(Some)
167,094✔
216
        } else {
217
            Ok(None)
2,600,148✔
218
        }
219
    }
2,767,242✔
220

221
    /// Return the insertion point of `index` in the [Self::indices].
222
    pub fn search_index(&self, index: usize) -> VortexResult<SearchResult> {
2,776,697✔
223
        Ok(self.indices.as_primitive_typed().search_sorted(
2,776,697✔
224
            &PValue::U64((index + self.offset) as u64),
2,776,697✔
225
            SearchSortedSide::Left,
2,776,697✔
226
        ))
2,776,697✔
227
    }
2,776,697✔
228

229
    /// Return the search_sorted result for the given target re-mapped into the original indices.
230
    pub fn search_sorted<T: Into<Scalar>>(
11✔
231
        &self,
11✔
232
        target: T,
11✔
233
        side: SearchSortedSide,
11✔
234
    ) -> VortexResult<SearchResult> {
11✔
235
        let target = target.into();
11✔
236

237
        let sr = if self.values().dtype().is_primitive() {
11✔
238
            self.values()
11✔
239
                .as_primitive_typed()
11✔
240
                .search_sorted(&target.as_primitive().pvalue(), side)
11✔
241
        } else {
242
            self.values().search_sorted(&target, side)
243
        };
244

245
        let index_idx = sr.to_offsets_index(self.indices().len(), side);
11✔
246
        let index = usize::try_from(&self.indices().scalar_at(index_idx)?)? - self.offset;
11✔
247
        Ok(match sr {
11✔
248
            // If we reached the end of patched values when searching then the result is one after the last patch index
249
            SearchResult::Found(i) => SearchResult::Found(
3✔
250
                if i == self.indices().len() || side == SearchSortedSide::Right {
3✔
251
                    index + 1
2✔
252
                } else {
253
                    index
1✔
254
                },
255
            ),
256
            // If the result is NotFound we should return index that's one after the nearest not found index for the corresponding value
257
            SearchResult::NotFound(i) => {
8✔
258
                SearchResult::NotFound(if i == 0 { index } else { index + 1 })
8✔
259
            }
260
        })
261
    }
11✔
262

263
    /// Returns the minimum patch index
264
    pub fn min_index(&self) -> VortexResult<usize> {
5,774✔
265
        Ok(usize::try_from(&self.indices().scalar_at(0)?)? - self.offset)
5,774✔
266
    }
5,774✔
267

268
    /// Returns the maximum patch index
269
    pub fn max_index(&self) -> VortexResult<usize> {
5,774✔
270
        Ok(usize::try_from(&self.indices().scalar_at(self.indices().len() - 1)?)? - self.offset)
5,774✔
271
    }
5,774✔
272

273
    /// Filter the patches by a mask, resulting in new patches for the filtered array.
274
    pub fn filter(&self, mask: &Mask) -> VortexResult<Option<Self>> {
4,546✔
275
        if mask.len() != self.array_len {
4,546✔
276
            vortex_bail!(
×
277
                "Filter mask length {} does not match array length {}",
×
278
                mask.len(),
279
                self.array_len
280
            );
281
        }
4,546✔
282

283
        match mask.indices() {
4,546✔
284
            AllOr::All => Ok(Some(self.clone())),
1✔
285
            AllOr::None => Ok(None),
1✔
286
            AllOr::Some(mask_indices) => {
4,544✔
287
                let flat_indices = self.indices().to_primitive()?;
4,544✔
288
                match_each_unsigned_integer_ptype!(flat_indices.ptype(), |I| {
4,544✔
289
                    filter_patches_with_mask(
×
290
                        flat_indices.as_slice::<I>(),
×
291
                        self.offset(),
×
292
                        self.values(),
×
293
                        mask_indices,
294
                    )
295
                })
296
            }
297
        }
298
    }
4,546✔
299

300
    /// Mask the patches, REMOVING the patches where the mask is true.
301
    /// Unlike filter, this preserves the patch indices.
302
    /// Unlike mask on a single array, this does not set masked values to null.
303
    pub fn mask(&self, mask: &Mask) -> VortexResult<Option<Self>> {
169✔
304
        if mask.len() != self.array_len {
169✔
305
            vortex_bail!(
1✔
306
                "Filter mask length {} does not match array length {}",
1✔
307
                mask.len(),
1✔
308
                self.array_len
309
            );
310
        }
168✔
311

312
        let filter_mask = match mask.boolean_buffer() {
168✔
313
            AllOr::All => return Ok(None),
1✔
314
            AllOr::None => return Ok(Some(self.clone())),
1✔
315
            AllOr::Some(masked) => {
166✔
316
                let patch_indices = self.indices().to_primitive()?;
166✔
317
                match_each_unsigned_integer_ptype!(patch_indices.ptype(), |P| {
166✔
NEW
318
                    let patch_indices = patch_indices.as_slice::<P>();
×
319
                    Mask::from_buffer(BooleanBuffer::collect_bool(patch_indices.len(), |i| {
884✔
320
                        #[allow(clippy::cast_possible_truncation)]
321
                        let idx = (patch_indices[i] as usize) - self.offset;
884✔
322
                        !masked.value(idx)
884✔
323
                    }))
884✔
324
                })
325
            }
326
        };
327

328
        if filter_mask.all_false() {
166✔
329
            return Ok(None);
2✔
330
        }
164✔
331

332
        Ok(Some(Self::new_unchecked(
164✔
333
            self.array_len,
164✔
334
            self.offset,
164✔
335
            filter(&self.indices, &filter_mask)?,
164✔
336
            filter(&self.values, &filter_mask)?,
164✔
337
        )))
338
    }
169✔
339

340
    /// Slice the patches by a range of the patched array.
341
    pub fn slice(&self, start: usize, stop: usize) -> VortexResult<Option<Self>> {
4,724✔
342
        let patch_start = self.search_index(start)?.to_index();
4,724✔
343
        let patch_stop = self.search_index(stop)?.to_index();
4,724✔
344

345
        if patch_start == patch_stop {
4,724✔
346
            return Ok(None);
1,291✔
347
        }
3,433✔
348

349
        // Slice out the values and indices
350
        let values = self.values().slice(patch_start, patch_stop)?;
3,433✔
351
        let indices = self.indices().slice(patch_start, patch_stop)?;
3,433✔
352

353
        Ok(Some(Self::new(
3,433✔
354
            stop - start,
3,433✔
355
            start + self.offset(),
3,433✔
356
            indices,
3,433✔
357
            values,
3,433✔
358
        )))
3,433✔
359
    }
4,724✔
360

361
    // https://docs.google.com/spreadsheets/d/1D9vBZ1QJ6mwcIvV5wIL0hjGgVchcEnAyhvitqWu2ugU
362
    const PREFER_MAP_WHEN_PATCHES_OVER_INDICES_LESS_THAN: f64 = 5.0;
363

364
    fn is_map_faster_than_search(&self, take_indices: &PrimitiveArray) -> bool {
6,124✔
365
        (self.num_patches() as f64 / take_indices.len() as f64)
6,124✔
366
            < Self::PREFER_MAP_WHEN_PATCHES_OVER_INDICES_LESS_THAN
6,124✔
367
    }
6,124✔
368

369
    /// Take the indices from the patches
370
    ///
371
    /// Any nulls in take_indices are added to the resulting patches.
372
    pub fn take_with_nulls(&self, take_indices: &dyn Array) -> VortexResult<Option<Self>> {
1,872✔
373
        if take_indices.is_empty() {
1,872✔
UNCOV
374
            return Ok(None);
×
375
        }
1,872✔
376

377
        let take_indices = take_indices.to_primitive()?;
1,872✔
378
        if self.is_map_faster_than_search(&take_indices) {
1,872✔
379
            self.take_map(take_indices, true)
1,833✔
380
        } else {
381
            self.take_search(take_indices, true)
39✔
382
        }
383
    }
1,872✔
384

385
    /// Take the indices from the patches.
386
    ///
387
    /// Any nulls in take_indices are ignored.
388
    pub fn take(&self, take_indices: &dyn Array) -> VortexResult<Option<Self>> {
4,252✔
389
        if take_indices.is_empty() {
4,252✔
UNCOV
390
            return Ok(None);
×
391
        }
4,252✔
392

393
        let take_indices = take_indices.to_primitive()?;
4,252✔
394
        if self.is_map_faster_than_search(&take_indices) {
4,252✔
395
            self.take_map(take_indices, false)
3,940✔
396
        } else {
397
            self.take_search(take_indices, false)
312✔
398
        }
399
    }
4,252✔
400

401
    pub fn take_search(
351✔
402
        &self,
351✔
403
        take_indices: PrimitiveArray,
351✔
404
        include_nulls: bool,
351✔
405
    ) -> VortexResult<Option<Self>> {
351✔
406
        let indices = self.indices.to_primitive()?;
351✔
407
        let new_length = take_indices.len();
351✔
408

409
        let Some((new_indices, values_indices)) =
273✔
410
            match_each_unsigned_integer_ptype!(indices.ptype(), |Indices| {
351✔
411
                match_each_integer_ptype!(take_indices.ptype(), |TakeIndices| {
×
412
                    take_search::<_, TakeIndices>(
×
413
                        indices.as_slice::<Indices>(),
×
414
                        take_indices,
×
415
                        self.offset(),
×
416
                        include_nulls,
×
417
                    )?
418
                })
419
            })
420
        else {
421
            return Ok(None);
78✔
422
        };
423

424
        Ok(Some(Self::new(
273✔
425
            new_length,
273✔
426
            0,
427
            new_indices,
273✔
428
            take(self.values(), &values_indices)?,
273✔
429
        )))
430
    }
351✔
431

432
    pub fn take_map(
5,773✔
433
        &self,
5,773✔
434
        take_indices: PrimitiveArray,
5,773✔
435
        include_nulls: bool,
5,773✔
436
    ) -> VortexResult<Option<Self>> {
5,773✔
437
        let indices = self.indices.to_primitive()?;
5,773✔
438
        let new_length = take_indices.len();
5,773✔
439

440
        let Some((new_sparse_indices, value_indices)) =
4,096✔
441
            match_each_unsigned_integer_ptype!(indices.ptype(), |Indices| {
5,773✔
442
                match_each_integer_ptype!(take_indices.ptype(), |TakeIndices| {
78✔
443
                    take_map::<_, TakeIndices>(
×
444
                        indices.as_slice::<Indices>(),
×
445
                        take_indices,
×
446
                        self.offset(),
×
447
                        self.min_index()?,
×
448
                        self.max_index()?,
×
449
                        include_nulls,
×
450
                    )?
451
                })
452
            })
453
        else {
454
            return Ok(None);
1,677✔
455
        };
456

457
        Ok(Some(Patches::new(
4,096✔
458
            new_length,
4,096✔
459
            0,
460
            new_sparse_indices,
4,096✔
461
            take(self.values(), &value_indices)?,
4,096✔
462
        )))
463
    }
5,773✔
464

465
    pub fn map_values<F>(self, f: F) -> VortexResult<Self>
360✔
466
    where
360✔
467
        F: FnOnce(ArrayRef) -> VortexResult<ArrayRef>,
360✔
468
    {
469
        let values = f(self.values)?;
360✔
470
        if self.indices.len() != values.len() {
332✔
471
            vortex_bail!(
×
472
                "map_values must preserve length: expected {} received {}",
×
473
                self.indices.len(),
×
474
                values.len()
475
            )
476
        }
332✔
477
        Ok(Self::new(self.array_len, self.offset, self.indices, values))
332✔
478
    }
360✔
479
}
480

481
fn take_search<I: NativePType + NumCast + PartialOrd, T: NativePType + NumCast>(
351✔
482
    indices: &[I],
351✔
483
    take_indices: PrimitiveArray,
351✔
484
    indices_offset: usize,
351✔
485
    include_nulls: bool,
351✔
486
) -> VortexResult<Option<(ArrayRef, ArrayRef)>>
351✔
487
where
351✔
488
    usize: TryFrom<T>,
351✔
489
    VortexError: From<<usize as TryFrom<T>>::Error>,
351✔
490
{
491
    let take_indices_validity = take_indices.validity();
351✔
492
    let indices_offset = I::from(indices_offset).vortex_expect("indices_offset out of range");
351✔
493

494
    let (values_indices, new_indices): (BufferMut<u64>, BufferMut<u64>) = take_indices
351✔
495
        .as_slice::<T>()
351✔
496
        .iter()
351✔
497
        .enumerate()
351✔
498
        .filter_map(|(i, &v)| {
1,365✔
499
            I::from(v)
1,365✔
500
                .and_then(|v| {
1,365✔
501
                    // If we have to take nulls the take index doesn't matter, make it 0 for consistency
502
                    if include_nulls && take_indices_validity.is_null(i).vortex_unwrap() {
1,365✔
503
                        Some(0)
504
                    } else {
505
                        indices
1,365✔
506
                            .search_sorted(&(v + indices_offset), SearchSortedSide::Left)
1,365✔
507
                            .to_found()
1,365✔
508
                            .map(|patch_idx| patch_idx as u64)
1,365✔
509
                    }
510
                })
1,365✔
511
                .map(|patch_idx| (patch_idx, i as u64))
1,365✔
512
        })
1,365✔
513
        .unzip();
351✔
514

515
    if new_indices.is_empty() {
351✔
516
        return Ok(None);
78✔
517
    }
273✔
518

519
    let new_indices = new_indices.into_array();
273✔
520
    let values_validity = take_indices_validity.take(&new_indices)?;
273✔
521
    Ok(Some((
273✔
522
        new_indices,
273✔
523
        PrimitiveArray::new(values_indices, values_validity).into_array(),
273✔
524
    )))
273✔
525
}
351✔
526

527
fn take_map<I: NativePType + Hash + Eq + TryFrom<usize>, T: NativePType>(
5,773✔
528
    indices: &[I],
5,773✔
529
    take_indices: PrimitiveArray,
5,773✔
530
    indices_offset: usize,
5,773✔
531
    min_index: usize,
5,773✔
532
    max_index: usize,
5,773✔
533
    include_nulls: bool,
5,773✔
534
) -> VortexResult<Option<(ArrayRef, ArrayRef)>>
5,773✔
535
where
5,773✔
536
    usize: TryFrom<T>,
5,773✔
537
    VortexError: From<<I as TryFrom<usize>>::Error>,
5,773✔
538
{
539
    let take_indices_validity = take_indices.validity();
5,773✔
540
    let take_indices = take_indices.as_slice::<T>();
5,773✔
541
    let offset_i = I::try_from(indices_offset)?;
5,773✔
542

543
    let sparse_index_to_value_index: HashMap<I, usize> = indices
5,773✔
544
        .iter()
5,773✔
545
        .copied()
5,773✔
546
        .map(|idx| idx - offset_i)
17,085✔
547
        .enumerate()
5,773✔
548
        .map(|(value_index, sparse_index)| (sparse_index, value_index))
17,085✔
549
        .collect();
5,773✔
550

551
    let (new_sparse_indices, value_indices): (BufferMut<u64>, BufferMut<u64>) = take_indices
5,773✔
552
        .iter()
5,773✔
553
        .copied()
5,773✔
554
        .map(usize::try_from)
5,773✔
555
        .process_results(|iter| {
5,773✔
556
            iter.enumerate()
5,773✔
557
                .filter_map(|(idx_in_take, ti)| {
241,412✔
558
                    // If we have to take nulls the take index doesn't matter, make it 0 for consistency
559
                    if include_nulls && take_indices_validity.is_null(idx_in_take).vortex_unwrap() {
241,412✔
560
                        Some((idx_in_take as u64, 0))
351✔
561
                    } else if ti < min_index || ti > max_index {
241,061✔
562
                        None
53,470✔
563
                    } else {
564
                        sparse_index_to_value_index
187,591✔
565
                            .get(
187,591✔
566
                                &I::try_from(ti)
187,591✔
567
                                    .vortex_expect("take index is between min and max index"),
187,591✔
568
                            )
569
                            .map(|value_index| (idx_in_take as u64, *value_index as u64))
187,591✔
570
                    }
571
                })
241,412✔
572
                .unzip()
5,773✔
573
        })
5,773✔
574
        .map_err(|_| vortex_err!("Failed to convert index to usize"))?;
5,773✔
575

576
    if new_sparse_indices.is_empty() {
5,773✔
577
        return Ok(None);
1,677✔
578
    }
4,096✔
579

580
    let new_sparse_indices = new_sparse_indices.into_array();
4,096✔
581
    let values_validity = take_indices_validity.take(&new_sparse_indices)?;
4,096✔
582
    Ok(Some((
4,096✔
583
        new_sparse_indices,
4,096✔
584
        PrimitiveArray::new(value_indices, values_validity).into_array(),
4,096✔
585
    )))
4,096✔
586
}
5,773✔
587

588
/// Filter patches with the provided mask (in flattened space).
589
///
590
/// The filter mask may contain indices that are non-patched. The return value of this function
591
/// is a new set of `Patches` with the indices relative to the provided `mask` rank, and the
592
/// patch values.
593
fn filter_patches_with_mask<T: ToPrimitive + Copy + Ord>(
4,544✔
594
    patch_indices: &[T],
4,544✔
595
    offset: usize,
4,544✔
596
    patch_values: &dyn Array,
4,544✔
597
    mask_indices: &[usize],
4,544✔
598
) -> VortexResult<Option<Patches>> {
4,544✔
599
    let true_count = mask_indices.len();
4,544✔
600
    let mut new_patch_indices = BufferMut::<u64>::with_capacity(true_count);
4,544✔
601
    let mut new_mask_indices = Vec::with_capacity(true_count);
4,544✔
602

603
    // Attempt to move the window by `STRIDE` elements on each iteration. This assumes that
604
    // the patches are relatively sparse compared to the overall mask, and so many indices in the
605
    // mask will end up being skipped.
606
    const STRIDE: usize = 4;
607

608
    let mut mask_idx = 0usize;
4,544✔
609
    let mut true_idx = 0usize;
4,544✔
610

611
    while mask_idx < patch_indices.len() && true_idx < true_count {
634,373✔
612
        // NOTE: we are searching for overlaps between sorted, unaligned indices in `patch_indices`
613
        //  and `mask_indices`. We assume that Patches are sparse relative to the global space of
614
        //  the mask (which covers both patch and non-patch values of the parent array), and so to
615
        //  quickly jump through regions with no overlap, we attempt to move our pointers by STRIDE
616
        //  elements on each iteration. If we cannot rule out overlap due to min/max values, we
617
        //  fallback to performing a two-way iterator merge.
618
        if (mask_idx + STRIDE) < patch_indices.len() && (true_idx + STRIDE) < mask_indices.len() {
629,829✔
619
            // Load a vector of each into our registers.
620
            let left_min = patch_indices[mask_idx].to_usize().vortex_expect("left_min") - offset;
486,108✔
621
            let left_max = patch_indices[mask_idx + STRIDE]
486,108✔
622
                .to_usize()
486,108✔
623
                .vortex_expect("left_max")
486,108✔
624
                - offset;
486,108✔
625
            let right_min = mask_indices[true_idx];
486,108✔
626
            let right_max = mask_indices[true_idx + STRIDE];
486,108✔
627

628
            if left_min > right_max {
486,108✔
629
                // Advance right side
630
                true_idx += STRIDE;
52,503✔
631
                continue;
52,503✔
632
            } else if right_min > left_max {
433,605✔
633
                mask_idx += STRIDE;
11,874✔
634
                continue;
11,874✔
635
            } else {
421,731✔
636
                // Fallthrough to direct comparison path.
421,731✔
637
            }
421,731✔
638
        }
143,721✔
639

640
        // Two-way sorted iterator merge:
641

642
        let left = patch_indices[mask_idx].to_usize().vortex_expect("left") - offset;
565,452✔
643
        let right = mask_indices[true_idx];
565,452✔
644

645
        match left.cmp(&right) {
565,452✔
646
            Ordering::Less => {
188,639✔
647
                mask_idx += 1;
188,639✔
648
            }
188,639✔
649
            Ordering::Greater => {
365,949✔
650
                true_idx += 1;
365,949✔
651
            }
365,949✔
652
            Ordering::Equal => {
10,864✔
653
                // Save the mask index as well as the positional index.
10,864✔
654
                new_mask_indices.push(mask_idx);
10,864✔
655
                new_patch_indices.push(true_idx as u64);
10,864✔
656

10,864✔
657
                mask_idx += 1;
10,864✔
658
                true_idx += 1;
10,864✔
659
            }
10,864✔
660
        }
661
    }
662

663
    if new_mask_indices.is_empty() {
4,544✔
664
        return Ok(None);
1,101✔
665
    }
3,443✔
666

667
    let new_patch_indices = new_patch_indices.into_array();
3,443✔
668
    let new_patch_values = filter(
3,443✔
669
        patch_values,
3,443✔
670
        &Mask::from_indices(patch_values.len(), new_mask_indices),
3,443✔
671
    )?;
672

673
    Ok(Some(Patches::new(
3,443✔
674
        true_count,
3,443✔
675
        0,
3,443✔
676
        new_patch_indices,
3,443✔
677
        new_patch_values,
3,443✔
678
    )))
3,443✔
679
}
4,544✔
680

681
#[cfg(test)]
682
mod test {
683
    use rstest::{fixture, rstest};
684
    use vortex_buffer::buffer;
685
    use vortex_mask::Mask;
686

687
    use crate::arrays::PrimitiveArray;
688
    use crate::patches::Patches;
689
    use crate::search_sorted::{SearchResult, SearchSortedSide};
690
    use crate::validity::Validity;
691
    use crate::{IntoArray, ToCanonical};
692

693
    #[test]
694
    fn test_filter() {
1✔
695
        let patches = Patches::new(
1✔
696
            100,
697
            0,
698
            buffer![10u32, 11, 20].into_array(),
1✔
699
            buffer![100, 110, 200].into_array(),
1✔
700
        );
701

702
        let filtered = patches
1✔
703
            .filter(&Mask::from_indices(100, vec![10, 20, 30]))
1✔
704
            .unwrap()
1✔
705
            .unwrap();
1✔
706

707
        let indices = filtered.indices().to_primitive().unwrap();
1✔
708
        let values = filtered.values().to_primitive().unwrap();
1✔
709
        assert_eq!(indices.as_slice::<u64>(), &[0, 1]);
1✔
710
        assert_eq!(values.as_slice::<i32>(), &[100, 200]);
1✔
711
    }
1✔
712

713
    #[fixture]
714
    fn patches() -> Patches {
715
        Patches::new(
716
            20,
717
            0,
718
            buffer![2u64, 9, 15].into_array(),
719
            PrimitiveArray::new(buffer![33_i32, 44, 55], Validity::AllValid).into_array(),
720
        )
721
    }
722

723
    #[rstest]
724
    fn search_larger_than(patches: Patches) {
725
        let res = patches.search_sorted(66, SearchSortedSide::Left).unwrap();
726
        assert_eq!(res, SearchResult::NotFound(16));
727
    }
728

729
    #[rstest]
730
    fn search_less_than(patches: Patches) {
731
        let res = patches.search_sorted(22, SearchSortedSide::Left).unwrap();
732
        assert_eq!(res, SearchResult::NotFound(2));
733
    }
734

735
    #[rstest]
736
    fn search_found(patches: Patches) {
737
        let res = patches.search_sorted(44, SearchSortedSide::Left).unwrap();
738
        assert_eq!(res, SearchResult::Found(9));
739
    }
740

741
    #[rstest]
742
    fn search_not_found_right(patches: Patches) {
743
        let res = patches.search_sorted(56, SearchSortedSide::Right).unwrap();
744
        assert_eq!(res, SearchResult::NotFound(16));
745
    }
746

747
    #[rstest]
748
    fn search_sliced(patches: Patches) {
749
        let sliced = patches.slice(7, 20).unwrap().unwrap();
750
        assert_eq!(
751
            sliced.search_sorted(22, SearchSortedSide::Left).unwrap(),
752
            SearchResult::NotFound(2)
753
        );
754
    }
755

756
    #[test]
757
    fn search_right() {
1✔
758
        let patches = Patches::new(
1✔
759
            6,
760
            0,
761
            buffer![0u8, 1, 4, 5].into_array(),
1✔
762
            buffer![-128i8, -98, 8, 50].into_array(),
1✔
763
        );
764

765
        assert_eq!(
1✔
766
            patches.search_sorted(-98, SearchSortedSide::Right).unwrap(),
1✔
767
            SearchResult::Found(2)
768
        );
769
        assert_eq!(
1✔
770
            patches.search_sorted(50, SearchSortedSide::Right).unwrap(),
1✔
771
            SearchResult::Found(6),
772
        );
773
        assert_eq!(
1✔
774
            patches.search_sorted(7, SearchSortedSide::Right).unwrap(),
1✔
775
            SearchResult::NotFound(2),
776
        );
777
        assert_eq!(
1✔
778
            patches.search_sorted(51, SearchSortedSide::Right).unwrap(),
1✔
779
            SearchResult::NotFound(6)
780
        );
781
    }
1✔
782

783
    #[test]
784
    fn search_left() {
1✔
785
        let patches = Patches::new(
1✔
786
            20,
787
            0,
788
            buffer![0u64, 1, 17, 18, 19].into_array(),
1✔
789
            buffer![11i32, 22, 33, 44, 55].into_array(),
1✔
790
        );
791
        assert_eq!(
1✔
792
            patches.search_sorted(30, SearchSortedSide::Left).unwrap(),
1✔
793
            SearchResult::NotFound(2)
794
        );
795
        assert_eq!(
1✔
796
            patches.search_sorted(54, SearchSortedSide::Left).unwrap(),
1✔
797
            SearchResult::NotFound(19)
798
        );
799
    }
1✔
800

801
    #[rstest]
802
    fn take_with_nulls(patches: Patches) {
803
        let taken = patches
804
            .take(
805
                &PrimitiveArray::new(buffer![9, 0], Validity::from_iter(vec![true, false]))
806
                    .into_array(),
807
            )
808
            .unwrap()
809
            .unwrap();
810
        let primitive_values = taken.values().to_primitive().unwrap();
811
        assert_eq!(taken.array_len(), 2);
812
        assert_eq!(primitive_values.as_slice::<i32>(), [44]);
813
        assert_eq!(
814
            primitive_values.validity_mask().unwrap(),
815
            Mask::from_iter(vec![true])
816
        );
817
    }
818

819
    #[test]
820
    fn test_slice() {
1✔
821
        let values = buffer![15_u32, 135, 13531, 42].into_array();
1✔
822
        let indices = buffer![10_u64, 11, 50, 100].into_array();
1✔
823

824
        let patches = Patches::new(101, 0, indices, values);
1✔
825

826
        let sliced = patches.slice(15, 100).unwrap().unwrap();
1✔
827
        assert_eq!(sliced.array_len(), 100 - 15);
1✔
828
        let primitive = sliced.values().to_primitive().unwrap();
1✔
829

830
        assert_eq!(primitive.as_slice::<u32>(), &[13531]);
1✔
831
    }
1✔
832

833
    #[test]
834
    fn doubly_sliced() {
1✔
835
        let values = buffer![15_u32, 135, 13531, 42].into_array();
1✔
836
        let indices = buffer![10_u64, 11, 50, 100].into_array();
1✔
837

838
        let patches = Patches::new(101, 0, indices, values);
1✔
839

840
        let sliced = patches.slice(15, 100).unwrap().unwrap();
1✔
841
        assert_eq!(sliced.array_len(), 100 - 15);
1✔
842
        let primitive = sliced.values().to_primitive().unwrap();
1✔
843

844
        assert_eq!(primitive.as_slice::<u32>(), &[13531]);
1✔
845

846
        let doubly_sliced = sliced.slice(35, 36).unwrap().unwrap();
1✔
847
        let primitive_doubly_sliced = doubly_sliced.values().to_primitive().unwrap();
1✔
848

849
        assert_eq!(primitive_doubly_sliced.as_slice::<u32>(), &[13531]);
1✔
850
    }
1✔
851

852
    #[test]
853
    fn test_mask_all_true() {
1✔
854
        let patches = Patches::new(
1✔
855
            10,
856
            0,
857
            buffer![2u64, 5, 8].into_array(),
1✔
858
            buffer![100i32, 200, 300].into_array(),
1✔
859
        );
860

861
        let mask = Mask::new_true(10);
1✔
862
        let masked = patches.mask(&mask).unwrap();
1✔
863
        assert!(masked.is_none());
1✔
864
    }
1✔
865

866
    #[test]
867
    fn test_mask_all_false() {
1✔
868
        let patches = Patches::new(
1✔
869
            10,
870
            0,
871
            buffer![2u64, 5, 8].into_array(),
1✔
872
            buffer![100i32, 200, 300].into_array(),
1✔
873
        );
874

875
        let mask = Mask::new_false(10);
1✔
876
        let masked = patches.mask(&mask).unwrap().unwrap();
1✔
877

878
        // No patch values should be masked
879
        let masked_values = masked.values().to_primitive().unwrap();
1✔
880
        assert_eq!(masked_values.as_slice::<i32>(), &[100, 200, 300]);
1✔
881
        assert!(masked_values.is_valid(0).unwrap());
1✔
882
        assert!(masked_values.is_valid(1).unwrap());
1✔
883
        assert!(masked_values.is_valid(2).unwrap());
1✔
884

885
        // Indices should remain unchanged
886
        let indices = masked.indices().to_primitive().unwrap();
1✔
887
        assert_eq!(indices.as_slice::<u64>(), &[2, 5, 8]);
1✔
888
    }
1✔
889

890
    #[test]
891
    fn test_mask_partial() {
1✔
892
        let patches = Patches::new(
1✔
893
            10,
894
            0,
895
            buffer![2u64, 5, 8].into_array(),
1✔
896
            buffer![100i32, 200, 300].into_array(),
1✔
897
        );
898

899
        // Mask that removes patches at indices 2 and 8 (but not 5)
900
        let mask = Mask::from_iter([
1✔
901
            false, false, true, false, false, false, false, false, true, false,
1✔
902
        ]);
1✔
903
        let masked = patches.mask(&mask).unwrap().unwrap();
1✔
904

905
        // Only the patch at index 5 should remain
906
        let masked_values = masked.values().to_primitive().unwrap();
1✔
907
        assert_eq!(masked_values.len(), 1);
1✔
908
        assert_eq!(masked_values.as_slice::<i32>(), &[200]);
1✔
909

910
        // Only index 5 should remain
911
        let indices = masked.indices().to_primitive().unwrap();
1✔
912
        assert_eq!(indices.as_slice::<u64>(), &[5]);
1✔
913
    }
1✔
914

915
    #[test]
916
    fn test_mask_with_offset() {
1✔
917
        let patches = Patches::new(
1✔
918
            10,
919
            5,                                  // offset
920
            buffer![7u64, 10, 13].into_array(), // actual indices are 2, 5, 8
1✔
921
            buffer![100i32, 200, 300].into_array(),
1✔
922
        );
923

924
        // Mask that sets actual index 2 to null
925
        let mask = Mask::from_iter([
1✔
926
            false, false, true, false, false, false, false, false, false, false,
1✔
927
        ]);
1✔
928

929
        let masked = patches.mask(&mask).unwrap().unwrap();
1✔
930
        assert_eq!(masked.array_len(), 10);
1✔
931
        assert_eq!(masked.offset(), 5);
1✔
932
        let indices = masked.indices().to_primitive().unwrap();
1✔
933
        assert_eq!(indices.as_slice::<u64>(), &[10, 13]);
1✔
934
        let masked_values = masked.values().to_primitive().unwrap();
1✔
935
        assert_eq!(masked_values.as_slice::<i32>(), &[200, 300]);
1✔
936
    }
1✔
937

938
    #[test]
939
    fn test_mask_nullable_values() {
1✔
940
        let patches = Patches::new(
1✔
941
            10,
942
            0,
943
            buffer![2u64, 5, 8].into_array(),
1✔
944
            PrimitiveArray::from_option_iter([Some(100i32), None, Some(300)]).into_array(),
1✔
945
        );
946

947
        // Test masking removes patch at index 2
948
        let mask = Mask::from_iter([
1✔
949
            false, false, true, false, false, false, false, false, false, false,
1✔
950
        ]);
1✔
951
        let masked = patches.mask(&mask).unwrap().unwrap();
1✔
952

953
        // Patches at indices 5 and 8 should remain
954
        let indices = masked.indices().to_primitive().unwrap();
1✔
955
        assert_eq!(indices.as_slice::<u64>(), &[5, 8]);
1✔
956

957
        // Values should be the null and 300
958
        let masked_values = masked.values().to_primitive().unwrap();
1✔
959
        assert_eq!(masked_values.len(), 2);
1✔
960
        assert!(!masked_values.is_valid(0).unwrap()); // the null value at index 5
1✔
961
        assert!(masked_values.is_valid(1).unwrap()); // the 300 value at index 8
1✔
962
        assert_eq!(
1✔
963
            i32::try_from(&masked_values.scalar_at(1).unwrap()).unwrap(),
1✔
964
            300i32
965
        );
966
    }
1✔
967

968
    #[test]
969
    fn test_filter_keep_all() {
1✔
970
        let patches = Patches::new(
1✔
971
            10,
972
            0,
973
            buffer![2u64, 5, 8].into_array(),
1✔
974
            buffer![100i32, 200, 300].into_array(),
1✔
975
        );
976

977
        // Keep all indices (mask with indices 0-9)
978
        let mask = Mask::from_indices(10, (0..10).collect());
1✔
979
        let filtered = patches.filter(&mask).unwrap().unwrap();
1✔
980

981
        let indices = filtered.indices().to_primitive().unwrap();
1✔
982
        let values = filtered.values().to_primitive().unwrap();
1✔
983
        assert_eq!(indices.as_slice::<u64>(), &[2, 5, 8]);
1✔
984
        assert_eq!(values.as_slice::<i32>(), &[100, 200, 300]);
1✔
985
    }
1✔
986

987
    #[test]
988
    fn test_filter_none() {
1✔
989
        let patches = Patches::new(
1✔
990
            10,
991
            0,
992
            buffer![2u64, 5, 8].into_array(),
1✔
993
            buffer![100i32, 200, 300].into_array(),
1✔
994
        );
995

996
        // Filter out all (empty mask means keep nothing)
997
        let mask = Mask::from_indices(10, vec![]);
1✔
998
        let filtered = patches.filter(&mask).unwrap();
1✔
999
        assert!(filtered.is_none());
1✔
1000
    }
1✔
1001

1002
    #[test]
1003
    fn test_filter_with_indices() {
1✔
1004
        let patches = Patches::new(
1✔
1005
            10,
1006
            0,
1007
            buffer![2u64, 5, 8].into_array(),
1✔
1008
            buffer![100i32, 200, 300].into_array(),
1✔
1009
        );
1010

1011
        // Keep indices 2, 5, 9 (so patches at 2 and 5 remain)
1012
        let mask = Mask::from_indices(10, vec![2, 5, 9]);
1✔
1013
        let filtered = patches.filter(&mask).unwrap().unwrap();
1✔
1014

1015
        let indices = filtered.indices().to_primitive().unwrap();
1✔
1016
        let values = filtered.values().to_primitive().unwrap();
1✔
1017
        assert_eq!(indices.as_slice::<u64>(), &[0, 1]); // Adjusted indices
1✔
1018
        assert_eq!(values.as_slice::<i32>(), &[100, 200]);
1✔
1019
    }
1✔
1020

1021
    #[test]
1022
    fn test_slice_full_range() {
1✔
1023
        let patches = Patches::new(
1✔
1024
            10,
1025
            0,
1026
            buffer![2u64, 5, 8].into_array(),
1✔
1027
            buffer![100i32, 200, 300].into_array(),
1✔
1028
        );
1029

1030
        let sliced = patches.slice(0, 10).unwrap().unwrap();
1✔
1031

1032
        let indices = sliced.indices().to_primitive().unwrap();
1✔
1033
        let values = sliced.values().to_primitive().unwrap();
1✔
1034
        assert_eq!(indices.as_slice::<u64>(), &[2, 5, 8]);
1✔
1035
        assert_eq!(values.as_slice::<i32>(), &[100, 200, 300]);
1✔
1036
    }
1✔
1037

1038
    #[test]
1039
    fn test_slice_partial() {
1✔
1040
        let patches = Patches::new(
1✔
1041
            10,
1042
            0,
1043
            buffer![2u64, 5, 8].into_array(),
1✔
1044
            buffer![100i32, 200, 300].into_array(),
1✔
1045
        );
1046

1047
        // Slice from 3 to 8 (includes patch at 5)
1048
        let sliced = patches.slice(3, 8).unwrap().unwrap();
1✔
1049

1050
        let indices = sliced.indices().to_primitive().unwrap();
1✔
1051
        let values = sliced.values().to_primitive().unwrap();
1✔
1052
        assert_eq!(indices.as_slice::<u64>(), &[5]); // Index stays the same
1✔
1053
        assert_eq!(values.as_slice::<i32>(), &[200]);
1✔
1054
        assert_eq!(sliced.array_len(), 5); // 8 - 3 = 5
1✔
1055
        assert_eq!(sliced.offset(), 3); // New offset
1✔
1056
    }
1✔
1057

1058
    #[test]
1059
    fn test_slice_no_patches() {
1✔
1060
        let patches = Patches::new(
1✔
1061
            10,
1062
            0,
1063
            buffer![2u64, 5, 8].into_array(),
1✔
1064
            buffer![100i32, 200, 300].into_array(),
1✔
1065
        );
1066

1067
        // Slice from 6 to 7 (no patches in this range)
1068
        let sliced = patches.slice(6, 7).unwrap();
1✔
1069
        assert!(sliced.is_none());
1✔
1070
    }
1✔
1071

1072
    #[test]
1073
    fn test_slice_with_offset() {
1✔
1074
        let patches = Patches::new(
1✔
1075
            10,
1076
            5,                                  // offset
1077
            buffer![7u64, 10, 13].into_array(), // actual indices are 2, 5, 8
1✔
1078
            buffer![100i32, 200, 300].into_array(),
1✔
1079
        );
1080

1081
        // Slice from 3 to 8 (includes patch at actual index 5)
1082
        let sliced = patches.slice(3, 8).unwrap().unwrap();
1✔
1083

1084
        let indices = sliced.indices().to_primitive().unwrap();
1✔
1085
        let values = sliced.values().to_primitive().unwrap();
1✔
1086
        assert_eq!(indices.as_slice::<u64>(), &[10]); // Index stays the same (offset + 5 = 10)
1✔
1087
        assert_eq!(values.as_slice::<i32>(), &[200]);
1✔
1088
        assert_eq!(sliced.offset(), 8); // New offset = 5 + 3
1✔
1089
    }
1✔
1090

1091
    #[test]
1092
    fn test_patch_values() {
1✔
1093
        let patches = Patches::new(
1✔
1094
            10,
1095
            0,
1096
            buffer![2u64, 5, 8].into_array(),
1✔
1097
            buffer![100i32, 200, 300].into_array(),
1✔
1098
        );
1099

1100
        let values = patches.values().to_primitive().unwrap();
1✔
1101
        assert_eq!(
1✔
1102
            i32::try_from(&values.scalar_at(0).unwrap()).unwrap(),
1✔
1103
            100i32
1104
        );
1105
        assert_eq!(
1✔
1106
            i32::try_from(&values.scalar_at(1).unwrap()).unwrap(),
1✔
1107
            200i32
1108
        );
1109
        assert_eq!(
1✔
1110
            i32::try_from(&values.scalar_at(2).unwrap()).unwrap(),
1✔
1111
            300i32
1112
        );
1113
    }
1✔
1114

1115
    #[test]
1116
    fn test_indices_range() {
1✔
1117
        let patches = Patches::new(
1✔
1118
            10,
1119
            0,
1120
            buffer![2u64, 5, 8].into_array(),
1✔
1121
            buffer![100i32, 200, 300].into_array(),
1✔
1122
        );
1123

1124
        assert_eq!(patches.min_index().unwrap(), 2);
1✔
1125
        assert_eq!(patches.max_index().unwrap(), 8);
1✔
1126
    }
1✔
1127

1128
    #[test]
1129
    fn test_search_index() {
1✔
1130
        let patches = Patches::new(
1✔
1131
            10,
1132
            0,
1133
            buffer![2u64, 5, 8].into_array(),
1✔
1134
            buffer![100i32, 200, 300].into_array(),
1✔
1135
        );
1136

1137
        // Search for exact indices
1138
        assert_eq!(patches.search_index(2).unwrap(), SearchResult::Found(0));
1✔
1139
        assert_eq!(patches.search_index(5).unwrap(), SearchResult::Found(1));
1✔
1140
        assert_eq!(patches.search_index(8).unwrap(), SearchResult::Found(2));
1✔
1141

1142
        // Search for non-patch indices
1143
        assert_eq!(patches.search_index(0).unwrap(), SearchResult::NotFound(0));
1✔
1144
        assert_eq!(patches.search_index(3).unwrap(), SearchResult::NotFound(1));
1✔
1145
        assert_eq!(patches.search_index(6).unwrap(), SearchResult::NotFound(2));
1✔
1146
        assert_eq!(patches.search_index(9).unwrap(), SearchResult::NotFound(3));
1✔
1147
    }
1✔
1148

1149
    #[test]
1150
    fn test_mask_boundary_patches() {
1✔
1151
        // Test masking patches at array boundaries
1152
        let patches = Patches::new(
1✔
1153
            10,
1154
            0,
1155
            buffer![0u64, 9].into_array(),
1✔
1156
            buffer![100i32, 200].into_array(),
1✔
1157
        );
1158

1159
        let mask = Mask::from_iter([
1✔
1160
            true, false, false, false, false, false, false, false, false, false,
1✔
1161
        ]);
1✔
1162
        let masked = patches.mask(&mask).unwrap();
1✔
1163
        assert!(masked.is_some());
1✔
1164
        let masked = masked.unwrap();
1✔
1165
        let indices = masked.indices().to_primitive().unwrap();
1✔
1166
        assert_eq!(indices.as_slice::<u64>(), &[9]);
1✔
1167
        let values = masked.values().to_primitive().unwrap();
1✔
1168
        assert_eq!(values.as_slice::<i32>(), &[200]);
1✔
1169
    }
1✔
1170

1171
    #[test]
1172
    fn test_mask_all_patches_removed() {
1✔
1173
        // Test when all patches are masked out
1174
        let patches = Patches::new(
1✔
1175
            10,
1176
            0,
1177
            buffer![2u64, 5, 8].into_array(),
1✔
1178
            buffer![100i32, 200, 300].into_array(),
1✔
1179
        );
1180

1181
        // Mask that removes all patches
1182
        let mask = Mask::from_iter([
1✔
1183
            false, false, true, false, false, true, false, false, true, false,
1✔
1184
        ]);
1✔
1185
        let masked = patches.mask(&mask).unwrap();
1✔
1186
        assert!(masked.is_none());
1✔
1187
    }
1✔
1188

1189
    #[test]
1190
    fn test_mask_no_patches_removed() {
1✔
1191
        // Test when no patches are masked
1192
        let patches = Patches::new(
1✔
1193
            10,
1194
            0,
1195
            buffer![2u64, 5, 8].into_array(),
1✔
1196
            buffer![100i32, 200, 300].into_array(),
1✔
1197
        );
1198

1199
        // Mask that doesn't affect any patches
1200
        let mask = Mask::from_iter([
1✔
1201
            true, false, false, true, false, false, true, false, false, true,
1✔
1202
        ]);
1✔
1203
        let masked = patches.mask(&mask).unwrap().unwrap();
1✔
1204

1205
        let indices = masked.indices().to_primitive().unwrap();
1✔
1206
        assert_eq!(indices.as_slice::<u64>(), &[2, 5, 8]);
1✔
1207
        let values = masked.values().to_primitive().unwrap();
1✔
1208
        assert_eq!(values.as_slice::<i32>(), &[100, 200, 300]);
1✔
1209
    }
1✔
1210

1211
    #[test]
1212
    fn test_mask_single_patch() {
1✔
1213
        // Test with a single patch
1214
        let patches = Patches::new(
1✔
1215
            5,
1216
            0,
1217
            buffer![2u64].into_array(),
1✔
1218
            buffer![42i32].into_array(),
1✔
1219
        );
1220

1221
        // Mask that removes the single patch
1222
        let mask = Mask::from_iter([false, false, true, false, false]);
1✔
1223
        let masked = patches.mask(&mask).unwrap();
1✔
1224
        assert!(masked.is_none());
1✔
1225

1226
        // Mask that keeps the single patch
1227
        let mask = Mask::from_iter([true, false, false, true, false]);
1✔
1228
        let masked = patches.mask(&mask).unwrap().unwrap();
1✔
1229
        let indices = masked.indices().to_primitive().unwrap();
1✔
1230
        assert_eq!(indices.as_slice::<u64>(), &[2]);
1✔
1231
    }
1✔
1232

1233
    #[test]
1234
    fn test_mask_contiguous_patches() {
1✔
1235
        // Test with contiguous patches
1236
        let patches = Patches::new(
1✔
1237
            10,
1238
            0,
1239
            buffer![3u64, 4, 5, 6].into_array(),
1✔
1240
            buffer![100i32, 200, 300, 400].into_array(),
1✔
1241
        );
1242

1243
        // Mask that removes middle patches
1244
        let mask = Mask::from_iter([
1✔
1245
            false, false, false, false, true, true, false, false, false, false,
1✔
1246
        ]);
1✔
1247
        let masked = patches.mask(&mask).unwrap().unwrap();
1✔
1248

1249
        let indices = masked.indices().to_primitive().unwrap();
1✔
1250
        assert_eq!(indices.as_slice::<u64>(), &[3, 6]);
1✔
1251
        let values = masked.values().to_primitive().unwrap();
1✔
1252
        assert_eq!(values.as_slice::<i32>(), &[100, 400]);
1✔
1253
    }
1✔
1254

1255
    #[test]
1256
    fn test_mask_with_large_offset() {
1✔
1257
        // Test with a large offset that shifts all indices
1258
        let patches = Patches::new(
1✔
1259
            20,
1260
            15,
1261
            buffer![16u64, 17, 19].into_array(), // actual indices are 1, 2, 4
1✔
1262
            buffer![100i32, 200, 300].into_array(),
1✔
1263
        );
1264

1265
        // Mask that removes the patch at actual index 2
1266
        let mask = Mask::from_iter([
1✔
1267
            false, false, true, false, false, false, false, false, false, false, false, false,
1✔
1268
            false, false, false, false, false, false, false, false,
1✔
1269
        ]);
1✔
1270
        let masked = patches.mask(&mask).unwrap().unwrap();
1✔
1271

1272
        let indices = masked.indices().to_primitive().unwrap();
1✔
1273
        assert_eq!(indices.as_slice::<u64>(), &[16, 19]);
1✔
1274
        let values = masked.values().to_primitive().unwrap();
1✔
1275
        assert_eq!(values.as_slice::<i32>(), &[100, 300]);
1✔
1276
    }
1✔
1277

1278
    #[test]
1279
    #[should_panic(expected = "Filter mask length 5 does not match array length 10")]
1280
    fn test_mask_wrong_length() {
1✔
1281
        let patches = Patches::new(
1✔
1282
            10,
1283
            0,
1284
            buffer![2u64, 5, 8].into_array(),
1✔
1285
            buffer![100i32, 200, 300].into_array(),
1✔
1286
        );
1287

1288
        // Mask with wrong length
1289
        let mask = Mask::from_iter([false, false, true, false, false]);
1✔
1290
        let _ = patches.mask(&mask).unwrap();
1✔
1291
    }
1✔
1292
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc