selen 0.15.5

Constraint Satisfaction Problem (CSP) solver
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
//! Core variable types and fundamental operations.
//!
//! This module contains the fundamental types that form the variable system:
//! - `VarId`: Unique identifier for variables
//! - `Val`: Value type (integer or float) 
//! - `Var`: Variable domain representation
//! - `Vars`: Collection of variables

use crate::variables::domain::{SparseSet, sparse_set::SparseSetState, float_interval::FloatInterval};
use crate::constraints::props::PropId;
use crate::core::solution::Solution;
use std::ops::{Index, IndexMut};

/// Value type that can represent either an integer or a floating-point number.
#[derive(Copy, Clone, Debug)]
pub enum Val {
    /// Single integer value
    ValI(i32),
    /// Single floating-point value
    ValF(f64),
}

impl Val {
    /// Create an integer value
    pub const fn int(value: i32) -> Self {
        Val::ValI(value)
    }

    /// Create a floating-point value
    pub const fn float(value: f64) -> Self {
        Val::ValF(value)
    }

    /// Create a boolean value (represented as integer 0 or 1)
    /// 
    /// # Examples
    /// 
    /// ```
    /// use selen::prelude::Val;
    /// 
    /// let true_val = Val::bool(true);   // Val::ValI(1)
    /// let false_val = Val::bool(false); // Val::ValI(0)
    /// ```
    pub const fn bool(value: bool) -> Self {
        Val::ValI(if value { 1 } else { 0 })
    }

    /// Get the previous representable value
    pub fn prev(self) -> Self {
        match self {
            Val::ValI(i) => Val::ValI(i - 1),
            Val::ValF(_f) => {
                // For single values, we can't use prev/next without knowing the interval
                // This would need to be handled at the variable level
                self // Return unchanged for now
            }
        }
    }

    /// Get the next representable value  
    pub fn next(self) -> Self {
        match self {
            Val::ValI(i) => Val::ValI(i + 1),
            Val::ValF(_f) => {
                // For single values, we can't use prev/next without knowing the interval
                // This would need to be handled at the variable level
                self // Return unchanged for now
            }
        }
    }
    
    /// Extract integer value if this is an integer, None otherwise
    pub fn as_int(self) -> Option<i32> {
        match self {
            Val::ValI(i) => Some(i),
            Val::ValF(_) => None,
        }
    }
    
    /// Extract float value if this is a float, None otherwise  
    pub fn as_float(self) -> Option<f64> {
        match self {
            Val::ValF(f) => Some(f),
            Val::ValI(_) => None,
        }
    }

    /// Check if this value is safe to divide by (not zero or close to zero)
    pub fn is_safe_divisor(self) -> bool {
        match self {
            Val::ValI(i) => i != 0,
            Val::ValF(f) => f.abs() >= f64::EPSILON * 1000.0, // Use a larger epsilon for safety
        }
    }

    /// Safe division that returns None if divisor is too close to zero
    pub fn safe_div(self, other: Val) -> Option<Val> {
        if !other.is_safe_divisor() {
            return None;
        }
        Some(self / other)
    }

    /// Safe modulo that returns None if divisor is too close to zero
    pub fn safe_mod(self, other: Val) -> Option<Val> {
        if !other.is_safe_divisor() {
            return None;
        }
        Some(self % other)
    }

    /// Check if the range [min, max] contains zero or values close to zero
    pub fn range_contains_unsafe_divisor(min: Val, max: Val) -> bool {
        match (min, max) {
            (Val::ValI(min_i), Val::ValI(max_i)) => min_i <= 0 && max_i >= 0,
            (Val::ValF(min_f), Val::ValF(max_f)) => min_f <= f64::EPSILON && max_f >= -f64::EPSILON,
            (Val::ValI(min_i), Val::ValF(max_f)) => min_i as f64 <= f64::EPSILON && max_f >= -f64::EPSILON,
            (Val::ValF(min_f), Val::ValI(max_i)) => min_f <= f64::EPSILON && max_i as f64 >= -f64::EPSILON,
        }
    }

