TVM  0.9.4
MapWithVariableAsKey.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <tvm/Variable.h>
6 
8 
9 namespace tvm::utils::internal
10 {
12 struct with_sub
13 {};
14 
15 template<typename T>
16 struct slice_traits
17 {};
18 
36 template<typename T, typename Slicer = slice_traits<T>, bool tSize = false>
37 class MapWithVariableAsKey : public tvm::utils::internal::map<const Variable * const, T>
38 {
39 public:
41  using Base::at;
42 
43  typename Slicer::Type at(const typename Base::key_type & key, with_sub);
44  typename Slicer::ConstType at(const typename Base::key_type & key, with_sub) const;
45 };
46 
47 template<typename T, typename Slicer, bool tSize>
48 inline typename Slicer::Type MapWithVariableAsKey<T, Slicer, tSize>::at(const typename Base::key_type & key, with_sub)
49 {
50  auto it = Base::find(key);
51  if(it != Base::end())
52  {
53  // Key variable is directly in the map
54  return it->second;
55  }
56  else
57  {
58  // Key variable is not directly in the map, we test if it is a subvariable of one of the map variables.
59  for(auto it = Base::begin(); it != Base::end(); ++it)
60  {
61  if(it->first->contains(*key))
62  {
63  if((*it->first) == (*key))
64  {
65  return it->second;
66  }
67  else
68  {
69  Range r;
70  if constexpr(tSize)
71  r = it->first->tSubvariableRange().relativeRange(key->tSubvariableRange());
72  else
73  r = it->first->subvariableRange().relativeRange(key->subvariableRange());
74  return Slicer::get(it->second, r);
75  }
76  }
77  }
78  }
79  // We didn't find the key in the map
80  throw std::out_of_range("[MapWithVariableAsKey::at] Variable " + key->name() + " is not part of this map.");
81 }
82 
83 template<typename T, typename Slicer, bool tSize>
84 inline typename Slicer::ConstType MapWithVariableAsKey<T, Slicer, tSize>::at(const typename Base::key_type & key,
85  with_sub) const
86 {
87  auto it = Base::find(key);
88  if(it != Base::end())
89  {
90  // Key variable is directly in the map
91  return it->second;
92  }
93  else
94  {
95  // Key variable is not directly in the map, we test if it is a subvariable of one of the map variables.
96  for(auto it = Base::begin(); it != Base::end(); ++it)
97  {
98  if(it->first->contains(*key))
99  {
100  if((*it->first) == (*key))
101  {
102  return it->second;
103  }
104  else
105  {
106  Range r;
107  if constexpr(tSize)
108  r = it->first->tSubvariableRange().relativeRange(key->tSubvariableRange());
109  else
110  r = it->first->subvariableRange().relativeRange(key->subvariableRange());
111  return Slicer::get(it->second, r);
112  }
113  }
114  }
115  }
116  // We didn't find the key in the map
117  throw std::out_of_range("[MapWithVariableAsKey::at] Variable " + key->name() + " is not part of this map.");
118 }
119 
120 } // namespace tvm::utils::internal
Type
Definition: enums.h:15
Definition: AffineExprDetail.h:15
std::map< KeyWithId, Value, IdLess< KeyWithId >, Allocator > map
Definition: map.h:41