carl  24.04
Computer ARithmetic Library
UFManager.h
Go to the documentation of this file.
1 /**
2  * @file UFManager.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 "UninterpretedFunction.h"
15 
16 #include <cassert>
17 #include <iostream>
18 #include <utility>
19 #include <vector>
20 
21 namespace carl {
22 
23 // Forward declaration.
24 class UFManager;
25 
26 /**
27  * The actual content of an uninterpreted function instance.
28  */
29 class UFContent {
30  friend class UFManager;
31 
32 private:
33  /// The uninterpreted function's name.
34  std::string mName;
35  /// The uninterpreted function's domain.
36  std::vector<Sort> mDomain;
37  /// The uninterpreted function's codomain.
39 
40 public:
41  /**
42  * Constructs the content of an uninterpreted function.
43  * @param name The name of the uninterpreted function to construct.
44  * @param domain The domain of the uninterpreted function to construct.
45  * @param codomain The codomain of the uninterpreted function to construct.
46  */
47  explicit UFContent(std::string&& name, std::vector<Sort>&& domain, Sort codomain)
48  : mName(std::move(name)),
49  mDomain(std::move(domain)),
51  UFContent() = delete;
52  UFContent(const UFContent&) = delete;
53  UFContent(UFContent&&) = delete;
54 
55  /**
56  * @return The name of the uninterpreted function.
57  */
58  const std::string& name() const {
59  return mName;
60  }
61 
62  /**
63  * @return The domain of the uninterpreted function.
64  */
65  const std::vector<Sort>& domain() const {
66  return mDomain;
67  }
68 
69  /**
70  * @return The codomain of the uninterpreted function.
71  */
72  Sort codomain() const {
73  return mCodomain;
74  }
75 };
76 
77 /**
78  * @param lhs Left UFContent.
79  * @param rhs Right UFContent.
80  * @return true, if lhs and rhs are the same.
81  */
82 inline bool operator==(const UFContent& lhs, const UFContent& rhs) {
83  return std::forward_as_tuple(lhs.codomain(), lhs.name(), lhs.domain()) == std::forward_as_tuple(rhs.codomain(), rhs.name(), rhs.domain());
84 }
85 /**
86  * @param lhs Left UFContent.
87  * @param rhs Right UFContent.
88  * @return true, if lhs is smaller than rhs.
89  */
90 inline bool operator<(const UFContent& lhs, const UFContent& rhs) {
91  return std::forward_as_tuple(lhs.codomain(), lhs.name(), lhs.domain()) < std::forward_as_tuple(rhs.codomain(), rhs.name(), rhs.domain());
92 }
93 } // end namespace carl
94 
95 namespace std {
96 /**
97  * Implements std::hash for uninterpreted function's contents.
98  */
99 template<>
100 struct hash<carl::UFContent> {
101  /**
102  * @param ufun The uninterpreted function to get the hash for.
103  * @return The hash of the given uninterpreted function.
104  */
105  std::size_t operator()(const carl::UFContent& ufun) const {
106  return carl::hash_all(ufun.name(), ufun.domain());
107  }
108 };
109 } // end namespace std
110 
111 namespace carl {
112 
113 /**
114  * Implements a manager for uninterpreted functions, containing their actual contents and allocating their ids.
115  */
116 class UFManager : public Singleton<UFManager> {
118 
119 private:
120  /// Stores all instantiated uninterpreted function's contents and maps them to their unique id.
122  /// Maps the unique ids to the instantiated uninterpreted function's content.
123  std::vector<std::unique_ptr<UFContent>> mUFs;
124 
125  /**
126  * Constructs an uninterpreted functions manager.
127  */
129  mUFs.emplace_back(nullptr); // default value
130  }
131 
132  /**
133  * Tries to add the given uninterpreted function's content to the so far stored uninterpreted function's
134  * contents. If it has already been stored, if will be deleted and the id of the already existing uninterpreted
135  * function's content will be used to create the returned uninterpreted function.
136  * @param sc The uninterpreted function's content to store.
137  * @return The uninterpreted function corresponding to the given content.
138  */
139  UninterpretedFunction newUF(std::unique_ptr<UFContent>&& sc);
140 
141  const auto& getUF(const UninterpretedFunction& uf) const {
142  assert(uf.id() != 0);
143  assert(uf.id() < mUFs.size());
144  return mUFs[uf.id()];
145  }
146 
147 public:
148  const auto& ufContents() const {
149  return mUFs;
150  }
151  const auto& ufIDMap() const {
152  return mUFIdMap;
153  }
154 
155  /**
156  * @param uf An uninterpreted function.
157  * @return The name of the uninterpreted function of the given uninterpreted function.
158  */
159  const std::string& get_name(const UninterpretedFunction& uf) const {
160  return getUF(uf)->name();
161  }
162 
163  /**
164  * @param uf An uninterpreted function.
165  * @return The domain of the uninterpreted function of the given uninterpreted function.
166  */
167  const std::vector<Sort>& getDomain(const UninterpretedFunction& uf) const {
168  return getUF(uf)->domain();
169  }
170 
171  /**
172  * @param uf An uninterpreted function.
173  * @return The codomain of the uninterpreted function of the given uninterpreted function.
174  */
176  return getUF(uf)->codomain();
177  }
178 
179  /**
180  * Gets the uninterpreted function with the given name, domain, arguments and codomain.
181  * @param name The name of the uninterpreted function of the uninterpreted function to get.
182  * @param domain The domain of the uninterpreted function of the uninterpreted function to get.
183  * @param codomain The codomain of the uninterpreted function of the uninterpreted function to get.
184  * @return The resulting uninterpreted function.
185  */
186  UninterpretedFunction newUninterpretedFunction(std::string&& name, std::vector<Sort>&& domain, Sort codomain) {
187  return newUF(std::make_unique<UFContent>(std::move(name), std::move(domain), codomain));
188  }
189 };
190 
191 /**
192  * Gets the uninterpreted function with the given name, domain, arguments and codomain.
193  * @param name The name of the uninterpreted function of the uninterpreted function to get.
194  * @param domain The domain of the uninterpreted function of the uninterpreted function to get.
195  * @param codomain The codomain of the uninterpreted function of the uninterpreted function to get.
196  * @return The resulting uninterpreted function.
197  */
198 inline UninterpretedFunction newUninterpretedFunction(std::string name, std::vector<Sort> domain, Sort codomain) {
199  return UFManager::getInstance().newUninterpretedFunction(std::move(name), std::move(domain), codomain);
200 }
201 
202 } // namespace carl
carl is the main namespace for the library.
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
UninterpretedFunction newUninterpretedFunction(std::string name, std::vector< Sort > domain, Sort codomain)
Gets the uninterpreted function with the given name, domain, arguments and codomain.
Definition: UFManager.h:198
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
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 UFManager & getInstance()
Returns the single instance of this class by reference.
Definition: Singleton.h:45
Implements a sort (for defining types of variables and functions).
Definition: Sort.h:20
The actual content of an uninterpreted function instance.
Definition: UFManager.h:29
std::string mName
The uninterpreted function's name.
Definition: UFManager.h:34
UFContent(UFContent &&)=delete
const std::vector< Sort > & domain() const
Definition: UFManager.h:65
UFContent(const UFContent &)=delete
UFContent(std::string &&name, std::vector< Sort > &&domain, Sort codomain)
Constructs the content of an uninterpreted function.
Definition: UFManager.h:47
UFContent()=delete
const std::string & name() const
Definition: UFManager.h:58
std::vector< Sort > mDomain
The uninterpreted function's domain.
Definition: UFManager.h:36
Sort codomain() const
Definition: UFManager.h:72
Sort mCodomain
The uninterpreted function's codomain.
Definition: UFManager.h:38
std::size_t operator()(const carl::UFContent &ufun) const
Definition: UFManager.h:105
Implements a manager for uninterpreted functions, containing their actual contents and allocating the...
Definition: UFManager.h:116
const auto & getUF(const UninterpretedFunction &uf) const
Definition: UFManager.h:141
UninterpretedFunction newUninterpretedFunction(std::string &&name, std::vector< Sort > &&domain, Sort codomain)
Gets the uninterpreted function with the given name, domain, arguments and codomain.
Definition: UFManager.h:186
Sort getCodomain(const UninterpretedFunction &uf) const
Definition: UFManager.h:175
const auto & ufContents() const
Definition: UFManager.h:148
FastPointerMap< UFContent, std::size_t > mUFIdMap
Stores all instantiated uninterpreted function's contents and maps them to their unique id.
Definition: UFManager.h:121
const std::string & get_name(const UninterpretedFunction &uf) const
Definition: UFManager.h:159
const auto & ufIDMap() const
Definition: UFManager.h:151
const std::vector< Sort > & getDomain(const UninterpretedFunction &uf) const
Definition: UFManager.h:167
std::vector< std::unique_ptr< UFContent > > mUFs
Maps the unique ids to the instantiated uninterpreted function's content.
Definition: UFManager.h:123
UninterpretedFunction newUF(std::unique_ptr< UFContent > &&sc)
Tries to add the given uninterpreted function's content to the so far stored uninterpreted function's...
Definition: UFManager.cpp:13
UFManager()
Constructs an uninterpreted functions manager.
Definition: UFManager.h:128
Implements an uninterpreted function.