Repository navigation
Expand file tree
/
Copy pathutils.h
More file actions
114 lines (95 loc) · 2.33 KB
/
Copy pathutils.h
File metadata and controls
114 lines (95 loc) · 2.33 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef UTILS_CF_H
#define UTILS_CF_H
#include <vector>
#include <map>
#include <stdexcept>
#include <iostream>
#include "parser.h"
#include "lexer.h"
using std::cout;
using std::endl;
template <typename K, typename V>
class OrderedMap
{
private:
std::vector<std::pair<K, V>> cpp_map;
public:
OrderedMap() {}
void initialize(std::map<std::string, std::vector<std::string>> s);
std::pair<K, V>& operator[](size_t i);
V& value(K key);
size_t size();
};
template <typename K, typename V>
inline void OrderedMap<K, V>::initialize(std::map<std::string, std::vector<std::string>> s)
{
for (auto i = s.begin(); i != s.end(); ++i) {
std::string first = i->first;
if (isinteger(i->first.back())) {
first.erase(first.begin() + first.size() - 1);
}
cpp_map.push_back(std::make_pair(first, i->second));
}
}
template <typename K, typename V>
inline std::pair<K, V>& OrderedMap<K, V>::operator[](size_t i)
{
if (i < 0 || i >= size()) throw std::invalid_argument("Index out of range.");
return cpp_map[i];
}
template <typename K, typename V>
inline V& OrderedMap<K, V>::value(K key)
{
for (auto& i : cpp_map) {
if (i->first == key) return i->second;
}
throw std::invalid_argument("Key not defined.");
}
template <typename K, typename V>
inline size_t OrderedMap<K, V>::size()
{
return cpp_map.size();
}
template <typename T>
class Stack {
private:
std::vector<T> cpp_vect;
public:
Stack() {};
Stack(std::vector<T>& inp);
void push(T obj);
T pull();
T& access();
bool is_empty();
size_t size();
std::vector<T>& cvect();
void print();
};
template <typename T>
inline Stack<T>::Stack(std::vector<T>& inp) { cpp_vect = inp; }
template <typename T>
inline void Stack<T>::push(T obj) { cpp_vect.push_back(obj); }
template <typename T>
inline T Stack<T>::pull() {
T tor = cpp_vect.back();
cpp_vect.erase(cpp_vect.begin() + size() - 1);
return tor;
}
template <typename T>
inline T& Stack<T>::access() { return cpp_vect.back(); }
template <typename T>
inline bool Stack<T>::is_empty() { return cpp_vect.size() == 0; }
template <typename T>
inline size_t Stack<T>::size() { return cpp_vect.size(); }
template <typename T>
inline std::vector<T>& Stack<T>::cvect() { return cpp_vect; }
template<typename T>
inline void Stack<T>::print()
{
cout << "[";
for (T& i : cpp_vect) {
cout << i.value << ", ";
}
cout << "\b\b]\n";
}
#endif // UTILS_CF_H