GraphUtils.hpp 12,3 КБ
Newer Older
Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
1
2
3
#pragma once

#include <algorithm>
Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
4
#include <array>
Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
5
6
7
8
9
10
11
12
13
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>

/// @file GraphUtils.hpp

Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
14
namespace CG_Graph {
Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
15
16
17
18
19
20

#ifndef DotReturn
#define DotReturn \
  std::vector<std::pair<DotTypes, std::map<std::string, std::string>>>
#endif

Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
21
22
23
/// @brief VertexTypes
/// Enumeration of vertex types

Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
24
25
enum VertexTypes : uint8_t {
  input = 0,    ///  input vertex
Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
26
  output = 6,   ///  output vertex
Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
27
28
  constant = 1, /// constant vertex
  gate = 2,     /// vertex representing a logical element
Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  subGraph = 3, /// subgraph that makes up the vertex
  dataBus = 4,
  seuqential = 5
};

// CGG - CiruitGenGraph
#define CGG_FF_TYPE(S, V) S = V, n##S = NEGEDGE | V

/// @brief Types of all sequential cells being supported
enum SequentialTypes : uint8_t {
  EN = 1 << 0,
  latch = EN,
  SET = 1 << 1,
  CLR = 1 << 2,
  RST = 1 << 3,
  ASYNC = 1 << 5,
  NEGEDGE = 1 << 6,
  // DEFAULT TYPES
  CGG_FF_TYPE(ff, 1 << 4),

  // ASYNC
  CGG_FF_TYPE(affr, ASYNC | ff | RST),
  CGG_FF_TYPE(affre, ASYNC | EN | RST),

  // LATCHES
  latchr = latch | RST,
  latchc = latch | CLR,
  latchs = latch | SET,
  // COMBINED
  latchrs = latch | RST | SET,
  latchcs = latch | CLR | SET,

