optima 0.0.4

An easy to set up and easy optimization and planning toolbox, particularly for robotics.
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
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash;
use serde_with::{serde_as};
use serde::de::DeserializeOwned;
use serde::{Serialize, Deserialize};
use crate::utils::utils_errors::OptimaError;
use crate::utils::utils_files::optima_path::load_object_from_json_string;
use crate::utils::utils_se3::optima_se3_pose::OptimaSE3Pose;
use crate::utils::utils_traits::SaveAndLoadable;

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Array1D<T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    #[serde_as(as = "Vec<_>")]
    array: Vec<T>
}
impl <T> Array1D <T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    pub fn new(length: usize, initialization_value: Option<T>) -> Self {
        return match initialization_value {
            None => { Self::new_without_initialization_value(length) }
            Some(initialization_value) => { Self::new_with_initialization_value(length, initialization_value) }
        }
    }
    fn new_without_initialization_value(length: usize) -> Self {
        return Self::new_with_initialization_value(length, T::default())
    }
    fn new_with_initialization_value(length: usize, initialization_value: T) -> Self {
        let mut array = vec![];
        for _ in 0..length { array.push(initialization_value.clone()) }
        return Self {
            array
        }
    }
    pub fn replace_data(&mut self, data: T, idx: usize) -> Result<(), OptimaError> {
        OptimaError::new_check_for_idx_out_of_bound_error(idx, self.array.len(), file!(), line!())?;

        self.array[idx] = data;

        Ok(())
    }
    pub fn replace_data_on_every_cell(&mut self, data: T) {
        for cell in &mut self.array {
            *cell = data.clone();
        }
    }
    pub fn append_new_cell(&mut self, data: T) {
        self.array.push(data);
    }
    pub fn mix(&mut self, other: &Self) -> Result<(), OptimaError> {
        if self.array.len() != other.array.len() {
            return Err(OptimaError::new_generic_error_str("Cannot combine Array1Ds of different sizes.", file!(), line!()));
        }

        let length = self.array.len();

        for i in 0..length {
            self.array[i] = self.array[i].mix(&other.array[i]);
        }

        Ok(())
    }
    pub fn data_cell(&self, idx: usize) -> Result<&T, OptimaError> {
        OptimaError::new_check_for_idx_out_of_bound_error(idx, self.array.len(), file!(), line!())?;

        Ok(&self.array[idx])
    }
    pub fn data_cell_mut(&mut self, idx: usize) -> Result<&mut T, OptimaError> {
        OptimaError::new_check_for_idx_out_of_bound_error(idx, self.array.len(), file!(), line!())?;

        Ok(&mut self.array[idx])
    }
    pub fn len(&self) -> usize { self.array.len() }
    pub fn convert_to_memory_cells(&self) -> Array1D<MemoryCell<T>> {
        let mut out = Array1D::new(self.len(), None);

        let l = self.len();
        for i in 0..l {
            out.replace_data(MemoryCell::new(self.array[i].clone()), i).expect("error");
        }

        out
    }
}

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SquareArray2D<T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    #[serde_as(as = "Vec<Vec<_>>")]
    array: Vec<Vec<T>>,
    side_length: usize,
    symmetric: bool
}
impl <T> SquareArray2D <T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    pub fn new(side_length: usize, symmetric: bool, initialization_value: Option<T>) -> Self {
        return match initialization_value {
            None => { Self::new_without_initialization_value(side_length, symmetric) }
            Some(initialization_value) => { Self::new_with_initialization_value(side_length, symmetric, initialization_value) }
        }
    }
    /// Concatenation places the second matrix in the lower right corner such that an m x m matrix
    /// concatenated with an n x n matrix will become an m + n x m + n matrix.  Off diagonal terms
    /// will be T::default
    pub fn new_concatenated(s1: &SquareArray2D<T>, s2: &SquareArray2D<T>, symmetric: bool, off_diagonal_value: Option<T>) -> Self {
        let l1 = s1.side_length;
        let l2 = s2.side_length;

        let mut out_self = Self::new(l1 + l2, symmetric, off_diagonal_value);

        for i in 0..l1 {
            for j in 0..l1 {
                out_self.array[i][j] = s1.array[i][j].clone();
            }
        }

        for i in 0..l2 {
            for j in 0..l2 {
                out_self.array[i + l1][j + l1] = s2.array[i][j].clone();
            }
        }

        return out_self
    }
    fn new_without_initialization_value(side_length: usize, symmetric: bool) -> Self {
        return Self::new_with_initialization_value(side_length, symmetric, T::default())
    }
    fn new_with_initialization_value(side_length: usize, symmetric: bool, initialization_value: T) -> Self {
        let mut array = vec![];

        for _ in 0..side_length {
            let mut tmp = vec![];
            for _ in 0..side_length {
                tmp.push(initialization_value.clone());
            }
            array.push(tmp);
        }

        Self {
            array,
            side_length,
            symmetric
        }
    }
    pub fn replace_data(&mut self, data: T, row_idx: usize, col_idx: usize) -> Result<(), OptimaError> {
        OptimaError::new_check_for_idx_out_of_bound_error(row_idx, self.side_length, file!(), line!())?;
        OptimaError::new_check_for_idx_out_of_bound_error(col_idx, self.side_length, file!(), line!())?;

        if self.symmetric && row_idx != col_idx {
            self.array[row_idx][col_idx] = data.clone();
            self.array[col_idx][row_idx] = data;
        } else {
            self.array[row_idx][col_idx] = data;
        }


        Ok(())
    }
    pub fn replace_data_on_every_cell(&mut self, data: T) {
        let l = self.side_length;
        for row in 0..l {
            for col in 0..l {
                self.array[col][row] = data.clone();
            }
        }
    }
    pub fn adjust_data<F: Fn(&mut T)>(&mut self, adjustment: F, row_idx: usize, col_idx: usize) -> Result<(), OptimaError> {
        OptimaError::new_check_for_idx_out_of_bound_error(row_idx, self.side_length, file!(), line!())?;
        OptimaError::new_check_for_idx_out_of_bound_error(col_idx, self.side_length, file!(), line!())?;

        let data = &mut self.array[row_idx][col_idx];
        adjustment(data);

        if self.symmetric && row_idx != col_idx {
            let data = &mut self.array[col_idx][row_idx];
            adjustment(data);
        }

        Ok(())
    }
    pub fn adjust_data_on_every_cell<F: Fn(&mut T)>(&mut self, adjustment: F) {
        let l = self.side_length;
        for row in 0..l {
            for col in 0..l {
                let data = &mut self.array[row][col];
                adjustment(data);
            }
        }
    }
    pub fn append_new_row_and_column(&mut self, data: Option<T>) {
        for col in &mut self.array {
            match &data {
                None => { col.push( T::default() ) }
                Some(data) => { col.push(data.clone()) }
            }
        }

        self.side_length += 1;

        let mut new_row = vec![];
        for _ in 0..self.side_length {
            match &data {
                None => { new_row.push( T::default() ) }
                Some(data) => { new_row.push(data.clone()) }
            }
        }
        self.array.push(new_row);
    }
    pub fn mix(&mut self, other: &Self) -> Result<(), OptimaError> {
        if self.side_length != other.side_length {
            return Err(OptimaError::new_generic_error_str("Cannot combine SquareArray2Ds of different sizes.", file!(), line!()));
        }

        let side_length = self.side_length;

        for col in 0..side_length {
            for row in 0..side_length {
                self.array[col][row] = self.array[col][row].mix(&other.array[col][row]);
            }
        }

        Ok(())
    }
    /// Concatenation places the second matrix in the lower right corner such that an m x m matrix
    /// concatenated with an n x n matrix will become an m + n x m + n matrix.  Off diagonal terms
    /// will be T::default
    pub fn concatenate_in_place(&mut self, other: &Self, off_diagonal_value: Option<T>) {
        let l1 = self.side_length;
        let l2 = other.side_length;
        for _ in 0..l2 { self.append_new_row_and_column(off_diagonal_value.clone()); }
        for i in 0..l2 {
            for j in 0..l2 {
                self.array[i + l1][j + l1] = other.array[i][j].clone();
            }
        }
    }
    pub fn data_cell(&self, row_idx: usize, col_idx: usize) -> Result<&T, OptimaError> {
        OptimaError::new_check_for_idx_out_of_bound_error(row_idx, self.side_length, file!(), line!())?;
        OptimaError::new_check_for_idx_out_of_bound_error(col_idx, self.side_length, file!(), line!())?;

        Ok(&self.array[row_idx][col_idx])
    }
    pub fn data_cell_mut(&mut self, row_idx: usize, col_idx: usize) -> Result<&mut T, OptimaError> {
        assert!(!self.symmetric, "cannot get mutable data cell on symmetric grid.  To change cells in this situation, you must use the `adjust_data` function");

        OptimaError::new_check_for_idx_out_of_bound_error(row_idx, self.side_length, file!(), line!())?;
        OptimaError::new_check_for_idx_out_of_bound_error(col_idx, self.side_length, file!(), line!())?;

        Ok(&mut self.array[row_idx][col_idx])
    }
    pub fn side_length(&self) -> usize {
        self.side_length
    }
    pub fn convert_to_memory_cells(&self) -> SquareArray2D<MemoryCell<T>> {
        let mut out_self = SquareArray2D::new(self.side_length, self.symmetric, None);
        for i in 0..self.side_length {
            for j in 0..self.side_length {
                out_self.replace_data(MemoryCell::new(self.array[i][j].clone()), i, j).expect("error");
            }
        }
        return out_self;
    }
}
impl <T> SaveAndLoadable for SquareArray2D<T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    type SaveType = Self;

    fn get_save_serialization_object(&self) -> Self::SaveType {
        self.clone()
    }

    fn load_from_json_string(json_str: &str) -> Result<Self, OptimaError> where Self: Sized {
        let load: Self::SaveType = load_object_from_json_string(json_str)?;
        return Ok(load);
    }
}

