2019-04-16 11:40:38 +02:00
|
|
|
// Copyright © 2006, Ondra Kamenik
|
2019-01-08 17:12:05 +01:00
|
|
|
|
|
|
|
// $Id: static_atoms.cpp 1360 2007-07-10 11:44:20Z kamenik $
|
|
|
|
|
|
|
|
#include "static_atoms.hh"
|
|
|
|
#include "utils/cc/exception.hh"
|
|
|
|
|
|
|
|
using namespace ogp;
|
|
|
|
|
|
|
|
StaticAtoms::StaticAtoms(const StaticAtoms &a)
|
|
|
|
: Atoms(), Constants(a), varnames(a.varnames),
|
|
|
|
varorder(), vars(), indices()
|
|
|
|
{
|
|
|
|
// fill varorder
|
2019-01-09 15:44:26 +01:00
|
|
|
for (auto i : a.varorder)
|
2019-01-08 17:12:05 +01:00
|
|
|
{
|
2019-01-09 15:44:26 +01:00
|
|
|
const char *s = varnames.query(i);
|
2019-01-08 17:12:05 +01:00
|
|
|
varorder.push_back(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill vars
|
2019-01-09 15:44:26 +01:00
|
|
|
for (auto var : a.vars)
|
2019-01-08 17:12:05 +01:00
|
|
|
{
|
2019-01-09 15:44:26 +01:00
|
|
|
const char *s = varnames.query(var.first);
|
2019-04-19 17:09:04 +02:00
|
|
|
vars.emplace(s, var.second);
|
2019-01-08 17:12:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fill indices
|
2019-01-09 15:44:26 +01:00
|
|
|
for (auto indice : a.indices)
|
2019-01-08 17:12:05 +01:00
|
|
|
{
|
2019-01-09 15:44:26 +01:00
|
|
|
const char *s = varnames.query(indice.second);
|
2019-04-19 17:09:04 +02:00
|
|
|
indices.emplace(indice.first, s);
|
2019-01-08 17:12:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StaticAtoms::import_atoms(const DynamicAtoms &da, OperationTree &otree, Tintintmap &tmap)
|
|
|
|
{
|
|
|
|
Constants::import_constants(da, otree, tmap);
|
|
|
|
|
|
|
|
for (int i = 0; i < da.get_name_storage().num(); i++)
|
|
|
|
{
|
|
|
|
const char *name = da.get_name_storage().get_name(i);
|
|
|
|
register_name(name);
|
|
|
|
int tnew = otree.add_nulary();
|
|
|
|
assign(name, tnew);
|
|
|
|
if (da.is_referenced(name))
|
|
|
|
{
|
|
|
|
const DynamicAtoms::Tlagmap &lmap = da.lagmap(name);
|
2019-01-09 15:44:26 +01:00
|
|
|
for (auto it : lmap)
|
2019-01-08 17:12:05 +01:00
|
|
|
{
|
2019-01-09 15:44:26 +01:00
|
|
|
int told = it.second;
|
2019-04-19 17:09:04 +02:00
|
|
|
tmap.emplace(told, tnew);
|
2019-01-08 17:12:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
StaticAtoms::check(const char *name) const
|
|
|
|
{
|
|
|
|
if (DynamicAtoms::is_string_constant(name))
|
2019-04-19 17:09:04 +02:00
|
|
|
return Constants::check(name);
|
2019-01-08 17:12:05 +01:00
|
|
|
else
|
2019-04-19 17:09:04 +02:00
|
|
|
return check_variable(name);
|
2019-01-08 17:12:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
StaticAtoms::index(const char *name) const
|
|
|
|
{
|
2019-01-09 15:51:19 +01:00
|
|
|
auto it = vars.find(name);
|
2019-01-08 17:12:05 +01:00
|
|
|
if (it == vars.end())
|
|
|
|
return -1;
|
|
|
|
else
|
2019-04-19 17:09:04 +02:00
|
|
|
return it->second;
|
2019-01-08 17:12:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
StaticAtoms::inv_index(int t) const
|
|
|
|
{
|
2019-01-09 15:51:19 +01:00
|
|
|
auto it = indices.find(t);
|
2019-01-08 17:12:05 +01:00
|
|
|
if (it == indices.end())
|
2019-01-09 16:25:31 +01:00
|
|
|
return nullptr;
|
2019-01-08 17:12:05 +01:00
|
|
|
else
|
2019-04-19 17:09:04 +02:00
|
|
|
return it->second;
|
2019-01-08 17:12:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StaticAtoms::assign(const char *name, int t)
|
|
|
|
{
|
|
|
|
if (DynamicAtoms::is_string_constant(name))
|
|
|
|
{
|
2019-04-23 18:57:52 +02:00
|
|
|
double val = std::stod(name);
|
2019-01-08 17:12:05 +01:00
|
|
|
add_constant(t, val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *ss = varnames.insert(name);
|
2019-04-19 17:09:04 +02:00
|
|
|
vars.emplace(ss, t);
|
|
|
|
indices.emplace(t, ss);
|
2019-01-08 17:12:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<int>
|
|
|
|
StaticAtoms::variables() const
|
|
|
|
{
|
|
|
|
vector<int> res;
|
2019-01-09 15:44:26 +01:00
|
|
|
for (auto var : vars)
|
2019-04-19 17:09:04 +02:00
|
|
|
res.push_back(var.second);
|
2019-01-08 17:12:05 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StaticAtoms::register_name(const char *name)
|
|
|
|
{
|
|
|
|
const char *ss = varnames.insert(name);
|
|
|
|
varorder.push_back(ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StaticAtoms::print() const
|
|
|
|
{
|
|
|
|
printf("constants:\n");
|
|
|
|
Constants::print();
|
|
|
|
printf("variable names:\n");
|
|
|
|
varnames.print();
|
|
|
|
printf("map to tree indices:\n");
|
2019-01-09 15:44:26 +01:00
|
|
|
for (auto var : vars)
|
2019-04-19 17:09:04 +02:00
|
|
|
printf(u8"%s\t→\t%d\n", var.first, var.second);
|
2019-01-08 17:12:05 +01:00
|
|
|
}
|