isomatch
wireId.h
Go to the documentation of this file.
1 
9 #pragma once
10 #include <string>
11 #include <vector>
12 #include <unordered_set>
13 #include <exception>
14 
15 // Circular inclusion
16 class CircuitTree;
17 class CircuitGroup;
18 class WireManager;
19 class IOPin;
20 
21 class WireId {
22  public:
24  struct PinConnection {
26  pin(pin), other(other) {}
29  };
30 
31  class CircIterator {
32  typedef std::vector<CircuitTree*>::iterator CircIter;
33  typedef std::vector<PinConnection>::iterator PinIter;
34 
35  public:
36  CircIterator(const CircIter& iter, WireId* parent);
37  CircIterator(const PinIter& iter, WireId* parent) :
38  pinIter(iter), isCircIter(false), parent(parent) {}
39 
40  CircIterator(const CircIterator& oth) {
41  operator=(oth);
42  }
43  void operator=(const CircIterator& oth) {
44  isCircIter = oth.isCircIter;
45  if(isCircIter)
46  circIter = oth.circIter;
47  else
48  pinIter = oth.pinIter;
49  }
50 
51  CircIterator& operator++();
52 
53  CircuitTree* operator*() const;
54 
55  bool operator==(const CircIterator& oth) const {
56  return parent == oth.parent &&
57  isCircIter == oth.isCircIter &&
58  ((isCircIter && circIter == oth.circIter)
59  || (!isCircIter && pinIter == oth.pinIter));
60  }
61  bool operator!=(const CircIterator& oth) const {
62  return !operator==(oth);
63  }
64 
65 
66  private:
67  union {
68  CircIter circIter;
69  PinIter pinIter;
70  };
71  bool isCircIter;
72  WireId* parent;
73  };
74 
75  class NoSuchConnection : public std::exception {};
76 
84  WireId(size_t id, const std::string& name, WireManager* manager);
85 
86  ~WireId();
87 
89  bool operator==(const WireId& oth) const;
90 
92  bool operator==(WireId& oth);
93 
95  bool operator!=(const WireId& oth) const;
97  bool operator!=(WireId& oth);
98 
100  bool operator<(const WireId& oth) const;
101 
103  bool operator<(WireId& oth);
104 
108  void connect(CircuitTree* circ);
109 
111  void connect(const PinConnection& pin);
112 
115  void connect(IOPin* pin, WireId* other);
116 
118  void disconnect(CircuitTree* circ);
119 
121  void disconnect(IOPin* pin);
122 
124  const std::vector<CircuitTree*>& connectedCirc();
125 
127  const std::vector<PinConnection>& connectedPins();
128 
130  size_t connectedCount() {
131  return connectedCirc().size() + connectedPins().size();
132  }
133 
143 
147 
151  std::vector<CircuitTree*> connected();
152 
154  const std::string& name() { return inner()->name; }
155 
157  const std::string& name() const { return inner()->name; }
158 
160  std::string uniqueName();
161 
162  private:
163  void walkConnected(std::unordered_set<CircuitTree*>& curConnected,
164  std::unordered_set<WireId>& seenWires,
165  WireId* curWire);
166 
167  struct Inner {
168  size_t id;
169  std::string name;
170  WireManager* manager;
171  std::vector<CircuitTree*> connected;
172  std::vector<PinConnection> connectedPins;
173  };
174 
175  void merge(WireId* other);
176  void rename(const std::string& nName) { inner()->name = nName; }
177 
178  WireId* ufRoot();
179  inline Inner* inner() { return ufRoot()->end; };
180  const Inner* inner() const;
181 
182  union {
183  Inner* end;
185  };
186  bool isEndpoint;
187  unsigned short ufDepth;
188 
189  friend WireManager; // set the wire's name
190  friend struct std::hash<WireId>;
191  friend struct std::hash<WireId*>;
192  friend struct HashWirePtr;
193 };
194 
195 namespace std {
196  template<> struct hash<WireId> {
198  typedef std::size_t result_type;
199  result_type operator()(const argument_type& wire) const {
200  return wire.inner()->id;
201  }
202  };
203 
204  template<> struct hash<WireId*> {
206  typedef std::size_t result_type;
207  result_type operator()(const argument_type& wire) const {
208  return wire->inner()->id;
209  }
210  };
211 
212  template<> struct equal_to<WireId*> {
213  bool operator()(const WireId* lhs, const WireId* rhs) const
214  {
215  return *lhs == *rhs;
216  }
217  };
218 }
bool operator==(const CircIterator &oth) const
Definition: wireId.h:55
std::size_t result_type
Definition: wireId.h:206
CircIterator adjacent_begin()
Definition: wireId.cpp:119
CircIterator(const CircIterator &oth)
Definition: wireId.h:40
bool operator()(const WireId *lhs, const WireId *rhs) const
Definition: wireId.h:213
bool operator!=(const CircIterator &oth) const
Definition: wireId.h:61
void disconnect(CircuitTree *circ)
Disconnect the given CircuitTree
Definition: wireId.cpp:94
CircIter circIter
Definition: wireId.h:68
const std::string & name() const
Definition: wireId.h:157
result_type operator()(const argument_type &wire) const
Definition: wireId.h:207
bool operator!=(const WireId &oth) const
Id-based equality.
Definition: wireId.cpp:66
Definition: wireId.h:195
const std::vector< CircuitTree * > & connectedCirc()
Definition: wireId.cpp:111
result_type operator()(const argument_type &wire) const
Definition: wireId.h:199
WireId argument_type
Definition: wireId.h:197
void operator=(const CircIterator &oth)
Definition: wireId.h:43
WireId * other
Definition: wireId.h:28
Definition: wireId.h:21
WireId * chain
Definition: wireId.h:184
std::vector< CircuitTree * > connected()
Definition: wireId.cpp:127
const std::string & name()
Definition: wireId.h:154
Definition: wireManager.h:17
void connect(CircuitTree *circ)
Definition: wireId.cpp:82
Definition: wireId.h:24
Definition: circuitGroup.h:47
std::size_t result_type
Definition: wireId.h:198
friend struct HashWirePtr
Definition: wireId.h:192
PinConnection(IOPin *pin, WireId *other)
Definition: wireId.h:25
IOPin * pin
Definition: wireId.h:27
size_t connectedCount()
Definition: wireId.h:130
WireId * argument_type
Definition: wireId.h:205
Definition: circuitGroup.h:14
Inner * end
Definition: wireId.h:183
bool operator<(const WireId &oth) const
Definition: wireId.cpp:76
const std::vector< PinConnection > & connectedPins()
Definition: wireId.cpp:115
CircIterator(const PinIter &iter, WireId *parent)
Definition: wireId.h:37
Definition: wireId.h:75
std::string uniqueName()
Definition: wireId.cpp:138
CircIterator adjacent_end()
Definition: wireId.cpp:123
bool operator==(const WireId &oth) const
Definition: wireId.cpp:58
WireId(size_t id, const std::string &name, WireManager *manager)
Definition: wireId.cpp:40
PinIter pinIter
Definition: wireId.h:69
Definition: wireId.h:31
Definition: circuitTree.h:10
~WireId()
Definition: wireId.cpp:48