2023-04-17 11:31:00 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-05-06 19:26:14 +03:00
|
|
|
#include <cstdlib>
|
2023-05-21 14:58:18 +03:00
|
|
|
#include <unordered_set>
|
2023-04-17 11:14:33 +03:00
|
|
|
#include <vector>
|
2023-05-09 17:42:35 +03:00
|
|
|
#include <string>
|
2023-05-11 14:56:27 +03:00
|
|
|
#include <memory>
|
|
|
|
|
#include <optional>
|
2023-04-17 11:14:33 +03:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
|
|
namespace utils {
|
|
|
|
|
|
2023-04-29 00:02:37 +03:00
|
|
|
using std::size_t;
|
|
|
|
|
|
|
|
|
|
using IdType = size_t;
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-05-09 17:42:35 +03:00
|
|
|
const std::string ClassInternalVarName = "self";
|
2023-05-13 13:11:12 +03:00
|
|
|
const size_t MaxOperatorPrecedence = 4;
|
2023-05-09 17:42:35 +03:00
|
|
|
|
2023-05-21 11:58:23 +03:00
|
|
|
struct ValueReturnedMarker {};
|
|
|
|
|
struct ValueBroughtMarker {};
|
|
|
|
|
|
2023-05-13 13:11:12 +03:00
|
|
|
enum class ReferenceModifier { Reference = 0, UniqueReference = 1, Dereference = 2 };
|
2023-05-05 16:35:13 +03:00
|
|
|
enum class IsConstModifier { Const = 0, Var = 1 };
|
2023-05-14 11:28:37 +03:00
|
|
|
enum class ClassInternalsModifier { Static = 0, Const = 1, Var = 2};
|
2023-05-05 16:35:13 +03:00
|
|
|
enum class ClassModifier { Struct = 0, Class = 1 };
|
|
|
|
|
enum class AssignmentModifier { Assign = 0, Move = 1 };
|
|
|
|
|
enum class AliasModifier { Alias = 0, Type = 1, Let = 2 };
|
|
|
|
|
enum class AbstractTypeModifier { Basic = 0, Abstract = 1 };
|
2023-05-07 22:58:15 +03:00
|
|
|
enum class PartitionModifier { Exec = 0, Test = 1 };
|
2023-04-21 14:27:55 +03:00
|
|
|
|
2023-05-06 19:26:14 +03:00
|
|
|
enum class ValueType { Const = 0, Var = 1, Tmp = 2 };
|
|
|
|
|
|
|
|
|
|
inline ValueType IsConstModifierToValueType(IsConstModifier modifier) {
|
|
|
|
|
switch (modifier) {
|
|
|
|
|
case IsConstModifier::Const:
|
|
|
|
|
return ValueType::Const;
|
|
|
|
|
case IsConstModifier::Var:
|
|
|
|
|
return ValueType::Var;
|
|
|
|
|
}
|
2023-05-14 11:28:37 +03:00
|
|
|
|
|
|
|
|
exit(1); // unreachable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline ValueType ClassInternalsModifierToValueType(ClassInternalsModifier modifier) {
|
|
|
|
|
switch (modifier) {
|
|
|
|
|
case ClassInternalsModifier::Const:
|
|
|
|
|
return ValueType::Const;
|
|
|
|
|
case ClassInternalsModifier::Var:
|
|
|
|
|
return ValueType::Var;
|
|
|
|
|
case ClassInternalsModifier::Static:
|
|
|
|
|
throw std::bad_cast(); // ??
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exit(1); // unreachable
|
2023-05-06 19:26:14 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-21 14:58:18 +03:00
|
|
|
inline bool IsBuiltinFunction(const std::string& name) { // optimize ??
|
|
|
|
|
std::unordered_set<std::string> builtin_functions;
|
|
|
|
|
|
|
|
|
|
builtin_functions.insert("=");
|
|
|
|
|
builtin_functions.insert("<-");
|
|
|
|
|
builtin_functions.insert("==");
|
|
|
|
|
builtin_functions.insert("!=");
|
|
|
|
|
builtin_functions.insert("<");
|
|
|
|
|
builtin_functions.insert(">");
|
|
|
|
|
builtin_functions.insert("<=");
|
|
|
|
|
builtin_functions.insert(">=");
|
|
|
|
|
builtin_functions.insert("+=");
|
|
|
|
|
builtin_functions.insert("-=");
|
|
|
|
|
builtin_functions.insert("*=");
|
|
|
|
|
builtin_functions.insert("//=");
|
|
|
|
|
builtin_functions.insert("%=");
|
|
|
|
|
builtin_functions.insert("/=");
|
|
|
|
|
builtin_functions.insert("+");
|
|
|
|
|
builtin_functions.insert("-");
|
|
|
|
|
builtin_functions.insert("*");
|
|
|
|
|
builtin_functions.insert("//");
|
|
|
|
|
builtin_functions.insert("%");
|
|
|
|
|
builtin_functions.insert("/");
|
|
|
|
|
builtin_functions.insert("&&");
|
|
|
|
|
builtin_functions.insert("||");
|
|
|
|
|
builtin_functions.insert("size");
|
|
|
|
|
builtin_functions.insert("random");
|
|
|
|
|
builtin_functions.insert("print");
|
|
|
|
|
builtin_functions.insert("scan");
|
|
|
|
|
|
|
|
|
|
return builtin_functions.count(name) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
template<typename T>
|
|
|
|
|
class Storage {
|
|
|
|
|
public:
|
2023-04-21 14:27:55 +03:00
|
|
|
Storage() = default;
|
|
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
IdType GetId(const T& value) {
|
|
|
|
|
IdType id = 0;
|
|
|
|
|
auto value_position = value_to_id_.find(value);
|
|
|
|
|
|
|
|
|
|
if (value_position == value_to_id_.end()) {
|
|
|
|
|
id = id_to_value_.size();
|
|
|
|
|
value_to_id_[value] = id;
|
|
|
|
|
id_to_value_.push_back(value);
|
|
|
|
|
} else {
|
|
|
|
|
id = value_position->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const T& GetValue(IdType id) {
|
|
|
|
|
return id_to_value_[id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<T> id_to_value_;
|
|
|
|
|
std::unordered_map<T, IdType> value_to_id_;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-11 14:56:27 +03:00
|
|
|
template<typename Key, typename Value>
|
|
|
|
|
class Trie { // optimize ??
|
|
|
|
|
public:
|
|
|
|
|
Trie() {
|
|
|
|
|
verticles_.emplace_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Insert(const std::vector<Key>& path, const Value& value) {
|
|
|
|
|
return RecursiveInsert(verticles_[0], path, 0, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<Value*> Find(const std::vector<Key>& path) {
|
|
|
|
|
return RecursiveFind(verticles_[0], path, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<Value*> FindByPrefix(const std::vector<Key>& path) {
|
|
|
|
|
return RecursiveFindByPrefix(verticles_[0], path, 0);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
struct Vertex {
|
|
|
|
|
std::unordered_map<Key, size_t> children_;
|
|
|
|
|
std::optional<Value> value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool RecursiveInsert(Vertex& vertex,
|
|
|
|
|
const std::vector<Key>& path,
|
|
|
|
|
size_t path_position,
|
|
|
|
|
const Value& value) {
|
|
|
|
|
if (path_position == path.size()) {
|
|
|
|
|
if (vertex.value.has_value()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
vertex.value = value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto child_iter = vertex.children_.find(path[path_position]);
|
|
|
|
|
|
|
|
|
|
if (child_iter != vertex.children_.end()) {
|
|
|
|
|
return RecursiveInsert(verticles_[child_iter->second], path, path_position + 1, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vertex.children_[path[path_position]] = verticles_.size();
|
|
|
|
|
verticles_.emplace_back();
|
|
|
|
|
return RecursiveInsert(verticles_.back(), path, path_position + 1, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<Value*> RecursiveFind(Vertex& vertex,
|
|
|
|
|
const std::vector<Key>& path,
|
|
|
|
|
size_t path_position) {
|
|
|
|
|
if (path_position == path.size()) {
|
2023-05-17 18:29:39 +03:00
|
|
|
if (!vertex.value.has_value()) {
|
2023-05-11 14:56:27 +03:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
return &vertex.value.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto child_iter = vertex.children_.find(path[path_position]);
|
|
|
|
|
|
|
|
|
|
if (child_iter != vertex.children_.end()) {
|
|
|
|
|
return RecursiveFind(verticles_[child_iter->second], path, path_position + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<Value*> RecursiveFindByPrefix(Vertex& vertex,
|
|
|
|
|
const std::vector<Key>& path,
|
|
|
|
|
size_t path_position) {
|
|
|
|
|
if (path_position == path.size()) {
|
|
|
|
|
std::vector<Value*> ans;
|
|
|
|
|
RecursiveGetAll(vertex, ans);
|
|
|
|
|
return ans;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto child_iter = vertex.children_.find(path[path_position]);
|
|
|
|
|
|
|
|
|
|
if (child_iter != vertex.children_.end()) {
|
|
|
|
|
return RecursiveFindByPrefix(verticles_[child_iter->second], path, path_position + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RecursiveGetAll(Vertex& vertex, std::vector<Value*>& accumulator) {
|
|
|
|
|
std::vector<Value*> ans;
|
|
|
|
|
|
|
|
|
|
if (vertex.value.has_value()) {
|
|
|
|
|
accumulator.push_back(&vertex.value.value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& child : vertex.children_) {
|
|
|
|
|
RecursiveGetAll(verticles_[child.second], accumulator);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<Vertex> verticles_;
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-29 12:33:05 +03:00
|
|
|
class GroupsManager { // TODO: recall right algorithm name
|
2023-04-29 00:02:37 +03:00
|
|
|
public:
|
2023-04-29 12:33:05 +03:00
|
|
|
GroupsManager() = default;
|
|
|
|
|
|
|
|
|
|
explicit GroupsManager(size_t n) {
|
2023-04-29 00:02:37 +03:00
|
|
|
edges_.resize(n);
|
|
|
|
|
ranks_.resize(n);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
|
edges_[i] = i;
|
|
|
|
|
ranks_[i] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-29 12:33:05 +03:00
|
|
|
void Unite(size_t u, size_t v) { // TODO: recall choice algorithm
|
|
|
|
|
u = GetGroupRoot(u);
|
|
|
|
|
v = GetGroupRoot(v);
|
2023-04-29 00:02:37 +03:00
|
|
|
if (ranks_[v] >= ranks_[u]) {
|
|
|
|
|
edges_[u] = v;
|
|
|
|
|
ranks_[v] = std::max(ranks_[u] + 1, ranks_[v]);
|
|
|
|
|
} else {
|
|
|
|
|
edges_[v] = u;
|
2023-04-29 12:33:05 +03:00
|
|
|
// always ranks_[u] > ranks_[v]
|
2023-04-29 00:02:37 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-29 12:33:05 +03:00
|
|
|
bool IsInOneGroup(size_t u, size_t v) {
|
|
|
|
|
return GetGroupRoot(u) == GetGroupRoot(v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t AddElement() {
|
|
|
|
|
size_t id = edges_.size();
|
|
|
|
|
edges_.push_back(id);
|
|
|
|
|
ranks_.push_back(1);
|
|
|
|
|
return id;
|
2023-04-29 00:02:37 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-29 12:33:05 +03:00
|
|
|
size_t GetGroupRoot(size_t v) {
|
2023-04-29 00:02:37 +03:00
|
|
|
if (edges_[v] == v) {
|
|
|
|
|
return v;
|
|
|
|
|
}
|
2023-04-29 12:33:05 +03:00
|
|
|
return edges_[v] = GetGroupRoot(edges_[v]);
|
2023-04-29 00:02:37 +03:00
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
std::vector<size_t> edges_;
|
|
|
|
|
std::vector<size_t> ranks_;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-17 17:57:56 +03:00
|
|
|
// move to .cpp ??
|
|
|
|
|
inline void BackVisitDfs(size_t id,
|
2023-05-14 11:28:37 +03:00
|
|
|
std::vector<size_t>& verticles,
|
|
|
|
|
std::vector<size_t>& marks,
|
|
|
|
|
const std::vector<std::vector<size_t>>& edges,
|
|
|
|
|
size_t mark) {
|
|
|
|
|
if (marks[id] != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marks[id] = mark;
|
|
|
|
|
verticles.push_back(id);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < edges[id].size(); ++i) {
|
|
|
|
|
BackVisitDfs(id, verticles, marks, edges, mark);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 17:57:56 +03:00
|
|
|
// move to .cpp ??
|
|
|
|
|
inline std::vector<size_t> BackTopSort(const std::vector<std::vector<size_t>>& edges_) {
|
2023-05-14 11:28:37 +03:00
|
|
|
std::vector<size_t> sorted_verticles;
|
|
|
|
|
std::vector<size_t> marks(edges_.size(), 0);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < marks.size(); ++i) {
|
|
|
|
|
BackVisitDfs(i, sorted_verticles, marks, edges_, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sorted_verticles;
|
|
|
|
|
}
|
2023-05-07 15:17:37 +03:00
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
} // namespace utils
|