-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.h
160 lines (130 loc) · 3.33 KB
/
metadata.h
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
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
#ifndef METADATA_H
#define METADATA_H
#include <string>
#include <vector>
#include "token.h"
namespace amps
{
enum class metatype : uint8_t
{
TEXT,
CODE,
ECHO,
COMMENT,
};
struct metarange
{
size_t start;
size_t end;
size_t line;
};
struct metadata
{
size_t hash_tokens;
metatype type;
metarange range;
std::string data;
std::vector<token_t> tokens;
void add_token(const token_t &tk);
};
inline void metadata::add_token(const token_t &tk)
{
tokens.emplace_back(tk);
hash_tokens += tk.hash();
}
class metainfo
{
size_t hash_metadata;
std::vector<metadata> metadata_;
public:
typedef metadata value_type;
typedef std::vector<metadata>::difference_type difference_type;
metainfo() :
hash_metadata(0)
{
}
void add_metadata(const metadata &data);
size_t hash() const;
void rehash();
void push_back(const metadata &data);
metadata &back();
void remove(size_t idx);
void clear();
size_t size() const;
void resize(size_t new_size);
const metadata &operator[](size_t idx) const;
metainfo &operator=(std::vector<metadata> &&other) noexcept;
std::vector<metadata>::iterator begin() noexcept;
std::vector<metadata>::iterator end() noexcept;
std::vector<metadata>::const_iterator begin() const noexcept;
std::vector<metadata>::const_iterator end() const noexcept;
};
inline void metainfo::rehash()
{
for (const auto &tk : metadata_) {
hash_metadata += tk.hash_tokens;
}
}
inline void metainfo::remove(size_t idx)
{
if (idx > metadata_.size()) {
return;
}
metadata_.erase(metadata_.begin() + idx);
}
inline size_t metainfo::hash() const
{
return hash_metadata;
}
inline void metainfo::push_back(const metadata &data)
{
add_metadata(data);
}
inline metainfo &metainfo::operator=(std::vector<metadata> &&other) noexcept
{
metadata_.operator=(other);
return *this;
}
inline void metainfo::resize(size_t new_size)
{
metadata_.resize(new_size);
}
inline std::vector<metadata>::iterator metainfo::begin() noexcept
{
return metadata_.begin();
}
inline std::vector<metadata>::iterator metainfo::end() noexcept
{
return metadata_.end();
}
inline std::vector<metadata>::const_iterator metainfo::begin() const noexcept
{
return metadata_.begin();
}
inline std::vector<metadata>::const_iterator metainfo::end() const noexcept
{
return metadata_.end();
}
inline const metadata &metainfo::operator[](size_t idx) const
{
return metadata_[idx];
}
inline size_t metainfo::size() const
{
return metadata_.size();
}
inline metadata &metainfo::back()
{
return metadata_.back();
}
inline void metainfo::clear()
{
metadata_.clear();
}
inline void metainfo::add_metadata(const metadata &data)
{
metadata_.emplace_back(data);
hash_metadata += data.hash_tokens;
}
}
#endif // METADATA_H