carl  24.04
Computer ARithmetic Library
UFInstanceManager.h
Go to the documentation of this file.
1 /**
2  * @file UFInstanceManager.h
3  * @author Florian Corzilius <corzilius@cs.rwth-aachen.de>
4  * @since 2014-10-30
5  * @version 2014-10-30
6  */
7 
8 #pragma once
9 
12 #include <carl-common/util/hash.h>
13 #include "../sort/Sort.h"
14 #include "UFInstance.h"
15 #include "UTerm.h"
16 #include "UVariable.h"
17 
18 #include <cassert>
19 #include <iostream>
20 #include <set>
21 #include <unordered_map>
22 #include <utility>
23 #include <vector>
24 
25 namespace carl {
26 
27 // Forward declaration.
28 class UFInstanceManager;
29 
30 /**
31  * The actual content of an uninterpreted function instance.
32  */
34  friend class UFInstanceManager;
35 
36 private:
37  /// The underlying uninterpreted function of theinstance.
39  /// The uninterpreted function instance's arguments.
40  std::vector<UTerm> mArgs;
41 
42 public:
43  UFInstanceContent() = delete;
46 
47  /**
48  * Constructs the content of an uninterpreted function instance.
49  * @param uf The underlying function of the uninterpreted function instance to construct.
50  * @param args The arguments of the uninterpreted function instance to construct.
51  */
52  explicit UFInstanceContent(const UninterpretedFunction& uf, std::vector<UTerm>&& args)
54  mArgs(std::move(args)) {}
55 
56  /**
57  * Constructs the content of an uninterpreted function instance.
58  * @param uf The underlying function of the uninterpreted function instance to construct.
59  * @param args The arguments of the uninterpreted function instance to construct.
60  */
61  explicit UFInstanceContent(const UninterpretedFunction& uf, const std::vector<UTerm>& args)
63  mArgs(args) {}
64 
65  /**
66  * @return The underlying function of the uninterpreted function instance
67  */
70  }
71 
72  /**
73  * @return The arguments of the uninterpreted function instance.
74  */
75  const std::vector<UTerm>& args() const {
76  return mArgs;
77  }
78 
79  /**
80  * @param ufic The uninterpreted function instance's content to compare with.
81  * @return true, if this uninterpreted function instance's content is less than the given one.
82  */
83  bool operator==(const UFInstanceContent& ufic) const {
84  return (mUninterpretedFunction == ufic.uninterpretedFunction() && mArgs == ufic.args());
85  }
86 
87  /**
88  * @param ufic The uninterpreted function instance's content to compare with.
89  * @return true, if this uninterpreted function instance's content is less than the given one.
90  */
91  bool operator<(const UFInstanceContent& ufic) const {
93  return true;
94  }
96  return false;
97  }
98  if (mArgs.size() < ufic.args().size()) {
99  return true;
100  }
101  if (mArgs.size() > ufic.args().size()) {
102  return false;
103  }
104  auto argA = mArgs.begin();
105  auto argB = ufic.args().begin();
106  while (argA != mArgs.end()) {
107  assert(argB != ufic.args().end());
108  if (*argA < *argB) {
109  return true;
110  }
111  if (*argB < *argA) {
112  return false;
113  }
114  ++argA;
115  ++argB;
116  }
117  return false;
118  }
119 };
120 } // end namespace carl
121 
122 namespace std {
123 /**
124  * Implements std::hash for uninterpreted function instance's contents.
125  */
126 template<>
127 struct hash<carl::UFInstanceContent> {
128 public:
129  /**
130  * @param ufun The uninterpreted function to get the hash for.
131  * @return The hash of the given uninterpreted function.
132  */
133  std::size_t operator()(const carl::UFInstanceContent& ufun) const {
134  return carl::hash_all(ufun.uninterpretedFunction(), ufun.args());
135  }
136 };
137 } // end namespace std
138 
139 namespace carl {
140 
141 /**
142  * Implements a manager for uninterpreted function instances, containing their actual contents and allocating their ids.
143  */
144 class UFInstanceManager : public Singleton<UFInstanceManager> {
146 
147 private:
148  /// Stores all instantiated uninterpreted function instance's contents and maps them to their unique id.
150  /// Maps the unique ids to the instantiated uninterpreted function instance's content.
151  std::vector<std::unique_ptr<UFInstanceContent>> mUFInstances;
152 
153  /**
154  * Constructs an uninterpreted function instances manager.
155  */
157  : mUFInstanceIdMap(),
158  mUFInstances() {
159  mUFInstances.emplace_back(nullptr); // default value
160  }
161  ~UFInstanceManager() override {
162  mUFInstanceIdMap.clear();
163  mUFInstances.clear();
164  }
165 
166  /**
167  * Tries to add the given uninterpreted function instance's content to the so far stored uninterpreted function instance's
168  * contents. If it has already been stored, if will be deleted and the id of the already existing uninterpreted
169  * function instance's content will be used to create the returned uninterpreted function instance.
170  * @param ufic The uninterpreted function instance's content to store.
171  * @return The uninterpreted function instance corresponding to the given content.
172  */
173  UFInstance newUFInstance(std::unique_ptr<UFInstanceContent>&& ufic);
174 
175 public:
176  /**
177  * @param ufi An uninterpreted function instance.
178  * @return The underlying uninterpreted function of the uninterpreted function of the given uninterpreted function instance.
179  */
181  assert(ufi.id() != 0);
182  assert(ufi.id() < mUFInstances.size());
183  return mUFInstances[ufi.id()]->uninterpretedFunction();
184  }
185 
186  /**
187  * @param ufi An uninterpreted function instance.
188  * @return The arguments of the given uninterpreted function instance.
189  */
190  const std::vector<UTerm>& getArgs(const UFInstance& ufi) const {
191  assert(ufi.id() != 0);
192  assert(ufi.id() < mUFInstances.size());
193  return mUFInstances[ufi.id()]->args();
194  }
195 
196  /**
197  * Gets the uninterpreted function instance with the given name, domain, arguments and codomain.
198  * @param uf The underlying function of the uninterpreted function instance to get.
199  * @param args The arguments of the uninterpreted function instance to get.
200  * @return The resulting uninterpreted function instance.
201  */
202  UFInstance newUFInstance(const UninterpretedFunction& uf, std::vector<UTerm>&& args) {
203  auto result = std::make_unique<UFInstanceContent>(uf, std::move(args));
204  assert(argsCorrect(*result));
205  return newUFInstance(std::move(result));
206  }
207 
208  /**
209  * Gets the uninterpreted function instance with the given name, domain, arguments and codomain.
210  * @param uf The underlying function of the uninterpreted function instance to get.
211  * @param args The arguments of the uninterpreted function instance to get.
212  * @return The resulting uninterpreted function instance.
213  */
214  UFInstance newUFInstance(const UninterpretedFunction& uf, const std::vector<UTerm>& args) {
215  auto result = std::make_unique<UFInstanceContent>(uf, args);
216  assert(argsCorrect(*result));
217  return newUFInstance(std::move(result));
218  }
219 
220  /**
221  * @return true, if the arguments domains coincide with those of the domain.
222  */
223  static bool argsCorrect(const UFInstanceContent& ufic);
224 };
225 
226 /**
227  * Gets the uninterpreted function instance with the given name, domain, arguments and codomain.
228  * @param uf The underlying function of the uninterpreted function instance to get.
229  * @param args The arguments of the uninterpreted function instance to get.
230  * @return The resulting uninterpreted function instance.
231  */
232 inline UFInstance newUFInstance(const UninterpretedFunction& uf, std::vector<UTerm>&& args) {
233  return UFInstanceManager::getInstance().newUFInstance(uf, std::move(args));
234 }
235 
236 /**
237  * Gets the uninterpreted function instance with the given name, domain, arguments and codomain.
238  * @param uf The underlying function of the uninterpreted function instance to get.
239  * @param args The arguments of the uninterpreted function instance to get.
240  * @return The resulting uninterpreted function instance.
241  */
242 inline UFInstance newUFInstance(const UninterpretedFunction& uf, const std::vector<UTerm>& args) {
244 }
245 
246 } // end namespace carl
carl is the main namespace for the library.
UFInstance newUFInstance(const UninterpretedFunction &uf, std::vector< UTerm > &&args)
Gets the uninterpreted function instance with the given name, domain, arguments and codomain.
std::size_t hash_all(Args &&... args)
Hashes an arbitrary number of values.
Definition: hash.h:71
std::unordered_map< const T1 *, T2, pointerHash< T1 >, pointerEqual< T1 > > FastPointerMap
Base class that implements a singleton.
Definition: Singleton.h:24
static UFInstanceManager & getInstance()
Returns the single instance of this class by reference.
Definition: Singleton.h:45
Implements an uninterpreted function instance.
Definition: UFInstance.h:25
std::size_t id() const
Definition: UFInstance.h:44
The actual content of an uninterpreted function instance.
UFInstanceContent(const UninterpretedFunction &uf, const std::vector< UTerm > &args)
Constructs the content of an uninterpreted function instance.
bool operator==(const UFInstanceContent &ufic) const
bool operator<(const UFInstanceContent &ufic) const
const std::vector< UTerm > & args() const
std::vector< UTerm > mArgs
The uninterpreted function instance's arguments.
UFInstanceContent(UFInstanceContent &&)=delete
UFInstanceContent(const UFInstanceContent &)=delete
UninterpretedFunction mUninterpretedFunction
The underlying uninterpreted function of theinstance.
UFInstanceContent(const UninterpretedFunction &uf, std::vector< UTerm > &&args)
Constructs the content of an uninterpreted function instance.
const UninterpretedFunction & uninterpretedFunction() const
std::size_t operator()(const carl::UFInstanceContent &ufun) const
Implements a manager for uninterpreted function instances, containing their actual contents and alloc...
static bool argsCorrect(const UFInstanceContent &ufic)
UFInstance newUFInstance(const UninterpretedFunction &uf, const std::vector< UTerm > &args)
Gets the uninterpreted function instance with the given name, domain, arguments and codomain.
UFInstance newUFInstance(const UninterpretedFunction &uf, std::vector< UTerm > &&args)
Gets the uninterpreted function instance with the given name, domain, arguments and codomain.
UFInstance newUFInstance(std::unique_ptr< UFInstanceContent > &&ufic)
Tries to add the given uninterpreted function instance's content to the so far stored uninterpreted f...
std::vector< std::unique_ptr< UFInstanceContent > > mUFInstances
Maps the unique ids to the instantiated uninterpreted function instance's content.
FastPointerMap< UFInstanceContent, std::size_t > mUFInstanceIdMap
Stores all instantiated uninterpreted function instance's contents and maps them to their unique id.
const std::vector< UTerm > & getArgs(const UFInstance &ufi) const
const UninterpretedFunction & getUninterpretedFunction(const UFInstance &ufi) const
UFInstanceManager()
Constructs an uninterpreted function instances manager.
Implements an uninterpreted function.