summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/symbol.h
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/utils/asymptote/symbol.h')
-rw-r--r--Build/source/utils/asymptote/symbol.h95
1 files changed, 55 insertions, 40 deletions
diff --git a/Build/source/utils/asymptote/symbol.h b/Build/source/utils/asymptote/symbol.h
index c4cb5a504df..35204f16f85 100644
--- a/Build/source/utils/asymptote/symbol.h
+++ b/Build/source/utils/asymptote/symbol.h
@@ -3,7 +3,7 @@
* Andy Hammerlindl 2002/06/18
*
* Creates symbols from strings so that multiple calls for a symbol of
- * the same string will return a pointer to the same object.
+ * the same string will return an identical object.
*****/
#ifndef SYMBOL_H
@@ -18,6 +18,8 @@ using std::ostream;
namespace sym {
+void initTable();
+
struct GCInit {
#ifdef _AIX
typedef char * GC_PTR;
@@ -28,53 +30,55 @@ struct GCInit {
GC_dont_expand=0;
GC_INIT();
#endif
+
+ // Put the symbol table into a state where symbols can be translated.
+ initTable();
}
};
+typedef unsigned int uint;
+
+/* The symbol class, just a wrapper around the augmented hash value. This
+ * wrapper is so that
+ * cout << s << endl;
+ * prints the symbol name instead of a meaningless integer.
+ *
+ * This is a lightweight class and should have no virtual functions for speed
+ * reasons.
+ */
struct symbol {
+ // Is there any particular reason why this is in symbol?
static GCInit initialize;
-private:
- string name;
-public:
- static mem::map<CONST string,symbol> dict;
+ uint hashplus;
+#if 0
+ symbol() {}
+ symbol(uint h) : hashplus(h) {}
+#endif
- static symbol *specialTrans(string s) {
- assert(dict.find(s) == dict.end());
- return &(dict[s]=symbol(s,true));
+ static symbol nullsym;
+ static symbol initsym;
+ static symbol castsym;
+ static symbol ecastsym;
+
+ bool special() const {
+ return *this == initsym || *this == castsym || *this == ecastsym;
}
- symbol() : special(false) {}
- symbol(string name, bool special=false)
- : name(name), special(special) {}
+ // Translate a string into a unique symbol, such that two strings are equal
+ // if and only if their resulting symbols are equal.
+ // len should be equal to strlen(s)+1
+ static symbol rawTrans(const char *s, size_t len);
-public:
- friend class mem::map<CONST string,symbol>;
- bool special; // NOTE: make this const (later).
-
- static symbol *initsym;
- static symbol *castsym;
- static symbol *ecastsym;
-
- static symbol *literalTrans(string s) {
-#ifdef SYMACC
- symbol &slot = dict[s];
- if (slot.name.empty())
- slot=symbol(s);
- return &slot;
-#else
- if (dict.find(s) != dict.end())
- return &dict[s];
- else
- return &(dict[s]=symbol(s));
-#endif
+ static symbol literalTrans(string s) {
+ return rawTrans(s.c_str(), s.size() + 1);
}
- static symbol *opTrans(string s) {
+ static symbol opTrans(string s) {
return literalTrans("operator "+s);
}
- static symbol *trans(string s) {
+ static symbol trans(string s) {
// Figure out whether it's an operator or an identifier by looking at the
// first character.
char c=s[0];
@@ -83,16 +87,27 @@ public:
// Make a symbol that is guaranteed to be unique. It will not match any other
// symbol in the namespace.
- static symbol *gensym(string s) {
- return new symbol("gensym "+s);
+ static symbol gensym(string s);
+
+ size_t hash() const {
+ return (size_t)this->hashplus;
+ }
+
+ friend bool operator== (symbol s1, symbol s2) {
+ return s1.hashplus == s2.hashplus;
}
- operator string () { return string(name); }
+ friend bool operator< (symbol s1, symbol s2) {
+ return s1.hashplus < s2.hashplus;
+ }
- friend ostream& operator<< (ostream& out, const symbol& sym)
- { return out << sym.name; }
+ operator bool () const { return this->hashplus != 0; }
+
+ operator string () const;
+
+ friend ostream& operator<< (ostream& out, const symbol sym);
};
-} // namespace sym
+} // end namespace
-#endif
+#endif // SYMBOL_H