    /// Compare two values with interval context for mathematically correct precision.
    /// This is the preferred method for constraint propagation where interval context is available.
    pub fn equals_with_intervals(&self, other: &Self, 
                                self_interval: Option<&FloatInterval>,
                                other_interval: Option<&FloatInterval>) -> bool {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => a == b,
            (Val::ValF(a), Val::ValF(b)) => {
                match (self_interval, other_interval) {
                    (Some(int_a), Some(int_b)) => {
                        // Use the sum of half-steps as tolerance - this ensures values that
                        // could represent the same conceptual value are considered equal
                        let tolerance = (int_a.step + int_b.step) / 2.0;
                        (a - b).abs() <= tolerance
                    }
                    (Some(int_a), None) => {
                        // Only one interval available, use its precision
                        let tolerance = int_a.step / 2.0;
                        (a - b).abs() <= tolerance
                    }
                    (None, Some(int_b)) => {
                        // Only one interval available, use its precision
                        let tolerance = int_b.step / 2.0;
                        (a - b).abs() <= tolerance
                    }
                    (None, None) => {
                        // No interval context - use direct equality for step-aligned values
                        // This should rarely happen in practice since variables have intervals
                        *a == *b
                    }
                }
            }
            (Val::ValI(i), Val::ValF(f)) => {
                if let Some(f_interval) = other_interval {
                    let tolerance = f_interval.step / 2.0;
                    ((*i as f64) - f).abs() <= tolerance
                } else {
                    // No context, use direct comparison for step-aligned values
                    (*i as f64) == *f
                }
            }
            (Val::ValF(f), Val::ValI(i)) => {
                if let Some(f_interval) = self_interval {
                    let tolerance = f_interval.step / 2.0;
                    (f - (*i as f64)).abs() <= tolerance
                } else {
                    // No context, use direct comparison for step-aligned values
                    *f == (*i as f64)
                }
            }
        }
    }
    
    /// Compare two values with explicit precision tolerance.
    /// Useful when you know the appropriate tolerance but don't have interval objects.
    pub fn equals_with_precision(&self, other: &Self, precision: f64) -> bool {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => a == b,
            (Val::ValF(a), Val::ValF(b)) => (a - b).abs() <= precision,
            (Val::ValI(a), Val::ValF(b)) => ((*a as f64) - b).abs() <= precision,
            (Val::ValF(a), Val::ValI(b)) => (a - (*b as f64)).abs() <= precision,
        }
    }
}

impl From<i32> for Val {
    fn from(value: i32) -> Self {
        Val::ValI(value)
    }
}

impl From<f64> for Val {
    fn from(value: f64) -> Self {
        Val::ValF(value)
    }
}

impl PartialEq for Val {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => a == b,
            (Val::ValF(a), Val::ValF(b)) => a == b,  // Direct equality for step-aligned values
            (Val::ValI(a), Val::ValF(b)) => (*a as f64) == *b,
            (Val::ValF(a), Val::ValI(b)) => *a == (*b as f64),
        }
    }
}

impl Eq for Val {}

impl PartialOrd for Val {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => a.partial_cmp(b),
            (Val::ValF(a), Val::ValF(b)) => a.partial_cmp(b),
            (Val::ValI(a), Val::ValF(b)) => (*a as f64).partial_cmp(b),
            (Val::ValF(a), Val::ValI(b)) => a.partial_cmp(&(*b as f64)),
        }
    }
}

impl Ord for Val {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.partial_cmp(other).unwrap_or(std::cmp::Ordering::Equal)
    }
}

impl std::ops::Add for Val {
    type Output = Val;

    fn add(self, other: Val) -> Val {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => Val::ValI(a + b),
            (Val::ValF(a), Val::ValF(b)) => Val::ValF(a + b),
            (Val::ValI(a), Val::ValF(b)) => Val::ValF(a as f64 + b),
            (Val::ValF(a), Val::ValI(b)) => Val::ValF(a + b as f64),
        }
    }
}

impl std::iter::Sum for Val {
    fn sum<I: Iterator<Item = Val>>(iter: I) -> Self {
        iter.fold(Val::ValI(0), |acc, x| acc + x)
    }
}

