@@ -26,44 +26,49 @@ class engine;
2626using primitive_id = std::string;
2727using memory_ptr = std::shared_ptr<memory>;
2828
29- template <typename Key, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key> >
29+ template <typename Key>
3030class memory_restricter {
3131 private:
32- const std::unordered_set <Key, Hash, KeyEqual >* set1; // Const reference to immutable set
33- std::unordered_set <Key, Hash, KeyEqual > set2; // Internal mutable set
32+ const std::set <Key>* set1; // Const reference to immutable set
33+ std::set <Key> set2; // Internal mutable set
3434
3535 public:
3636 memory_restricter () : set1(nullptr ) {};
3737
3838 // Constructor to initialize with a const reference for set1
39- explicit memory_restricter (const std::unordered_set <Key, Hash, KeyEqual >* externalSet)
39+ explicit memory_restricter (const std::set <Key>* externalSet)
4040 : set1(externalSet) {}
4141
4242 // Insert into set2 (set1 is read-only)
4343 void insert (const Key& key) {
44- if (set1->find (key) == set1->end ())
44+ if (!set1 || set1->find (key) == set1->end ())
4545 set2.insert (key);
4646 }
4747
4848 // Check existence in either set
4949 bool contains (const Key& key) const {
50- return set1->find (key) != set1->end () || set2.find (key) != set2.end ();
50+ const bool in_set1 = set1 ? (set1->find (key) != set1->end ()) : false ;
51+ return in_set1 || set2.find (key) != set2.end ();
5152 }
5253
5354 // Total size of both sets
5455 size_t size () const {
55- return set1->size () + set2.size ();
56+ return ( set1 ? set1 ->size () : 0 ) + set2.size ();
5657 }
5758
5859 // Check if both sets are empty
5960 bool empty () const {
60- return set1->empty () && set2.empty ();
61+ return (! set1 || set1 ->empty () ) && set2.empty ();
6162 }
6263
6364 // Iterate over both sets
6465 void for_each (void (*func)(const Key&)) const {
65- for (const auto & key : set1) func (key);
66- for (const auto & key : set2) func (key);
66+ if (set1) {
67+ for (const auto & key : *set1)
68+ func (key);
69+ }
70+ for (const auto & key : set2)
71+ func (key);
6772 }
6873}; // end of memory_restricter
6974
0 commit comments