00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef __MMX_SYMBOL_HPP
00015 #define __MMX_SYMBOL_HPP
00016 #include <basix/table.hpp>
00017
00019
00020 namespace mmx {
00021 #define TMPL_DEF template<typename C, typename V= exact_eq_table>
00022 #define TMPL template<typename C, typename V>
00023 #define Symbol symbol<C,V>
00024 #define Symbol_rep symbol_rep<C,V>
00025 TMPL class symbol_rep;
00026 TMPL class symbol;
00027 TMPL inline nat hash (const Symbol& s);
00028 TMPL inline bool operator == (const Symbol& s1, const Symbol& s2);
00029 TMPL inline bool operator != (const Symbol& s1, const Symbol& s2);
00030 TMPL inline nat exact_hash (const Symbol& s);
00031 TMPL inline bool exact_eq (const Symbol& s1, const Symbol& s2);
00032 TMPL inline bool exact_neq (const Symbol& s1, const Symbol& s2);
00033
00034
00035
00036
00037
00038 TMPL_DEF
00039 class symbol_rep REP_STRUCT {
00040 private:
00041 typedef table<Symbol_rep*,C,V> table_type;
00042
00043 static inline table_type& ptr () {
00044 typedef table<Symbol_rep*,C,V> table_type;
00045 static table_type* t= mmx_new_one<table_type> ();
00046
00047
00048
00049 return *t; }
00050 static inline table_type all_entries () {
00051 return copy (ptr ()); }
00052 static inline Symbol_rep* get_entry (const C& ref) {
00053 return (inside (ptr ())) -> get (ref); }
00054 static inline void set_entry (const C& ref, Symbol_rep* val) {
00055 (inside (ptr ())) -> set (ref)= val; }
00056 static inline void reset_entry (const C& ref) {
00057 (inside (ptr ())) -> reset (ref); }
00058
00059 private:
00060 C ref;
00061
00062 public:
00063 inline Symbol_rep (const C& ref2): ref (ref2) {
00064 set_entry (ref, this); }
00065 inline ~Symbol_rep () {
00066 reset_entry (ref); }
00067 friend class Symbol;
00068 static void show_symbol_entries () {
00069 mmout << "symbols\t= " << all_entries () << "\n"; }
00070 static nat number_entries () {
00071 return N (all_entries ()); }
00072 static double complexity_entries () {
00073 return complexity (all_entries ()); }
00074 };
00075
00076 TMPL_DEF
00077 class symbol {
00078 INDIRECT_PROTO_2 (symbol, symbol_rep, C, V)
00079 public:
00080 symbol (const C& ref= C()) {
00081 rep= Symbol_rep::get_entry (ref);
00082 if (rep == (Symbol_rep*) NULL) rep= new Symbol_rep (ref);
00083 else INC_COUNT (rep); }
00084 inline const C& operator * () const { return rep->ref; };
00085 friend nat hash LESSGTR (const Symbol& s);
00086 friend bool operator == LESSGTR (const Symbol& s1, const Symbol& s2);
00087 friend bool operator != LESSGTR (const Symbol& s1, const Symbol& s2);
00088 friend nat exact_hash LESSGTR (const Symbol& s);
00089 friend bool exact_eq LESSGTR (const Symbol& s1, const Symbol& s2);
00090 friend bool exact_neq LESSGTR (const Symbol& s1, const Symbol& s2);
00091 };
00092 INDIRECT_IMPL_2 (symbol, symbol_rep, typename C, C, typename V, V)
00093
00094 template<> struct symbolic_type_information<symbol<string> > {
00095 static const nat id= SYMBOLIC_LITERAL; };
00096 template<typename C> struct symbolic_type_information<symbol<vector<C> > > {
00097 static const nat id= SYMBOLIC_COMPOUND; };
00098
00099
00100
00101
00102
00103 TMPL inline nat hash (const Symbol& s) {
00104 return hash (*s); }
00105 TMPL inline bool operator == (const Symbol& s1, const Symbol& s2) {
00106 return (*s1) == (*s2); }
00107 TMPL inline bool operator != (const Symbol& s1, const Symbol& s2) {
00108 return (*s1) != (*s2); }
00109
00110 TMPL inline nat exact_hash (const Symbol& s) {
00111 return as_hash (s.rep); }
00112 TMPL inline bool exact_eq (const Symbol& s1, const Symbol& s2) {
00113 return s1.rep == s2.rep; }
00114 TMPL inline bool exact_neq (const Symbol& s1, const Symbol& s2) {
00115 return s1.rep != s2.rep; }
00116
00117 template<typename C> inline nat hash (const symbol<C,equal_table>& s) {
00118 return exact_hash (s); }
00119 template<typename C> inline bool
00120 operator == (const symbol<C,equal_table>& s1,
00121 const symbol<C,equal_table>& s2) {
00122 return exact_eq (s1, s2); }
00123 template<typename C> inline bool
00124 operator != (const symbol<C,equal_table>& s1,
00125 const symbol<C,equal_table>& s2) {
00126 return exact_neq (s1, s2); }
00127
00128
00129
00130
00131
00132 TMPL void show_symbol_entries () {
00133 Symbol_rep::show_symbol_entries (); }
00134
00135 template<typename C> nat number_entries () {
00136 return symbol_rep<C>::number_entries (); }
00137
00138 template<typename C> double complexity_entries () {
00139 return symbol_rep<C>::complexity_entries (); }
00140
00141 template<typename C> nat discrete_complexity_entries () {
00142 return (nat) symbol_rep<C>::complexity_entries (); }
00143
00144 TMPL inline syntactic flatten (const Symbol& s) {
00145 return apply ("symbol", flatten (*s)); }
00146
00147 TMPL
00148 struct binary_helper<Symbol >: public void_binary_helper<Symbol > {
00149 static inline string short_type_name () {
00150 return "Sy" * Short_type_name (C); }
00151 static inline generic full_type_name () {
00152 return gen ("Symbol", Full_type_name (C)); }
00153 static inline generic disassemble (const Symbol& x) {
00154 return as<generic> (C (*x)); }
00155 static inline Symbol assemble (const generic& x) {
00156 return Symbol (as<C> (x)); }
00157 static inline void write (const port& out, const Symbol& s) {
00158 binary_write<C> (out, *s); }
00159 static inline Symbol read (const port& in) {
00160 return Symbol (binary_read<C> (in)); }
00161 };
00162
00163 #undef TMPL_DEF
00164 #undef TMPL
00165 #undef Symbol
00166 #undef Symbol_rep
00167 }
00168 #endif // __MMX_SYMBOL_HPP