SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Difference.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 
17 class Difference: public IFilter {
18 public:
19  FILTER_ACTION_CLASS(Difference)
20  FILTER_ACTION_FACTORY(Difference)
21 
22  void filter (const AIPtr& entity) override;
23 };
24 
25 inline void Difference::filter (const AIPtr& entity) {
26  FilteredEntities& filtered = getFilteredEntities(entity);
27  // create a copy
28  const FilteredEntities alreadyFiltered = filtered;
29  // now clear the entity list
30  filtered.clear();
31 
32  std::vector<FilteredEntities> filteredArray(_filters.size());
33  int n = 0;
34  size_t max = 0u;
35  for (auto& f : _filters) {
36  f->filter(entity);
37  filteredArray[n++] = filtered;
38  max = std::max(filtered.size(), max);
39  // safe and clear
40  filtered.clear();
41  }
42 
43  for (size_t i = 0; i < filteredArray.size(); ++i) {
44  std::sort(filteredArray[i].begin(), filteredArray[i].end());
45  }
46 
47  FilteredEntities result(max);
48  std::set_difference(
49  filteredArray[0].begin(), filteredArray[0].end(),
50  filteredArray[1].begin(), filteredArray[1].end(),
51  std::back_inserter(result));
52 
53  if (filteredArray.size() >= 2) {
54  FilteredEntities buffer(max);
55  for (size_t i = 2; i < filteredArray.size(); ++i) {
56  buffer.clear();
57  std::sort(result.begin(), result.end());
58  std::set_difference(
59  result.begin(), result.end(),
60  filteredArray[i].begin(), filteredArray[i].end(),
61  std::back_inserter(buffer));
62  std::swap(result, buffer);
63  }
64  }
65 
66  filtered.reserve(alreadyFiltered.size() + max);
67  for (auto& e : alreadyFiltered) {
68  filtered.push_back(e);
69  }
70  for (auto& e : result) {
71  filtered.push_back(e);
72  }
73 }
74 
75 }
This filter performs a difference operation between several filter results. The result consists of el...
Definition: Difference.h:17