impl std::ops::Sub for Val {
    type Output = Val;

    fn sub(self, other: Val) -> Val {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => Val::ValI(a - b),
            (Val::ValF(a), Val::ValF(b)) => Val::ValF(a - b),
            (Val::ValI(a), Val::ValF(b)) => Val::ValF(a as f64 - b),
            (Val::ValF(a), Val::ValI(b)) => Val::ValF(a - b as f64),
        }
    }
}

impl std::ops::Mul for Val {
    type Output = Val;

    fn mul(self, other: Val) -> Val {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => Val::ValI(a * b),
            (Val::ValF(a), Val::ValF(b)) => Val::ValF(a * b),
            (Val::ValI(a), Val::ValF(b)) => Val::ValF(a as f64 * b),
            (Val::ValF(a), Val::ValI(b)) => Val::ValF(a * b as f64),
        }
    }
}

impl std::ops::Div for Val {
    type Output = Val;

    fn div(self, other: Val) -> Val {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => {
                if b == 0 {
                    // Return infinity for division by zero
                    if a >= 0 { Val::ValF(f64::INFINITY) } else { Val::ValF(f64::NEG_INFINITY) }
                } else {
                    // For integer division, convert to float to avoid truncation issues
                    Val::ValF(a as f64 / b as f64)
                }
            },
            (Val::ValF(a), Val::ValF(b)) => {
                if b.abs() < f64::EPSILON {
                    // Return infinity for division by value too close to zero
                    if a >= 0.0 { Val::ValF(f64::INFINITY) } else { Val::ValF(f64::NEG_INFINITY) }
                } else {
                    Val::ValF(a / b)
                }
            },
            (Val::ValI(a), Val::ValF(b)) => {
                if b.abs() < f64::EPSILON {
                    // Return infinity for division by value too close to zero
                    if a >= 0 { Val::ValF(f64::INFINITY) } else { Val::ValF(f64::NEG_INFINITY) }
                } else {
                    Val::ValF(a as f64 / b)
                }
            },
            (Val::ValF(a), Val::ValI(b)) => {
                if b == 0 {
                    // Return infinity for division by zero
                    if a >= 0.0 { Val::ValF(f64::INFINITY) } else { Val::ValF(f64::NEG_INFINITY) }
                } else {
                    Val::ValF(a / b as f64)
                }
            },
        }
    }
}

impl std::ops::Rem for Val {
    type Output = Val;

    fn rem(self, other: Val) -> Val {
        match (self, other) {
            (Val::ValI(a), Val::ValI(b)) => {
                if b == 0 {
                    // Return NaN for modulo by zero (undefined behavior)
                    Val::ValF(f64::NAN)
                } else {
                    Val::ValI(a % b)
                }
            },
            (Val::ValF(a), Val::ValF(b)) => {
                if b.abs() < f64::EPSILON {
                    // Return NaN for modulo by value too close to zero
                    Val::ValF(f64::NAN)
                } else {
                    Val::ValF(a % b)
                }
            },
            (Val::ValI(a), Val::ValF(b)) => {
                if b.abs() < f64::EPSILON {
                    // Return NaN for modulo by value too close to zero
                    Val::ValF(f64::NAN)
                } else {
                    Val::ValF(a as f64 % b)
                }
            },
            (Val::ValF(a), Val::ValI(b)) => {
                if b == 0 {
                    // Return NaN for modulo by zero
                    Val::ValF(f64::NAN)
                } else {
                    Val::ValF(a % b as f64)
                }
            },
        }
    }
}

/// Domain for a decision variable
#[derive(Clone, Debug)]
pub enum Var {
    /// interval of floating-point numbers with fixed step size
    VarF(FloatInterval),
    /// sparse set for integer domains
    VarI(SparseSet),
}

impl Var {
    /// Assigned variables have a domain reduced to a singleton.
    #[doc(hidden)]
    pub fn is_assigned(&self) -> bool {
        match self {
            Var::VarF(interval) => interval.is_fixed(),
            Var::VarI(sparse_set) => sparse_set.is_fixed(),
        }
    }

