vortex_array/arrays/primitive/array/
top_value.rs1use std::hash::Hash;
5
6use rustc_hash::FxBuildHasher;
7use vortex_error::VortexExpect;
8use vortex_error::VortexResult;
9use vortex_mask::AllOr;
10use vortex_mask::Mask;
11use vortex_utils::aliases::hash_map::HashMap;
12
13use crate::LEGACY_SESSION;
14use crate::VortexSessionExecute;
15use crate::arrays::PrimitiveArray;
16use crate::arrays::primitive::NativeValue;
17use crate::dtype::NativePType;
18use crate::match_each_native_ptype;
19use crate::scalar::PValue;
20
21impl PrimitiveArray {
22 pub fn top_value(&self) -> VortexResult<Option<(PValue, usize)>> {
24 if self.is_empty() {
25 return Ok(None);
26 }
27
28 if self.validity()?.definitely_all_null() {
29 return Ok(None);
30 }
31
32 match_each_native_ptype!(self.ptype(), |P| {
33 let (top, count) = typed_top_value(
34 self.as_slice::<P>(),
35 self.as_ref().validity()?.execute_mask(
36 self.as_ref().len(),
37 &mut LEGACY_SESSION.create_execution_ctx(),
38 )?,
39 );
40 Ok(Some((top.into(), count)))
41 })
42 }
43}
44
45fn typed_top_value<T>(values: &[T], mask: Mask) -> (T, usize)
46where
47 T: NativePType,
48 NativeValue<T>: Eq + Hash,
49{
50 let mut distinct_values: HashMap<NativeValue<T>, usize, FxBuildHasher> =
51 HashMap::with_hasher(FxBuildHasher);
52 match mask.indices() {
53 AllOr::All => {
54 for value in values.iter().copied() {
55 *distinct_values.entry(NativeValue(value)).or_insert(0) += 1;
56 }
57 }
58 AllOr::None => unreachable!("All invalid arrays should be handled earlier"),
59 AllOr::Some(idxs) => {
60 for &i in idxs {
61 *distinct_values
62 .entry(NativeValue(unsafe { *values.get_unchecked(i) }))
63 .or_insert(0) += 1
64 }
65 }
66 }
67
68 let (&top_value, &top_count) = distinct_values
69 .iter()
70 .max_by_key(|&(_, &count)| count)
71 .vortex_expect("non-empty");
72 (top_value.0, top_count)
73}