pandrs 0.3.0

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
83
84
85
86
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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Type system for JIT compilation
//!
//! This module provides abstractions for handling different data types
//! in JIT-compiled functions, allowing for type-parameterized operations.

use std::any::Any;
use std::convert::TryFrom;
use std::fmt;

/// A trait for numeric types that can be used in JIT-compiled functions
pub trait JitNumeric: Copy + Send + Sync + 'static + fmt::Debug {
    /// Convert to f64 for universal operations
    fn to_f64(&self) -> f64;

    /// Convert from f64 back to native type
    fn from_f64(value: f64) -> Self;

    /// Return the zero value of this type
    fn zero() -> Self;

    /// Return the one value of this type
    fn one() -> Self;

    /// Return the minimum value of this type
    fn min_value() -> Self;

    /// Return the maximum value of this type
    fn max_value() -> Self;

    /// Add two values of this type
    fn add(&self, other: &Self) -> Self;

    /// Subtract two values of this type
    fn sub(&self, other: &Self) -> Self;

    /// Multiply two values of this type
    fn mul(&self, other: &Self) -> Self;

    /// Divide two values of this type
    fn div(&self, other: &Self) -> Self;

    /// Compute the square root (may convert to f64 internally)
    fn sqrt(&self) -> Self;

    /// Raise to a power (may convert to f64 internally)
    fn pow(&self, exp: i32) -> Self;

    /// Check if this value is NaN
    fn is_nan(&self) -> bool;

    /// Get the type name for debugging
    fn type_name() -> &'static str;
}

/// Implementation of JitNumeric for f64
impl JitNumeric for f64 {
    fn to_f64(&self) -> f64 {
        *self
    }

    fn from_f64(value: f64) -> Self {
        value
    }

    fn zero() -> Self {
        0.0
    }

    fn one() -> Self {
        1.0
    }

    fn min_value() -> Self {
        f64::MIN
    }

    fn max_value() -> Self {
        f64::MAX
    }

    fn add(&self, other: &Self) -> Self {
        self + other
    }

    fn sub(&self, other: &Self) -> Self {
        self - other
    }

    fn mul(&self, other: &Self) -> Self {
        self * other
    }

    fn div(&self, other: &Self) -> Self {
        self / other
    }

    fn sqrt(&self) -> Self {
        f64::sqrt(*self)
    }

    fn pow(&self, exp: i32) -> Self {
        self.powi(exp)
    }

    fn is_nan(&self) -> bool {
        f64::is_nan(*self)
    }

    fn type_name() -> &'static str {
        "f64"
    }
}

/// Implementation of JitNumeric for f32
impl JitNumeric for f32 {
    fn to_f64(&self) -> f64 {
        *self as f64
    }

    fn from_f64(value: f64) -> Self {
        value as f32
    }

    fn zero() -> Self {
        0.0
    }

    fn one() -> Self {
        1.0
    }

    fn min_value() -> Self {
        f32::MIN
    }

    fn max_value() -> Self {
        f32::MAX
    }

    fn add(&self, other: &Self) -> Self {
        self + other
    }

    fn sub(&self, other: &Self) -> Self {
        self - other
    }

    fn mul(&self, other: &Self) -> Self {
        self * other
    }

    fn div(&self, other: &Self) -> Self {
        self / other
    }

    fn sqrt(&self) -> Self {
        f32::sqrt(*self)
    }

    fn pow(&self, exp: i32) -> Self {
        self.powi(exp)
    }

    fn is_nan(&self) -> bool {
        f32::is_nan(*self)
    }

    fn type_name() -> &'static str {
        "f32"
    }
}

/// Implementation of JitNumeric for i64
impl JitNumeric for i64 {
    fn to_f64(&self) -> f64 {
        *self as f64
    }

    fn from_f64(value: f64) -> Self {
        value as i64
    }

    fn zero() -> Self {
        0
    }

    fn one() -> Self {
        1
    }

    fn min_value() -> Self {
        i64::MIN
    }

    fn max_value() -> Self {
        i64::MAX
    }

    fn add(&self, other: &Self) -> Self {
        self + other
    }

    fn sub(&self, other: &Self) -> Self {
        self - other
    }

    fn mul(&self, other: &Self) -> Self {
        self * other
    }

    fn div(&self, other: &Self) -> Self {
        self / other
    }

    fn sqrt(&self) -> Self {
        (*self as f64).sqrt() as i64
    }

    fn pow(&self, exp: i32) -> Self {
        (*self as f64).powi(exp) as i64
    }