    /// Midpoint of domain for easier binary splits.
    #[doc(hidden)]
    pub fn mid(&self) -> Val {
        match self {
            Var::VarF(interval) => Val::ValF(interval.mid()),
            Var::VarI(sparse_set) => {
                if sparse_set.is_empty() {
                    // Should not happen in a valid CSP, but provide a fallback
                    Val::ValI(0)
                } else {
                    // Use the midpoint between min and max for binary search
                    // For proper binary search, we need to ensure the midpoint is always valid
                    // and that the split makes progress
                    let min_val = sparse_set.min();
                    let max_val = sparse_set.max();
                    
                    // If domain is a single value, return that value
                    if min_val == max_val {
                        return Val::ValI(min_val);
                    }
                    
                    // Calculate midpoint - use floor division to ensure left bias
                    // This ensures that for [-1, 0], mid = -1, giving:
                    // Left: x <= -1 (just [-1])
                    // Right: x >= 0 (just [0])
                    let mid_val = min_val + (max_val - min_val) / 2;
                    Val::ValI(mid_val)
                }
            }
        }
    }

    /// Extract assignment for decision variable.
    ///
    /// # Panics
    ///
    /// This function will panic if the decision variable is not assigned.
    #[doc(hidden)]
    pub fn get_assignment(&self) -> Val {
        debug_assert!(self.is_assigned());

        match self {
            Var::VarF(interval) => Val::ValF(interval.min),
            Var::VarI(sparse_set) => {
                debug_assert!(sparse_set.is_fixed());
                // For a fixed sparse set, min == max, so we can use either
                Val::ValI(sparse_set.min())
            }
        }
    }
}

/// Decision variable handle that is not bound to a specific memory location.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct VarId(usize);

impl VarId {
    /// Create a VarId from a usize index (for internal use)
    pub(crate) fn from_index(index: usize) -> Self {
        VarId(index)
    }
    
    /// Get the internal index as usize (for internal use)
    pub(crate) fn to_index(self) -> usize {
        self.0
    }
}

impl std::fmt::Debug for VarId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "VarId({})", self.0)
    }
}

/// Collection of decision variables
#[derive(Clone, Debug, Default)]
pub struct Vars {
    vars: Vec<Var>,
    /// Count of integer variables
    pub int_var_count: usize,
    /// Count of boolean variables (subset of int_var_count where domain is [0,1])
    pub bool_var_count: usize,
    /// Count of float variables
    pub float_var_count: usize,
    /// Count of set variables (reserved for future use)
    pub set_var_count: usize,
}

impl Vars {
    /// Create a new empty collection of variables.
    #[doc(hidden)]
    pub fn new() -> Self {
        Vars {
            vars: Vec::new(),
            int_var_count: 0,
            bool_var_count: 0,
            float_var_count: 0,
            set_var_count: 0,
        }
    }

    /// Create a new decision variable.
    #[doc(hidden)]
    pub fn new_var_with_bounds(&mut self, min: Val, max: Val) -> VarId {
        let v = VarId(self.vars.len());

        match (min, max) {
            (Val::ValI(min), Val::ValI(max)) => {
                // Create SparseSet for integer variables
                let sparse_set = SparseSet::new(min, max);
                self.vars.push(Var::VarI(sparse_set));
                
                // Track if it's a boolean variable (domain is [0, 1])
                if min == 0 && max == 1 {
                    self.bool_var_count += 1;
                } else {
                    self.int_var_count += 1;
                }
            },
            (Val::ValF(min), Val::ValF(max)) => {
                let interval = FloatInterval::new(min as f64, max as f64);
                self.vars.push(Var::VarF(interval));
                self.float_var_count += 1;
            },
            // type coercion
            (Val::ValI(min), Val::ValF(max)) => {
                let interval = FloatInterval::new(min as f64, max as f64);
                self.vars.push(Var::VarF(interval));
                self.float_var_count += 1;
            },
            (Val::ValF(min), Val::ValI(max)) => {
                let interval = FloatInterval::new(min as f64, max as f64);
                self.vars.push(Var::VarF(interval));
                self.float_var_count += 1;
            },
        }

        v
    }

