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  QPoint pos = QPoint(gatePosition(gateId)->first, gatePosition(gateId)->second); //position of current gate to move
148  hal::Node nd = key(QPoint(p.first, p.second)); //find the node in the destination
149 
150  if(!nd.isNull() && !swap) //if the destination placement is not available
151  log_warning("gui", "Target position is already occupied");
152  else if (!nd.isNull() && swap)
153  {
154  operator[](hal::Node(gateId,hal::Node::Gate)) = QPoint(p.first,p.second);//set the position of the first node to the destination
155  if(nd.isGate())
156  operator[](hal::Node(nd.id(), hal::Node::Gate)) = pos;//set the position of the destination node to the position of the first node
157  else operator[](hal::Node(nd.id(), hal::Node::Module)) = pos;
158  }
159  else
160  operator[](hal::Node(gateId,hal::Node::Gate)) = QPoint(p.first,p.second);
161  }
162  void setModulePosition(u32 moduleId, std::pair<int,int>p, bool swap = false){
163  QPoint pos = QPoint(modulePosition(moduleId)->first, modulePosition(moduleId)->second);
164  hal::Node nd = key(QPoint(p.first, p.second));
165 
166  if(!nd.isNull() && !swap)
167  log_warning("gui", "Target position is already occupied");
168  else if (!nd.isNull() && swap)
169  {
170  operator[](hal::Node(moduleId,hal::Node::Module)) = QPoint(p.first,p.second);
171  if(nd.isGate())
172  operator[](hal::Node(nd.id(), hal::Node::Gate)) = pos;
173  else operator[](hal::Node(nd.id(), hal::Node::Module)) = pos;
174  }
175  else
176  operator[](hal::Node(moduleId,hal::Node::Module)) = QPoint(p.first,p.second);};
177  std::pair<int,int>* gatePosition(u32 gateId) const
178  {
179  auto it = constFind(hal::Node(gateId,hal::Node::Gate));
180  return (it == constEnd() ? nullptr : new std::pair<int,int>(it->x(),it->y()));
181  }
182  std::pair<int,int>* modulePosition(u32 moduleId) const
183  {
184  auto it = constFind(hal::Node(moduleId,hal::Node::Module));
185  return (it == constEnd() ? nullptr : new std::pair(it->x(),it->y()));
186  }
187  };
188 
196  {
197  public:
203 
210  : mMode(mod), mPreferredOrigin(orign) {;}
211 
216  PlacementHint(const GridPlacement& gridPlc)
217  : mMode(GridPosition), mPreferredOrigin(Node()), mGridPos(gridPlc) {;}
218 
223  PlacementModeType mode() const { return mMode; }
224 
231  Node preferredOrigin() const { return mPreferredOrigin; }
232 
238  bool operator<(const PlacementHint& rhs) const
239  {
240  if (mMode < rhs.mMode)
241  return true;
242  if (mMode > rhs.mMode)
243  return false;
244  return mPreferredOrigin < rhs.mPreferredOrigin;
245  }
246 
252  bool operator==(const PlacementHint& rhs) const
253  {
254  return mMode == rhs.mMode && mPreferredOrigin == rhs.mPreferredOrigin;
255  }
256 
263  void addGridPosition(const Node& nd, const QPoint& p)
264  {
265  mGridPos.insert(nd,p);
266  }
267 
273  const QHash<Node,QPoint>& gridPosition() const { return mGridPos; }
274  private:
275  PlacementModeType mMode;
276  Node mPreferredOrigin;
277  GridPlacement mGridPos;
278  };
279 
284  public:
295  PlacementEntry(const PlacementHint& plc, const QSet<u32>& mods, const QSet<u32>& gats)
296  : mPlacementHint(plc), mModules(mods), mGates(gats) {;}
297  };
298 
299 }
300 // must stand globally at the end
Definition: gate.h:58
std::pair< int, int > * gatePosition(u32 gateId) const
Definition: gui_def.h:177
std::pair< int, int > * modulePosition(u32 moduleId) const
Definition: gui_def.h:182
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:162
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:283
PlacementHint mPlacementHint
Definition: gui_def.h:285
QSet< u32 > mGates
Definition: gui_def.h:287
PlacementEntry(const PlacementHint &plc, const QSet< u32 > &mods, const QSet< u32 > &gats)
Definition: gui_def.h:295
QSet< u32 > mModules
Definition: gui_def.h:286
The PlacementHint class object provides hints for the layouter how new box objects are placed on a vi...
Definition: gui_def.h:196
bool operator<(const PlacementHint &rhs) const
operator < provide an order structure for placement hints
Definition: gui_def.h:238
void addGridPosition(const Node &nd, const QPoint &p)
Definition: gui_def.h:263
bool operator==(const PlacementHint &rhs) const
operator == test whether two placement hints are equal
Definition: gui_def.h:252
Node preferredOrigin() const
preferredOrigin getter for placement origin if any.
Definition: gui_def.h:231
PlacementHint(const GridPlacement &gridPlc)
PlacementHint constructor for grid placement.
Definition: gui_def.h:216
PlacementHint(PlacementModeType mod=Standard, const Node &orign=Node())
PlacementHint standard constructor.
Definition: gui_def.h:209
PlacementModeType mode() const
mode getter for placement mode type
Definition: gui_def.h:223
PlacementModeType
The PlacementModeType enum either most compact arrangement (Standard) or to the left or right of give...
Definition: gui_def.h:202
const QHash< Node, QPoint > & gridPosition() const
Definition: gui_def.h:273
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)