Commit e3494744 authored by Klepl Jiří's avatar Klepl Jiří 🐉
Browse files

homework_macrosol_init

parent da42ae06
......@@ -4,13 +4,66 @@
#include "macrosol.hpp"
namespace macrosol {
bool macroprocessor_impl::process(
const std::string& s,
std::string& output
) {
std::vector<std::string_view> parsed;
template< typename policy>
bool macroprocessor<policy>::process(const std::string& s, std::string& output)
{
output = s;
return true;
}
for (
auto [i, j] = std::tuple{s.begin(), s.end()};;
++i;
) {
if (j != s.end()) {
if (i == s.end()) {
parsed.emplace_back(&*j, &*i - &*j);
break;
} else if (*i == ' ') {
}
} else {
if (i == s.end()) {
break;
} else if (*i != ' ') {
j = i;
}
}
}
template class macroprocessor< policy_scalar>;
if (parsed.size() > 1 && parsed[1] == "=") {
if (parsed.size() != 2) {
auto tmp = std::vector<word*> {};
tmp.reserve(parsed - 2);
for (auto i = parsed.begin() + 2; i != parse.end(); i++) {
auto it = word_.find(*i);
if (it != words_.end()) {
tmp.emplace_back(*i);
} else {
tmp.emplace_back(
std::string {parsed[0]},
words_.emplace(std::string {parsed[0]}, tmp).second);
}
}
words_.emplace(std::string {parsed[0]}, tmp);
} else {
words_.emplace(std::string {parsed[0]}, std::vector<word*> {});
}
} else {
for (auto&& p : parsed) {
auto it = words_.find(p);
if (it != words_.end()) {
output.reserve(output.size() + it->cached_length);
it->append_to(output);
} else {
it += *p;
}
}
}
return true;
}
template class macroprocessor< policy_scalar>;
}
......@@ -4,32 +4,93 @@
#ifndef macrosol_hpp_
#define macrosol_hpp_
#include <iomanip>
#include <array>
#include <memory>
#include <map>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
#include <type_traits>
namespace macrosol {
struct policy_scalar {
};
struct policy_sse {
};
struct policy_scalar {
};
struct policy_sse {
};
#ifdef USE_AVX
struct policy_avx {
};
struct policy_avx {
};
#endif
#ifdef USE_AVX512
struct policy_avx512 {
};
struct policy_avx512 {
};
#endif
template< typename policy>
class macroprocessor {
public:
bool process(const std::string& s, std::string& output);
private:
};
class macroprocessor_impl {
public:
bool process(const std::string& s, std::string& output);
private:
struct compare {
bool operator()(
const std::string& lhs,
const std::string& rhs) const {
return lhs < rhs;
}
bool operator()(
const std::string_view& lhs,
const std::string& rhs) const {
return lhs < rhs;
}
};
struct word {
word(std::string_view sv) {
value_ = sv;
cached_length = sv.size();
}
word(std::vector<word*>&& vector) {
value_ = vector;
cached_length = 0;
for (const word* i : vector) {
cached_length += i->cached_length;
}
}
void append_to(std::string&& output) {
std::visit([](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::string_view>) {
output += arg;
} else if constexpr (std::is_same_v<T, std::vector<word*>) {
cached_length = 0;
for (word* w : arg) {
w->append_to(output);
cached_length += w->cached_length;
}
}
}, value_);
}
std::variant<std::string_view, std::vector<word*>> value_;
std::size_t cached_length;
};
std::map<std::string, word, compare> words_;
};
template< typename policy>
class macroprocessor : public macroprocessor_impl {};
}
#endif
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