    /// Create a new decision variable with custom float step size.
    #[doc(hidden)]
    pub fn new_var_with_bounds_and_step(&mut self, min: Val, max: Val, float_step: f64) -> VarId {
        let v = VarId(self.vars.len());

        match (min, max) {
            (Val::ValI(min), Val::ValI(max)) => {
                // Create SparseSet for integer variables - use unchecked to preserve invalid bounds
                let sparse_set = SparseSet::new_unchecked(min, max);
                self.vars.push(Var::VarI(sparse_set));
                
                // Track if it's a boolean variable (domain is [0, 1])
                if min == 0 && max == 1 {
                    self.bool_var_count += 1;
                } else {
                    self.int_var_count += 1;
                }
            },
            (Val::ValF(min), Val::ValF(max)) => {
                let interval = FloatInterval::with_step_unchecked(min as f64, max as f64, float_step);
                self.vars.push(Var::VarF(interval));
                self.float_var_count += 1;
            },
            // type coercion
            (Val::ValI(min), Val::ValF(max)) => {
                let interval = FloatInterval::with_step_unchecked(min as f64, max as f64, float_step);
                self.vars.push(Var::VarF(interval));
                self.float_var_count += 1;
            },
            (Val::ValF(min), Val::ValI(max)) => {
                let interval = FloatInterval::with_step_unchecked(min as f64, max as f64, float_step);
                self.vars.push(Var::VarF(interval));
                self.float_var_count += 1;
            },
        }

        v
    }

    /// Create a new integer decision variable from a vector of specific values.
    /// This is useful for creating variables with non-contiguous domains.
    /// 
    /// # Arguments
    /// * `values` - Vector of integer values that the variable can take
    /// 
    /// # Returns
    /// A new VarId for the created variable
    /// 
    /// # Example
    /// ```
    /// use selen::prelude::*;
    /// let mut vars = Vars::new();
    /// let var = vars.new_var_with_values(vec![2, 4, 6, 8]); // Even numbers only
    /// ```
    #[doc(hidden)]
    pub fn new_var_with_values(&mut self, values: Vec<i32>) -> VarId {
        let v = VarId(self.vars.len());
        let sparse_set = SparseSet::new_from_values(values);
        self.vars.push(Var::VarI(sparse_set));
        
        // Track as integer variable (not boolean, since this is for arbitrary value sets)
        self.int_var_count += 1;
        
        v
    }

    /// Get handle to an unassigned decision variable using Most Restricted Variable (MRV) heuristic.
    /// 
    /// Get the first unassigned variable.
    #[doc(hidden)]
    pub fn get_unassigned_var(&self) -> Option<VarId> {
        for (index, var) in self.vars.iter().enumerate() {
            if !var.is_assigned() {
                return Some(VarId(index));
            }
        }
        
        None
    }

    /// Determine if all decision variables are assigned.
    #[doc(hidden)]
    pub fn is_assigned_all(&self) -> bool {
        self.get_unassigned_var().is_none()
    }
    
    /// Get the number of variables in this collection.
    #[doc(hidden)]
    pub fn count(&self) -> usize {
        self.vars.len()
    }

    /// Get an iterator over all variables with their indices for validation.
    #[doc(hidden)]
    pub fn iter_with_indices(&self) -> impl Iterator<Item = (usize, &Var)> {
        self.vars.iter().enumerate()
    }
    
    /// Get the FloatInterval for a variable if it's a float variable.
    /// Returns None for integer variables.
    #[doc(hidden)]
    pub fn get_float_interval(&self, var_id: VarId) -> Option<&FloatInterval> {
        match &self.vars[var_id.0] {
            Var::VarF(interval) => Some(interval),
            Var::VarI(_) => None,
        }
    }
    
    /// Compare two variable values with proper interval context.
    /// This is the mathematically correct way to compare values from different variables.
    #[doc(hidden)]
    pub fn values_equal(&self, var_a: VarId, val_a: &Val, var_b: VarId, val_b: &Val) -> bool {
        let interval_a = self.get_float_interval(var_a);
        let interval_b = self.get_float_interval(var_b);
        val_a.equals_with_intervals(val_b, interval_a, interval_b)
    }

