SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Union.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include "filter/IFilter.h"
9 #include <algorithm>
10 
11 namespace ai {
12 
16 class Union: public IFilter {
17 public:
18  FILTER_ACTION_CLASS(Union)
19  FILTER_ACTION_FACTORY(Union)
20 
21  void filter (const AIPtr& entity) override;
22 };
23 
24 inline void Union::filter (const AIPtr& entity) {
25  FilteredEntities& filtered = getFilteredEntities(entity);
26  // create a copy
27  const FilteredEntities alreadyFiltered = filtered;
28  // now clear the entity list
29  filtered.clear();
30 
31  std::vector<FilteredEntities> filteredArray(_filters.size());
32  int n = 0;
33  size_t max = 0u;
34  for (auto& f : _filters) {
35  f->filter(entity);
36  filteredArray[n++] = filtered;
37  max = std::max(filtered.size(), max);
38  // safe and clear
39  filtered.clear();
40  }
41 
42  for (size_t i = 0; i < filteredArray.size(); ++i) {
43  std::sort(filteredArray[i].begin(), filteredArray[i].end());
44  }
45 
46  FilteredEntities result(max);
47  std::set_union(
48  filteredArray[0].begin(), filteredArray[0].end(),
49  filteredArray[1].begin(), filteredArray[1].end(),
50  std::back_inserter(result));
51 
52  if (filteredArray.size() >= 2) {
53  FilteredEntities buffer(max);
54  for (size_t i = 2; i < filteredArray.size(); ++i) {
55  buffer.clear();
56  std::sort(result.begin(), result.end());
57  std::set_union(
58  result.begin(), result.end(),
59  filteredArray[i].begin(), filteredArray[i].end(),
60  std::back_inserter(buffer));
61  std::swap(result, buffer);
62  }
63  }
64 
65  filtered.reserve(alreadyFiltered.size() + max);
66  for (auto& e : alreadyFiltered) {
67  filtered.push_back(e);
68  }
69  for (auto& e : result) {
70  filtered.push_back(e);
71  }
72 }
73 
74 }
This filter merges several other filter results.
Definition: Union.h:16