summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/table.h
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2009-05-16 00:19:13 +0000
committerKarl Berry <karl@freefriends.org>2009-05-16 00:19:13 +0000
commitbab45528d65eaafe68a705dbb2a57075c7b7cbd8 (patch)
tree10b4ae2b5195c8dede153ab89359ec00f55f325f /Build/source/utils/asymptote/table.h
parent8643d90372e9c31e0f461c15c596b60a545bd7d3 (diff)
asymptote 1.72 sources (not integrated into build yet)
git-svn-id: svn://tug.org/texlive/trunk@13110 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/utils/asymptote/table.h')
-rw-r--r--Build/source/utils/asymptote/table.h141
1 files changed, 141 insertions, 0 deletions
diff --git a/Build/source/utils/asymptote/table.h b/Build/source/utils/asymptote/table.h
new file mode 100644
index 00000000000..e8717cfa8ed
--- /dev/null
+++ b/Build/source/utils/asymptote/table.h
@@ -0,0 +1,141 @@
+/*****
+ * table.h
+ * Andy Hammerlindl 2002/06/18
+ *
+ * Table used to bind symbols to vars and types in a namespace.
+ *****/
+
+#ifndef TABLE_H
+#define TABLE_H
+
+#include <cassert>
+#include <utility>
+
+#include "symbol.h"
+#include "common.h"
+
+namespace sym {
+
+template <class B>
+class table;
+
+template <class B>
+std::ostream& operator<< (std::ostream& out, const table<B>& t);
+
+template <class B>
+class table {
+protected:
+ typedef mem::multimap<symbol*CONST,B> scope_t;
+ typedef typename scope_t::iterator scope_iterator;
+ typedef mem::list<scope_t> scopes_t;
+ typedef mem::list<B> name_t;
+ typedef typename name_t::iterator name_iterator;
+ typedef mem::map<symbol*CONST,name_t> names_t;
+ typedef typename names_t::iterator names_iterator;
+
+ scopes_t scopes;
+ names_t names;
+
+ void remove(symbol *key);
+public :
+ table();
+
+ void enter(symbol *key, B value);
+ B look(symbol *key);
+
+ // Checks if a symbol was added in the last scope. Useful for
+ // stopping multiple definitions.
+ B lookInTopScope(symbol *key);
+
+ // Allows scoping and overloading of symbols of the same name
+ void beginScope();
+ void endScope();
+
+ // Copies all bindings in the top scope to the scope underneath it, and
+ // removes the the top scope.
+ void collapseScope();
+
+ // Adds to l, all names prefixed by start.
+ void completions(mem::list<symbol *>& l, string start);
+
+ friend std::ostream& operator<< <B> (std::ostream& out, const table& t);
+};
+
+template <class B>
+inline table<B>::table()
+{
+ beginScope();
+}
+
+template <class B>
+inline void table<B>::enter(symbol *key, B value)
+{
+ scopes.front().insert(std::make_pair(key,value));
+ names[key].push_front(value);
+}
+
+template <class B>
+inline B table<B>::look(symbol *key)
+{
+ if (!names[key].empty())
+ return names[key].front();
+ return 0;
+}
+
+template <class B>
+inline B table<B>::lookInTopScope(symbol *key)
+{
+ scope_iterator p = scopes.front().find(key);
+ if (p!=scopes.front().end())
+ return p->second;
+ return 0;
+}
+
+template <class B>
+inline void table<B>::beginScope()
+{
+ scopes.push_front(scope_t());
+}
+
+template <class B>
+inline void table<B>::remove(symbol *key)
+{
+ if (!names[key].empty())
+ names[key].pop_front();
+}
+
+template <class B>
+inline void table<B>::endScope()
+{
+ scope_t &scope = scopes.front();
+ for (scope_iterator p = scope.begin(); p != scope.end(); ++p)
+ remove(p->first);
+ scopes.pop_front();
+}
+
+template <class B>
+inline void table<B>::collapseScope()
+{
+ scope_t scope = scopes.front();
+ scopes.pop_front();
+
+ scopes.front().insert(scope.begin(), scope.end());
+}
+
+// Returns true if start is a prefix for name; eg, mac is a prefix of machine.
+inline bool prefix(string start, string name) {
+ return equal(start.begin(), start.end(), name.begin());
+}
+
+template <class B>
+inline void table<B>::completions(mem::list<symbol *>& l, string start)
+{
+ for (names_iterator p = names.begin(); p != names.end(); ++p)
+ if (prefix(start, *(p->first)) && !p->second.empty())
+ l.push_back(p->first);
+}
+
+
+} // namespace sym
+
+#endif