    fn is_nan(&self) -> bool {
        false // Integers don't have NaN
    }

    fn type_name() -> &'static str {
        "i64"
    }
}

/// Implementation of JitNumeric for i32
impl JitNumeric for i32 {
    fn to_f64(&self) -> f64 {
        *self as f64
    }

    fn from_f64(value: f64) -> Self {
        value as i32
    }

    fn zero() -> Self {
        0
    }

    fn one() -> Self {
        1
    }

    fn min_value() -> Self {
        i32::MIN
    }

    fn max_value() -> Self {
        i32::MAX
    }

    fn add(&self, other: &Self) -> Self {
        self + other
    }

    fn sub(&self, other: &Self) -> Self {
        self - other
    }

    fn mul(&self, other: &Self) -> Self {
        self * other
    }

    fn div(&self, other: &Self) -> Self {
        self / other
    }

    fn sqrt(&self) -> Self {
        (*self as f64).sqrt() as i32
    }

    fn pow(&self, exp: i32) -> Self {
        (*self as f64).powi(exp) as i32
    }

    fn is_nan(&self) -> bool {
        false // Integers don't have NaN
    }

    fn type_name() -> &'static str {
        "i32"
    }
}

/// Type-erased numeric value for interoperability between different types
#[derive(Debug, Clone, PartialEq)]
pub enum NumericValue {
    /// 64-bit floating point
    F64(f64),
    /// 32-bit floating point
    F32(f32),
    /// 64-bit signed integer
    I64(i64),
    /// 32-bit signed integer
    I32(i32),
}

impl NumericValue {
    /// Convert to f64 for universal operations
    pub fn to_f64(&self) -> f64 {
        match self {
            NumericValue::F64(val) => *val,
            NumericValue::F32(val) => *val as f64,
            NumericValue::I64(val) => *val as f64,
            NumericValue::I32(val) => *val as f64,
        }
    }

    /// Get the type name for debugging
    pub fn type_name(&self) -> &'static str {
        match self {
            NumericValue::F64(_) => "f64",
            NumericValue::F32(_) => "f32",
            NumericValue::I64(_) => "i64",
            NumericValue::I32(_) => "i32",
        }
    }
}

impl From<f64> for NumericValue {
    fn from(val: f64) -> Self {
        NumericValue::F64(val)
    }
}

impl From<f32> for NumericValue {
    fn from(val: f32) -> Self {
        NumericValue::F32(val)
    }
}

impl From<i64> for NumericValue {
    fn from(val: i64) -> Self {
        NumericValue::I64(val)
    }
}

impl From<i32> for NumericValue {
    fn from(val: i32) -> Self {
        NumericValue::I32(val)
    }
}

/// Marker trait for types that can be used in JIT-compiled functions
pub trait JitType: Send + Sync + 'static {
    /// Type returned by the operation
    type Output;

    /// Convert to a type-erased value
    fn to_numeric_value(&self) -> Option<NumericValue>;

    /// Try to create this type from a type-erased value
    fn from_numeric_value(value: &NumericValue) -> Option<Self>
    where
        Self: Sized;

    /// Get the type name for debugging
    fn type_name() -> &'static str;
}

impl JitType for f64 {
    type Output = f64;

    fn to_numeric_value(&self) -> Option<NumericValue> {
        Some(NumericValue::F64(*self))
    }

    fn from_numeric_value(value: &NumericValue) -> Option<Self> {
        match value {
            NumericValue::F64(val) => Some(*val),
            NumericValue::F32(val) => Some(*val as f64),
            NumericValue::I64(val) => Some(*val as f64),
            NumericValue::I32(val) => Some(*val as f64),
        }
    }

    fn type_name() -> &'static str {
        "f64"
    }
}

impl JitType for f32 {
    type Output = f32;

    fn to_numeric_value(&self) -> Option<NumericValue> {
        Some(NumericValue::F32(*self))
    }

    fn from_numeric_value(value: &NumericValue) -> Option<Self> {
        match value {
            NumericValue::F64(val) => Some(*val as f32),
            NumericValue::F32(val) => Some(*val),
            NumericValue::I64(val) => Some(*val as f32),
            NumericValue::I32(val) => Some(*val as f32),
        }
    }

    fn type_name() -> &'static str {
        "f32"
    }
}

impl JitType for i64 {
    type Output = i64;

    fn to_numeric_value(&self) -> Option<NumericValue> {
        Some(NumericValue::I64(*self))
    }