    /// Extract assignment for all decision variables.
    ///
    /// # Panics
    ///
    /// This function will panic if any decision variables are not assigned.
    #[doc(hidden)]
    pub fn into_solution(self) -> Solution {
        // Extract values for each decision variable - convert to old Val type for now
        let values: Vec<crate::variables::Val> = self.vars.into_iter().map(|v| {
            let val = v.get_assignment();
            match val {
                Val::ValI(i) => crate::variables::Val::ValI(i),
                Val::ValF(f) => crate::variables::Val::ValF(f),
            }
        }).collect();

        Solution::from(values)
    }

    /// Extract assignment for all decision variables with statistics.
    ///
    /// # Panics
    ///
    /// This function will panic if any decision variables are not assigned.
    #[doc(hidden)]
    pub fn into_solution_with_stats(self, stats: crate::core::solution::SolveStats) -> Solution {
        // Extract values for each decision variable - convert to old Val type for now
        let values: Vec<crate::variables::Val> = self.vars.into_iter().map(|v| {
            let val = v.get_assignment();
            match val {
                Val::ValI(i) => crate::variables::Val::ValI(i),
                Val::ValF(f) => crate::variables::Val::ValF(f),
            }
        }).collect();

        Solution::new(values, stats)
    }

    /// Save state of all sparse set variables for efficient backtracking
    #[doc(hidden)]
    pub fn save_sparse_states(&self) -> Vec<Option<SparseSetState>> {
        self.vars.iter().map(|var| {
            match var {
                Var::VarF(_) => None, // Float variables don't need state saving
                Var::VarI(sparse_set) => Some(sparse_set.save_state()),
            }
        }).collect()
    }

    /// Restore state of all sparse set variables from saved state
    #[doc(hidden)]
    pub fn restore_sparse_states(&mut self, states: &[Option<SparseSetState>]) {
        debug_assert_eq!(self.vars.len(), states.len(), "State vector size mismatch");
        
        for (var, state_opt) in self.vars.iter_mut().zip(states.iter()) {
            match (var, state_opt) {
                (Var::VarF(_), None) => {
                    // Float variables don't have saved state - nothing to restore
                }
                (Var::VarI(sparse_set), Some(state)) => {
                    sparse_set.restore_state(state);
                }
                _ => {
                    debug_assert!(false, "Mismatched variable type and state");
                }
            }
        }
    }

    /// Iterator over variables for analysis
    #[doc(hidden)]
    pub fn iter(&self) -> std::slice::Iter<'_, Var> {
        self.vars.iter()
    }
}

// Index implementations
impl Index<VarId> for Vars {
    type Output = Var;

    fn index(&self, index: VarId) -> &Self::Output {
        &self.vars[index.0]
    }
}

impl IndexMut<VarId> for Vars {
    fn index_mut(&mut self, index: VarId) -> &mut Self::Output {
        &mut self.vars[index.0]
    }
}

impl Index<VarId> for Vec<Var> {
    type Output = Var;

    fn index(&self, index: VarId) -> &Self::Output {
        &self[index.0]
    }
}

impl IndexMut<VarId> for Vec<Var> {
    fn index_mut(&mut self, index: VarId) -> &mut Self::Output {
        &mut self[index.0]
    }
}

impl Index<VarId> for Vec<Vec<PropId>> {
    type Output = Vec<PropId>;

    fn index(&self, index: VarId) -> &Self::Output {
        &self[index.0]
    }
}

impl IndexMut<VarId> for Vec<Vec<PropId>> {
    fn index_mut(&mut self, index: VarId) -> &mut Self::Output {
        &mut self[index.0]
    }
}

impl Index<VarId> for Vec<Val> {
    type Output = Val;

    fn index(&self, index: VarId) -> &Self::Output {
        &self[index.0]
    }
}

impl IndexMut<VarId> for Vec<Val> {
    fn index_mut(&mut self, index: VarId) -> &mut Self::Output {
        &mut self[index.0]
    }
}

