This commit is contained in:
ProgramSnail 2023-04-29 00:02:37 +03:00
parent 05eccf3a2e
commit 6e986f9a33
3 changed files with 101 additions and 2 deletions

View file

@ -93,6 +93,12 @@ public:
back_edges_[to].push_back(from);
}
void SetEqual(size_t u, size_t v) {
AddEdge(u, v);
AddEdge(v, u);
// TODO: set equality
}
void Calculate() {
if (is_calculated_) {
return;

View file

@ -1,6 +1,60 @@
#pragma once
#include <string>
#include <unordered_map>
// for calngd
#include "utils.hpp"
#include "interpreter_tree.hpp"
#include "error_handling.hpp"
namespace info {
class TypeclassGraph {
public:
struct TypeclassMethod {
std::string name;
interpreter::tokens::FunctionDeclaration* definition = nullptr;
};
struct ParametrizedTypeclass {
utils::IdType typeclass;
std::vector<utils::IdType> parameter_ids; // ??
};
struct TypeclassVertex {
std::vector<std::pair<std::string, std::vector<ParametrizedTypeclass>>> parameters;
std::vector<TypeclassMethod> methods;
std::vector<ParametrizedTypeclass> dependencies;
interpreter::tokens::TypeclassDefinitionStatement* definition = nullptr;
};
utils::IdType AddTypeclass(const TypeclassVertex& typeclass) { // TODO: universal reference
for (auto& method : typeclass.methods) {
if (method_to_typeclass_.count(method.name) != 0) {
error_handling::HandleTypecheckError("");
}
}
for (auto& method : typeclass.methods) {
method_to_typeclass_[method.name] = typeclasses_.size();
}
typeclasses_.push_back(typeclass);
return typeclasses_.size() - 1;
}
std::optional<utils::IdType> FindMethodTypeclass(const std::string& name) {
auto method_iter = method_to_typeclass_.find(name);
if (method_iter == method_to_typeclass_.end()) {
return std::nullopt;
}
return method_iter->second;
}
const TypeclassVertex& GetTypeclass(utils::IdType id) {
return typeclasses_.at(id);
}
private:
std::unordered_map<std::string, utils::IdType> method_to_typeclass_;
std::vector<TypeclassVertex> typeclasses_;
};
} // namespace info

View file

@ -1,12 +1,13 @@
#pragma once
#include <endian.h>
#include <vector>
#include <unordered_map>
namespace utils {
using IdType = std::size_t;
using std::size_t;
using IdType = size_t;
enum class ReferenceType { Reference, UniqueReference };
@ -39,4 +40,42 @@ private:
std::unordered_map<T, IdType> value_to_id_;
};
class Union { // TODO: recall right algorithm name
public:
Union(size_t n) {
edges_.resize(n);
ranks_.resize(n);
for (size_t i = 0; i < n; ++i) {
edges_[i] = i;
ranks_[i] = 1;
}
}
void Connect(size_t u, size_t v) { // TODO: recall choice algorithm
u = GetRoot(u);
v = GetRoot(v);
if (ranks_[v] >= ranks_[u]) {
edges_[u] = v;
ranks_[v] = std::max(ranks_[u] + 1, ranks_[v]);
} else {
edges_[v] = u;
}
}
bool IsConnected(size_t u, size_t v) {
return GetRoot(u) == GetRoot(v);
}
size_t GetRoot(size_t v) {
if (edges_[v] == v) {
return v;
}
return edges_[v] = GetRoot(edges_[v]);
}
private:
std::vector<size_t> edges_;
std::vector<size_t> ranks_;
};
} // namespace utils