Verilog to Routing - VPR
metadata_storage.h
Go to the documentation of this file.
1 #ifndef _METADATA_STORAGE_H_
2 #define _METADATA_STORAGE_H_
3 
4 #include "vtr_string_interning.h"
5 
18 template<typename LookupKey>
20  public:
21  void reserve(size_t s) {
22  VTR_ASSERT(map_.empty());
23  data_.reserve(s);
24  }
25  void add_metadata(const LookupKey& lookup_key, vtr::interned_string meta_key, vtr::interned_string meta_value) {
26  // Can only add metadata prior to building the map.
27  VTR_ASSERT(map_.empty());
28  data_.push_back(std::make_tuple(lookup_key, meta_key, meta_value));
29  }
30 
31  typename vtr::flat_map<LookupKey, t_metadata_dict>::const_iterator find(const LookupKey& lookup_key) const {
32  check_for_map();
33 
34  return map_.find(lookup_key);
35  }
36 
37  void clear() {
38  data_.clear();
39  map_.clear();
40  }
41 
42  size_t size() const {
43  check_for_map();
44  return map_.size();
45  }
46 
47  typename vtr::flat_map<LookupKey, t_metadata_dict>::const_iterator begin() const {
48  check_for_map();
49  return map_.begin();
50  }
51 
52  typename vtr::flat_map<LookupKey, t_metadata_dict>::const_iterator end() const {
53  check_for_map();
54  return map_.end();
55  }
56 
57  private:
58  // Check to see if the map has been built yet, builds it if it hasn't been
59  // built.
60  void check_for_map() const {
61  if (map_.empty() && !data_.empty()) {
62  build_map();
63  }
64  }
65 
66  // Constructs the metadata lookup map from the flat data.
67  void build_map() const {
68  VTR_ASSERT(!data_.empty());
69  VTR_ASSERT(map_.empty());
70 
71  std::sort(data_.begin(), data_.end(), [](const std::tuple<LookupKey, vtr::interned_string, vtr::interned_string>& lhs, const std::tuple<LookupKey, vtr::interned_string, vtr::interned_string>& rhs) {
72  return std::get<0>(lhs) < std::get<0>(rhs);
73  });
74 
75  std::vector<typename vtr::flat_map<LookupKey, t_metadata_dict>::value_type> storage;
76  storage.push_back(std::make_pair(std::get<0>(data_[0]), t_metadata_dict()));
77  for (const auto& value : data_) {
78  if (storage.back().first != std::get<0>(value)) {
79  storage.push_back(std::make_pair(std::get<0>(value), t_metadata_dict()));
80  }
81 
82  storage.back().second.add(std::get<1>(value), std::get<2>(value));
83  }
84 
85  map_.assign_sorted(std::move(storage));
86 
87  data_.clear();
88  data_.shrink_to_fit();
89  }
90 
91  mutable std::vector<std::tuple<LookupKey, vtr::interned_string, vtr::interned_string>> data_;
92  mutable vtr::flat_map<LookupKey, t_metadata_dict> map_;
93 };
94 
95 #endif /* _METADATA_STORAGE_H_ */
vtr::flat_map< LookupKey, t_metadata_dict > map_
Definition: metadata_storage.h:92
vtr::flat_map< LookupKey, t_metadata_dict >::const_iterator end() const
Definition: metadata_storage.h:52
std::vector< std::tuple< LookupKey, vtr::interned_string, vtr::interned_string > > data_
Definition: metadata_storage.h:91
void build_map() const
Definition: metadata_storage.h:67
Definition: metadata_storage.h:19
void check_for_map() const
Definition: metadata_storage.h:60
size_t size() const
Definition: metadata_storage.h:42
void clear()
Definition: metadata_storage.h:37
void reserve(size_t s)
Definition: metadata_storage.h:21
void add_metadata(const LookupKey &lookup_key, vtr::interned_string meta_key, vtr::interned_string meta_value)
Definition: metadata_storage.h:25
vtr::flat_map< LookupKey, t_metadata_dict >::const_iterator begin() const
Definition: metadata_storage.h:47
vtr::flat_map< LookupKey, t_metadata_dict >::const_iterator find(const LookupKey &lookup_key) const
Definition: metadata_storage.h:31