HAL
coordinate_from_data.cpp
Go to the documentation of this file.
2 #include "gui/gui_def.h"
3 #include "gui/gui_globals.h"
6 #include <QMap>
8 
9 namespace hal {
10 
12  : QPoint(x_,y_)
13  {;}
14 
16  {
17  Module* m;
18  Gate* g;
19  switch (nd.type()) {
20  case Node::Module:
21  m = gNetlist->get_module_by_id(nd.id());
22  Q_ASSERT(m);
23  return fromData(m->get_data_map());
24  case Node::Gate:
25  g = gNetlist->get_gate_by_id(nd.id());
26  Q_ASSERT(g);
27  if (g->has_location())
28  {
29  int lx = (int) floor(g->get_location_x()+0.5);
30  int ly = (int) floor(g->get_location_y()+0.5);
31  return CoordinateFromData(lx,ly);
32  }
33  return fromData(g->get_data_map());
34  default:
35  break;
36  }
37  return CoordinateFromData();
38  }
39 
40  CoordinateFromData CoordinateFromData::fromData(const std::map<std::tuple<std::string, std::string>,
41  std::tuple<std::string, std::string> >& dc)
42  {
43  CoordinateFromData retval;
44 
45  for(const auto& [key, value] : dc)
46  {
47  QString keyTxt = QString::fromStdString(std::get<1>(key));
48  QString valueTxt = QString::fromStdString(std::get<1>(value));
49  if (keyTxt.isEmpty()) continue;
50  QChar xy = keyTxt.at(0);
51  keyTxt.remove(0,1);
52  if (keyTxt.toUpper() != "_COORDINATE") continue;
53  bool ok = false;
54  int val = valueTxt.toInt(&ok);
55  if (!ok) continue;
56  if (xy.toUpper().unicode() == 'X')
57  retval.setX(val);
58  else if (xy.toUpper().unicode() == 'Y')
59  retval.setY(val);
60  }
61 
62  return retval;
63  }
64 
66  : mUndefCount(0), mDoubleCount(0)
67  {
68  QHash<QPoint,int> posCount;
69  for (u32 mid : modules)
70  {
73  if (cfd.isUndefined())
74  mUndefCount++;
75  else
76  {
77  insertNode(nd, cfd);
78  mPlacedModules.insert(mid);
79  }
80  }
81  for (u32 gid : gates)
82  {
83  hal::Node nd(gid, hal::Node::Gate);
85  if (cfd.isUndefined())
86  mUndefCount++;
87  else
88  {
89  insertNode(nd, cfd);
90  mPlacedGates.insert(gid);
91  }
92  }
93  }
94 
95  void CoordinateFromDataMap::insertNode(const hal::Node& nd, const CoordinateFromData& cfd)
96  {
97  insert(nd,cfd);
98  int n = mPositionHash[cfd]++;
99 
100  if (n) ++ mDoubleCount;
101  }
102 
104  {
105  mUndefCount = 0;
106  mPlacedCount = 0;
107  mDoubleCount = 0;
108  mPositionHash.clear();
109  mPlacedGates.clear();
110  mPlacedModules.clear();
111  }
112 
114  {
115  return mUndefCount == 0;
116  }
117 
119  {
120  QMap<int,int> xGrid;
121  QMap<int,int> yGrid;
122  for (const CoordinateFromData& cfd : values())
123  {
124  xGrid[cfd.x()] = 0;
125  yGrid[cfd.y()] = 0;
126  }
127 
128  int x = 0;
129  for (auto itx = xGrid.begin(); itx != xGrid.end(); ++itx)
130  *itx = x++;
131 
132  int y = 0;
133  for (auto ity = yGrid.begin(); ity != yGrid.end(); ++ity)
134  *ity = y++;
135 
136  for (auto it = begin(); it != end(); ++it)
137  {
138  int px = xGrid.value(it.value().x());
139  int py = yGrid.value(it.value().y());
140  *it = CoordinateFromData(px,py);
141  }
142  }
143 
145  {
146  if (mDoubleCount!=0)
147  log_info("gui", "multiple assignment to same gate coordinate(s)");
148 
149  return (!mPlacedGates.isEmpty() || !mPlacedModules.isEmpty())
150  && mDoubleCount == 0;
151  }
152 }
Utility class that extracts (if possible) x and y coordinates contained in the netlist.
static CoordinateFromData fromNode(const Node &nd)
CoordinateFromData(int x_=INT_MIN, int y_=INT_MIN)
CoordinateFromDataMap(const QSet< u32 > &modules, const QSet< u32 > &gates)
const std::map< std::tuple< std::string, std::string >, std::tuple< std::string, std::string > > & get_data_map() const
Definition: gate.h:58
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:193
Module * get_module_by_id(u32 module_id) const
Definition: netlist.cpp:613
The Node class object represents a module or a gate.
Definition: gui_def.h:61
NodeType type() const
type getter for type information
Definition: gui_def.h:71
@ Module
Definition: gui_def.h:63
@ Gate
Definition: gui_def.h:63
u32 id() const
id getter for ID information
Definition: gui_def.h:77
#define log_info(channel,...)
Definition: log.h:70
Netlist * gNetlist
Definition: plugin_gui.cpp:80
n
Definition: test.py:6
quint32 u32
QChar toUpper() const const
ushort unicode() const const
void clear()
QHash::iterator insert(const Key &key, const T &value)
QList< T > values() const const
QMap::iterator begin()
QMap::iterator end()
const T value(const Key &key, const T &defaultValue) const const
void setX(int x)
void setY(int y)
int x() const const
int y() const const
void clear()
QSet::iterator insert(const T &value)
bool isEmpty() const const
const QChar at(int position) const const
QString fromStdString(const std::string &str)
bool isEmpty() const const
QString & remove(int position, int n)
int toInt(bool *ok, int base) const const
QString toUpper() const const