AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
per_state_storage.h
1#ifndef PROBFD_STORAGE_PER_STATE_STORAGE_H
2#define PROBFD_STORAGE_PER_STATE_STORAGE_H
3
4#include "probfd/utils/iterators.h"
5
6#include "probfd/types.h"
7
8#include "downward/algorithms/segmented_vector.h"
9
10#include <type_traits>
11#include <unordered_map>
12#include <unordered_set>
13#include <vector>
14
16namespace probfd::storage {
17
18template <typename T, typename Alloc>
19struct resizing_vector : public std::vector<T, Alloc> {
20 explicit resizing_vector(
21 const T& default_value = T(),
22 const Alloc& allocator = Alloc())
23 : std::vector<T, Alloc>(allocator)
24 , default_value_(default_value)
25 {
26 }
27
28 typename std::vector<T, Alloc>::reference operator[](StateID idx)
29 {
30 if (idx >= this->size()) {
31 this->resize(idx + 1, default_value_);
32 }
33 return std::vector<T, Alloc>::operator[](idx);
34 }
35
36 typename std::vector<T, Alloc>::const_reference
37 operator[](StateID idx) const
38 {
39 if (idx >= this->size()) {
40 return default_value_;
41 }
42 return std::vector<T, Alloc>::operator[](idx);
43 }
44
45private:
46 const T default_value_;
47};
48
49template <class Element, class Allocator = std::allocator<Element>>
50class PerStateStorage
51 : public segmented_vector::SegmentedVector<Element, Allocator> {
52 Element default_value_;
53
54public:
55 explicit PerStateStorage(
56 const Element& default_value = Element(),
57 const Allocator& alloc = Allocator())
58 : segmented_vector::SegmentedVector<Element>(alloc)
59 , default_value_(default_value)
60 {
61 }
62
63 Element& operator[](size_t index)
64 {
65 if (index >= this->size()) {
66 this->resize(index + 1, default_value_);
67 }
68 return segmented_vector::SegmentedVector<Element, Allocator>::
69 operator[](index);
70 }
71
72 const Element& operator[](size_t index) const
73 {
74 if (index >= this->size()) {
75 return default_value_;
76 }
77 return segmented_vector::SegmentedVector<Element, Allocator>::
78 operator[](index);
79 }
80
81 [[nodiscard]]
82 bool empty() const
83 {
84 return this->size() == 0;
85 }
86};
87
88template <
89 typename T,
90 typename Hash = std::hash<StateID>,
91 typename Equal = std::equal_to<StateID>,
92 typename Alloc = std::allocator<std::pair<const StateID, T>>>
93class StateHashMap {
94public:
95 using iterator =
96 typename std::unordered_map<StateID, T, Hash, Equal, Alloc>::iterator;
97
98 explicit StateHashMap(
99 const T& default_value = T(),
100 const Hash& h = Hash(),
101 const Equal& e = Equal(),
102 const Alloc& a = Alloc())
103 : store_(1024, h, e, a)
104 , default_value_(default_value)
105 {
106 }
107
108 T& operator[](StateID idx)
109 {
110 auto res = store_.emplace(idx, default_value_);
111 return res.first->second;
112 }
113
114 const T& operator[](StateID idx) const
115 {
116 auto it = store_.find(idx);
117 return it != store_.end() ? it->second : default_value_;
118 }
119
120 [[nodiscard]]
121 bool contains(StateID idx) const
122 {
123 return store_.find(idx) != store_.end();
124 }
125
126 [[nodiscard]]
127 StateID size() const
128 {
129 return store_.size();
130 }
131
132 [[nodiscard]]
133 bool empty() const
134 {
135 return store_.empty();
136 }
137
138 void clear() { store_.clear(); }
139
140 iterator begin() { return store_.begin(); }
141
142 iterator end() { return store_.end(); }
143
144 iterator erase(iterator it) { return store_.erase(it); }
145
146 template <class Pred>
147 typename std::unordered_map<StateID, T, Hash, Equal, Alloc>::size_type
148 erase_if(Pred pred)
149 {
150 return std::erase_if(store_, pred);
151 }
152
153private:
154 std::unordered_map<StateID, T, Hash, Equal, Alloc> store_;
155 const T default_value_;
156};
157
158template <typename Alloc>
159class PerStateStorage<bool, Alloc> : public resizing_vector<bool, Alloc> {
160public:
161 using resizing_vector<bool, Alloc>::resizing_vector;
162};
163
164template <typename State>
165using StateHashSet = std::unordered_set<State>;
166
167using StateIDHashSet = std::unordered_set<StateID>;
168
169} // namespace probfd::storage
170
171#endif
This namespace contains commonly used storage and container types.
Definition per_state_storage.h:16
A StateID represents a state within a StateIDMap. Just like Fast Downward's StateID type,...
Definition types.h:22