impl <T> SquareArray2D<MemoryCell<T>> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    pub fn convert_to_standard_cells(&self) -> SquareArray2D<T> {
        let mut out = SquareArray2D::new(self.side_length, self.symmetric, None);

        let l = self.side_length;
        for i in 0..l {
            for j in 0..l {
                let val = self.array[i][j].curr_value.clone();
                out.replace_data(val, i, j).expect("error");
            }
        }

        out
    }
}

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MemoryCell <T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    #[serde_as(as = "_")]
    curr_value: T,
    #[serde_as(as = "_")]
    base_value: T,
    #[serde_as(as = "Vec<_>")]
    history: Vec<T>
}
impl <T> MemoryCell<T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    pub fn new(value: T) -> Self {
        Self {
            curr_value: value.clone(),
            base_value: value.clone(),
            history: vec![]
        }
    }
    pub fn new_default() -> Self {
        return Self::new(T::default());
    }
    pub fn replace_value(&mut self, data: T, add_to_history: bool) {
        if add_to_history {
            self.history.push(self.curr_value.clone());
            self.curr_value = data.clone();
        } else {
            self.curr_value = data;
        }
    }
    pub fn replace_base_value(&mut self, data: T) {
        self.base_value = data.clone();
        self.history = vec![data]
    }
    pub fn adjust_value<F: Fn(&mut T)>(&mut self, f: F, add_to_history: bool) {
        if add_to_history {
            self.history.push(self.curr_value.clone());
        }
        f(&mut self.curr_value);
    }
    pub fn reset_to_base_value(&mut self, add_to_history: bool) {
        if add_to_history {
            self.history.push(self.curr_value.clone());
        }
        self.curr_value = self.base_value.clone();
    }
    pub fn curr_value(&self) -> &T {
        &self.curr_value
    }
    pub fn base_value(&self) -> &T {
        &self.base_value
    }
    pub fn history(&self) -> &Vec<T> {
        &self.history
    }
}
impl <T> Default for MemoryCell<T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    fn default() -> Self {
        MemoryCell::new_default()
    }
}

