refactor(util/trie): modify interface to avoid the creation of many temporary optional values and inc/dec reference counters

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-06-05 12:40:31 -07:00
parent 38f471b390
commit 5b898aa3ed
3 changed files with 99 additions and 100 deletions

View file

@ -7,18 +7,24 @@ Author: Leonardo de Moura
#include "library/token_set.h"
namespace lean {
token_set token_set::add_command_token(char const * token) const { return token_set(insert(m_tokens, token, info(token))); }
token_set token_set::add_token(char const * token, unsigned prec) const { return token_set(insert(m_tokens, token, info(token, prec))); }
token_set token_set::add_token(char const * token, char const * val, unsigned prec) const {
return token_set(insert(m_tokens, token, info(val, prec)));
token_set add_command_token(token_set const & s, char const * token) {
return insert(s, token, token_info(token));
}
token_set token_set::merge(token_set const & s) const { return token_set(::lean::merge(m_tokens, s.m_tokens)); }
optional<token_set> token_set::find(char c) const {
auto r = m_tokens.find(c);
return r ? optional<token_set>(token_set(*r)) : optional<token_set>();
token_set add_token(token_set const & s, char const * token, unsigned prec) {
return insert(s, token, token_info(token, prec));
}
token_set add_token(token_set const & s, char const * token, char const * val, unsigned prec) {
return insert(s, token, token_info(val, prec));
}
token_set merge(token_set const & s1, token_set const & s2) {
return merge(s1, s2);
}
token_set const * find(token_set const & s, char c) {
return s.find(c);
}
token_info const * value_of(token_set const & s) {
return s.value();
}
optional<token_set::info> token_set::value() const { return m_tokens.value(); }
static char const * g_lambda_unicode = "\u03BB";
static char const * g_pi_unicode = "\u03A0";
static char const * g_forall_unicode = "\u2200";
@ -28,20 +34,20 @@ static char const * g_arrow_unicode = "\u2192";
class init_token_set_fn {
public:
token_set m_token_set;
init_token_set_fn():m_token_set(true) {
init_token_set_fn() {
char const * builtin[] = {"fun", "forall", "let", "in", "have", "show", "by", "from", "(", ")", "{", "}",
".{", "Type", "...", ",", ".", ":", "calc", ":=", "--", "(*", "(--", "->", 0};
char const ** it = builtin;
while (*it) {
m_token_set = m_token_set.add_token(*it);
m_token_set = add_token(m_token_set, *it);
it++;
}
m_token_set = m_token_set.add_token(g_lambda_unicode, "fun");
m_token_set = m_token_set.add_token(g_forall_unicode, "forall");
m_token_set = m_token_set.add_token(g_pi_unicode, "forall");
m_token_set = m_token_set.add_token(g_arrow_unicode, "->");
m_token_set = add_token(m_token_set, g_lambda_unicode, "fun");
m_token_set = add_token(m_token_set, g_forall_unicode, "forall");
m_token_set = add_token(m_token_set, g_pi_unicode, "forall");
m_token_set = add_token(m_token_set, g_arrow_unicode, "->");
}
};
static init_token_set_fn g_init;
token_set::token_set():token_set(g_init.m_token_set) {}
token_set mk_default_token_set() { return g_init.m_token_set; }
}

View file

@ -8,34 +8,28 @@ Author: Leonardo de Moura
#include <utility>
#include <string>
#include "util/trie.h"
#include "util/name.h"
namespace lean {
/** \brief Set of tokens and their precedences. This information is used by the scanner. */
class token_set {
class info {
bool m_command;
std::string m_value;
unsigned m_precedence;
public:
info():m_command(true) {}
info(char const * val):m_command(true), m_value(val), m_precedence(0) {}
info(char const * val, unsigned prec):m_command(false), m_value(val), m_precedence(prec) {}
bool is_command() const { return m_command; }
std::string const & value() const { return m_value; }
unsigned precedence() const { return m_precedence; }
};
ctrie<info> m_tokens;
explicit token_set(ctrie<info> const & t):m_tokens(t) {}
friend class init_token_set_fn;
token_set(bool) {} // NOLINT
class token_info {
bool m_command;
name m_value;
unsigned m_precedence;
public:
token_set();
token_set add_command_token(char const * token) const;
token_set add_token(char const * token, unsigned prec = 0) const;
token_set add_token(char const * token, char const * val, unsigned prec = 0) const;
token_set merge(token_set const & s) const;
optional<token_set> find(char c) const;
optional<info> value() const;
token_info():m_command(true) {}
token_info(char const * val):m_command(true), m_value(val), m_precedence(0) {}
token_info(char const * val, unsigned prec):m_command(false), m_value(val), m_precedence(prec) {}
bool is_command() const { return m_command; }
name const & value() const { return m_value; }
unsigned precedence() const { return m_precedence; }
};
typedef ctrie<token_info> token_set;
token_set mk_default_token_set();
token_set add_command_token(token_set const & s, char const * token);
token_set add_token(token_set const & s, char const * token, unsigned prec = 0);
token_set add_token(token_set const & s, char const * token, char const * val, unsigned prec = 0);
token_set merge(token_set const & s1, token_set const & s2);
token_set const * find(token_set const & s, char c);
token_info const * value_of(token_set const & s);
}

View file

@ -14,24 +14,10 @@ Author: Leonardo de Moura
namespace lean {
template<typename Key, typename Val, typename KeyCMP>
class trie : public KeyCMP {
struct cell;
struct node {
cell * m_ptr;
node():m_ptr(new cell()) { m_ptr->inc_ref(); }
node(cell * ptr):m_ptr(ptr) { if (m_ptr) ptr->inc_ref(); }
node(node const & s):m_ptr(s.m_ptr) { if (m_ptr) m_ptr->inc_ref(); }
node(node && s):m_ptr(s.m_ptr) { s.m_ptr = nullptr; }
~node() { if (m_ptr) m_ptr->dec_ref(); }
node & operator=(node const & n) { LEAN_COPY_REF(n); }
node & operator=(node&& n) { LEAN_MOVE_REF(n); }
cell * operator->() const { lean_assert(m_ptr); return m_ptr; }
bool is_shared() const { return m_ptr && m_ptr->get_rc() > 1; }
friend void swap(node & n1, node & n2) { std::swap(n1.m_ptr, n2.m_ptr); }
node steal() { node r; swap(r, *this); return r; }
};
friend struct cell;
struct cell {
rb_map<Key, node, KeyCMP> m_children;
rb_map<Key, trie, KeyCMP> m_children;
optional<Val> m_value;
MK_LEAN_RC();
void dealloc() { delete this; }
@ -40,27 +26,29 @@ class trie : public KeyCMP {
cell(cell const & s):m_children(s.m_children), m_value(s.m_value), m_rc(0) {}
};
static node ensure_unshared(node && n) {
static trie ensure_unshared(trie && n) {
if (n.is_shared())
return node(new cell(*n.m_ptr));
return trie(new cell(*n.m_ptr));
else
return n;
}
template<typename It>
static node insert(node && n, It const & begin, It const & end, Val const & v) {
node h = ensure_unshared(n.steal());
static trie insert(trie && n, It const & begin, It const & end, Val const & v) {
trie h = ensure_unshared(n.steal());
if (!h.m_ptr)
h = trie(new cell());
if (begin == end) {
h->m_value = v;
return h;
} else {
Key k = *begin;
node const * c = h->m_children.find(k);
trie const * c = h->m_children.find(k);
It it(begin); it++;
if (c == nullptr) {
h->m_children.insert(k, insert(node(), it, end, v));
h->m_children.insert(k, insert(trie(), it, end, v));
} else {
node n(*c);
trie n(*c);
h->m_children.erase(k);
h->m_children.insert(k, insert(n.steal(), it, end, v));
}
@ -68,15 +56,15 @@ class trie : public KeyCMP {
}
}
static node merge(node && t1, node const & t2) {
node new_t1 = ensure_unshared(t1.steal());
static trie merge(trie && t1, trie const & t2) {
trie new_t1 = ensure_unshared(t1.steal());
new_t1->m_value = t2->m_value;
t2->m_children.for_each([&](Key const & k, node const & c2) {
node const * c1 = new_t1->m_children.find(k);
t2->m_children.for_each([&](Key const & k, trie const & c2) {
trie const * c1 = new_t1->m_children.find(k);
if (c1 == nullptr) {
new_t1->m_children.insert(k, c2);
} else {
node n1(*c1);
trie n1(*c1);
new_t1->m_children.erase(k);
new_t1->m_children.insert(k, merge(n1.steal(), c2));
}
@ -85,63 +73,74 @@ class trie : public KeyCMP {
}
template<typename F>
static void for_each(F && f, node const & n, buffer<Key> & prefix) {
static void for_each(F && f, trie const & n, buffer<Key> & prefix) {
if (n->m_value) {
f(prefix.size(), prefix.data(), *(n->m_value));
}
n->m_children.for_each([&](Key const & k, node const & c) {
n->m_children.for_each([&](Key const & k, trie const & c) {
prefix.push_back(k);
for_each(f, c, prefix);
prefix.pop_back();
});
}
node m_root;
trie(node const & n):m_root(n) {}
public:
trie() {}
trie(trie const & s):m_root(s.m_root) {}
trie(trie && s):m_root(s.m_root) {}
cell * m_ptr;
cell * operator->() const { lean_assert(m_ptr); return m_ptr; }
trie & operator=(trie const & s) { m_root = s.m_root; return *this; }
trie & operator=(trie && s) { m_root = s.m_root; return *this; }
trie(cell * ptr):m_ptr(ptr) { if (m_ptr) ptr->inc_ref(); }
trie steal() { trie r; swap(r, *this); return r; }
public:
trie():m_ptr(nullptr) {}
trie(trie const & s):m_ptr(s.m_ptr) { if (m_ptr) m_ptr->inc_ref(); }
trie(trie && s):m_ptr(s.m_ptr) { s.m_ptr = nullptr; }
~trie() { if (m_ptr) m_ptr->dec_ref(); }
trie & operator=(trie const & n) { LEAN_COPY_REF(n); }
trie & operator=(trie&& n) { LEAN_MOVE_REF(n); }
bool is_shared() const { return m_ptr && m_ptr->get_rc() > 1; }
friend void swap(trie & n1, trie & n2) { std::swap(n1.m_ptr, n2.m_ptr); }
template<typename It>
optional<Val> find(It const & begin, It const & end) const {
node const * n = &m_root;
Val const * find(It const & begin, It const & end) const {
if (!m_ptr)
return nullptr;
cell const * c = m_ptr;
for (It it = begin; it != end; ++it) {
n = (*n)->m_children.find(*it);
trie const * n = c->m_children.find(*it);
if (!n)
return optional<Val>();
return nullptr;
c = n->m_ptr;
}
return (*n)->m_value;
if (c->m_value)
return &c->m_value.value();
else
return nullptr;
}
template<typename It>
void insert(It const & begin, It const & end, Val const & v) {
m_root = insert(m_root.steal(), begin, end, v);
*this = insert(steal(), begin, end, v);
}
optional<trie> find(Key const & k) const {
node const * c = m_root->m_children.find(k);
if (c)
return optional<trie>(trie(*c));
trie const * find(Key const & k) const {
return m_ptr ? m_ptr->m_children.find(k) : nullptr;
}
Val const * value() const {
if (m_ptr && m_ptr->m_value)
return &m_ptr->m_value.value();
else
return optional<trie>();
}
optional<Val> value() const {
return m_root->m_value;
return nullptr;
}
void merge(trie const & t) {
m_root = merge(m_root.steal(), t.m_root);
*this = merge(steal(), t);
}
template<typename F>
void for_each(F && f) const {
buffer<Key> prefix;
for_each(f, m_root, prefix);
for_each(f, *this, prefix);
}
// for debugging purposes
@ -176,10 +175,10 @@ inline ctrie<Val> insert(ctrie<Val> const & t, char const * str, Val const & v)
}
template<typename Val>
optional<Val> find(ctrie<Val> const & t, std::string const & str) { return t.find(str.begin(), str.end()); }
Val const * find(ctrie<Val> const & t, std::string const & str) { return t.find(str.begin(), str.end()); }
template<typename Val>
optional<Val> find(ctrie<Val> const & t, char const * str) { return t.find(str, str + strlen(str)); }
Val const * find(ctrie<Val> const & t, char const * str) { return t.find(str, str + strlen(str)); }
template<typename Val>
inline ctrie<Val> merge(ctrie<Val> const & t1, ctrie<Val> const & t2) {