Back to index

texmacs  1.0.7.15
memorizer.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : memorizer.hpp
00004 * DESCRIPTION: memorizing computations during style rewriting
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef MEMORIZER_H
00013 #define MEMORIZER_H
00014 #include "tree.hpp"
00015 
00016 class environment;
00017 
00018 #define MEMORIZE_EVALUATE      0
00019 #define MEMORIZE_REWRITE       1
00020 #define MEMORIZE_INACTIVE      2
00021 
00022 #define MEMORIZE_ASSIGN        10
00023 #define MEMORIZE_BEGIN_WITH    11
00024 #define MEMORIZE_END_WITH      12
00025 #define MEMORIZE_MACRO_DOWN    13
00026 #define MEMORIZE_MACRO_REDOWN  14
00027 #define MEMORIZE_MACRO_UP      15
00028 
00029 /******************************************************************************
00030 * Abstract memorizers
00031 ******************************************************************************/
00032 
00033 extern int memorizer_count;
00034 class memorizer;
00035 class memorizer_rep: public abstract_struct {
00036 public:
00037   inline memorizer_rep () { TM_DEBUG (memorizer_count++); }
00038   inline virtual ~memorizer_rep () { TM_DEBUG (memorizer_count--); }
00039 
00040   virtual void print (tm_ostream& out) = 0;
00041   virtual int  type () = 0;
00042   virtual int  hash () = 0;
00043   virtual bool equal (memorizer_rep* mem) = 0;
00044 
00045   virtual void compute ();
00046   virtual void set_children (memorizer* a, int n);
00047   virtual void get_children (memorizer*& a, int& n);
00048   virtual void set_tree (tree t);
00049   virtual tree get_tree ();
00050   virtual void set_environment (environment env);
00051   virtual environment get_environment ();
00052 };
00053 
00054 class memorizer {
00055 public: // should rebecome private
00056   memorizer_rep* rep;
00057 public:
00058   inline memorizer (): rep (NULL) {}
00059   inline memorizer (const memorizer& mem): rep (mem.rep) {
00060     if (rep != NULL) {
00061       //cout << "copy " << rep << LF;
00062       rep->ref_count++; } }
00063   memorizer (memorizer_rep* rep);
00064   ~memorizer ();
00065   memorizer& operator = (memorizer mem);
00066   inline memorizer_rep* operator -> () { return rep; }
00067   inline friend bool is_memorized (const memorizer& mem) {
00068     return mem.rep->ref_count >= 3; }
00069   inline friend bool operator == (memorizer o1, memorizer o2) {
00070     return o1.rep->type () == o2.rep->type () && o1.rep->equal (o2.rep); }
00071   inline friend bool operator != (memorizer o1, memorizer o2) {
00072     return o1.rep->type () != o2.rep->type () || !o1.rep->equal (o2.rep); }
00073   inline friend int hash (memorizer o1) {
00074     return o1.rep->hash (); }
00075   inline friend tm_ostream& operator << (tm_ostream& out, memorizer mem) {
00076     mem->print (out); return out; }
00077 };
00078 
00079 /******************************************************************************
00080 * Compound memorizers
00081 ******************************************************************************/
00082 
00083 class compound_memorizer_rep: public memorizer_rep {
00084 protected:
00085   memorizer* a;
00086   int n;
00087 public:
00088   inline compound_memorizer_rep (): memorizer_rep (), a (NULL), n (0) {}
00089   inline ~compound_memorizer_rep () { if (n!=0) tm_delete_array (a); }
00090   void set_children (memorizer* a, int n);
00091   void get_children (memorizer*& a, int& n);
00092 };
00093 
00094 void print_tree (memorizer mem);
00095 
00096 /******************************************************************************
00097 * Public interface
00098 ******************************************************************************/
00099 
00100 void memorize_initialize ();
00101 memorizer memorize_finalize ();
00102 void memorize_start ();
00103 void memorize_end ();
00104 
00105 #endif // defined MEMORIZER_H