HAL
remove_duplicates.cpp
Go to the documentation of this file.
2 
7 
8 #include <list>
9 #include <map>
10 
11 namespace hal
12 {
13  namespace dataflow
14  {
15  namespace remove_duplicates
16  {
17  std::shared_ptr<Grouping> process(const processing::Configuration& config, const std::shared_ptr<Grouping>& state, bool delete_from_smaller)
18  {
19  UNUSED(config);
20 
21  auto new_state = std::make_shared<Grouping>(state->netlist_abstr);
22 
23  std::map<u32, std::unordered_set<u32>> size_group_map;
24  for (const auto& [group_id, gates] : state->gates_of_group)
25  {
26  size_group_map[gates.size()].insert(group_id);
27  }
28 
29  u32 id_counter = -1;
30  std::unordered_set<u32> merged_gates;
31  if (delete_from_smaller)
32  {
33  /* iterate from smallest to largest group */
34  for (auto it = size_group_map.rbegin(); it != size_group_map.rend(); it++)
35  {
36  /* iterate through same sized groups */
37  for (const auto& group_id : it->second)
38  {
39  std::unordered_set<u32> new_group;
40 
41  for (auto gate_id : state->gates_of_group.at(group_id))
42  {
43  if (merged_gates.find(gate_id) == merged_gates.end())
44  {
45  new_group.insert(gate_id);
46  merged_gates.insert(gate_id);
47  }
48  }
49 
50  if (new_group.size() > 0)
51  {
52  u32 new_group_id = ++id_counter;
53 
54  new_state->group_control_fingerprint_map[new_group_id] = new_state->netlist_abstr.gate_to_fingerprint.at(*new_group.begin());
55  new_state->operations_on_group_allowed[new_group_id] = state->operations_on_group_allowed.at(group_id);
56  new_state->gates_of_group[new_group_id].insert(new_group.begin(), new_group.end());
57  for (const auto& sg : new_group)
58  {
59  new_state->parent_group_of_gate[sg] = new_group_id;
60  }
61  }
62  }
63  }
64  }
65  else
66  {
67  /* remove dublicates from smallest groups */
68  for (auto it = size_group_map.begin(); it != size_group_map.end(); it++)
69  {
70  /* ignore single gates */
71  if (it->first == 1)
72  {
73  continue;
74  }
75 
76  /* iterate through same sized groups */
77  for (const auto& group_id : it->second)
78  {
79  std::unordered_set<u32> new_group;
80 
81  for (auto gate_id : state->gates_of_group.at(group_id))
82  {
83  if (merged_gates.find(gate_id) == merged_gates.end())
84  {
85  new_group.insert(gate_id);
86  merged_gates.insert(gate_id);
87  }
88  }
89 
90  if (new_group.size() > 0)
91  {
92  u32 new_group_id = ++id_counter;
93 
94  new_state->group_control_fingerprint_map[new_group_id] = new_state->netlist_abstr.gate_to_fingerprint.at(*new_group.begin());
95  new_state->operations_on_group_allowed[new_group_id] = state->operations_on_group_allowed[group_id];
96 
97  new_state->gates_of_group[new_group_id].insert(new_group.begin(), new_group.end());
98  for (const auto& sg : new_group)
99  {
100  new_state->parent_group_of_gate[sg] = new_group_id;
101  }
102  }
103  }
104  }
105 
106  /* insert missing gates */
107  for (auto gate : state->netlist_abstr.target_gates)
108  {
109  u32 gate_id = gate->get_id();
110  if (merged_gates.find(gate_id) == merged_gates.end())
111  {
112  u32 new_group_id = ++id_counter;
113 
114  new_state->group_control_fingerprint_map[new_group_id] = new_state->netlist_abstr.gate_to_fingerprint.at(gate_id);
115  new_state->operations_on_group_allowed[new_group_id] = state->operations_on_group_allowed[state->parent_group_of_gate[gate_id]];
116 
117  new_state->gates_of_group[new_group_id].insert(gate_id);
118  new_state->parent_group_of_gate[gate_id] = new_group_id;
119  }
120  }
121  }
122 
123  return new_state;
124  }
125  } // namespace remove_duplicates
126  } // namespace dataflow
127 } // namespace hal
#define UNUSED(expr)
Definition: defines.h:49
std::shared_ptr< Grouping > process(const processing::Configuration &config, const std::shared_ptr< Grouping > &state, bool delete_from_smaller)
quint32 u32
This file contains the struct that holds all information on the netlist abstraction used for dataflow...
This file contains the class that holds all information of a dataflow analysis grouping.