carl  24.04
Computer ARithmetic Library
Sort.h
Go to the documentation of this file.
1 /**
2  * @file Sort.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  * @author Florian Corzilius <corzilius@cs.rwth-aachen.de>
5  * @since 2014-10-30
6  * @version 2014-10-30
7  */
8 
9 #pragma once
10 
11 #include <ostream>
12 #include <type_traits>
13 #include <utility>
14 
15 namespace carl {
16 
17 /**
18  * Implements a sort (for defining types of variables and functions).
19  */
20 class Sort {
21  public:
22  friend class SortManager;
23 
24  private:
25  // Members.
26 
27  /// A unique id to identify this sort in the sort manager.
28  std::size_t mId = 0;
29 
30  /**
31  * Constructs a sort.
32  * @param _id The id of the sort to construct.
33  */
34  explicit Sort(std::size_t id): mId(id) {}
35 
36  public:
37 
38  Sort() noexcept = default;
39 
40  /**
41  * @return The aritiy of this sort.
42  */
43  std::size_t arity() const;
44 
45  /**
46  * @return The id of this sort.
47  */
48  std::size_t id() const
49  {
50  return mId;
51  }
52 
53  /**
54  * Prints the given sort on the given output stream.
55  * @param _os The output stream to print on.
56  * @param _sort The sort to print.
57  * @return The output stream after printing the given sort on it.
58  */
59  friend std::ostream& operator<<(std::ostream& _os, const Sort& _sort);
60 };
61 
62 static_assert(std::is_trivially_copyable<Sort>::value, "Sort should be trivially copyable.");
63 static_assert(sizeof(Sort) == sizeof(std::size_t), "Sort should be as large as its id type");
64 
65 /**
66 * @param lhs The left sort.
67 * @param rhs The right sort.
68 * @return true, if the sorts are the same.
69 */
70 inline bool operator==(Sort lhs, Sort rhs) {
71  return lhs.id() == rhs.id();
72 }
73 /**
74 * @param lhs The left sort.
75 * @param rhs The right sort.
76 * @return true, if the sorts are different.
77 */
78 inline bool operator!=(Sort lhs, Sort rhs) {
79  return lhs.id() != rhs.id();
80 }
81 
82 /**
83 * Checks whether one sort is smaller than another.
84 * @return true, if lhs is less than rhs.
85 */
86 inline bool operator<(Sort lhs, Sort rhs) {
87  return lhs.id() < rhs.id();
88 }
89 
90 } // end namespace carl
91 
92 namespace std {
93  /**
94  * Implements std::hash for sort.
95  */
96  template<>
97  struct hash<carl::Sort> {
98  /**
99  * @param _sort The sort to get the hash for.
100  * @return The hash of the given sort.
101  */
102  std::size_t operator()(const carl::Sort& _sort) const {
103  return _sort.id();
104  }
105  };
106 } // end namespace std
carl is the main namespace for the library.
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator!=(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
Implements a sort (for defining types of variables and functions).
Definition: Sort.h:20
Sort() noexcept=default
friend std::ostream & operator<<(std::ostream &_os, const Sort &_sort)
Prints the given sort on the given output stream.
Definition: Sort.cpp:18
std::size_t mId
A unique id to identify this sort in the sort manager.
Definition: Sort.h:28
std::size_t arity() const
Definition: Sort.cpp:14
Sort(std::size_t id)
Constructs a sort.
Definition: Sort.h:34
std::size_t id() const
Definition: Sort.h:48
std::size_t operator()(const carl::Sort &_sort) const
Definition: Sort.h:102
Implements a manager for sorts, containing the actual contents of these sort and allocating their ids...
Definition: SortManager.h:105