    fn from_numeric_value(value: &NumericValue) -> Option<Self> {
        match value {
            NumericValue::F64(val) => Some(*val as i64),
            NumericValue::F32(val) => Some(*val as i64),
            NumericValue::I64(val) => Some(*val),
            NumericValue::I32(val) => Some(*val as i64),
        }
    }

    fn type_name() -> &'static str {
        "i64"
    }
}

impl JitType for i32 {
    type Output = i32;

    fn to_numeric_value(&self) -> Option<NumericValue> {
        Some(NumericValue::I32(*self))
    }

    fn from_numeric_value(value: &NumericValue) -> Option<Self> {
        match value {
            NumericValue::F64(val) => Some(*val as i32),
            NumericValue::F32(val) => Some(*val as i32),
            NumericValue::I64(val) => Some(*val as i32),
            NumericValue::I32(val) => Some(*val),
        }
    }

    fn type_name() -> &'static str {
        "i32"
    }
}

/// Type-safe vector for JIT operations
#[derive(Debug, Clone)]
pub enum TypedVector {
    /// 64-bit floating point vector
    F64(Vec<f64>),
    /// 32-bit floating point vector
    F32(Vec<f32>),
    /// 64-bit signed integer vector
    I64(Vec<i64>),
    /// 32-bit signed integer vector
    I32(Vec<i32>),
}

impl TypedVector {
    /// Create a new vector from values
    pub fn new<T: JitType + Clone>(values: Vec<T>) -> Option<Self> {
        // This would require specialization in a full implementation
        // For now, just a simple implementation for numeric types
        if T::type_name() == "f64" {
            if let Some(vals) = to_f64_vec(&values) {
                return Some(TypedVector::F64(vals));
            }
        } else if T::type_name() == "f32" {
            if let Some(vals) = to_f32_vec(&values) {
                return Some(TypedVector::F32(vals));
            }
        } else if T::type_name() == "i64" {
            if let Some(vals) = to_i64_vec(&values) {
                return Some(TypedVector::I64(vals));
            }
        } else if T::type_name() == "i32" {
            if let Some(vals) = to_i32_vec(&values) {
                return Some(TypedVector::I32(vals));
            }
        }

        None
    }

    /// Get the length of the vector
    pub fn len(&self) -> usize {
        match self {
            TypedVector::F64(vec) => vec.len(),
            TypedVector::F32(vec) => vec.len(),
            TypedVector::I64(vec) => vec.len(),
            TypedVector::I32(vec) => vec.len(),
        }
    }

    /// Check if the vector is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the type name
    pub fn type_name(&self) -> &'static str {
        match self {
            TypedVector::F64(_) => "f64",
            TypedVector::F32(_) => "f32",
            TypedVector::I64(_) => "i64",
            TypedVector::I32(_) => "i32",
        }
    }

    /// Convert to f64 vector for universal operations
    pub fn to_f64_vec(&self) -> Vec<f64> {
        match self {
            TypedVector::F64(vec) => vec.clone(),
            TypedVector::F32(vec) => vec.iter().map(|&x| x as f64).collect(),
            TypedVector::I64(vec) => vec.iter().map(|&x| x as f64).collect(),
            TypedVector::I32(vec) => vec.iter().map(|&x| x as f64).collect(),
        }
    }
}

// Helper functions for type conversion
fn to_f64_vec<T: Any>(values: &[T]) -> Option<Vec<f64>> {
    if let Some(vals) = downcast_slice::<T, f64>(values) {
        return Some(vals.to_vec());
    }

    None
}

fn to_f32_vec<T: Any>(values: &[T]) -> Option<Vec<f32>> {
    if let Some(vals) = downcast_slice::<T, f32>(values) {
        return Some(vals.to_vec());
    }

    None
}

fn to_i64_vec<T: Any>(values: &[T]) -> Option<Vec<i64>> {
    if let Some(vals) = downcast_slice::<T, i64>(values) {
        return Some(vals.to_vec());
    }

    None
}

fn to_i32_vec<T: Any>(values: &[T]) -> Option<Vec<i32>> {
    if let Some(vals) = downcast_slice::<T, i32>(values) {
        return Some(vals.to_vec());
    }

    None
}

// Attempt to downcast a slice of one type to another
fn downcast_slice<T: Any, U: 'static>(slice: &[T]) -> Option<&[U]> {
    // This is a simplistic implementation that would be more robust in a real codebase
    // It works only for numeric types with direct type matching
    if std::any::TypeId::of::<T>() == std::any::TypeId::of::<U>() {
        let ptr = slice as *const [T] as *const [U];
        unsafe { Some(&*ptr) }
    } else {
        None
    }
}