AIToolbox
A library that offers tools for AI problem solving.
FilterMap.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_FACTORED_FILTER_MAP_HEADER_FILE
2 #define AI_TOOLBOX_FACTORED_FILTER_MAP_HEADER_FILE
3 
8 
9 namespace AIToolbox::Factored {
21  template <typename T, typename TrieType = FasterTrie>
22  class FilterMap {
23  public:
24  using ItemsContainer = std::vector<T>;
27 
36  FilterMap(Factors f) : ids_(std::move(f)) {}
37 
55  FilterMap(TrieType t, ItemsContainer c) :
56  ids_(std::move(t)), items_(std::move(c))
57  {
58  if (ids_.size() != items_.size())
59  throw std::invalid_argument("Input trie and container have different sizes!");
60  }
61 
67  Factors getF() const {
68  return ids_.getF();
69  }
70 
85  template <typename... Args>
86  void emplace(const PartialFactors & pf, Args&&... args) {
87  ids_.insert(pf);
88  items_.emplace_back(std::forward<Args>(args)...);
89  }
90 
102  Iterable filter(const Factors & f) {
103  return Iterable(ids_.filter(f), items_);
104  }
105 
117  ConstIterable filter(const Factors & f) const {
118  return ConstIterable(ids_.filter(f), items_);
119  }
120 
133  template <typename TT = TrieType, typename = std::enable_if_t<std::is_same_v<TT, Trie>>>
134  Iterable filter(const Factors & f, size_t offset) {
135  return Iterable(ids_.filter(f, offset), items_);
136  }
137 
150  template <typename TT = TrieType, typename = std::enable_if_t<std::is_same_v<TT, Trie>>>
151  ConstIterable filter(const Factors & f, size_t offset) const {
152  return ConstIterable(ids_.filter(f, offset), items_);
153  }
154 
167  return Iterable(ids_.filter(pf), items_);
168  }
169 
181  ConstIterable filter(const PartialFactors & pf) const {
182  return ConstIterable(ids_.filter(pf), items_);
183  }
184 
190  void reserve(size_t size) {
191  if constexpr (std::is_same_v<TrieType, Trie>)
192  ids_.reserve(size);
193 
194  items_.reserve(size);
195  }
196 
202  size_t size() const {
203  return items_.size();
204  }
205 
211  typename ItemsContainer::iterator begin() {
212  return items_.begin();
213  }
214 
220  typename ItemsContainer::const_iterator begin() const {
221  return items_.begin();
222  }
223 
229  typename ItemsContainer::iterator end() {
230  return items_.end();
231  }
232 
238  typename ItemsContainer::const_iterator end() const {
239  return items_.end();
240  }
241 
251  const T & operator[](size_t id) const {
252  return items_[id];
253  }
254 
264  T & operator[](size_t id) {
265  return items_[id];
266  }
267 
273  const ItemsContainer & getContainer() const {
274  return items_;
275  }
276 
285  const TrieType & getTrie() const {
286  return ids_;
287  }
288 
289  private:
290  TrieType ids_;
291  ItemsContainer items_;
292  };
293 }
294 
295 #endif
IndexMap.hpp
AIToolbox::Factored::PartialFactors
std::pair< PartialKeys, PartialValues > PartialFactors
Definition: Types.hpp:65
AIToolbox::IndexMap
This class is an iterable construct on a list of ids on a given container.
Definition: IndexMap.hpp:169
AIToolbox::Factored::FilterMap::operator[]
T & operator[](size_t id)
This function allows direct access to the items in the container.
Definition: FilterMap.hpp:264
Trie.hpp
AIToolbox::Factored::FilterMap::getContainer
const ItemsContainer & getContainer() const
This function provides a direct view on the items contained by the container.
Definition: FilterMap.hpp:273
AIToolbox::Factored::FilterMap::begin
ItemsContainer::iterator begin()
This function returns the beginning of a range containing all items added to the container.
Definition: FilterMap.hpp:211
AIToolbox::Factored::FilterMap::end
ItemsContainer::iterator end()
This function returns the end of a range containing all items added to the container.
Definition: FilterMap.hpp:229
AIToolbox::Factored::FilterMap::size
size_t size() const
This function returns the number of values that have been added to the FilterMap.
Definition: FilterMap.hpp:202
AIToolbox::Factored::FilterMap::emplace
void emplace(const PartialFactors &pf, Args &&... args)
This function creates an instance of T within the container.
Definition: FilterMap.hpp:86
AIToolbox::Factored::Factors
std::vector< size_t > Factors
Definition: Types.hpp:62
AIToolbox::Factored::FilterMap::operator[]
const T & operator[](size_t id) const
This function allows direct access to the items in the container.
Definition: FilterMap.hpp:251
AIToolbox::Factored::FilterMap::reserve
void reserve(size_t size)
This function reserves the specified space to avoid reallocations.
Definition: FilterMap.hpp:190
AIToolbox::Factored::FilterMap::getF
Factors getF() const
This function returns the set factor space for the FilterMap.
Definition: FilterMap.hpp:67
AIToolbox::Factored::FilterMap::FilterMap
FilterMap(TrieType t, ItemsContainer c)
Constructor from trie and items.
Definition: FilterMap.hpp:55
AIToolbox::Factored::FilterMap< AIToolbox::Factored::Bandit::QFunctionRule >::ItemsContainer
std::vector< AIToolbox::Factored::Bandit::QFunctionRule > ItemsContainer
Definition: FilterMap.hpp:24
AIToolbox::Factored
Definition: GraphUtils.hpp:12
Types.hpp
AIToolbox::Factored::FilterMap::FilterMap
FilterMap(Factors f)
Basic constructor.
Definition: FilterMap.hpp:36
AIToolbox::Factored::FilterMap::filter
Iterable filter(const Factors &f, size_t offset)
This function creates an iterable object over all values matching the input key.
Definition: FilterMap.hpp:134
AIToolbox::Factored::FilterMap::filter
ConstIterable filter(const Factors &f) const
This function creates an iterable object over all values matching the input key.
Definition: FilterMap.hpp:117
AIToolbox::Factored::FilterMap::begin
ItemsContainer::const_iterator begin() const
This function returns the beginning of a range containing all items added to the container.
Definition: FilterMap.hpp:220
AIToolbox::Factored::FilterMap::filter
ConstIterable filter(const Factors &f, size_t offset) const
This function creates an iterable object over all values matching the input key.
Definition: FilterMap.hpp:151
AIToolbox::Factored::FilterMap::Iterable
IndexMap< std::vector< size_t >, ItemsContainer > Iterable
Definition: FilterMap.hpp:25
AIToolbox::Factored::FilterMap
This class is a container which uses PartialFactors as keys.
Definition: FilterMap.hpp:22
AIToolbox::Factored::FilterMap::getTrie
const TrieType & getTrie() const
This function returns the underlying trie object.
Definition: FilterMap.hpp:285
FasterTrie.hpp
AIToolbox::Factored::FilterMap::filter
Iterable filter(const Factors &f)
This function creates an iterable object over all values matching the input key.
Definition: FilterMap.hpp:102
AIToolbox::Factored::FilterMap::filter
ConstIterable filter(const PartialFactors &pf) const
This function creates an iterable object over all values matching the input key.
Definition: FilterMap.hpp:181
AIToolbox::Factored::FilterMap::end
ItemsContainer::const_iterator end() const
This function returns the end of a range containing all items added to the container.
Definition: FilterMap.hpp:238
AIToolbox::Factored::FilterMap::filter
Iterable filter(const PartialFactors &pf)
This function creates an iterable object over all values matching the input key.
Definition: FilterMap.hpp:166
AIToolbox::Factored::FilterMap::ConstIterable
IndexMap< std::vector< size_t >, const ItemsContainer > ConstIterable
Definition: FilterMap.hpp:26