Commit e9580b15 authored by s_kleplj's avatar s_kleplj
Browse files

did some optimizations; map changed to unordered_map

parent c98ccffa
......@@ -8,15 +8,15 @@ bool macroprocessor_impl::process(
const std::string& s,
std::string& output
) {
std::vector<std::string_view> parsed;
parsed.clear();
bool is_def = false;
for (
auto [i, j] = std::tuple{s.begin(), s.end()};;
auto [i, j] = std::tuple{s.cbegin(), s.cend()};;
++i
) {
if (j != s.end()) {
if (i == s.end()) {
if (j != s.cend()) {
if (i == s.cend()) {
parsed.emplace_back(&*j, &*i - &*j);
break;
} else if (*i == ' ' || *i == '=') {
......@@ -25,10 +25,10 @@ bool macroprocessor_impl::process(
}
parsed.emplace_back(&*j, &*i - &*j);
j = s.end();
j = s.cend();
}
} else {
if (i == s.end()) {
if (i == s.cend()) {
break;
} else if (*i == '=') {
is_def = true;
......@@ -47,13 +47,14 @@ bool macroprocessor_impl::process(
const_cast<std::string_view&>(it->first) = it->second.name_;
} else {
it->second.value_.clear();
it->second.length = 0;
}
if (parsed.size() != 1) {
std::vector<word*>* vector = &(it->second.value_);
vector->reserve(parsed.size() - 1);
for (auto i = parsed.begin() + 1; i != parsed.end(); ++i) {
for (auto i = parsed.cbegin() + 1; i != parsed.cend(); ++i) {
auto&& [it, inserted] = words_.try_emplace(
*i,
*i,
......@@ -70,31 +71,36 @@ bool macroprocessor_impl::process(
return false;
} else {
std::size_t length = 0;
found.clear();
found.reserve(parsed.size());
for (auto&& p : parsed) {
auto it = words_.find(p);
if (it != words_.end()) {
std::size_t w_length = it->second.update_length() + 1;
found.push_back(&it->second);
if (w_length > 0) {
length += w_length + 1;
}
} else {
found.push_back(nullptr);
length += p.size() + 1;
}
}
output.reserve(length);
for (auto&& p : parsed) {
auto it = words_.find(p);
if (it != words_.end()) {
if (it->second.length > 0) {
it->second.append_to(output);
for (std::size_t i = 0; i < parsed.size(); ++i) {
if (found[i] != nullptr) {
if (found[i]->length > 0) {
found[i]->append_to(output);
output += ' ';
}
} else {
output += p;
output += parsed[i];
output += ' ';
}
}
......@@ -105,8 +111,6 @@ bool macroprocessor_impl::process(
return true;
}
}
template class macroprocessor< policy_scalar>;
......
......@@ -7,13 +7,11 @@
#include <iomanip>
#include <array>
#include <memory>
#include <map>
#include <unordered_map>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
#include <type_traits>
namespace macrosol {
......@@ -40,15 +38,15 @@ class macroprocessor_impl {
private:
struct word {
word (std::string_view name) :
name_{std::string{name}},
value_{},
length{}
name_{std::string{name}},
length{0}
{}
word (std::string_view name, bool) :
value_{},
name_{std::string{name}},
value_{this},
length{}
length{name_.size()}
{}
void append_to(std::string& output) const {
......@@ -56,7 +54,7 @@ class macroprocessor_impl {
return;
}
if (value_.size() == 1 && value_[0] == this) {
if (value_.size() == 0 && length > 0) {
output += name_;
} else {
for (word* w : value_) {
......@@ -71,9 +69,7 @@ class macroprocessor_impl {
}
std::size_t update_length() {
if (value_.size() == 1 && value_[0] == this) {
length = name_.size();
} else {
if (value_.size() != 0 || length == 0) {
length = 0;
for (word* w : value_) {
std::size_t w_length = w->update_length();
......@@ -90,12 +86,14 @@ class macroprocessor_impl {
return length;
}
std::string name_;
std::vector<word*> value_;
std::string name_;
std::size_t length;
};
std::map<std::string_view, word> words_;
std::unordered_map<std::string_view, word> words_;
std::vector<std::string_view> parsed;
std::vector<word*> found;
};
template< typename policy>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment