-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_string.cpp
More file actions
94 lines (74 loc) · 1.95 KB
/
format_string.cpp
File metadata and controls
94 lines (74 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <unordered_map>
#include <string>
#include <string_view>
#include <stack>
#include <vector>
// #define STRING std::string
const std::vector<int> ids = {0, 1, 2};
struct TreeNode
{
int arity;
STRING name;
TreeNode(int arity, STRING name) : arity(arity), name(name) {}
};
#define MAP_TREENODE std::unordered_map<int, TreeNode>
MAP_TREENODE getTreeNodeMap()
{
MAP_TREENODE nodes;
nodes.emplace(0, TreeNode{2, "prog2"});
nodes.emplace(1, TreeNode{0, "get9"});
nodes.emplace(2, TreeNode{0, "get0"});
return nodes;
}
std::string concatenate_arguments(const std::vector<std::string *> *args)
{
std::string str = "(";
for (int i = 0; i < args->size(); i++)
{
if (str.size() != 1)
{
str += ",";
}
str += *args->at(i);
}
return str + ")";
}
std::string format(const STRING &name, const std::vector<std::string *> *args)
{
return std::string(name) + concatenate_arguments(args);
}
std::string toString(const std::vector<signed char> &nodes, MAP_TREENODE *nmap)
{
std::string string;
std::stack<std::pair<int, std::vector<std::string *> *>> stack;
for (const int &node : nodes)
{
std::vector<std::string *> *v = new std::vector<std::string *>();
stack.push({node, v});
while (!stack.empty() && stack.top().second->size() == nmap->at(stack.top().first).arity)
{
auto &[prim, args] = stack.top();
stack.pop();
string = format(nmap->at(prim).name, args);
for (int i = 0; i < args->size(); i++)
{
delete args->at(i);
}
delete args;
if (stack.empty())
{
break;
}
stack.top().second->push_back(new std::string(string));
}
}
return string;
}
/*
int main()
{
for (int i = 0; i < 1000000;i++)
toString(ids);
return 0;
}*/