From c721dba11702c8f2efab7687c9cefb07bdae4ad8 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Thu, 6 Oct 2016 14:14:50 +0100 Subject: [PATCH] Merged 'ir/tagpool.{h,cc}' files into 'ir/tag.{h,cc}' files. --- re2c/Makefile.am | 2 - re2c/src/ir/dfa/dfa.h | 2 +- re2c/src/ir/nfa/nfa.h | 2 +- re2c/src/ir/rule.h | 1 - re2c/src/ir/tag.cc | 117 +++++++++++++++++++++++++++++++++++++++++ re2c/src/ir/tag.h | 27 +++++++++- re2c/src/ir/tagpool.cc | 111 -------------------------------------- re2c/src/ir/tagpool.h | 51 ------------------ 8 files changed, 145 insertions(+), 168 deletions(-) delete mode 100644 re2c/src/ir/tagpool.cc delete mode 100644 re2c/src/ir/tagpool.h diff --git a/re2c/Makefile.am b/re2c/Makefile.am index 36b6ee66..5d46e447 100644 --- a/re2c/Makefile.am +++ b/re2c/Makefile.am @@ -40,7 +40,6 @@ SRC_HDR = \ src/ir/compile.h \ src/ir/rule.h \ src/ir/tag.h \ - src/ir/tagpool.h \ src/ir/skeleton/path.h \ src/ir/skeleton/skeleton.h \ src/globals.h \ @@ -118,7 +117,6 @@ SRC = \ src/ir/skeleton/maxpath.cc \ src/ir/skeleton/skeleton.cc \ src/ir/tag.cc \ - src/ir/tagpool.cc \ src/main.cc \ src/parse/code.cc \ src/parse/input.cc \ diff --git a/re2c/src/ir/dfa/dfa.h b/re2c/src/ir/dfa/dfa.h index ef3e63e4..25616143 100644 --- a/re2c/src/ir/dfa/dfa.h +++ b/re2c/src/ir/dfa/dfa.h @@ -8,7 +8,7 @@ #include "src/ir/regexp/regexp.h" #include "src/ir/rule.h" -#include "src/ir/tagpool.h" +#include "src/ir/tag.h" #include "src/util/forbid_copy.h" namespace re2c diff --git a/re2c/src/ir/nfa/nfa.h b/re2c/src/ir/nfa/nfa.h index 5d4f24e4..be376179 100644 --- a/re2c/src/ir/nfa/nfa.h +++ b/re2c/src/ir/nfa/nfa.h @@ -8,7 +8,7 @@ #include "src/ir/regexp/regexp.h" #include "src/ir/rule.h" -#include "src/ir/tagpool.h" +#include "src/ir/tag.h" #include "src/util/forbid_copy.h" namespace re2c diff --git a/re2c/src/ir/rule.h b/re2c/src/ir/rule.h index 35a3098b..b1dd91d6 100644 --- a/re2c/src/ir/rule.h +++ b/re2c/src/ir/rule.h @@ -6,7 +6,6 @@ #include #include "src/ir/tag.h" -#include "src/ir/tagpool.h" #include "src/parse/code.h" #include "src/parse/loc.h" #include "src/util/forbid_copy.h" diff --git a/re2c/src/ir/tag.cc b/re2c/src/ir/tag.cc index 6e49d7ea..0f5ebf82 100644 --- a/re2c/src/ir/tag.cc +++ b/re2c/src/ir/tag.cc @@ -1,11 +1,26 @@ +#include #include +#include // malloc +#include // memcpy, memcmp #include "src/ir/rule.h" #include "src/ir/tag.h" +#include "src/util/hash32.h" namespace re2c { +struct eqtag_t +{ + size_t ntags; + + explicit eqtag_t(size_t n): ntags(n) {} + inline bool operator()(const bool *x, const bool *y) + { + return memcmp(x, y, ntags * sizeof(bool)) == 0; + } +}; + const size_t Tag::NONE = std::numeric_limits::max(); Tag::Tag() @@ -31,4 +46,106 @@ void init_fix_tag(Tag &tag, size_t r, const std::string *n, size_t b, size_t d) tag.fix.dist = d; } +Tagpool::Tagpool(size_t n) + : lookup() + , buffer(new bool[n * 3]) + , ntags(n) + , buffer1(&buffer[n * 1]) + , buffer2(&buffer[n * 2]) +{ + // all-zero tag configuration must have static number zero + std::fill(buffer, buffer + ntags, false); + assert(ZERO_TAGS == insert(buffer)); +} + +Tagpool::~Tagpool() +{ + delete[] buffer; + const size_t n = lookup.size(); + for (size_t i = 0; i < n; ++i) { + free(const_cast(lookup[i])); + } +} + +size_t Tagpool::insert(const bool *tags) +{ + const size_t size = ntags * sizeof(bool); + const uint32_t hash = hash32(0, tags, size); + + eqtag_t eq(ntags); + const size_t idx = lookup.find_with(hash, tags, eq); + if (idx != taglookup_t::NIL) { + return idx; + } + + bool *copy = static_cast(malloc(size)); + memcpy(copy, tags, size); + return lookup.push(hash, copy); +} + +const bool *Tagpool::operator[](size_t idx) const +{ + return lookup[idx]; +} + +size_t Tagpool::orl(size_t t, size_t o) +{ + if (t == o || o == 0) { + return t; + } else if (t == ZERO_TAGS) { + return o; + } + + const bool *tags = operator[](t); + const bool *ortags = operator[](o); + for (size_t i = 0; i < ntags; ++i) { + buffer[i] = tags[i] | ortags[i]; + } + return insert(buffer); +} + +size_t Tagpool::andl(size_t t, size_t a) +{ + if (t == a) { + return t; + } else if (t == ZERO_TAGS || a == ZERO_TAGS) { + return ZERO_TAGS; + } + + const bool *tags = operator[](t); + const bool *andtags = operator[](a); + for (size_t i = 0; i < ntags; ++i) { + buffer[i] = tags[i] & andtags[i]; + } + return insert(buffer); +} + +size_t Tagpool::andlinv(size_t t, size_t a) +{ + if (a == ZERO_TAGS) { + return t; + } else if (t == ZERO_TAGS || t == a) { + return ZERO_TAGS; + } + + const bool *tags = operator[](t); + const bool *andinvtags = operator[](a); + for (size_t i = 0; i < ntags; ++i) { + buffer[i] = tags[i] & ~andinvtags[i]; + } + return insert(buffer); +} + +size_t Tagpool::subst(size_t t, const size_t *represent) +{ + const bool *tags = operator[](t); + memset(buffer, 0, ntags * sizeof(bool)); + for (size_t i = 0; i < ntags; ++i) { + if (tags[i]) { + buffer[represent[i]] = true; + } + } + return insert(buffer); +} + } // namespace re2c diff --git a/re2c/src/ir/tag.h b/re2c/src/ir/tag.h index b9fbdfc8..9b981eba 100644 --- a/re2c/src/ir/tag.h +++ b/re2c/src/ir/tag.h @@ -3,7 +3,7 @@ #include -#include "src/ir/tagpool.h" +#include "src/util/lookup.h" #include "src/util/forbid_copy.h" namespace re2c @@ -36,6 +36,31 @@ struct Tag void init_var_tag(Tag &tag, size_t r, const std::string *n, size_t o); void init_fix_tag(Tag &tag, size_t r, const std::string *n, size_t b, size_t d); +static const size_t ZERO_TAGS = 0; + +struct Tagpool +{ +private: + typedef lookup_t taglookup_t; + taglookup_t lookup; + bool *buffer; + +public: + const size_t ntags; + bool *buffer1; + bool *buffer2; + + explicit Tagpool(size_t n); + ~Tagpool(); + size_t insert(const bool *tags); + size_t orl(size_t t, size_t o); + size_t andl(size_t t, size_t a); + size_t andlinv(size_t t, size_t a); + size_t subst(size_t t, const size_t *represent); + const bool *operator[](size_t idx) const; + FORBID_COPY(Tagpool); +}; + /* must be packed */ struct tagcmd_t { diff --git a/re2c/src/ir/tagpool.cc b/re2c/src/ir/tagpool.cc deleted file mode 100644 index ffd0b3e1..00000000 --- a/re2c/src/ir/tagpool.cc +++ /dev/null @@ -1,111 +0,0 @@ -#include - -#include "src/ir/tagpool.h" -#include "src/util/hash32.h" - -namespace re2c -{ - -Tagpool::Tagpool(size_t n) - : lookup() - , buffer(new bool[n * 3]) - , ntags(n) - , buffer1(&buffer[n * 1]) - , buffer2(&buffer[n * 2]) -{ - // all-zero tag configuration must have static number zero - std::fill(buffer, buffer + ntags, false); - assert(ZERO_TAGS == insert(buffer)); -} - -Tagpool::~Tagpool() -{ - delete[] buffer; - const size_t n = lookup.size(); - for (size_t i = 0; i < n; ++i) { - free(const_cast(lookup[i])); - } -} - -size_t Tagpool::insert(const bool *tags) -{ - const size_t size = ntags * sizeof(bool); - const uint32_t hash = hash32(0, tags, size); - - eqtag_t eq(ntags); - const size_t idx = lookup.find_with(hash, tags, eq); - if (idx != taglookup_t::NIL) { - return idx; - } - - bool *copy = static_cast(malloc(size)); - memcpy(copy, tags, size); - return lookup.push(hash, copy); -} - -const bool *Tagpool::operator[](size_t idx) const -{ - return lookup[idx]; -} - -size_t Tagpool::orl(size_t t, size_t o) -{ - if (t == o || o == 0) { - return t; - } else if (t == ZERO_TAGS) { - return o; - } - - const bool *tags = operator[](t); - const bool *ortags = operator[](o); - for (size_t i = 0; i < ntags; ++i) { - buffer[i] = tags[i] | ortags[i]; - } - return insert(buffer); -} - -size_t Tagpool::andl(size_t t, size_t a) -{ - if (t == a) { - return t; - } else if (t == ZERO_TAGS || a == ZERO_TAGS) { - return ZERO_TAGS; - } - - const bool *tags = operator[](t); - const bool *andtags = operator[](a); - for (size_t i = 0; i < ntags; ++i) { - buffer[i] = tags[i] & andtags[i]; - } - return insert(buffer); -} - -size_t Tagpool::andlinv(size_t t, size_t a) -{ - if (a == ZERO_TAGS) { - return t; - } else if (t == ZERO_TAGS || t == a) { - return ZERO_TAGS; - } - - const bool *tags = operator[](t); - const bool *andinvtags = operator[](a); - for (size_t i = 0; i < ntags; ++i) { - buffer[i] = tags[i] & ~andinvtags[i]; - } - return insert(buffer); -} - -size_t Tagpool::subst(size_t t, const size_t *represent) -{ - const bool *tags = operator[](t); - memset(buffer, 0, ntags * sizeof(bool)); - for (size_t i = 0; i < ntags; ++i) { - if (tags[i]) { - buffer[represent[i]] = true; - } - } - return insert(buffer); -} - -} // namespace re2c diff --git a/re2c/src/ir/tagpool.h b/re2c/src/ir/tagpool.h deleted file mode 100644 index 5f032bab..00000000 --- a/re2c/src/ir/tagpool.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RE2C_IR_TAGPOOL_ -#define _RE2C_IR_TAGPOOL_ - -#include // malloc -#include // memcpy, memcmp - -#include "src/util/lookup.h" -#include "src/util/forbid_copy.h" - -namespace re2c -{ - -static const size_t ZERO_TAGS = 0; - -struct eqtag_t -{ - size_t ntags; - - explicit eqtag_t(size_t n): ntags(n) {} - inline bool operator()(const bool *x, const bool *y) - { - return memcmp(x, y, ntags * sizeof(bool)) == 0; - } -}; - -struct Tagpool -{ -private: - typedef lookup_t taglookup_t; - taglookup_t lookup; - bool *buffer; - -public: - const size_t ntags; - bool *buffer1; - bool *buffer2; - - explicit Tagpool(size_t n); - ~Tagpool(); - size_t insert(const bool *tags); - size_t orl(size_t t, size_t o); - size_t andl(size_t t, size_t a); - size_t andlinv(size_t t, size_t a); - size_t subst(size_t t, const size_t *represent); - const bool *operator[](size_t idx) const; - FORBID_COPY(Tagpool); -}; - -} // namespace re2c - -#endif // _RE2C_IR_TAGPOOL_ -- 2.40.0