/// Wrapper to provide specific helper methods for binary decision variables.
#[derive(Clone, Copy, Debug)]
pub struct VarIdBin(pub(crate) VarId);

impl From<VarId> for VarIdBin {
    fn from(var_id: VarId) -> Self {
        VarIdBin(var_id)
    }
}

impl From<VarIdBin> for VarId {
    fn from(var_id_bin: VarIdBin) -> Self {
        var_id_bin.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_var_with_values_basic() {
        let mut vars = Vars::new();
        let var_id = vars.new_var_with_values(vec![2, 4, 6, 8]);
        
        let var = &vars[var_id];
        let Var::VarI(sparse_set) = var else {
            assert!(false, "Expected integer variable");
            return;
        };
        assert_eq!(sparse_set.size(), 4);
        assert!(sparse_set.contains(2));
        assert!(sparse_set.contains(4));
        assert!(sparse_set.contains(6));
        assert!(sparse_set.contains(8));
        assert!(!sparse_set.contains(3));
        assert!(!sparse_set.contains(5));
    }

    #[test]
    fn test_new_var_with_values_single() {
        let mut vars = Vars::new();
        let var_id = vars.new_var_with_values(vec![42]);
        
        let var = &vars[var_id];
        let Var::VarI(sparse_set) = var else {
            assert!(false, "Expected integer variable");
            return;
        };
        assert_eq!(sparse_set.size(), 1);
        assert!(sparse_set.is_fixed());
        assert!(sparse_set.contains(42));
        assert!(!sparse_set.contains(41));
    }

    #[test]
    fn test_new_var_with_values_duplicates() {
        let mut vars = Vars::new();
        let var_id = vars.new_var_with_values(vec![1, 3, 1, 5, 3]);
        
        let var = &vars[var_id];
        let Var::VarI(sparse_set) = var else {
            assert!(false, "Expected integer variable");
            return;
        };
        assert_eq!(sparse_set.size(), 3); // Should deduplicate
        assert!(sparse_set.contains(1));
        assert!(sparse_set.contains(3));
        assert!(sparse_set.contains(5));
    }

    #[test]
    fn test_var_with_values_assignment() {
        let mut vars = Vars::new();
        let var_id = vars.new_var_with_values(vec![10, 20, 30]);
        
        let var = &vars[var_id];
        assert!(!var.is_assigned());
        
        // Test midpoint calculation
        let mid = var.mid();
        let Val::ValI(val) = mid else {
            assert!(false, "Expected integer value");
            return;
        };
        // Midpoint should be reasonable
        assert!(val >= 10 && val <= 30);
    }

    #[test]
    fn test_equivalence_with_range_creation() {
        let mut vars1 = Vars::new();
        let mut vars2 = Vars::new();
        
        // Create equivalent variables using different methods
        let var1_id = vars1.new_var_with_bounds(Val::int(1), Val::int(5));
        let var2_id = vars2.new_var_with_values(vec![1, 2, 3, 4, 5]);
        
        let var1 = &vars1[var1_id];
        let var2 = &vars2[var2_id];
        
        // Both should have the same domain
        let (Var::VarI(sparse1), Var::VarI(sparse2)) = (var1, var2) else {
            assert!(false, "Expected both to be integer variables");
            return;
        };
        assert_eq!(sparse1.size(), sparse2.size());
        assert_eq!(sparse1.min(), sparse2.min());
        assert_eq!(sparse1.max(), sparse2.max());
        
        // All values should be the same
        for i in 1..=5 {
            assert_eq!(sparse1.contains(i), sparse2.contains(i));
        }
    }
}

// Re-export core types from vars.rs


// Note: Core variable types are currently implemented in vars.rs:
//
// VarId - Opaque variable handle (lines 751-763)
// VarIdBin - Binary variable handle (line 829) 
// Val - Value type enum (lines 52-58, with implementations 59-377)
// Var - Variable domain enum (lines 43-51, with implementations 379-443)
// Vars - Variable storage struct (lines 444-752)
//
// These core types form the foundation of the variable system and could
// be moved to this module in a future refactoring phase.