Skip to main content

rs_ints2uniq2arrow/
lib.rs

1pub use arrow;
2
3#[cfg(feature = "fs")]
4pub mod fs;
5
6use std::io;
7
8use std::collections::BTreeSet;
9
10use arrow::array::PrimitiveArray;
11
12use arrow::datatypes::ArrowPrimitiveType;
13
14use arrow::datatypes::Int8Type;
15use arrow::datatypes::Int16Type;
16use arrow::datatypes::Int32Type;
17use arrow::datatypes::Int64Type;
18
19use arrow::datatypes::UInt8Type;
20use arrow::datatypes::UInt16Type;
21use arrow::datatypes::UInt32Type;
22use arrow::datatypes::UInt64Type;
23
24pub fn ints2uniq_bt<I, T>(ints: I) -> BTreeSet<T::Native>
25where
26    T: ArrowPrimitiveType,
27    T::Native: Ord,
28    I: Iterator<Item = T::Native>,
29{
30    BTreeSet::from_iter(ints)
31}
32
33pub fn uniq2arrow_bt<T>(uniq: BTreeSet<T::Native>) -> PrimitiveArray<T>
34where
35    T: ArrowPrimitiveType,
36    T::Native: Ord,
37{
38    PrimitiveArray::from_iter_values(uniq)
39}
40
41pub fn ints2uniq2arrow_bt<I, T>(ints: I) -> PrimitiveArray<T>
42where
43    T: ArrowPrimitiveType,
44    T::Native: Ord,
45    I: Iterator<Item = T::Native>,
46{
47    let uniq: BTreeSet<T::Native> = ints2uniq_bt::<I, T>(ints);
48    uniq2arrow_bt(uniq)
49}
50
51macro_rules! create_ints2uniq2arrow_bt {
52    ($fname: ident, $ity: ty) => {
53        /// Creates an array of unique integers from the integers.
54        pub fn $fname<I>(ints: I) -> PrimitiveArray<$ity>
55        where
56            I: Iterator<Item = <$ity as ArrowPrimitiveType>::Native>,
57        {
58            ints2uniq2arrow_bt(ints)
59        }
60    };
61}
62
63create_ints2uniq2arrow_bt!(ints2uniq2arrow_bt8, Int8Type);
64create_ints2uniq2arrow_bt!(ints2uniq2arrow_bt16, Int16Type);
65create_ints2uniq2arrow_bt!(ints2uniq2arrow_bt32, Int32Type);
66create_ints2uniq2arrow_bt!(ints2uniq2arrow_bt64, Int64Type);
67
68create_ints2uniq2arrow_bt!(uints2uniq2arrow_bt8, UInt8Type);
69create_ints2uniq2arrow_bt!(uints2uniq2arrow_bt16, UInt16Type);
70create_ints2uniq2arrow_bt!(uints2uniq2arrow_bt32, UInt32Type);
71create_ints2uniq2arrow_bt!(uints2uniq2arrow_bt64, UInt64Type);
72
73pub fn rints2uniq_bt<I, T>(ints: I) -> Result<BTreeSet<T::Native>, io::Error>
74where
75    T: ArrowPrimitiveType,
76    T::Native: Ord,
77    I: Iterator<Item = Result<T::Native, io::Error>>,
78{
79    ints.collect()
80}
81
82pub fn rints2uniq2arrow_bt<I, T>(ints: I) -> Result<PrimitiveArray<T>, io::Error>
83where
84    T: ArrowPrimitiveType,
85    T::Native: Ord,
86    I: Iterator<Item = Result<T::Native, io::Error>>,
87{
88    let uniq: BTreeSet<T::Native> = rints2uniq_bt::<I, T>(ints)?;
89    Ok(uniq2arrow_bt(uniq))
90}
91
92macro_rules! create_rints2uniq2arrow_bt {
93    ($fname: ident, $ity: ty) => {
94        /// Tries to create an array of unique integers from the integers.
95        pub fn $fname<I>(ints: I) -> Result<PrimitiveArray<$ity>, io::Error>
96        where
97            I: Iterator<Item = Result<<$ity as ArrowPrimitiveType>::Native, io::Error>>,
98        {
99            rints2uniq2arrow_bt(ints)
100        }
101    };
102}
103
104create_rints2uniq2arrow_bt!(rints2uniq2arrow_bt8, Int8Type);
105create_rints2uniq2arrow_bt!(rints2uniq2arrow_bt16, Int16Type);
106create_rints2uniq2arrow_bt!(rints2uniq2arrow_bt32, Int32Type);
107create_rints2uniq2arrow_bt!(rints2uniq2arrow_bt64, Int64Type);
108
109create_rints2uniq2arrow_bt!(ruints2uniq2arrow_bt8, UInt8Type);
110create_rints2uniq2arrow_bt!(ruints2uniq2arrow_bt16, UInt16Type);
111create_rints2uniq2arrow_bt!(ruints2uniq2arrow_bt32, UInt32Type);
112create_rints2uniq2arrow_bt!(ruints2uniq2arrow_bt64, UInt64Type);