HAL
gui_def.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
29 #include "hal_core/utilities/log.h"
30 #include <QDebug>
31 #include <QPoint>
32 #include <QHash>
33 #include <QSet>
34 #include <QMetaType>
35 
36 namespace hal
37 {
44  enum class ItemType
45  {
46  None, Module, Gate, Net
47  };
48 
60  class Node
61  {
62  public:
64 
65  Node(u32 i=0, NodeType t=None) : mId(i), mType(t) {;}
66 
71  NodeType type() const { return mType; }
72 
77  u32 id() const { return mId; }
78 
83  bool isNull() const { return mType == None; }
84 
89  bool isGate() const { return mType == Gate; }
90 
95  bool isModule() const { return mType == Module; }
96 
102  bool operator<(const Node& rhs) const
103  {
104  if (mType < rhs.mType)
105  return true;
106  if (mType > rhs.mType)
107  return false;
108  return mId < rhs.mId;
109  }
110 
116  bool operator==(const Node& rhs) const
117  {
118  return mType == rhs.mType && mId == rhs.mId;
119  }
120 
126  bool operator!=(const Node& rhs) const
127  {
128  return !(*this == rhs);
129  }
130 
131  private:
132 
133  u32 mId;
134  NodeType mType;
135  };
136 
137 
138  uint qHash(const Node &n);
139 
140  class GridPlacement : public QHash<Node,QPoint>
141  {
142  public:
145 
146  void setGatePosition(u32 gateId, std::pair<int,int>p, bool swap = false) {
147  std::pair<int,int>* posPtr = gatePosition(gateId);
148  if (!posPtr)
149  {
150  log_warning("gui", "Gate id {} cannot be moved, not found in current placement", gateId);
151  return;
152  }
153  QPoint pos = QPoint(posPtr->first, posPtr->second); //position of current gate to move
154  hal::Node nd = key(QPoint(p.first, p.second)); //find the node in the destination
155 
156  if(!nd.isNull() && !swap) //if the destination placement is not available
157  log_warning("gui", "Target position is already occupied");
158  else if (!nd.isNull() && swap)
159  {
160  operator[](hal::Node(gateId,hal::Node::Gate)) = QPoint(p.first,p.second);//set the position of the first node to the destination
161  if(nd.isGate())
162  operator[](hal::Node(nd.id(), hal::Node::Gate)) = pos;//set the position of the destination node to the position of the first node
163  else operator[](hal::Node(nd.id(), hal::Node::Module)) = pos;
164  }
165  else
166  operator[](hal::Node(gateId,hal::Node::Gate)) = QPoint(p.first,p.second);
167  }
168 
169  void setModulePosition(u32 moduleId, std::pair<int,int>p, bool swap = false){
170  std::pair<int,int>* posPtr = modulePosition(moduleId);
171  if (!posPtr)
172  {
173  log_warning("gui", "Module id {} cannot be moved, not found in current placement", moduleId);
174  return;
175  }
176  QPoint pos = QPoint(posPtr->first, posPtr->second); //position of current module to move
177  hal::Node nd = key(QPoint(p.first, p.second));
178 
179  if(!nd.isNull() && !swap)
180  log_warning("gui", "Target position is already occupied");
181  else if (!nd.isNull() && swap)
182  {
183  operator[](hal::Node(moduleId,hal::Node::Module)) = QPoint(p.first,p.second);
184  if(nd.isGate())
185  operator[](hal::Node(nd.id(), hal::Node::Gate)) = pos;
186  else operator[](hal::Node(nd.id(), hal::Node::Module)) = pos;
187  }
188  else
189  operator[](hal::Node(moduleId,hal::Node::Module)) = QPoint(p.first,p.second);};
190 
191  std::pair<int,int>* gatePosition(u32 gateId) const
192  {
193  auto it = constFind(hal::Node(gateId,hal::Node::Gate));
194  return (it == constEnd() ? nullptr : new std::pair<int,int>(it->x(),it->y()));
195  }
196 
197  std::pair<int,int>* modulePosition(u32 moduleId) const
198  {
199  auto it = constFind(hal::Node(moduleId,hal::Node::Module));
200  return (it == constEnd() ? nullptr : new std::pair(it->x(),it->y()));
201  }
202  };
203 
211  {
212  public:
218 
225  : mMode(mod), mPreferredOrigin(orign) {;}
226 
231  PlacementHint(const GridPlacement& gridPlc)
232  : mMode(GridPosition), mPreferredOrigin(Node()), mGridPos(gridPlc) {;}
233 
238  PlacementModeType mode() const { return mMode; }
239 
246  Node preferredOrigin() const { return mPreferredOrigin; }
247 
253  bool operator<(const PlacementHint& rhs) const
254  {
255  if (mMode < rhs.mMode)
256  return true;
257  if (mMode > rhs.mMode)
258  return false;
259  return mPreferredOrigin < rhs.mPreferredOrigin;
260  }
261 
267  bool operator==(const PlacementHint& rhs) const
268  {
269  return mMode == rhs.mMode && mPreferredOrigin == rhs.mPreferredOrigin;
270  }
271 
278  void addGridPosition(const Node& nd, const QPoint& p)
279  {
280  mGridPos.insert(nd,p);
281  }
282 
288  const QHash<Node,QPoint>& gridPosition() const { return mGridPos; }
289  private:
290  PlacementModeType mMode;
291  Node mPreferredOrigin;
292  GridPlacement mGridPos;
293  };
294 
299  public:
310  PlacementEntry(const PlacementHint& plc, const QSet<u32>& mods, const QSet<u32>& gats)
311  : mPlacementHint(plc), mModules(mods), mGates(gats) {;}
312  };
313 
314 }
315 // must stand globally at the end
Definition: gate.h:58
std::pair< int, int > * gatePosition(u32 gateId) const
Definition: gui_def.h:191
std::pair< int, int > * modulePosition(u32 moduleId) const
Definition: gui_def.h:197
void setGatePosition(u32 gateId, std::pair< int, int >p, bool swap=false)
Definition: gui_def.h:146
GridPlacement(const QHash< hal::Node, QPoint > &data)
Definition: gui_def.h:144
void setModulePosition(u32 moduleId, std::pair< int, int >p, bool swap=false)
Definition: gui_def.h:169
Definition: net.h:58
The Node class object represents a module or a gate.
Definition: gui_def.h:61
bool operator<(const Node &rhs) const
operator < to provide an ordering scheme for maps and ordered lists
Definition: gui_def.h:102
bool isModule() const
isModule test wheter node is a module
Definition: gui_def.h:95
NodeType type() const
type getter for type information
Definition: gui_def.h:71
bool isGate() const
isGate test whether node is a gate
Definition: gui_def.h:89
@ Module
Definition: gui_def.h:63
@ Gate
Definition: gui_def.h:63
@ None
Definition: gui_def.h:63
bool operator==(const Node &rhs) const
operator == to test whether two nodes are equal
Definition: gui_def.h:116
Node(u32 i=0, NodeType t=None)
Definition: gui_def.h:65
bool isNull() const
isNull test for null-Node object typically returned from functions
Definition: gui_def.h:83
u32 id() const
id getter for ID information
Definition: gui_def.h:77
bool operator!=(const Node &rhs) const
operator != to test whether two nodes are not equal
Definition: gui_def.h:126
Container class to store a PlacementHint togerther with a set of modules and gates.
Definition: gui_def.h:298
PlacementHint mPlacementHint
Definition: gui_def.h:300
QSet< u32 > mGates
Definition: gui_def.h:302
PlacementEntry(const PlacementHint &plc, const QSet< u32 > &mods, const QSet< u32 > &gats)
Definition: gui_def.h:310
QSet< u32 > mModules
Definition: gui_def.h:301
The PlacementHint class object provides hints for the layouter how new box objects are placed on a vi...
Definition: gui_def.h:211
bool operator<(const PlacementHint &rhs) const
operator < provide an order structure for placement hints
Definition: gui_def.h:253
void addGridPosition(const Node &nd, const QPoint &p)
Definition: gui_def.h:278
bool operator==(const PlacementHint &rhs) const
operator == test whether two placement hints are equal
Definition: gui_def.h:267
Node preferredOrigin() const
preferredOrigin getter for placement origin if any.
Definition: gui_def.h:246
PlacementHint(const GridPlacement &gridPlc)
PlacementHint constructor for grid placement.
Definition: gui_def.h:231
PlacementHint(PlacementModeType mod=Standard, const Node &orign=Node())
PlacementHint standard constructor.
Definition: gui_def.h:224
PlacementModeType mode() const
mode getter for placement mode type
Definition: gui_def.h:238
PlacementModeType
The PlacementModeType enum either most compact arrangement (Standard) or to the left or right of give...
Definition: gui_def.h:217
const QHash< Node, QPoint > & gridPosition() const
Definition: gui_def.h:288
ItemType
The ItemType enum provides the enum type to classify graphic items into Modules, Gates or Nets....
Definition: gui_def.h:45
#define log_warning(channel,...)
Definition: log.h:76
Q_DECLARE_METATYPE(hal::Node)
uint qHash(const LaneIndex &ri)
n
Definition: test.py:6
quint32 u32
QHash::const_iterator constEnd() const const
QHash::const_iterator constFind(const Key &key) const const
QHash::iterator insert(const Key &key, const T &value)
const Key key(const T &value) const const
T & operator[](const Key &key)
void swap(QHash< K, V > &other)