  // FF
  CGG_FF_TYPE(ffe, ff | EN),
  CGG_FF_TYPE(ffr, ff | RST),
  CGG_FF_TYPE(ffc, ff | CLR),
  CGG_FF_TYPE(ffs, ff | SET),
  // COMBINED
  CGG_FF_TYPE(ffre, ff | EN | RST),
  CGG_FF_TYPE(ffce, ff | EN | CLR),
  CGG_FF_TYPE(ffse, ff | EN | SET),
  CGG_FF_TYPE(ffrse, ff | EN | RST | SET),
  CGG_FF_TYPE(ffcse, ff | EN | CLR | SET)

Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
};

/// @brief Gates
/// Enumeration of types of logical elements
/// This enumeration defines the various types of logic elements that can be
/// used in digital circuits.

enum Gates : uint8_t {
  GateAnd,    /// logical element - "AND" (AND)
  GateNand,   /// logical element "AND-NOT" (NAND)
  GateOr,     /// logical element - "OR" (OR)
  GateNor,    /// logical element - "OR-NOT" (NOR)
  GateXor,    /// lofical element - "Exclusive OR" (XOR)
  GateXnor,   /// logical element - XNOR
  GateNot,    /// logical element - NOT
  GateBuf,    /// logical element - Buffer
  GateDefault /// default logical element
};

/// @brief DotTypes
/// Enumeration for DOT generation
enum DotTypes : uint8_t {
  DotGraph = 0,
  DotInput = 1,
  DotConstant = 2,
  DotOutput = 3,
  DotGate = 4,
  DotEdge = 5,
  DotSubGraph = 6,
  DotExit = 7
};

/// @todo: To add Description some fields
/// class GraphUtils
///
/// This is the detailed one. More details. Private...
/// @param d_singleton Singleton ensures that only one instance of the class
/// exists in the application
/// @param d_logicOperations This is an associative std::map container that maps
/// strings (keys) into pairs of strings and integers. It is used to store
/// logical operations and their associated parameters, such as the symbolic
/// representation of the operation and its priority
/// @param d_operationsToHierarchy This is an associative std::map container
/// that maps integers to string vectors. It is used to store a hierarchy of
/// logical operations
/// @param d_operationsToName It is used to match symbolic representations of
/// logical operations and their names.
///

namespace GraphUtils {

/// @brief getLogicOperation Gets information about a logical operation by
/// its name
/// @param i_op A string containing the name of the logical operation
/// @return std::pair<std::string, int32_t> A pair containing the name and ID
/// of the logical operation
/// @code
/// GraphUtils settingsInstance;
/// try {
/// // Get information about the logical operation "and"
/// std::pair<std::string, int32_t> operationInfo =
/// settingsInstance.getLogicOperation("and");
/// // Output information about the logical operation
/// std::cout << "Operation name: " << operationInfo.first << std::endl;
/// std::cout << "Operation ID: " << operationInfo.second << std::endl;
/// } catch (const std::out_of_range& e) {
/// // Handle an exception if the operation is not found
/// LOG(ERROR) << "Error: " << e.what() << std::endl;
/// }
/// @endcode
/// @throws std::out_of_range If the passed operation name does not exist
/// in the list of logical operations

std::pair<std::string_view, int32_t> getLogicOperation(const std::string &i_op);

/// @brief getLogicOperationsKeys Returns the keys of logical operations
/// @return std::vector<Gates> A vector containing the keys of logical
/// operations

std::vector<Gates> getLogicOperationsKeys();

/// @brief getLogicOperationsWithGates Returns logical operations along with
/// information about the presence of a single input
/// The method returns a pair of vectors: the first vector contains
/// information about whether each a logical operation has only one
/// input(true if this is the case, false otherwise),
/// and the second vector contains the keys(enumerated values) of all
/// available logical operations
/// @return std::pair<std::vector<bool>, std::vector<Gates>> A pair of
/// vectors: information about the presence of a single input and the keys
/// of logical operations
/// @code
/// // Creating an instance of the GraphUtils class or getting it from an
/// // existing object
/// std::shared_ptr<GraphUtils>        settingsInstance =
/// GraphUtils::getDefaultInstance("/path/to/settings");
/// // Get logical operations together with information about the presence
/// // of a single input
/// std::pair<std::vector<bool>, std::vector<Gates>> logicOperationsInfo =
/// settingsInstance->getLogicOperationsWithGates();
/// // Output information about each logical operation
/// for (size_t i = 0; i < logicOperationsInfo.second.size(); ++i)
/// {
/// std::string operationName =
/// settingsInstance->parseGateToString(logicOperationsInfo.second[i]);
/// bool hasOneInput = logicOperationsInfo.first[i];
/// std::cout << "Operation: " << operationName;
/// if (hasOneInput)
/// {
///   std::cout << " (Has one input)";
/// }
/// else
/// {
///   std::cout << " (Does not have one input)";
/// }
/// std::cout << std::endl;
/// }
/// @endcode

std::pair<std::vector<bool>, std::vector<Gates>> getLogicOperationsWithGates();

/// @brief fromOperationsToName Converts the operation to its name
/// @param i_op a string representing the operation
/// @return std::string Operation name
/// @code
/// // Creating an instance of the GraphUtils class or getting it from
/// an existing object
/// std::shared_ptr<GraphUtils> settingsInstance =
/// GraphUtils::getDefaultInstance("/path/to/settings");
/// Convert the operation to its name
/// std::string operationName;
/// try
/// {
/// operationName = settingsInstance->fromOperationsToName("and");
/// std::cout << "Operation name: " << operationName << std::endl;
/// } catch (const std::out_of_range& e) {
/// LOG(ERROR) << "Error: " << e.what() << std::endl;
/// }
/// @endcode
/// @throw std::out_of_range If the passed operation does not exist in the
/// list of operations

std::string fromOperationsToName(std::string_view i_op);

/// @brief fromOperationsToHierarchy Converts an operation key to its
/// corresponding hierarchy
/// @param key The key representing the operation
/// @return std::vector<std::string> The hierarchy associated with the
/// operation key
/// @code
/// // Creating an instance of the GraphUtils class or getting it from an
/// existing object std::shared_ptr<GraphUtils> settingsInstance =
/// GraphUtils::getDefaultInstance("/path/to/settings");
/// // Get the hierarchy associated with the operation key 5
/// std::vector<std::string> operationHierarchy =
/// settingsInstance->fromOperationsToHierarchy(5);
/// // Output the hierarchy
/// for(const auto& element : operationHierarchy)
/// {
///     std::cout << element << " ";
/// }
/// std::cout << std::endl;
/// @endcode
/// @throws std::out_of_range If the provided key does not exist in the
/// internal map of operation keys to hierarchies

std::string_view fromOperationsToHierarchy(int32_t key);

/// @brief parseStringToGate Converts a string representation of a gate to
/// its corresponding enum value
/// @param i_gate The string representation of the gate
/// @return Gates The enum value corresponding to the provided string
/// representation of the gate
/// @code
/// // Creating an instance of the GraphUtils class or getting it from an
/// existing object std::shared_ptr<GraphUtils> settingsInstance =
/// GraphUtils::getDefaultInstance("/path/to/settings");
/// // Convert the string representation "and" to its corresponding enum value
/// Gates gate = settingsInstance->parseStringToGate("and");
/// std::cout << "Enum value of 'and': " << gate << std::endl;
/// @endcode

Gates parseStringToGate(std::string i_gate);

/// @brief parseVertexToString Converts an enum value of a vertex type to its
/// corresponding string representation
/// @param vertex The enum value representing the vertex type
/// @return std::string The string representation of the provided vertex type
/// enum value
/// @code
/// // Creating an instance of the GraphUtils class or getting it from an
/// existing object std::shared_ptr<GraphUtils> settingsInstance =
/// GraphUtils::getDefaultInstance("/path/to/settings");
/// // Convert the enum value VertexTypes::input to its corresponding string
/// representation std::string vertexString =
/// settingsInstance->parseVertexToString(VertexTypes::input); std::cout <<
/// "String representation of VertexTypes::input: " << vertexString <<
/// std::endl;
/// @endcode

std::string parseVertexToString(VertexTypes vertex);

/// @brief parseGateToString Converts an enum value of a gate to its
/// corresponding string representation
/// @param gate The enum value representing the gate
/// @return std::string The string representation of the provided gate enum
/// value This method converts an enum value representing a gate to its
/// corresponding string representation.
/// It retrieves the string representation from the internal map date
/// ToString, which maps enum values of gates to their string
/// representations.
/// @code
/// // Creating an instance of the GraphUtils class or getting it from an
/// existing object std::shared_ptr<GraphUtils> settingsInstance =
/// GraphUtils::getDefaultInstance("/path/to/settings");
/// // Convert the enum value Gates::GateAnd to its corresponding string
/// representation std::string gateString =
/// settingsInstance->parseGateToString(Gates::GateAnd); std::cout << "String
/// representation of Gates::GateAnd: " << gateString << std::endl;
/// @endcode

std::string parseGateToString(Gates gate);

template<typename T, typename M, size_t N>
static std::pair<T, M> *findPairByKey(std::pair<T, M> (&iterable)[N],
                                      const T &key) {
  auto *iter = std::find_if(std::begin(iterable), std::end(iterable),
                            [key](const auto &x) { return x.first == key; });
  return iter;
}

static constexpr std::array<
    std::pair<std::string_view, std::pair<std::string_view, int32_t>>, 11>
    d_logicOperations = {{{"input", {"", 10}},
                          {"output", {"=", 0}},
                          {"const", {"1'b0", 9}},
                          {"and", {"and", 4}},
                          {"nand", {"nand", 3}},
                          {"or", {"or", 6}},
                          {"nor", {"nor", 5}},
                          {"not", {"not", 7}},
                          {"buf", {"buf", 8}},
                          {"xor", {"xor", 2}},
                          {"xnor", {"xnor", 1}}}};

static std::pair<std::string, Gates> stringToGate[] = {
    {"and", Gates::GateAnd}, {"nand", Gates::GateNand}, {"or", Gates::GateOr},
    {"nor", Gates::GateNor}, {"not", Gates::GateNot},   {"buf", Gates::GateBuf},
    {"xor", Gates::GateXor}, {"xnor", Gates::GateXnor}};

static std::pair<VertexTypes, std::string_view> vertexToString[] = {
    {VertexTypes::input, "input"},
    {VertexTypes::output, "output"},
    {VertexTypes::constant, "const"},
    {VertexTypes::subGraph, "subGraph"},
    {VertexTypes::gate, "gate"}};

static std::pair<Gates, std::string_view> gateToString[] = {
    {Gates::GateAnd, "and"},      {Gates::GateNand, "nand"},
    {Gates::GateOr, "or"},        {Gates::GateNor, "nor"},
    {Gates::GateNot, "not"},      {Gates::GateBuf, "buf"},
    {Gates::GateXor, "xor"},      {Gates::GateXnor, "xnor"},
    {Gates::GateDefault, "ERROR"}};

// Here is located max value + 1 from the array d_logicOperations
static constexpr size_t d_hierarchySize = 11;

} // namespace GraphUtils
Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
341

Fuuulkrum7's avatar
Fuuulkrum7 включено в состав коммита
342
} // namespace CG_Graph