pub trait Mixable {
    fn mix(&self, other: &Self) -> Self;
}
impl Mixable for f64 {
    fn mix(&self, other: &Self) -> Self {
        (*self + *other) / 2.0
    }
}
impl Mixable for bool {
    fn mix(&self, other: &Self) -> Self {
        return *self || *other;
    }
}
impl Mixable for usize {
    fn mix(&self, other: &Self) -> Self {
        *self + *other
    }
}
impl Mixable for OptimaSE3Pose {
    fn mix(&self, other: &Self) -> Self {
        self.multiply(other, false).expect("error")
    }
}
impl Mixable for AveragingFloat {
    fn mix(&self, other: &Self) -> Self {
        let mut out_self = AveragingFloat::new();
        out_self.add_new_value(self.value);
        out_self.add_new_value(other.value);
        out_self.counter = self.counter + other.counter;
        return out_self
    }
}
impl <T> Mixable for Vec<T>  where T: Clone {
    fn mix(&self, other: &Self) -> Self {
        let mut out_vec = vec![];

        for o in self { out_vec.push(o.clone()) }
        for o in other { out_vec.push(o.clone()) }

        out_vec
    }
}
impl <T> Mixable for MemoryCell<T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    fn mix(&self, other: &Self) -> Self {
        MemoryCell::new(self.curr_value.mix(&other.curr_value))
    }
}
impl <T> Mixable for Option<T> where T: Clone + Debug + Serialize + DeserializeOwned + Default + Mixable {
    fn mix(&self, other: &Self) -> Self {
        return match self {
            None => { None }
            Some(s1) => {
                match other {
                    None => { None }
                    Some(s2) => { Some(s1.mix(s2)) }
                }
            }
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AveragingFloat {
    total_sum: f64,
    counter: f64,
    value: f64
}
impl AveragingFloat {
    pub fn new() -> Self {
        Self {
            total_sum: 0.0,
            counter: 0.0,
            value: 0.0
        }
    }
    pub fn add_new_value(&mut self, value: f64) {
        self.total_sum += value;
        self.counter += 1.0;
        self.value = self.total_sum / self.counter;
    }
    pub fn value(&self) -> f64 { self.value }
    pub fn counter(&self) -> f64 {
        self.counter
    }
}
impl Default for AveragingFloat {
    fn default() -> Self {
        Self::new()
    }
}

pub trait EnumMapToType<S> {
    fn map_to_type(&self) -> S;
}

pub trait EnumTypeContainer<T, S> where T: EnumMapToType<S> {
    fn insert_or_replace_object(&mut self, object: T);
    fn object_ref(&self, signature: &S) -> Option<&T>;
    fn object_mut_ref(&mut self, signature: &S) -> Option<&mut T>;
    fn remove_object(&mut self, signature: &S);
    fn contains_object(&self, signature: &S) -> bool;
}
impl <T, S> Clone for Box<dyn EnumTypeContainer<T, S>> where T: EnumMapToType<S> {
    fn clone(&self) -> Self {
        unimplemented!();
    }
}

#[derive(Clone)]
pub struct EnumBinarySearchTypeContainer<T, S>
    where T: EnumMapToType<S>,
          S: PartialEq + PartialOrd {
    enum_objects: Vec<T>,
    signatures: Vec<S>
}
impl <T, S> EnumBinarySearchTypeContainer<T, S>
    where T: EnumMapToType<S>,
          S: PartialEq + PartialOrd {
    pub fn new() -> Self {
        Self {
            enum_objects: vec![],
            signatures: vec![]
        }
    }
    fn binary_search_res(&self, signature: &S) -> Result<usize, usize> {
        return self.signatures.binary_search_by(|x| x.partial_cmp(signature).unwrap() )
    }
    pub fn get_object_idx(&self, signature: &S) -> Option<usize> {
        let binary_search_res = self.binary_search_res(&signature);
        return match binary_search_res {
            Ok(idx) => { Some(idx) }
            Err(_) => { None }
        }
    }
    pub fn insert_or_replace_object_with_idx(&mut self, object: T) -> usize {
        let signature = object.map_to_type();
        let binary_search_res = self.binary_search_res(&signature);
        return match binary_search_res {
            Ok(idx) => {
                self.enum_objects[idx] = object;
                idx
            }
            Err(idx) => {
                self.enum_objects.insert(idx, object);
                self.signatures.insert(idx, signature);
                idx
            }
        }
    }
    pub fn remove_object_with_idx(&mut self, signature: &S) -> Option<usize> {
        let binary_search_res = self.binary_search_res(signature);
        return match binary_search_res {
            Ok(idx) => {
                self.enum_objects.remove(idx);
                self.signatures.remove(idx);
                Some(idx)
            }
            _ => { None }
        }
    }
    pub fn remove_all_objects(&mut self) {
        self.enum_objects.clear();
        self.signatures.clear();
    }
    pub fn object_ref_with_idx(&self, signature: &S) -> Option<(&T, usize)> {
        let binary_search_res = self.binary_search_res(signature);
        return match binary_search_res {
            Ok(idx) => { Some((&self.enum_objects[idx], idx)) }
            Err(_) => { None }
        }
    }
    pub fn object_mut_ref_with_idx(&mut self, signature: &S) -> Option<(&mut T, usize)> {
        let binary_search_res = self.binary_search_res(signature);
        return match binary_search_res {
            Ok(idx) => { Some((&mut self.enum_objects[idx], idx)) }
            Err(_) => { None }
        }
    }
    pub fn object_ref_from_idx(&self, idx: usize) -> &T {
        &self.enum_objects[idx]
    }
    pub fn object_mut_ref_from_idx(&mut self, idx: usize) -> &mut T {
        &mut self.enum_objects[idx]
    }
    pub fn object_refs(&self) -> &Vec<T> {
        &self.enum_objects
    }
}
impl <T, S> EnumTypeContainer<T, S> for EnumBinarySearchTypeContainer<T, S>
    where T: EnumMapToType<S>,
          S: PartialEq + PartialOrd {
    fn insert_or_replace_object(&mut self, object: T) {
        self.insert_or_replace_object_with_idx(object);
    }
    fn object_ref(&self, signature: &S) -> Option<&T> {
        let binary_search_res = self.binary_search_res(signature);
        return match binary_search_res {
            Ok(idx) => { Some(&self.enum_objects[idx]) }
            Err(_) => { None }
        }
    }
    fn object_mut_ref(&mut self, signature: &S) -> Option<&mut T> {
        let binary_search_res = self.binary_search_res(signature);
        return match binary_search_res {
            Ok(idx) => { Some(&mut self.enum_objects[idx]) }
            Err(_) => { None }
        }
    }
    fn remove_object(&mut self, signature: &S) {
        self.remove_object_with_idx(signature);
    }
    fn contains_object(&self, signature: &S) -> bool {
        let binary_search_res = self.binary_search_res(signature);
        return binary_search_res.is_ok();
    }
}

pub struct EnumHashMapTypeContainer<T, S>
    where T: EnumMapToType<S>,
          S: Hash + Eq {
    hashmap: HashMap<S, T>
}
impl <T, S> EnumHashMapTypeContainer<T, S>
    where T: EnumMapToType<S>,
          S: Hash + Eq {
    pub fn new() -> Self {
        Self {
            hashmap: HashMap::new()
        }
    }
}
impl <T, S> EnumTypeContainer<T, S> for EnumHashMapTypeContainer<T, S>
    where T: EnumMapToType<S>,
          S: Hash + Eq {
    fn insert_or_replace_object(&mut self, object: T) {
        let signature = object.map_to_type();
        self.hashmap.insert(signature, object);
    }

    fn object_ref(&self, signature: &S) -> Option<&T> {
        return self.hashmap.get(signature);
    }

    fn object_mut_ref(&mut self, signature: &S) -> Option<&mut T> {
        return self.hashmap.get_mut(signature);
    }

    fn remove_object(&mut self, signature: &S) {
        self.hashmap.remove(signature);
    }

    fn contains_object(&self, signature: &S) -> bool {
        return self.hashmap.get(signature).is_some();
    }
}

pub enum EnumTypeContainerType {
    BinarySearch, HashMap
}
impl Default for EnumTypeContainerType {
    fn default() -> Self {
        Self::BinarySearch
    }
}

#[derive(Clone, Debug)]
pub struct WindowMemoryContainer<T: Clone + Debug> {
    window_size: isize,
    curr_head: isize,
    v: Vec<T>
}
impl<T: Clone + Debug> WindowMemoryContainer<T> {
    pub fn new(window_size: usize, starter_object: T) -> Self {
        let mut v = vec![];
        
        for _ in 0..window_size { v.push(starter_object.clone()); }
        
        Self {
            window_size: window_size as isize,
            curr_head: (window_size-1) as isize,
            v
        }
    }
    pub fn update(&mut self, object: T) {
        let update_idx = self.get_idx(self.curr_head, 1);
        self.v[update_idx as usize] = object;
        self.curr_head = update_idx;
    }
    /// if memory_idx is 0, this will return the "current" object.
    pub fn object_ref(&self, memory_idx: usize) -> &T {
        assert!(memory_idx < self.window_size as usize);
        let idx = self.get_idx(self.curr_head, -(memory_idx as isize));
        &self.v[idx as usize]
    }
    pub fn previous_n_object_refs(&self, n: usize) -> Vec<&T> {
        let mut out = vec![];

        for i in 0..n {
            out.push(self.object_ref(i));
        }

        out
    }
    fn get_idx(&self, base_idx: isize, offset: isize) -> isize {
        let out = (base_idx + offset) % self.window_size;
        return if out < 0 { self.window_size + out } else { out }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, PartialOrd)]
pub enum SimpleDataType {
    Float(f64),
    Usize(usize),
    Int(i32),
    VecOfData(Vec<SimpleDataType>)
}
impl SimpleDataType {
    pub fn unwrap_float(&self) -> f64 {
        return match self {
            SimpleDataType::Float(r) => { *r }
            _ => { panic!("wrong type") }
        }
    }
    pub fn unwrap_usize(&self) -> usize {
        return match self {
            SimpleDataType::Usize(r) => { *r }
            _ => { panic!("wrong type") }
        }
    }
    pub fn unwrap_int(&self) -> i32 {
        return match self {
            SimpleDataType::Int(r) => { *r }
            _ => { panic!("wrong type") }
        }
    }
    pub fn unwrap_vec(&self) -> &Vec<SimpleDataType> {
        return match self {
            SimpleDataType::VecOfData(r) => { r }
            _ => { panic!("wrong type") }
        }
    }
}

//// GARBAGE CODE
/*
pub struct EnumObjectContainer<T, S>
    where T: EnumIndexWrapper + EnumCount,
          S: EnumIndexWrapper + EnumCount {
    enum_objects: Vec<Option<T>>,
    _phantom: PhantomData<S>
}
impl <T, S> EnumObjectContainer<T, S>
    where T: EnumIndexWrapper + EnumCount,
          S: EnumIndexWrapper + EnumCount {
    pub fn new() -> Result<Self, OptimaError> {
        let c1 = T::COUNT;
        let c2 = S::COUNT;

        if c1 != c2 {
            let error_string = format!("Illegal Enums used in EnumObjectContainer.  \
            The base and signature enums must be of equal length.  The given lengths are {} and {}.", c1, c2);
            return Err(OptimaError::new_generic_error_str(&error_string, file!(), line!()));
        }

        let mut enum_objects = vec![];
        let count = T::COUNT;
        for _ in 0..count { enum_objects.push(None); }

        Ok(Self {
            enum_objects,
            _phantom: Default::default()
        })
    }
    pub fn insert_or_replace_object(&mut self, object: T) {
        let idx = object.enum_index_wrapper();
        self.enum_objects[idx] = Some(object);
    }
    pub fn object_ref(&self, signature: S) -> Option<&T> {
        let idx = signature.enum_index_wrapper();
        let o = &self.enum_objects[idx];
        return match o {
            None => { None }
            Some(o) => { Some(o) }
        }
    }
    pub fn object_mut_ref(&mut self, signature: S) -> Option<&mut T> {
        let idx = signature.enum_index_wrapper();
        let o = &mut self.enum_objects[idx];
        return match o {
            None => { None }
            Some(o) => { Some(o) }
        }
    }
    pub fn remove_object(&mut self, signature: S) {
        let idx = signature.enum_index_wrapper();
        self.enum_objects[idx] = None;
    }
    pub fn contains_object(&self, signature: S) -> bool {
        return self.object_ref(signature).is_some();
    }
}

pub trait EnumIndexWrapper {
    fn enum_index_wrapper(&self) -> usize;
}
impl <T> EnumIndexWrapper for T where T: EnumIndex + IndexEnum {
    fn enum_index_wrapper(&self) -> usize {
        self.enum_index()
    }
}
*/
/*
pub struct HashObjectContainer {
    default_hasher: DefaultHasher,
    objects: Vec<(u64, Box<dyn MappableToHashableObject>)>
}
impl HashObjectContainer {
    pub fn new() -> HashObjectContainer {
        Self {
            default_hasher: Default::default(),
            objects: vec![]
        }
    }
    pub fn insert_or_replace_object<T>(&mut self, o: T) where T: MappableToHashableObject + 'static {
        let h = o.map_to_hashable_object().get_hash(&mut self.default_hasher);
        let binary_search_res = self.get_binary_search_idx(h);
        match binary_search_res {
            BinarySearchRes::Found(idx) => {
                self.objects[idx] = (h, Box::new(o));
            }
            BinarySearchRes::NotFound(idx) => {
                self.objects.insert(idx, (h, Box::new(o)));
            }
        }
    }
    pub fn remove_object(&mut self, ho: HashableObject) {
        let h = ho.get_hash(&mut self.default_hasher);
        let binary_search_res = self.get_binary_search_idx(h);
        match binary_search_res {
            BinarySearchRes::Found(idx) => {
                self.objects.remove(idx);
            }
            _ => {  }
        }
    }
    pub fn object_ref<'a, T>(&mut self, ho: HashableObject) -> Option<&Box<T>>
        where T: MappableToHashableObject + 'static,
              &'a std::boxed::Box<T>: std::convert::From<&'a std::boxed::Box<dyn MappableToHashableObject>> {
        let h = ho.get_hash(&mut self.default_hasher);
        let binary_search_res = self.get_binary_search_idx(h);
        return match binary_search_res {
            BinarySearchRes::Found(idx) => {
                Some((&self.objects[idx].1).into())
            }
            BinarySearchRes::NotFound(_) => {
                None
            }
        }
    }
    pub fn object_mut_ref(&mut self, ho: HashableObject) -> Option<&mut Box<dyn MappableToHashableObject>> {
        let h = ho.get_hash(&mut self.default_hasher);
        let binary_search_res = self.get_binary_search_idx(h);
        return match binary_search_res {
            BinarySearchRes::Found(idx) => {
                Some(&mut self.objects[idx].1)
            }
            BinarySearchRes::NotFound(_) => {
                None
            }
        }
    }
    fn get_binary_search_idx(&self, idx: u64) -> BinarySearchRes {
        let binary_search_res = self.objects.binary_search_by(|x| x.0.partial_cmp(&idx).unwrap() );
        return match binary_search_res {
            Ok(idx) => { BinarySearchRes::Found(idx) }
            Err(idx) => { BinarySearchRes::NotFound(idx) }
        }
    }
}

pub enum BinarySearchRes {
    Found(usize),
    NotFound(usize)
}
impl BinarySearchRes {
    pub fn get_idx(&self) -> usize {
        return match self {
            BinarySearchRes::Found(idx) => { *idx }
            BinarySearchRes::NotFound(idx) => { *idx }
        }
    }
}

pub trait MappableToHashableObject : Debug {
    fn map_to_hashable_object(&self) -> HashableObject;
}
impl <T> MappableToHashableObject for T where T: HashWrapper {
    fn map_to_hashable_object(&self) -> HashableObject {
        HashableObject::Ref(Box::new(self))
    }
}

#[derive(Debug)]
pub enum HashableObject<'a> {
    Ref(Box<&'a dyn HashWrapper>),
    NonRef(Box<dyn HashWrapper>)
}
impl <'a> HashableObject <'a> {
    pub fn new_ref<T>(t: &'a T) -> Self where T: HashWrapper {
        return Self::Ref(Box::new(t));
    }
    pub fn new_non_ref<T>(t: T) -> Self where T: HashWrapper + 'static {
        return Self::NonRef(Box::new(t));
    }
    pub fn get_hash(&self, h: &mut DefaultHasher) -> u64 {
        return match self {
            HashableObject::Ref(hw) => {
                hw.hash_wrapper(h);
                h.finish()
            }
            HashableObject::NonRef(hw) => {
                hw.hash_wrapper(h);
                h.finish()
            }
        }
    }
}

pub trait HashWrapper : Debug {
    fn hash_wrapper(&self, h: &mut DefaultHasher);
}
impl<T: Hash + Debug> HashWrapper for T {
    fn hash_wrapper(&self, h: &mut DefaultHasher) {
        self.hash(h);
    }
}
*/