1use super::hashable_state::{HashableSignatureVariables, StateWithHashableSignatureVariables};
4use core::ops::Deref;
5use dypdl::{prelude::*, variable_type::Numeric, ReduceFunction};
6use rustc_hash::FxHashMap;
7use smallvec::SmallVec;
8use std::cmp::Ordering;
9use std::collections;
10use std::fmt::Debug;
11use std::hash::Hash;
12use std::rc::Rc;
13
14#[derive(Debug, PartialEq, Clone, Default)]
19pub struct StateInRegistry<K = Rc<HashableSignatureVariables>>
20where
21 K: Hash + Eq + Clone + Debug,
22{
23 pub signature_variables: K,
25 pub resource_variables: dypdl::ResourceVariables,
27}
28
29impl<K> From<dypdl::State> for StateInRegistry<K>
30where
31 K: Hash
32 + Eq
33 + Clone
34 + Debug
35 + Deref<Target = HashableSignatureVariables>
36 + From<HashableSignatureVariables>,
37{
38 fn from(state: dypdl::State) -> StateInRegistry<K> {
39 StateInRegistry {
40 signature_variables: K::from(HashableSignatureVariables::from(
41 state.signature_variables,
42 )),
43 resource_variables: state.resource_variables,
44 }
45 }
46}
47
48impl<K> From<StateWithHashableSignatureVariables> for StateInRegistry<K>
49where
50 K: Hash + Eq + Clone + Debug + From<HashableSignatureVariables>,
51{
52 fn from(state: StateWithHashableSignatureVariables) -> StateInRegistry<K> {
53 StateInRegistry {
54 signature_variables: K::from(state.signature_variables),
55 resource_variables: state.resource_variables,
56 }
57 }
58}
59
60impl<K> dypdl::StateInterface for StateInRegistry<K>
61where
62 K: Hash + Eq + Clone + Debug + Deref<Target = HashableSignatureVariables>,
63 StateInRegistry<K>: From<dypdl::State>,
64{
65 #[inline]
66 fn get_number_of_set_variables(&self) -> usize {
67 self.signature_variables.set_variables.len()
68 }
69
70 #[inline]
71 fn get_set_variable(&self, i: usize) -> &Set {
72 &self.signature_variables.set_variables[i]
73 }
74
75 #[inline]
76 fn get_number_of_vector_variables(&self) -> usize {
77 self.signature_variables.vector_variables.len()
78 }
79
80 #[inline]
81 fn get_vector_variable(&self, i: usize) -> &Vector {
82 &self.signature_variables.vector_variables[i]
83 }
84
85 #[inline]
86 fn get_number_of_element_variables(&self) -> usize {
87 self.signature_variables.element_variables.len()
88 }
89
90 #[inline]
91 fn get_element_variable(&self, i: usize) -> Element {
92 self.signature_variables.element_variables[i]
93 }
94
95 #[inline]
96 fn get_number_of_integer_variables(&self) -> usize {
97 self.signature_variables.integer_variables.len()
98 }
99
100 #[inline]
101 fn get_integer_variable(&self, i: usize) -> Integer {
102 self.signature_variables.integer_variables[i]
103 }
104
105 #[inline]
106 fn get_number_of_continuous_variables(&self) -> usize {
107 self.signature_variables.continuous_variables.len()
108 }
109
110 #[inline]
111 fn get_continuous_variable(&self, i: usize) -> Continuous {
112 self.signature_variables.continuous_variables[i].into_inner()
113 }
114
115 #[inline]
116 fn get_number_of_element_resource_variables(&self) -> usize {
117 self.resource_variables.element_variables.len()
118 }
119
120 #[inline]
121 fn get_element_resource_variable(&self, i: usize) -> Element {
122 self.resource_variables.element_variables[i]
123 }
124
125 #[inline]
126 fn get_number_of_integer_resource_variables(&self) -> usize {
127 self.resource_variables.integer_variables.len()
128 }
129
130 #[inline]
131 fn get_integer_resource_variable(&self, i: usize) -> Integer {
132 self.resource_variables.integer_variables[i]
133 }
134
135 #[inline]
136 fn get_number_of_continuous_resource_variables(&self) -> usize {
137 self.resource_variables.continuous_variables.len()
138 }
139
140 #[inline]
141 fn get_continuous_resource_variable(&self, i: usize) -> Continuous {
142 self.resource_variables.continuous_variables[i]
143 }
144}
145
146pub trait StateInformation<T, K = Rc<HashableSignatureVariables>>
148where
149 T: Numeric,
150 K: Hash + Eq + Clone + Debug,
151{
152 fn state(&self) -> &StateInRegistry<K>;
154
155 fn state_mut(&mut self) -> &mut StateInRegistry<K>;
157
158 fn cost(&self, model: &Model) -> T;
160
161 fn bound(&self, model: &Model) -> Option<T>;
163
164 fn is_closed(&self) -> bool;
166
167 fn close(&self);
169}
170
171pub fn remove_dominated<T, K, I, D>(
177 non_dominated: &mut Vec<D>,
178 model: &Model,
179 state: &StateInRegistry<K>,
180 cost: T,
181) -> (bool, SmallVec<[D; 1]>, Option<usize>)
182where
183 T: Numeric,
184 K: Hash
185 + Eq
186 + Clone
187 + Debug
188 + Deref<Target = HashableSignatureVariables>
189 + From<HashableSignatureVariables>,
190 I: StateInformation<T, K>,
191 D: Deref<Target = I>,
192{
193 let mut same_state_index = None;
194 let mut dominated_indices = SmallVec::<[usize; 1]>::new();
195
196 for (i, other) in non_dominated.iter().enumerate() {
197 match model.state_metadata.dominance(state, other.state()) {
198 Some(Ordering::Equal) | Some(Ordering::Less)
199 if (model.reduce_function == ReduceFunction::Max && cost <= other.cost(model))
200 || (model.reduce_function == ReduceFunction::Min
201 && cost >= other.cost(model)) =>
202 {
203 return (true, smallvec::smallvec![], None);
204 }
205 Some(Ordering::Equal) => {
206 if same_state_index.is_none() {
207 same_state_index = Some(dominated_indices.len());
208 }
209
210 dominated_indices.push(i);
211 }
212 Some(Ordering::Greater)
213 if (model.reduce_function == ReduceFunction::Max && cost >= other.cost(model))
214 || (model.reduce_function == ReduceFunction::Min
215 && cost <= other.cost(model)) =>
216 {
217 dominated_indices.push(i);
218 }
219 _ => {}
220 }
221 }
222
223 if dominated_indices.is_empty() {
224 return (false, smallvec::smallvec![], None);
225 }
226
227 let same_state_index = same_state_index.map(|i| dominated_indices.len() - 1 - i);
230 let mut dominated = SmallVec::with_capacity(dominated_indices.len());
231 dominated_indices.into_iter().rev().for_each(|i| {
232 dominated.push(non_dominated.swap_remove(i));
233 });
234
235 (false, dominated, same_state_index)
236}
237
238pub struct StateRegistry<T, I, R = Rc<dypdl::Model>>
377where
378 T: Numeric,
379 I: StateInformation<T>,
380 R: Deref<Target = dypdl::Model>,
381{
382 registry: FxHashMap<Rc<HashableSignatureVariables>, Vec<Rc<I>>>,
383 model: R,
384 phantom: std::marker::PhantomData<T>,
385}
386
387#[derive(Debug, Clone)]
389pub struct InsertionResult<D> {
390 pub information: Option<D>,
392 pub dominated: SmallVec<[D; 1]>,
394}
395
396impl<T, I, R> StateRegistry<T, I, R>
397where
398 T: Numeric,
399 I: StateInformation<T>,
400 R: Deref<Target = dypdl::Model>,
401{
402 #[inline]
404 pub fn new(model: R) -> StateRegistry<T, I, R> {
405 StateRegistry {
406 registry: FxHashMap::default(),
407 model,
408 phantom: std::marker::PhantomData,
409 }
410 }
411
412 #[inline]
414 pub fn model(&self) -> &R {
415 &self.model
416 }
417
418 #[inline]
420 pub fn reserve(&mut self, capacity: usize) {
421 self.registry.reserve(capacity);
422 }
423
424 #[inline]
426 pub fn clear(&mut self) {
427 self.registry.clear();
428 }
429
430 #[inline]
432 pub fn drain(
433 &mut self,
434 ) -> impl Iterator<Item = (Rc<HashableSignatureVariables>, Vec<Rc<I>>)> + '_ {
435 self.registry.drain()
436 }
437
438 pub fn get(&self, state: &StateInRegistry, cost: T) -> Option<&Rc<I>> {
440 if let Some(v) = self.registry.get(&state.signature_variables) {
441 for other in v {
442 let result = self.model.state_metadata.dominance(state, other.state());
443 match result {
444 Some(Ordering::Equal) | Some(Ordering::Less)
445 if (self.model.reduce_function == ReduceFunction::Max
446 && cost <= other.cost(&self.model))
447 || (self.model.reduce_function == ReduceFunction::Min
448 && cost >= other.cost(&self.model)) =>
449 {
450 return Some(other)
451 }
452 _ => {}
453 }
454 }
455 }
456 None
457 }
458
459 pub fn remove(&mut self, state: &Rc<HashableSignatureVariables>) -> Option<Vec<Rc<I>>> {
461 self.registry.remove(state)
462 }
463
464 pub fn insert_with<F>(
473 &mut self,
474 mut state: StateInRegistry,
475 cost: T,
476 constructor: F,
477 ) -> InsertionResult<Rc<I>>
478 where
479 F: FnOnce(StateInRegistry, T, Option<&I>) -> Option<I>,
480 {
481 let entry = self.registry.entry(state.signature_variables.clone());
482 match entry {
483 collections::hash_map::Entry::Occupied(entry) => {
484 state.signature_variables = entry.key().clone();
486
487 let v = entry.into_mut();
488 let (dominating, dominated, same_state_index) =
489 remove_dominated(v, &self.model, &state, cost);
490
491 if dominating {
492 return InsertionResult {
493 information: None,
494 dominated: smallvec::smallvec![],
495 };
496 }
497
498 let same_state_information = same_state_index.map(|i| dominated[i].as_ref());
499
500 if let Some(information) =
501 constructor(state, cost, same_state_information).map(Rc::new)
502 {
503 v.push(information.clone());
504
505 InsertionResult {
506 information: Some(information),
507 dominated,
508 }
509 } else {
510 InsertionResult {
511 information: None,
512 dominated,
513 }
514 }
515 }
516 collections::hash_map::Entry::Vacant(entry) => {
517 if let Some(information) = constructor(state, cost, None).map(Rc::new) {
518 entry.insert(vec![information.clone()]);
519 InsertionResult {
520 information: Some(information),
521 dominated: smallvec::smallvec![],
522 }
523 } else {
524 InsertionResult {
525 information: None,
526 dominated: smallvec::smallvec![],
527 }
528 }
529 }
530 }
531 }
532
533 pub fn insert(&mut self, mut information: I) -> InsertionResult<Rc<I>> {
535 let entry = self
536 .registry
537 .entry(information.state().signature_variables.clone());
538 let (v, removed) = match entry {
539 collections::hash_map::Entry::Occupied(entry) => {
540 information.state_mut().signature_variables = entry.key().clone();
542
543 let v = entry.into_mut();
544 let (dominating, dominated, _) = remove_dominated(
545 v,
546 &self.model,
547 information.state(),
548 information.cost(&self.model),
549 );
550
551 if dominating {
552 return InsertionResult {
553 information: None,
554 dominated: smallvec::smallvec![],
555 };
556 }
557
558 (v, dominated)
559 }
560 collections::hash_map::Entry::Vacant(entry) => {
561 (entry.insert(Vec::with_capacity(1)), smallvec::smallvec![])
562 }
563 };
564
565 let information = Rc::new(information);
566 v.push(information.clone());
567
568 InsertionResult {
569 information: Some(information),
570 dominated: removed,
571 }
572 }
573}
574
575#[cfg(test)]
576mod tests {
577 use super::super::hashable_state::HashableSignatureVariables;
578 use super::*;
579 use dypdl::expression::*;
580 use dypdl::variable_type::OrderedContinuous;
581 use dypdl::variable_type::Set;
582 use dypdl::ResourceVariables;
583 use dypdl::StateInterface;
584 use ordered_float::OrderedFloat;
585 use rustc_hash::FxHashMap;
586 use std::cell::Cell;
587
588 #[derive(Debug, Clone)]
589 struct MockInformation {
590 state: StateInRegistry,
591 cost: i32,
592 value: Cell<Option<i32>>,
593 }
594
595 impl StateInformation<Integer> for MockInformation {
596 fn state(&self) -> &StateInRegistry {
597 &self.state
598 }
599
600 fn state_mut(&mut self) -> &mut StateInRegistry {
601 &mut self.state
602 }
603
604 fn cost(&self, _: &Model) -> Integer {
605 self.cost
606 }
607
608 fn bound(&self, _: &Model) -> Option<Integer> {
609 None
610 }
611
612 fn is_closed(&self) -> bool {
613 false
614 }
615
616 fn close(&self) {}
617 }
618
619 impl PartialEq for MockInformation {
620 fn eq(&self, other: &Self) -> bool {
621 self.cost == other.cost
622 }
623 }
624
625 impl Eq for MockInformation {}
626
627 impl PartialOrd for MockInformation {
628 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
629 Some(self.cmp(other))
630 }
631 }
632
633 impl Ord for MockInformation {
634 fn cmp(&self, other: &Self) -> Ordering {
635 self.cost.cmp(&other.cost)
636 }
637 }
638
639 fn generate_model() -> dypdl::Model {
640 let mut name_to_integer_variable = FxHashMap::default();
641 name_to_integer_variable.insert("n1".to_string(), 0);
642 name_to_integer_variable.insert("n2".to_string(), 1);
643 name_to_integer_variable.insert("n3".to_string(), 2);
644
645 let mut name_to_integer_resource_variable = FxHashMap::default();
646 name_to_integer_resource_variable.insert("r1".to_string(), 0);
647 name_to_integer_resource_variable.insert("r2".to_string(), 1);
648 name_to_integer_resource_variable.insert("r3".to_string(), 2);
649
650 let state_metadata = dypdl::StateMetadata {
651 integer_variable_names: vec!["n1".to_string(), "n2".to_string(), "n3".to_string()],
652 name_to_integer_variable,
653 integer_resource_variable_names: vec![
654 "r1".to_string(),
655 "r2".to_string(),
656 "r3".to_string(),
657 ],
658 name_to_integer_resource_variable,
659 integer_less_is_better: vec![false, false, true],
660 ..Default::default()
661 };
662 dypdl::Model {
663 state_metadata,
664 reduce_function: dypdl::ReduceFunction::Min,
665 ..Default::default()
666 }
667 }
668
669 fn generate_signature_variables(
670 integer_variables: Vec<Integer>,
671 ) -> Rc<HashableSignatureVariables> {
672 Rc::new(HashableSignatureVariables {
673 integer_variables,
674 ..Default::default()
675 })
676 }
677
678 fn generate_resource_variables(integer_variables: Vec<Integer>) -> dypdl::ResourceVariables {
679 dypdl::ResourceVariables {
680 integer_variables,
681 ..Default::default()
682 }
683 }
684
685 fn generate_registry() -> dypdl::TableRegistry {
686 let tables_1d = vec![dypdl::Table1D::new(vec![10, 20, 30])];
687 let mut name_to_table_1d = FxHashMap::default();
688 name_to_table_1d.insert(String::from("f1"), 0);
689
690 let tables_2d = vec![dypdl::Table2D::new(vec![
691 vec![10, 20, 30],
692 vec![40, 50, 60],
693 ])];
694 let mut name_to_table_2d = FxHashMap::default();
695 name_to_table_2d.insert(String::from("f2"), 0);
696
697 dypdl::TableRegistry {
698 integer_tables: dypdl::TableData {
699 tables_1d,
700 name_to_table_1d,
701 tables_2d,
702 name_to_table_2d,
703 ..Default::default()
704 },
705 ..Default::default()
706 }
707 }
708
709 #[test]
710 fn state_in_registry_from_state() {
711 let mut set1 = Set::with_capacity(3);
712 set1.insert(0);
713 set1.insert(2);
714 let mut set2 = Set::with_capacity(3);
715 set2.insert(0);
716 set2.insert(1);
717 let signature_variables = dypdl::SignatureVariables {
718 set_variables: vec![set1, set2],
719 vector_variables: vec![vec![0, 2], vec![1, 2]],
720 element_variables: vec![1, 2],
721 integer_variables: vec![1, 2, 3],
722 continuous_variables: vec![1.0, 2.0, 3.0],
723 };
724 let resource_variables = dypdl::ResourceVariables {
725 element_variables: vec![],
726 integer_variables: vec![4, 5, 6],
727 continuous_variables: vec![4.0, 5.0, 6.0],
728 };
729 let state = StateInRegistry::<Rc<_>>::from(dypdl::State {
730 signature_variables: signature_variables.clone(),
731 resource_variables: resource_variables.clone(),
732 });
733 assert_eq!(
734 state.signature_variables.set_variables,
735 signature_variables.set_variables
736 );
737 assert_eq!(
738 state.signature_variables.vector_variables,
739 signature_variables.vector_variables
740 );
741 assert_eq!(
742 state.signature_variables.element_variables,
743 signature_variables.element_variables
744 );
745 assert_eq!(
746 state.signature_variables.integer_variables,
747 signature_variables.integer_variables
748 );
749 assert_eq!(
750 state.signature_variables.continuous_variables,
751 vec![OrderedFloat(1.0), OrderedFloat(2.0), OrderedFloat(3.0)]
752 );
753 assert_eq!(
754 state.resource_variables.element_variables,
755 resource_variables.element_variables
756 );
757 assert_eq!(
758 state.resource_variables.integer_variables,
759 resource_variables.integer_variables
760 );
761 assert_eq!(
762 state.resource_variables.continuous_variables,
763 resource_variables.continuous_variables
764 );
765 }
766
767 #[test]
768 fn state_in_registry_from_state_with_hashable_signature_variables() {
769 let mut set1 = Set::with_capacity(3);
770 set1.insert(0);
771 set1.insert(2);
772 let mut set2 = Set::with_capacity(3);
773 set2.insert(0);
774 set2.insert(1);
775 let signature_variables = HashableSignatureVariables {
776 set_variables: vec![set1, set2],
777 vector_variables: vec![vec![0, 2], vec![1, 2]],
778 element_variables: vec![1, 2],
779 integer_variables: vec![1, 2, 3],
780 continuous_variables: vec![
781 OrderedContinuous::from(1.0),
782 OrderedContinuous::from(2.0),
783 OrderedContinuous::from(3.0),
784 ],
785 };
786 let resource_variables = dypdl::ResourceVariables {
787 element_variables: vec![],
788 integer_variables: vec![4, 5, 6],
789 continuous_variables: vec![4.0, 5.0, 6.0],
790 };
791 let state = StateInRegistry::<Rc<_>>::from(StateWithHashableSignatureVariables {
792 signature_variables: signature_variables.clone(),
793 resource_variables: resource_variables.clone(),
794 });
795 assert_eq!(
796 state.signature_variables.set_variables,
797 signature_variables.set_variables
798 );
799 assert_eq!(
800 state.signature_variables.vector_variables,
801 signature_variables.vector_variables
802 );
803 assert_eq!(
804 state.signature_variables.element_variables,
805 signature_variables.element_variables
806 );
807 assert_eq!(
808 state.signature_variables.integer_variables,
809 signature_variables.integer_variables
810 );
811 assert_eq!(
812 state.signature_variables.continuous_variables,
813 signature_variables.continuous_variables
814 );
815 assert_eq!(
816 state.resource_variables.element_variables,
817 resource_variables.element_variables
818 );
819 assert_eq!(
820 state.resource_variables.integer_variables,
821 resource_variables.integer_variables
822 );
823 assert_eq!(
824 state.resource_variables.continuous_variables,
825 resource_variables.continuous_variables
826 );
827 }
828
829 #[test]
830 fn state_get_number_of_set_variables() {
831 let state = StateInRegistry {
832 signature_variables: Rc::new(HashableSignatureVariables {
833 set_variables: vec![Set::default()],
834 ..Default::default()
835 }),
836 ..Default::default()
837 };
838 assert_eq!(state.get_number_of_set_variables(), 1);
839 }
840
841 #[test]
842 fn state_get_set_variable() {
843 let mut set = Set::with_capacity(2);
844 set.insert(1);
845 let state = StateInRegistry {
846 signature_variables: Rc::new(HashableSignatureVariables {
847 set_variables: vec![Set::with_capacity(2), set.clone()],
848 ..Default::default()
849 }),
850 ..Default::default()
851 };
852 assert_eq!(state.get_set_variable(0), &Set::with_capacity(2));
853 assert_eq!(state.get_set_variable(1), &set);
854 }
855
856 #[test]
857 #[should_panic]
858 fn state_get_set_variable_panic() {
859 let state = StateInRegistry {
860 signature_variables: Rc::new(HashableSignatureVariables {
861 set_variables: vec![Set::default()],
862 ..Default::default()
863 }),
864 ..Default::default()
865 };
866 state.get_set_variable(1);
867 }
868
869 #[test]
870 fn state_get_number_of_vector_variables() {
871 let state = StateInRegistry {
872 signature_variables: Rc::new(HashableSignatureVariables {
873 vector_variables: vec![Vector::default()],
874 ..Default::default()
875 }),
876 ..Default::default()
877 };
878 assert_eq!(state.get_number_of_vector_variables(), 1);
879 }
880
881 #[test]
882 fn state_get_vector_variable() {
883 let state = StateInRegistry {
884 signature_variables: Rc::new(HashableSignatureVariables {
885 vector_variables: vec![Vector::default(), vec![1]],
886 ..Default::default()
887 }),
888 ..Default::default()
889 };
890 assert_eq!(state.get_vector_variable(0), &Vector::default());
891 assert_eq!(state.get_vector_variable(1), &vec![1]);
892 }
893
894 #[test]
895 #[should_panic]
896 fn state_get_vector_variable_panic() {
897 let state = StateInRegistry {
898 signature_variables: Rc::new(HashableSignatureVariables {
899 vector_variables: vec![Vector::default()],
900 ..Default::default()
901 }),
902 ..Default::default()
903 };
904 state.get_vector_variable(1);
905 }
906
907 #[test]
908 fn state_get_number_of_element_variables() {
909 let state = StateInRegistry {
910 signature_variables: Rc::new(HashableSignatureVariables {
911 element_variables: vec![0],
912 ..Default::default()
913 }),
914 ..Default::default()
915 };
916 assert_eq!(state.get_number_of_element_variables(), 1);
917 }
918
919 #[test]
920 fn state_get_element_variable() {
921 let state = StateInRegistry {
922 signature_variables: Rc::new(HashableSignatureVariables {
923 element_variables: vec![0, 1],
924 ..Default::default()
925 }),
926 ..Default::default()
927 };
928 assert_eq!(state.get_element_variable(0), 0);
929 assert_eq!(state.get_element_variable(1), 1);
930 }
931
932 #[test]
933 #[should_panic]
934 fn state_get_element_variable_panic() {
935 let state = StateInRegistry {
936 signature_variables: Rc::new(HashableSignatureVariables {
937 element_variables: vec![0],
938 ..Default::default()
939 }),
940 ..Default::default()
941 };
942 state.get_element_variable(1);
943 }
944
945 #[test]
946 fn state_get_number_of_integer_variables() {
947 let state = StateInRegistry {
948 signature_variables: Rc::new(HashableSignatureVariables {
949 integer_variables: vec![0],
950 ..Default::default()
951 }),
952 ..Default::default()
953 };
954 assert_eq!(state.get_number_of_integer_variables(), 1);
955 }
956
957 #[test]
958 fn state_get_integer_variable() {
959 let state = StateInRegistry {
960 signature_variables: Rc::new(HashableSignatureVariables {
961 integer_variables: vec![0, 1],
962 ..Default::default()
963 }),
964 ..Default::default()
965 };
966 assert_eq!(state.get_integer_variable(0), 0);
967 assert_eq!(state.get_integer_variable(1), 1);
968 }
969
970 #[test]
971 #[should_panic]
972 fn state_get_integer_variable_panic() {
973 let state = StateInRegistry {
974 signature_variables: Rc::new(HashableSignatureVariables {
975 integer_variables: vec![0],
976 ..Default::default()
977 }),
978 ..Default::default()
979 };
980 state.get_integer_variable(1);
981 }
982
983 #[test]
984 fn state_get_number_of_continuous_variables() {
985 let state = StateInRegistry {
986 signature_variables: Rc::new(HashableSignatureVariables {
987 continuous_variables: vec![OrderedContinuous::from(0.0)],
988 ..Default::default()
989 }),
990 ..Default::default()
991 };
992 assert_eq!(state.get_number_of_continuous_variables(), 1);
993 }
994
995 #[test]
996 fn state_get_continuous_variable() {
997 let state = StateInRegistry {
998 signature_variables: Rc::new(HashableSignatureVariables {
999 continuous_variables: vec![
1000 OrderedContinuous::from(0.0),
1001 OrderedContinuous::from(1.0),
1002 ],
1003 ..Default::default()
1004 }),
1005 ..Default::default()
1006 };
1007 assert_eq!(state.get_continuous_variable(0), 0.0);
1008 assert_eq!(state.get_continuous_variable(1), 1.0);
1009 }
1010
1011 #[test]
1012 #[should_panic]
1013 fn state_get_continuous_variable_panic() {
1014 let state = StateInRegistry {
1015 signature_variables: Rc::new(HashableSignatureVariables {
1016 continuous_variables: vec![OrderedContinuous::from(0.0)],
1017 ..Default::default()
1018 }),
1019 ..Default::default()
1020 };
1021 state.get_continuous_variable(1);
1022 }
1023
1024 #[test]
1025 fn state_get_number_of_element_resource_variables() {
1026 let state = StateInRegistry::<Rc<_>> {
1027 resource_variables: ResourceVariables {
1028 element_variables: vec![0],
1029 ..Default::default()
1030 },
1031 ..Default::default()
1032 };
1033 assert_eq!(state.get_number_of_element_resource_variables(), 1);
1034 }
1035
1036 #[test]
1037 fn state_get_element_resource_variable() {
1038 let state = StateInRegistry::<Rc<_>> {
1039 resource_variables: ResourceVariables {
1040 element_variables: vec![0, 1],
1041 ..Default::default()
1042 },
1043 ..Default::default()
1044 };
1045 assert_eq!(state.get_element_resource_variable(0), 0);
1046 assert_eq!(state.get_element_resource_variable(1), 1);
1047 }
1048
1049 #[test]
1050 #[should_panic]
1051 fn state_get_element_resource_variable_panic() {
1052 let state = StateInRegistry::<Rc<_>> {
1053 resource_variables: ResourceVariables {
1054 element_variables: vec![0],
1055 ..Default::default()
1056 },
1057 ..Default::default()
1058 };
1059 state.get_element_resource_variable(1);
1060 }
1061
1062 #[test]
1063 fn state_get_number_of_integer_resource_variables() {
1064 let state = StateInRegistry::<Rc<_>> {
1065 resource_variables: ResourceVariables {
1066 integer_variables: vec![0],
1067 ..Default::default()
1068 },
1069 ..Default::default()
1070 };
1071 assert_eq!(state.get_number_of_integer_resource_variables(), 1);
1072 }
1073
1074 #[test]
1075 fn state_get_integer_resource_variable() {
1076 let state = StateInRegistry::<Rc<_>> {
1077 resource_variables: ResourceVariables {
1078 integer_variables: vec![0, 1],
1079 ..Default::default()
1080 },
1081 ..Default::default()
1082 };
1083 assert_eq!(state.get_integer_resource_variable(0), 0);
1084 assert_eq!(state.get_integer_resource_variable(1), 1);
1085 }
1086
1087 #[test]
1088 #[should_panic]
1089 fn state_get_integer_resource_variable_panic() {
1090 let state = StateInRegistry::<Rc<_>> {
1091 resource_variables: ResourceVariables {
1092 integer_variables: vec![0],
1093 ..Default::default()
1094 },
1095 ..Default::default()
1096 };
1097 state.get_integer_resource_variable(1);
1098 }
1099
1100 #[test]
1101 fn state_get_number_of_continuous_resource_variables() {
1102 let state = StateInRegistry::<Rc<_>> {
1103 resource_variables: ResourceVariables {
1104 continuous_variables: vec![0.0],
1105 ..Default::default()
1106 },
1107 ..Default::default()
1108 };
1109 assert_eq!(state.get_number_of_continuous_resource_variables(), 1);
1110 }
1111
1112 #[test]
1113 fn state_get_continuous_resource_variable() {
1114 let state = StateInRegistry::<Rc<_>> {
1115 resource_variables: ResourceVariables {
1116 continuous_variables: vec![0.0, 1.0],
1117 ..Default::default()
1118 },
1119 ..Default::default()
1120 };
1121 assert_eq!(state.get_continuous_resource_variable(0), 0.0);
1122 assert_eq!(state.get_continuous_resource_variable(1), 1.0);
1123 }
1124
1125 #[test]
1126 #[should_panic]
1127 fn state_get_continuous_resource_variable_panic() {
1128 let state = StateInRegistry::<Rc<_>> {
1129 resource_variables: ResourceVariables {
1130 continuous_variables: vec![0.0],
1131 ..Default::default()
1132 },
1133 ..Default::default()
1134 };
1135 state.get_continuous_resource_variable(1);
1136 }
1137
1138 #[test]
1139 fn apply_effect() {
1140 let state_functions = StateFunctions::default();
1141 let mut function_cache = StateFunctionCache::new(&state_functions);
1142
1143 let mut set1 = Set::with_capacity(3);
1144 set1.insert(0);
1145 set1.insert(2);
1146 let mut set2 = Set::with_capacity(3);
1147 set2.insert(0);
1148 set2.insert(1);
1149 let state = StateInRegistry {
1150 signature_variables: Rc::new(HashableSignatureVariables {
1151 set_variables: vec![set1, set2],
1152 vector_variables: vec![vec![0, 2], vec![1, 2]],
1153 element_variables: vec![1, 2],
1154 integer_variables: vec![1, 2, 3],
1155 continuous_variables: vec![OrderedFloat(1.0), OrderedFloat(2.0), OrderedFloat(3.0)],
1156 }),
1157 resource_variables: ResourceVariables {
1158 element_variables: vec![0, 1],
1159 integer_variables: vec![4, 5, 6],
1160 continuous_variables: vec![4.0, 5.0, 6.0],
1161 },
1162 };
1163 let registry = generate_registry();
1164 let set_effect1 = SetExpression::SetElementOperation(
1165 SetElementOperator::Add,
1166 ElementExpression::Constant(1),
1167 Box::new(SetExpression::Reference(ReferenceExpression::Variable(0))),
1168 );
1169 let set_effect2 = SetExpression::SetElementOperation(
1170 SetElementOperator::Remove,
1171 ElementExpression::Constant(0),
1172 Box::new(SetExpression::Reference(ReferenceExpression::Variable(1))),
1173 );
1174 let vector_effect1 = VectorExpression::Push(
1175 ElementExpression::Constant(1),
1176 Box::new(VectorExpression::Reference(ReferenceExpression::Variable(
1177 0,
1178 ))),
1179 );
1180 let vector_effect2 = VectorExpression::Push(
1181 ElementExpression::Constant(0),
1182 Box::new(VectorExpression::Reference(ReferenceExpression::Variable(
1183 1,
1184 ))),
1185 );
1186 let element_effect1 = ElementExpression::Constant(2);
1187 let element_effect2 = ElementExpression::Constant(1);
1188 let integer_effect1 = IntegerExpression::BinaryOperation(
1189 BinaryOperator::Sub,
1190 Box::new(IntegerExpression::Variable(0)),
1191 Box::new(IntegerExpression::Constant(1)),
1192 );
1193 let integer_effect2 = IntegerExpression::BinaryOperation(
1194 BinaryOperator::Mul,
1195 Box::new(IntegerExpression::Variable(1)),
1196 Box::new(IntegerExpression::Constant(2)),
1197 );
1198 let continuous_effect1 = ContinuousExpression::BinaryOperation(
1199 BinaryOperator::Sub,
1200 Box::new(ContinuousExpression::Variable(0)),
1201 Box::new(ContinuousExpression::Constant(1.0)),
1202 );
1203 let continuous_effect2 = ContinuousExpression::BinaryOperation(
1204 BinaryOperator::Mul,
1205 Box::new(ContinuousExpression::Variable(1)),
1206 Box::new(ContinuousExpression::Constant(2.0)),
1207 );
1208 let element_resource_effect1 = ElementExpression::Constant(1);
1209 let element_resource_effect2 = ElementExpression::Constant(0);
1210 let integer_resource_effect1 = IntegerExpression::BinaryOperation(
1211 BinaryOperator::Add,
1212 Box::new(IntegerExpression::ResourceVariable(0)),
1213 Box::new(IntegerExpression::Constant(1)),
1214 );
1215 let integer_resource_effect2 = IntegerExpression::BinaryOperation(
1216 BinaryOperator::Div,
1217 Box::new(IntegerExpression::ResourceVariable(1)),
1218 Box::new(IntegerExpression::Constant(2)),
1219 );
1220 let continuous_resource_effect1 = ContinuousExpression::BinaryOperation(
1221 BinaryOperator::Add,
1222 Box::new(ContinuousExpression::ResourceVariable(0)),
1223 Box::new(ContinuousExpression::Constant(1.0)),
1224 );
1225 let continuous_resource_effect2 = ContinuousExpression::BinaryOperation(
1226 BinaryOperator::Div,
1227 Box::new(ContinuousExpression::ResourceVariable(1)),
1228 Box::new(ContinuousExpression::Constant(2.0)),
1229 );
1230 let effect = dypdl::Effect {
1231 set_effects: vec![(0, set_effect1), (1, set_effect2)],
1232 vector_effects: vec![(0, vector_effect1), (1, vector_effect2)],
1233 element_effects: vec![(0, element_effect1), (1, element_effect2)],
1234 integer_effects: vec![(0, integer_effect1), (1, integer_effect2)],
1235 continuous_effects: vec![(0, continuous_effect1), (1, continuous_effect2)],
1236 element_resource_effects: vec![
1237 (0, element_resource_effect1),
1238 (1, element_resource_effect2),
1239 ],
1240 integer_resource_effects: vec![
1241 (0, integer_resource_effect1),
1242 (1, integer_resource_effect2),
1243 ],
1244 continuous_resource_effects: vec![
1245 (0, continuous_resource_effect1),
1246 (1, continuous_resource_effect2),
1247 ],
1248 };
1249
1250 let mut set1 = Set::with_capacity(3);
1251 set1.insert(0);
1252 set1.insert(1);
1253 set1.insert(2);
1254 let mut set2 = Set::with_capacity(3);
1255 set2.insert(1);
1256 let expected = StateInRegistry {
1257 signature_variables: Rc::new(HashableSignatureVariables {
1258 set_variables: vec![set1, set2],
1259 vector_variables: vec![vec![0, 2, 1], vec![1, 2, 0]],
1260 element_variables: vec![2, 1],
1261 integer_variables: vec![0, 4, 3],
1262 continuous_variables: vec![OrderedFloat(0.0), OrderedFloat(4.0), OrderedFloat(3.0)],
1263 }),
1264 resource_variables: ResourceVariables {
1265 element_variables: vec![1, 0],
1266 integer_variables: vec![5, 2, 6],
1267 continuous_variables: vec![5.0, 2.5, 6.0],
1268 },
1269 };
1270 let successor: StateInRegistry = state.apply_effect(
1271 &effect,
1272 &mut function_cache,
1273 &state_functions,
1274 ®istry,
1275 );
1276 assert_eq!(successor, expected);
1277 }
1278
1279 #[test]
1280 fn remove_dominated_non_dominating_non_dominated() {
1281 let model = generate_model();
1282
1283 let state1 = StateInRegistry {
1284 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1285 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1286 };
1287 let state2 = StateInRegistry {
1288 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1289 resource_variables: generate_resource_variables(vec![2, 3, 3]),
1290 };
1291 let mut non_dominated = vec![
1292 Rc::new(MockInformation {
1293 state: state1,
1294 cost: 1,
1295 value: Cell::new(None),
1296 }),
1297 Rc::new(MockInformation {
1298 state: state2,
1299 cost: 3,
1300 value: Cell::new(None),
1301 }),
1302 ];
1303
1304 let state3 = StateInRegistry {
1305 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1306 resource_variables: generate_resource_variables(vec![2, 2, 3]),
1307 };
1308 let cost = 2;
1309
1310 let (dominating, dominated, same_state_index) =
1311 remove_dominated(&mut non_dominated, &model, &state3, cost);
1312 assert!(!dominating);
1313 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1314 assert_eq!(same_state_index, None);
1315 }
1316
1317 #[test]
1318 fn remove_dominated_dominated() {
1319 let model = generate_model();
1320
1321 let state1 = StateInRegistry {
1322 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1323 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1324 };
1325 let state2 = StateInRegistry {
1326 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1327 resource_variables: generate_resource_variables(vec![2, 3, 3]),
1328 };
1329 let state3 = StateInRegistry {
1330 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1331 resource_variables: generate_resource_variables(vec![3, 3, 3]),
1332 };
1333 let mut non_dominated = vec![
1334 Rc::new(MockInformation {
1335 state: state1.clone(),
1336 cost: 1,
1337 value: Cell::new(None),
1338 }),
1339 Rc::new(MockInformation {
1340 state: state2,
1341 cost: 3,
1342 value: Cell::new(None),
1343 }),
1344 Rc::new(MockInformation {
1345 state: state3,
1346 cost: 1,
1347 value: Cell::new(None),
1348 }),
1349 ];
1350
1351 let state4 = StateInRegistry {
1352 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1353 resource_variables: generate_resource_variables(vec![2, 3, 3]),
1354 };
1355 let cost = 0;
1356
1357 let (dominating, dominated, same_state_index) =
1358 remove_dominated(&mut non_dominated, &model, &state4, cost);
1359 assert!(!dominating);
1360 assert_eq!(dominated.len(), 2);
1361 assert!(same_state_index.is_some());
1362 let same_state_index = same_state_index.unwrap();
1363 assert_eq!(dominated[same_state_index].state, state4);
1364 assert_eq!(dominated[same_state_index].cost, 3);
1365 assert_eq!(dominated[1 - same_state_index].state, state1);
1366 assert_eq!(dominated[1 - same_state_index].cost, 1);
1367 }
1368
1369 #[test]
1370 fn remove_dominated_dominating() {
1371 let model = generate_model();
1372
1373 let state1 = StateInRegistry {
1374 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1375 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1376 };
1377 let state2 = StateInRegistry {
1378 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1379 resource_variables: generate_resource_variables(vec![2, 3, 3]),
1380 };
1381 let mut non_dominated = vec![
1382 Rc::new(MockInformation {
1383 state: state1,
1384 cost: 1,
1385 value: Cell::new(None),
1386 }),
1387 Rc::new(MockInformation {
1388 state: state2,
1389 cost: 3,
1390 value: Cell::new(None),
1391 }),
1392 ];
1393
1394 let state3 = StateInRegistry {
1395 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1396 resource_variables: generate_resource_variables(vec![2, 2, 3]),
1397 };
1398 let cost = 4;
1399
1400 let (dominating, dominated, same_state_index) =
1401 remove_dominated(&mut non_dominated, &model, &state3, cost);
1402 assert!(dominating);
1403 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1404 assert_eq!(same_state_index, None);
1405 }
1406
1407 #[test]
1408 fn insert_get_new_information() {
1409 let model = Rc::new(generate_model());
1410 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
1411
1412 let state = StateInRegistry {
1413 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1414 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1415 };
1416 assert_eq!(registry.get(&state, 1), None);
1417 let information = MockInformation {
1418 state: state.clone(),
1419 cost: 1,
1420 value: Cell::new(None),
1421 };
1422 let result = registry.insert(information);
1423 let information1 = result.information;
1424 assert!(information1.is_some());
1425 let information1 = information1.unwrap();
1426 let dominated = result.dominated;
1427 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1428 assert_eq!(information1.state, state);
1429 assert_eq!(information1.value.get(), None);
1430 assert_eq!(registry.get(&state, 1), Some(&information1));
1431
1432 let state = StateInRegistry {
1433 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1434 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1435 };
1436 assert_eq!(registry.get(&state, 1), None);
1437 let information = MockInformation {
1438 state: state.clone(),
1439 cost: 1,
1440 value: Cell::new(None),
1441 };
1442 let result = registry.insert(information);
1443 let information2 = result.information;
1444 assert!(information2.is_some());
1445 let information2 = information2.unwrap();
1446 let dominated = result.dominated;
1447 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1448 assert_eq!(information2.state, state);
1449 assert_eq!(information2.value.get(), None);
1450 assert_eq!(registry.get(&state, 1), Some(&information2));
1451
1452 let state = StateInRegistry {
1453 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1454 resource_variables: generate_resource_variables(vec![3, 1, 3]),
1455 };
1456 assert_eq!(registry.get(&state, 1), None);
1457 let information = MockInformation {
1458 state: state.clone(),
1459 cost: 1,
1460 value: Cell::new(None),
1461 };
1462 let result = registry.insert(information);
1463 let information3 = result.information;
1464 assert!(information3.is_some());
1465 let information3 = information3.unwrap();
1466 let dominated = result.dominated;
1467 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1468 assert_eq!(information3.state, state);
1469 assert_eq!(information3.value.get(), None);
1470 assert_eq!(registry.get(&state, 1), Some(&information3));
1471
1472 let state = StateInRegistry {
1473 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1474 resource_variables: generate_resource_variables(vec![0, 1, 3]),
1475 };
1476 assert_eq!(registry.get(&state, 0), None);
1477 let information = MockInformation {
1478 state: state.clone(),
1479 cost: 0,
1480 value: Cell::new(None),
1481 };
1482 let result = registry.insert(information);
1483 let information4 = result.information;
1484 assert!(information4.is_some());
1485 let information4 = information4.unwrap();
1486 let dominated = result.dominated;
1487 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1488 assert_eq!(information4.state, state);
1489 assert_eq!(information4.value.get(), None);
1490 assert_eq!(registry.get(&state, 0), Some(&information4));
1491
1492 let information_vec = registry.remove(&generate_signature_variables(vec![0, 1, 2]));
1493 assert_eq!(information_vec, Some(vec![information1]));
1494
1495 let information_vec = registry.remove(&generate_signature_variables(vec![1, 2, 3]));
1496 assert_eq!(
1497 information_vec,
1498 Some(vec![information2, information3, information4])
1499 );
1500
1501 let information_vec = registry.remove(&generate_signature_variables(vec![2, 3, 4]));
1502 assert_eq!(information_vec, None);
1503 }
1504
1505 #[test]
1506 fn insert_information_dominated() {
1507 let model = Rc::new(generate_model());
1508 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
1509
1510 let state = StateInRegistry {
1511 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1512 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1513 };
1514 let previous = MockInformation {
1515 state: state.clone(),
1516 cost: 1,
1517 value: Cell::new(None),
1518 };
1519 let result = registry.insert(previous);
1520 let previous = result.information;
1521 assert!(previous.is_some());
1522 let previous = previous.unwrap();
1523 let dominated = result.dominated;
1524 assert_eq!(previous.state, state);
1525 assert_eq!(previous.value.get(), None);
1526 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1527
1528 let state = StateInRegistry {
1529 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1530 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1531 };
1532 assert_eq!(registry.get(&state, 1), Some(&previous));
1533 let information = MockInformation {
1534 state: state.clone(),
1535 cost: 1,
1536 value: Cell::new(None),
1537 };
1538 let result = registry.insert(information);
1539 assert!(result.information.is_none());
1540 assert!(result.dominated.is_empty());
1541 assert_eq!(registry.get(&state, 1), Some(&previous));
1542
1543 let state = StateInRegistry {
1544 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1545 resource_variables: generate_resource_variables(vec![0, 2, 3]),
1546 };
1547 assert_eq!(registry.get(&state, 1), Some(&previous));
1548 let information = MockInformation {
1549 state: state.clone(),
1550 cost: 1,
1551 value: Cell::new(None),
1552 };
1553 let result = registry.insert(information);
1554 assert!(result.information.is_none());
1555 assert!(result.dominated.is_empty());
1556 assert_eq!(registry.get(&state, 1), Some(&previous));
1557
1558 let state = StateInRegistry {
1559 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1560 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1561 };
1562 assert_eq!(registry.get(&state, 2), Some(&previous));
1563 let information = MockInformation {
1564 state: state.clone(),
1565 cost: 2,
1566 value: Cell::new(None),
1567 };
1568 let result = registry.insert(information);
1569 assert!(result.information.is_none());
1570 assert!(result.dominated.is_empty());
1571 assert_eq!(registry.get(&state, 2), Some(&previous));
1572
1573 let mut iter = registry.drain();
1574 assert_eq!(
1575 iter.next(),
1576 Some((previous.state.signature_variables.clone(), vec![previous]))
1577 );
1578 assert_eq!(iter.next(), None);
1579 }
1580
1581 #[test]
1582 fn insert_get_dominating_information() {
1583 let model = Rc::new(generate_model());
1584 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
1585
1586 let state = StateInRegistry {
1587 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1588 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1589 };
1590 assert_eq!(registry.get(&state, 2), None);
1591 let information = MockInformation {
1592 state: state.clone(),
1593 cost: 2,
1594 value: Cell::new(None),
1595 };
1596 let result = registry.insert(information);
1597 let information1 = result.information;
1598 assert!(information1.is_some());
1599 let information1 = information1.unwrap();
1600 let dominated = result.dominated;
1601 assert_eq!(information1.state, state);
1602 assert_eq!(information1.value.get(), None);
1603 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1604 assert_eq!(registry.get(&information1.state, 2), Some(&information1));
1605
1606 let state = StateInRegistry {
1607 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1608 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1609 };
1610 assert_eq!(registry.get(&state, 1), None);
1611 let information = MockInformation {
1612 state,
1613 cost: 1,
1614 value: Cell::new(None),
1615 };
1616 let result = registry.insert(information);
1617 let information2 = result.information;
1618 assert!(information2.is_some());
1619 let information2 = information2.unwrap();
1620 let dominated = result.dominated;
1621 assert_eq!(
1622 information2.state.signature_variables,
1623 information1.state.signature_variables
1624 );
1625 assert_eq!(
1626 information2.state.resource_variables,
1627 information1.state.resource_variables
1628 );
1629 assert_eq!(information2.value.get(), None);
1630 assert_eq!(dominated, SmallVec::<[_; 1]>::from([information1.clone()]));
1631 assert_eq!(registry.get(&information1.state, 2), Some(&information2));
1632 assert_eq!(registry.get(&information2.state, 1), Some(&information2));
1633
1634 let state = StateInRegistry {
1635 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1636 resource_variables: generate_resource_variables(vec![2, 1, 3]),
1637 };
1638 assert_eq!(registry.get(&state, 0), None);
1639 let information = MockInformation {
1640 state,
1641 cost: 0,
1642 value: Cell::new(None),
1643 };
1644 let result = registry.insert(information);
1645 let information3 = result.information;
1646 assert!(information3.is_some());
1647 let information3 = information3.unwrap();
1648 let dominated = result.dominated;
1649 assert_eq!(
1650 information3.state.signature_variables,
1651 information2.state.signature_variables
1652 );
1653 assert_ne!(
1654 information3.state.resource_variables,
1655 information2.state.resource_variables,
1656 );
1657 assert_eq!(information3.value.get(), None);
1658 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1659 assert_eq!(registry.get(&information2.state, 1), Some(&information2));
1660 assert_eq!(registry.get(&information3.state, 0), Some(&information3));
1661
1662 let state = StateInRegistry {
1663 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1664 resource_variables: generate_resource_variables(vec![2, 3, 3]),
1665 };
1666 assert_eq!(registry.get(&state, 0), None);
1667 let information = MockInformation {
1668 state,
1669 cost: 0,
1670 value: Cell::new(None),
1671 };
1672 let result = registry.insert(information);
1673 let information4 = result.information;
1674 assert!(information4.is_some());
1675 let information4 = information4.unwrap();
1676 let mut dominated = result.dominated;
1677 assert_eq!(
1678 information4.state.signature_variables,
1679 information2.state.signature_variables
1680 );
1681 assert_eq!(
1682 information4.state.signature_variables,
1683 information3.state.signature_variables
1684 );
1685 assert_ne!(
1686 information4.state.resource_variables,
1687 information2.state.resource_variables,
1688 );
1689 assert_ne!(
1690 information4.state.resource_variables,
1691 information3.state.resource_variables,
1692 );
1693 assert_eq!(information4.value.get(), None);
1694 dominated.sort();
1695 assert_eq!(
1696 dominated,
1697 SmallVec::<[_; 1]>::from(vec![information3.clone(), information2.clone()])
1698 );
1699 assert_eq!(registry.get(&information1.state, 2), Some(&information4));
1700 assert_eq!(registry.get(&information2.state, 1), Some(&information4));
1701 assert_eq!(registry.get(&information3.state, 0), Some(&information4));
1702 assert_eq!(registry.get(&information4.state, 0), Some(&information4));
1703
1704 let mut iter = registry.drain();
1705 assert_eq!(
1706 iter.next(),
1707 Some((
1708 information4.state.signature_variables.clone(),
1709 vec![information4]
1710 ))
1711 );
1712 assert_eq!(iter.next(), None);
1713 }
1714
1715 #[test]
1716 fn insert_with_get_new_information() {
1717 let model = Rc::new(generate_model());
1718 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
1719 let constructor = |state: StateInRegistry, cost: Integer, _: Option<&MockInformation>| {
1720 Some(MockInformation {
1721 state,
1722 cost,
1723 value: Cell::new(None),
1724 })
1725 };
1726
1727 let state = StateInRegistry {
1728 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1729 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1730 };
1731 assert_eq!(registry.get(&state, 1), None);
1732 let result = registry.insert_with(state, 1, constructor);
1733 let information1 = result.information;
1734 assert!(information1.is_some());
1735 let information1 = information1.unwrap();
1736 let dominated = result.dominated;
1737 let state = StateInRegistry {
1738 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1739 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1740 };
1741 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1742 assert_eq!(information1.state, state);
1743 assert_eq!(information1.value.get(), None);
1744 assert_eq!(registry.get(&state, 1), Some(&information1));
1745
1746 let state = StateInRegistry {
1747 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1748 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1749 };
1750 assert_eq!(registry.get(&state, 1), None);
1751 let result = registry.insert_with(state, 1, constructor);
1752 let information2 = result.information;
1753 assert!(information2.is_some());
1754 let information2 = information2.unwrap();
1755 let dominated = result.dominated;
1756 let state = StateInRegistry {
1757 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1758 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1759 };
1760 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1761 assert_eq!(information2.state, state);
1762 assert_eq!(information2.value.get(), None);
1763 assert_eq!(registry.get(&state, 1), Some(&information2));
1764
1765 let state = StateInRegistry {
1766 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1767 resource_variables: generate_resource_variables(vec![3, 1, 3]),
1768 };
1769 assert_eq!(registry.get(&state, 1), None);
1770 let result = registry.insert_with(state, 1, constructor);
1771 let information3 = result.information;
1772 assert!(information3.is_some());
1773 let information3 = information3.unwrap();
1774 let dominated = result.dominated;
1775 let state = StateInRegistry {
1776 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1777 resource_variables: generate_resource_variables(vec![3, 1, 3]),
1778 };
1779 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1780 assert_eq!(information3.state, state);
1781 assert_eq!(information3.value.get(), None);
1782 assert_eq!(registry.get(&state, 1), Some(&information3));
1783
1784 let state = StateInRegistry {
1785 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1786 resource_variables: generate_resource_variables(vec![0, 1, 3]),
1787 };
1788 assert_eq!(registry.get(&state, 0), None);
1789 let result = registry.insert_with(state, 0, constructor);
1790 let information4 = result.information;
1791 assert!(information4.is_some());
1792 let information4 = information4.unwrap();
1793 let dominated = result.dominated;
1794 let state = StateInRegistry {
1795 signature_variables: generate_signature_variables(vec![1, 2, 3]),
1796 resource_variables: generate_resource_variables(vec![0, 1, 3]),
1797 };
1798 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1799 assert_eq!(information4.state, state);
1800 assert_eq!(information4.value.get(), None);
1801 assert_eq!(registry.get(&state, 0), Some(&information4));
1802
1803 let information_vec = registry.remove(&generate_signature_variables(vec![0, 1, 2]));
1804 assert_eq!(information_vec, Some(vec![information1]));
1805
1806 let information_vec = registry.remove(&generate_signature_variables(vec![1, 2, 3]));
1807 assert_eq!(
1808 information_vec,
1809 Some(vec![information2, information3, information4])
1810 );
1811
1812 let information_vec = registry.remove(&generate_signature_variables(vec![2, 3, 4]));
1813 assert_eq!(information_vec, None);
1814 }
1815
1816 #[test]
1817 fn insert_with_information_dominated() {
1818 let model = Rc::new(generate_model());
1819 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
1820
1821 let constructor = |state: StateInRegistry, cost: Integer, _: Option<&MockInformation>| {
1822 Some(MockInformation {
1823 state,
1824 cost,
1825 value: Cell::new(None),
1826 })
1827 };
1828
1829 let state = StateInRegistry {
1830 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1831 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1832 };
1833 let result = registry.insert_with(state, 1, constructor);
1834 let previous = result.information;
1835 assert!(previous.is_some());
1836 let previous = previous.unwrap();
1837 let dominated = result.dominated;
1838 let state = StateInRegistry {
1839 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1840 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1841 };
1842 assert_eq!(previous.state, state);
1843 assert_eq!(previous.value.get(), None);
1844 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1845
1846 let state = StateInRegistry {
1847 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1848 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1849 };
1850 assert_eq!(registry.get(&state, 1), Some(&previous));
1851 let result = registry.insert_with(state, 1, constructor);
1852 assert!(result.information.is_none());
1853 assert!(result.dominated.is_empty());
1854 let state = StateInRegistry {
1855 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1856 resource_variables: generate_resource_variables(vec![0, 2, 3]),
1857 };
1858 assert_eq!(registry.get(&state, 1), Some(&previous));
1859
1860 let state = StateInRegistry {
1861 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1862 resource_variables: generate_resource_variables(vec![0, 2, 3]),
1863 };
1864 assert_eq!(registry.get(&state, 1), Some(&previous));
1865 let result = registry.insert_with(state, 1, constructor);
1866 assert!(result.information.is_none());
1867 assert!(result.dominated.is_empty());
1868 let state = StateInRegistry {
1869 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1870 resource_variables: generate_resource_variables(vec![0, 2, 3]),
1871 };
1872 assert_eq!(registry.get(&state, 1), Some(&previous));
1873
1874 let state = StateInRegistry {
1875 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1876 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1877 };
1878 assert_eq!(registry.get(&state, 2), Some(&previous));
1879 let result = registry.insert_with(state, 2, constructor);
1880 assert!(result.information.is_none());
1881 assert!(result.dominated.is_empty());
1882 let state = StateInRegistry {
1883 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1884 resource_variables: generate_resource_variables(vec![0, 2, 3]),
1885 };
1886 assert_eq!(registry.get(&state, 2), Some(&previous));
1887
1888 let mut iter = registry.drain();
1889 assert_eq!(
1890 iter.next(),
1891 Some((previous.state.signature_variables.clone(), vec![previous]))
1892 );
1893 assert_eq!(iter.next(), None);
1894 }
1895
1896 #[test]
1897 fn insert_with_get_dominating_information() {
1898 let model = Rc::new(generate_model());
1899 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
1900 let constructor =
1901 |state: StateInRegistry, cost: Integer, other: Option<&MockInformation>| {
1902 if let Some(other) = other {
1903 other.value.get().map(|value| MockInformation {
1904 state,
1905 cost,
1906 value: Cell::new(Some(value)),
1907 })
1908 } else {
1909 Some(MockInformation {
1910 state,
1911 cost,
1912 value: Cell::new(None),
1913 })
1914 }
1915 };
1916
1917 let state = StateInRegistry {
1918 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1919 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1920 };
1921 assert_eq!(registry.get(&state, 2), None);
1922 let result = registry.insert_with(state, 2, constructor);
1923 let information1 = result.information;
1924 assert!(information1.is_some());
1925 let information1 = information1.unwrap();
1926 let dominated = result.dominated;
1927 let state = StateInRegistry {
1928 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1929 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1930 };
1931 assert_eq!(information1.state, state);
1932 assert_eq!(information1.value.get(), None);
1933 information1.value.set(Some(10));
1934 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1935 assert_eq!(registry.get(&information1.state, 2), Some(&information1));
1936
1937 let state = StateInRegistry {
1938 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1939 resource_variables: generate_resource_variables(vec![1, 2, 3]),
1940 };
1941 assert_eq!(registry.get(&state, 1), None);
1942 let result = registry.insert_with(state, 1, constructor);
1943 let information2 = result.information;
1944 assert!(information2.is_some());
1945 let information2 = information2.unwrap();
1946 let dominated = result.dominated;
1947 assert_eq!(
1948 information2.state.signature_variables,
1949 information1.state.signature_variables
1950 );
1951 assert_eq!(
1952 information2.state.resource_variables,
1953 information1.state.resource_variables
1954 );
1955 assert_eq!(information2.value.get(), Some(10));
1956 assert_eq!(dominated, SmallVec::<[_; 1]>::from([information1.clone()]));
1957 assert_eq!(registry.get(&information1.state, 2), Some(&information2));
1958 assert_eq!(registry.get(&information2.state, 1), Some(&information2));
1959
1960 let state = StateInRegistry {
1961 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1962 resource_variables: generate_resource_variables(vec![2, 1, 3]),
1963 };
1964 assert_eq!(registry.get(&state, 0), None);
1965 let result = registry.insert_with(state, 0, constructor);
1966 let information3 = result.information;
1967 assert!(information3.is_some());
1968 let information3 = information3.unwrap();
1969 let dominated = result.dominated;
1970 assert_eq!(
1971 information3.state.signature_variables,
1972 information2.state.signature_variables
1973 );
1974 assert_ne!(
1975 information3.state.resource_variables,
1976 information2.state.resource_variables,
1977 );
1978 assert_eq!(information3.value.get(), None);
1979 assert_eq!(dominated, SmallVec::<[_; 1]>::new());
1980 assert_eq!(registry.get(&information1.state, 2), Some(&information2));
1981 assert_eq!(registry.get(&information2.state, 1), Some(&information2));
1982 assert_eq!(registry.get(&information3.state, 0), Some(&information3));
1983
1984 let state = StateInRegistry {
1985 signature_variables: generate_signature_variables(vec![0, 1, 2]),
1986 resource_variables: generate_resource_variables(vec![2, 3, 3]),
1987 };
1988 assert_eq!(registry.get(&state, 0), None);
1989 let result = registry.insert_with(state, 0, constructor);
1990 let information4 = result.information;
1991 assert!(information4.is_some());
1992 let information4 = information4.unwrap();
1993 let mut dominated = result.dominated;
1994 assert_eq!(
1995 information4.state.signature_variables,
1996 information2.state.signature_variables
1997 );
1998 assert_eq!(
1999 information4.state.signature_variables,
2000 information3.state.signature_variables
2001 );
2002 assert_ne!(
2003 information4.state.resource_variables,
2004 information2.state.resource_variables,
2005 );
2006 assert_ne!(
2007 information4.state.resource_variables,
2008 information3.state.resource_variables,
2009 );
2010 assert_eq!(information4.value.get(), None);
2011 assert_eq!(information4.value.get(), None);
2012 dominated.sort();
2013 assert_eq!(
2014 dominated,
2015 SmallVec::<[_; 1]>::from(vec![information3.clone(), information2.clone()])
2016 );
2017 assert_eq!(registry.get(&information1.state, 2), Some(&information4));
2018 assert_eq!(registry.get(&information2.state, 1), Some(&information4));
2019 assert_eq!(registry.get(&information3.state, 0), Some(&information3));
2020 assert_eq!(registry.get(&information4.state, 0), Some(&information4));
2021
2022 let mut iter = registry.drain();
2023 assert_eq!(
2024 iter.next(),
2025 Some((
2026 information4.state.signature_variables.clone(),
2027 vec![information4]
2028 ))
2029 );
2030 assert_eq!(iter.next(), None);
2031 }
2032
2033 #[test]
2034 fn get_model() {
2035 let model = Rc::new(generate_model());
2036 let registry = StateRegistry::<i32, MockInformation>::new(model.clone());
2037 assert_eq!(registry.model(), &model);
2038 }
2039
2040 #[test]
2041 fn reserve() {
2042 let model = Rc::new(generate_model());
2043 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
2044 registry.reserve(10);
2045 assert!(registry.registry.capacity() >= 10);
2046 }
2047
2048 #[test]
2049 fn clear() {
2050 let model = Rc::new(generate_model());
2051 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
2052
2053 let state = StateInRegistry {
2054 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2055 resource_variables: generate_resource_variables(vec![1, 2, 3]),
2056 };
2057 let constructor = |state: StateInRegistry, cost: Integer, _: Option<&MockInformation>| {
2058 Some(MockInformation {
2059 state,
2060 cost,
2061 value: Cell::new(None),
2062 })
2063 };
2064 let result = registry.insert_with(state, 1, constructor);
2065 let information = result.information;
2066 assert!(information.is_some());
2067 let information = information.unwrap();
2068 let state = StateInRegistry {
2069 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2070 resource_variables: generate_resource_variables(vec![1, 2, 3]),
2071 };
2072 assert_eq!(information.state, state);
2073 assert_eq!(registry.get(&information.state, 1), Some(&information));
2074
2075 registry.clear();
2076
2077 let state = StateInRegistry {
2078 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2079 resource_variables: generate_resource_variables(vec![1, 2, 3]),
2080 };
2081 assert_eq!(registry.get(&information.state, 1), None);
2082 let result = registry.insert_with(state, 1, constructor);
2083 let information = result.information;
2084 assert!(information.is_some());
2085 let information = information.unwrap();
2086 let state = StateInRegistry {
2087 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2088 resource_variables: generate_resource_variables(vec![1, 2, 3]),
2089 };
2090 assert_eq!(information.state, state);
2091 assert_eq!(registry.get(&information.state, 1), Some(&information));
2092 }
2093
2094 #[test]
2095 fn remove() {
2096 let model = Rc::new(generate_model());
2097 let mut registry = StateRegistry::<i32, MockInformation>::new(model);
2098
2099 let information_vec = registry.remove(&generate_signature_variables(vec![0, 1, 2]));
2100 assert_eq!(information_vec, None);
2101
2102 let state = StateInRegistry {
2103 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2104 resource_variables: generate_resource_variables(vec![1, 2, 3]),
2105 };
2106 let information = MockInformation {
2107 state,
2108 cost: 0,
2109 value: Cell::new(None),
2110 };
2111 let information = registry.insert(information);
2112 assert!(information.information.is_some());
2113
2114 let state = StateInRegistry {
2115 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2116 resource_variables: generate_resource_variables(vec![1, 2, 4]),
2117 };
2118 let information = MockInformation {
2119 state,
2120 cost: 1,
2121 value: Cell::new(None),
2122 };
2123 let information = registry.insert(information);
2124 assert!(information.information.is_none());
2125
2126 let state = StateInRegistry {
2127 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2128 resource_variables: generate_resource_variables(vec![1, 2, 2]),
2129 };
2130 let information = MockInformation {
2131 state,
2132 cost: 0,
2133 value: Cell::new(None),
2134 };
2135 let information = registry.insert(information);
2136 assert!(information.information.is_some());
2137
2138 let state = StateInRegistry {
2139 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2140 resource_variables: generate_resource_variables(vec![2, 3, 4]),
2141 };
2142 let information = MockInformation {
2143 state,
2144 cost: 1,
2145 value: Cell::new(None),
2146 };
2147 let information = registry.insert(information);
2148 assert!(information.information.is_some());
2149
2150 let state = StateInRegistry {
2151 signature_variables: generate_signature_variables(vec![1, 1, 2]),
2152 resource_variables: generate_resource_variables(vec![2, 3, 4]),
2153 };
2154 let information = MockInformation {
2155 state,
2156 cost: 1,
2157 value: Cell::new(None),
2158 };
2159 let information = registry.insert(information);
2160 assert!(information.information.is_some());
2161
2162 let state = StateInRegistry {
2163 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2164 resource_variables: generate_resource_variables(vec![1, 2, 2]),
2165 };
2166 let expected1 = Rc::new(MockInformation {
2167 state,
2168 cost: 0,
2169 value: Cell::new(None),
2170 });
2171 let state = StateInRegistry {
2172 signature_variables: generate_signature_variables(vec![0, 1, 2]),
2173 resource_variables: generate_resource_variables(vec![2, 3, 4]),
2174 };
2175 let expected2 = Rc::new(MockInformation {
2176 state,
2177 cost: 1,
2178 value: Cell::new(None),
2179 });
2180
2181 let information_vec = registry.remove(&generate_signature_variables(vec![0, 1, 2]));
2182 assert_eq!(information_vec, Some(vec![expected1, expected2]));
2183 }
2184}