Verilog to Routing - VPR
netlist_utils.h
Go to the documentation of this file.
1 #ifndef NETLIST_UTILS_H
2 #define NETLIST_UTILS_H
3 
4 #include "vtr_vector_map.h"
5 #include <set>
6 
7 /*
8  *
9  * Templated utility functions for cleaning and reordering IdMaps
10  *
11  */
12 
17 template<typename T>
18 bool are_contiguous(vtr::vector_map<T, T>& values) {
19  size_t i = 0;
20  for (T val : values) {
21  if (val != T(i)) {
22  return false;
23  }
24  ++i;
25  }
26  return true;
27 }
28 
30 template<typename Container>
31 bool all_valid(const Container& values) {
32  for (auto val : values) {
33  if (!val) {
34  return false;
35  }
36  }
37  return true;
38 }
39 
41 template<typename Id>
42 vtr::vector_map<Id, Id> compress_ids(const vtr::vector_map<Id, Id>& ids) {
43  vtr::vector_map<Id, Id> id_map(ids.size());
44  size_t i = 0;
45  for (auto id : ids) {
46  if (id) {
47  //Valid
48  id_map.insert(id, Id(i));
49  ++i;
50  }
51  }
52 
53  return id_map;
54 }
55 
66 template<typename Id, typename T>
67 vtr::vector_map<Id, T> clean_and_reorder_values(const vtr::vector_map<Id, T>& values, const vtr::vector_map<Id, Id>& id_map) {
68  VTR_ASSERT(values.size() == id_map.size());
69 
70  //Allocate space for the values that will not be dropped
71  vtr::vector_map<Id, T> result;
72 
73  //Move over the valid entries to their new locations
74  for (size_t cur_idx = 0; cur_idx < values.size(); ++cur_idx) {
75  Id old_id = Id(cur_idx);
76 
77  Id new_id = id_map[old_id];
78  if (new_id) {
79  //There is a valid mapping
80  result.insert(new_id, std::move(values[old_id]));
81  }
82  }
83 
84  return result;
85 }
86 
92 template<typename Id>
93 vtr::vector_map<Id, Id> clean_and_reorder_ids(const vtr::vector_map<Id, Id>& id_map) {
94  //For IDs, the values are the new id's stored in the map
95 
96  //Allocate a new vector to store the values that have been not dropped
97  vtr::vector_map<Id, Id> result;
98 
99  //Move over the valid entries to their new locations
100  for (size_t cur_idx = 0; cur_idx < id_map.size(); ++cur_idx) {
101  Id old_id = Id(cur_idx);
102 
103  Id new_id = id_map[old_id];
104  if (new_id) {
105  result.insert(new_id, new_id);
106  }
107  }
108 
109  return result;
110 }
111 
116 template<typename R, typename Id>
117 size_t count_valid_refs(R range, const vtr::vector_map<Id, Id>& id_map) {
118  size_t valid_count = 0;
119 
120  for (Id old_id : range) {
121  if (id_map[old_id]) {
122  ++valid_count;
123  }
124  }
125 
126  return valid_count;
127 }
128 
133 template<typename Container, typename ValId>
134 Container update_all_refs(const Container& values, const vtr::vector_map<ValId, ValId>& id_map) {
135  Container updated;
136 
137  for (ValId orig_val : values) {
138  //The original item was valid
139  ValId new_val = id_map[orig_val];
140  //The original item exists in the new mapping
141  updated.emplace_back(new_val);
142  }
143 
144  return updated;
145 }
146 
147 template<typename Container, typename ValId>
148 Container update_valid_refs(const Container& values,
149  const vtr::vector_map<ValId, ValId>& id_map,
150  const std::set<size_t>& preserved_indices = {}) {
151  Container updated;
152 
153  size_t idx = 0;
154  for (ValId orig_val : values) {
155  if (preserved_indices.count(idx)) {
156  //Preserve the original value
157  if (!orig_val) {
158  updated.emplace_back(orig_val);
159  } else {
160  ValId new_val = id_map[orig_val];
161  if (new_val) {
162  //The original item exists in the new mapping
163  updated.emplace_back(new_val);
164  }
165  }
166  } else if (orig_val) {
167  //Original item valid
168 
169  ValId new_val = id_map[orig_val];
170  if (new_val) {
171  //The original item exists in the new mapping
172  updated.emplace_back(new_val);
173  }
174  }
175  ++idx;
176  }
177  return updated;
178 }
179 
180 #endif
vtr::vector_map< Id, Id > compress_ids(const vtr::vector_map< Id, Id > &ids)
Builds a mapping from old to new ids by skipping values marked invalid.
Definition: netlist_utils.h:42
size_t count_valid_refs(R range, const vtr::vector_map< Id, Id > &id_map)
Count how many of the Id&#39;s referenced in &#39;range&#39; have a valid new mapping in &#39;id_map&#39;.
Definition: netlist_utils.h:117
bool all_valid(const Container &values)
Returns true if all elements in the vector &#39;values&#39; evaluate true.
Definition: netlist_utils.h:31
bool are_contiguous(vtr::vector_map< T, T > &values)
Returns true if all elements are contiguously ascending values (i.e. equal to their index) ...
Definition: netlist_utils.h:18
Container update_valid_refs(const Container &values, const vtr::vector_map< ValId, ValId > &id_map, const std::set< size_t > &preserved_indices={})
Definition: netlist_utils.h:148
Container update_all_refs(const Container &values, const vtr::vector_map< ValId, ValId > &id_map)
Updates the Ids in &#39;values&#39; based on id_map, even if the original or new mapping is not valid...
Definition: netlist_utils.h:134
vtr::vector_map< Id, Id > clean_and_reorder_ids(const vtr::vector_map< Id, Id > &id_map)
Returns the set of new valid Ids defined by &#39;id_map&#39;.
Definition: netlist_utils.h:93
vtr::vector_map< Id, T > clean_and_reorder_values(const vtr::vector_map< Id, T > &values, const vtr::vector_map< Id, Id > &id_map)
Returns a vector based on &#39;values&#39;, which has had entries dropped & re-ordered according according to &#39;id...
Definition: netlist_utils.h:67