]> www.ginac.de Git - ginac.git/blob - ginac/remember.h
- fix LaTeX-output bug reported by Stefan, remove obsolete has(matrix,ex).
[ginac.git] / ginac / remember.h
1 /** @file remember.h
2  *
3  *  Interface to helper classes for using the remember option
4  *  in GiNaC functions */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #ifndef __GINAC_REMEMBER_H__
25 #define __GINAC_REMEMBER_H__
26
27 #include <vector>
28 #include <list>
29
30 namespace GiNaC {
31
32 class function;
33 class ex;
34         
35 /** A single entry in the remember table of a function.
36  *  Needs to be a friend of class function to access 'seq'.
37  *  'last_access' and 'successful_hits' are updated at each successful
38  *  'is_equal'. */
39 class remember_table_entry {
40 public:
41         remember_table_entry(function const & f, ex const & r);
42         bool is_equal(function const & f) const;
43         ex get_result(void) const { return result; }
44         unsigned long get_last_access(void) const { return last_access; }
45         unsigned long get_successful_hits(void) const { return successful_hits; };
46
47 protected:
48         unsigned hashvalue;
49         exvector seq;
50         ex result;
51         mutable unsigned long last_access;
52         mutable unsigned successful_hits;
53         static unsigned long access_counter;
54 };    
55
56 /** A list of entries in the remember table having some least
57  *  significant bits of the hashvalue in common. */
58 class remember_table_list : public std::list<remember_table_entry> {
59 public:
60         remember_table_list(unsigned as, unsigned strat);
61         void add_entry(function const & f, ex const & result);
62         bool lookup_entry(function const & f, ex & result) const;
63 protected:
64         unsigned max_assoc_size;
65         unsigned remember_strategy;
66 };
67
68 /** The remember table is organized like an n-fold associative cache
69  *  in a microprocessor.  The table has a width of 's' (which is rounded
70  *  to table_size, some power of 2 near 's', internally) and a depth of 'as'
71  *  (unless you choose that entries are never discarded). The place where
72  *  an entry is stored depends on the hashvalue of the parameters of the
73  *  function (this corresponds to the address of byte to be cached).
74  *  The 'log_2(table_size)' least significant bits of this hashvalue
75  *  give the slot in which the entry will be stored or looked up.
76  *  Each slot can take up to 'as' entries. If a slot is full, an older
77  *  entry is removed by one of the following strategies:
78  *   - oldest entry (the first one in the list)
79  *   - least recently used (the one with the lowest 'last_access')
80  *   - least frequently used (the one with the lowest 'successful_hits')
81  *  or all entries are kept which means that the table grows indefinitely. */
82 class remember_table : public std::vector<remember_table_list> {
83 public:
84         remember_table();
85         remember_table(unsigned s, unsigned as, unsigned strat);
86         bool lookup_entry(function const & f, ex & result) const;
87         void add_entry(function const & f, ex const & result);
88         void clear_all_entries(void);
89         void show_statistics(std::ostream & os, unsigned level) const;
90         static std::vector<remember_table> & remember_tables(void);
91 protected:
92         void init_table(void);
93         unsigned table_size;
94         unsigned max_assoc_size;
95         unsigned remember_strategy;
96 };      
97
98 } // namespace GiNaC
99
100 #endif // ndef __GINAC_REMEMBER_H__