FreeTensor
Loading...
Searching...
No Matches
rand_cond.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_RAND_COND_H
2#define FREE_TENSOR_RAND_COND_H
3
4#include <functional>
5#include <iostream>
6#include <sstream>
7#include <string>
8#include <typeindex>
9#include <typeinfo>
10
11#include <debug.h>
12#include <hash_combine.h>
13#include <shared_linked_list.h>
14
15namespace freetensor {
16
21 public:
22 virtual ~RandCondInterface() {}
23 virtual std::type_index typeId() const = 0;
24 virtual std::string name() const = 0;
25 virtual size_t hash() const = 0;
26 virtual bool sameAs(const RandCondInterface &other) const = 0;
27 virtual std::string toString() const = 0;
28
29 friend bool operator==(const RandCondInterface &lhs,
30 const RandCondInterface &rhs) {
31 return lhs.sameAs(rhs);
32 }
33
34 friend std::ostream &operator<<(std::ostream &os,
35 const RandCondInterface &self) {
36 return os << self.toString();
37 }
38};
39
40template <class Derived> class RandCondCRTP : public RandCondInterface {
41 std::type_index typeId_;
42
43 public:
44 RandCondCRTP() : typeId_(typeid(Derived)) {}
45
46 std::type_index typeId() const override final { return typeId_; }
47
48 bool sameAs(const RandCondInterface &other) const override final {
49 if (typeId_ != other.typeId()) {
50 return false;
51 }
52 return (const Derived &)(*this) == (const Derived &)other;
53 };
54};
55
56template <class T, class Hasher = std::hash<T>,
57 class Comparator = std::equal_to<T>>
58class RandCond : public RandCondCRTP<RandCond<T, Hasher, Comparator>> {
59 std::string name_;
60 T value_;
61
62 public:
63 RandCond(const std::string &name, const T &value)
64 : name_(name), value_(value) {}
65
66 std::string name() const override { return name_; }
67
68 size_t hash() const override {
69 return hashCombine(std::hash<std::string>{}(name_), Hasher{}(value_));
70 }
71
72 friend bool operator==(const RandCond &lhs, const RandCond &rhs) {
73 return lhs.name_ == rhs.name_ && Comparator{}(lhs.value_, rhs.value_);
74 }
75
76 std::string toString() const override {
77 std::ostringstream os;
78 os << name_ << " = " << value_;
79 return os.str();
80 }
81};
82
84
88template <class T, class Hasher = std::hash<T>,
89 class Comparator = std::equal_to<T>>
91 RandCondStack &stack_;
92
93 public:
94 RandCondGuard(RandCondStack &stack, const std::string &name, const T &value)
95 : stack_(stack) {
96 stack_ = stack_.push(
97 Ref<RandCond<T, Hasher, Comparator>>::make(name, value));
98 }
99
100 ~RandCondGuard() { stack_ = stack_.pop(); }
101};
102
103} // namespace freetensor
104
105namespace std {
106
107template <> struct hash<freetensor::RandCondInterface> {
108 size_t operator()(const freetensor::RandCondInterface &obj) const {
109 return obj.hash();
110 }
111};
112
113} // namespace std
114
115#endif // FREE_TENSOR_RAND_COND_H
Definition: hash.h:13
Definition: rand_cond.h:40
RandCondCRTP()
Definition: rand_cond.h:44
std::type_index typeId() const override final
Definition: rand_cond.h:46
bool sameAs(const RandCondInterface &other) const override final
Definition: rand_cond.h:48
Definition: rand_cond.h:90
RandCondGuard(RandCondStack &stack, const std::string &name, const T &value)
Definition: rand_cond.h:94
~RandCondGuard()
Definition: rand_cond.h:100
Definition: rand_cond.h:20
virtual ~RandCondInterface()
Definition: rand_cond.h:22
virtual std::string toString() const =0
friend bool operator==(const RandCondInterface &lhs, const RandCondInterface &rhs)
Definition: rand_cond.h:29
virtual bool sameAs(const RandCondInterface &other) const =0
virtual size_t hash() const =0
friend std::ostream & operator<<(std::ostream &os, const RandCondInterface &self)
Definition: rand_cond.h:34
virtual std::string name() const =0
virtual std::type_index typeId() const =0
Definition: rand_cond.h:58
std::string name() const override
Definition: rand_cond.h:66
std::string toString() const override
Definition: rand_cond.h:76
size_t hash() const override
Definition: rand_cond.h:68
friend bool operator==(const RandCond &lhs, const RandCond &rhs)
Definition: rand_cond.h:72
RandCond(const std::string &name, const T &value)
Definition: rand_cond.h:63
Definition: ref.h:24
Definition: shared_linked_list.h:21
SharedLinkedList push(const T &data) const
Definition: shared_linked_list.h:45
SharedLinkedList pop() const
Definition: shared_linked_list.h:64
Definition: allocator.h:9
auto && lhs
Definition: const_fold.cc:70
auto auto && rhs
Definition: const_fold.cc:70
size_t hashCombine(size_t seed, size_t other)
Definition: hash_combine.cc:5
SharedLinkedList< Ref< RandCondInterface > > RandCondStack
Definition: rand_cond.h:83
STL namespace.
size_t operator()(const freetensor::RandCondInterface &obj) const
Definition: rand_cond.h:108