9b06a464d8d9e33e8b5672fe6705d9d1637a73f1
[ginac.git] / ginac / remember.cpp
1 /** @file remember.cpp
2  *
3  *  Implementation of helper classes for using the remember option
4  *  in GiNaC functions */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2000 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 #include <stdexcept>
25
26 #include "function.h"
27 #include "utils.h" // for log_2
28 #include "remember.h"
29
30 #ifndef NO_NAMESPACE_GINAC
31 namespace GiNaC {
32 #endif // ndef NO_NAMESPACE_GINAC
33
34 //////////
35 // class remember_table_entry
36 //////////
37
38 remember_table_entry::remember_table_entry(function const & f, ex const & r)
39   : hashvalue(f.gethash()), seq(f.seq), result(r)
40 {
41         ++last_access=access_counter;
42         successful_hits=0;
43 }
44
45 bool remember_table_entry::is_equal(function const & f) const
46 {
47         GINAC_ASSERT(f.seq.size()==seq.size());
48         if (f.gethash()!=hashvalue) return false;
49         for (unsigned i=0; i<seq.size(); ++i) {
50                 if (!seq[i].is_equal(f.seq[i])) return false;
51         }
52         ++last_access=access_counter;
53         ++successful_hits;
54         return true;
55 }
56
57 unsigned long remember_table_entry::access_counter=0;
58
59 //////////
60 // class remember_table_list
61 //////////
62
63 remember_table_list::remember_table_list(unsigned as, unsigned strat)
64 {
65         max_assoc_size=as;
66         remember_strategy=strat;
67 }
68
69
70 void remember_table_list::add_entry(function const & f, ex const & result)
71 {
72         if ((max_assoc_size!=0)&&
73                 (remember_strategy!=remember_strategies::delete_never)&&
74                 (size()>=max_assoc_size)) {
75                 // table is full, we must delete an older entry
76                 GINAC_ASSERT(size()>0); // there must be at least one entry
77  
78                 switch (remember_strategy) {
79                 case remember_strategies::delete_cyclic:
80                         // delete oldest entry (first in list)
81                         erase(begin());
82                         break;
83                 case remember_strategies::delete_lru:
84                         {
85                                 // delete least recently used entry
86                                 iterator it=begin();
87                                 iterator lowest_access_it=it;
88                                 unsigned long lowest_access=it->get_last_access();
89                                 ++it;
90                                 while (it!=end()) {
91                                         if (it->get_last_access()<lowest_access) {
92                                                 lowest_access=it->get_last_access();
93                                                 lowest_access_it=it;
94                                         }
95                                         ++it;
96                                 }
97                                 erase(lowest_access_it);
98                         }
99                         break;
100                 case remember_strategies::delete_lfu:
101                         {
102                                 // delete least frequently used entry
103                                 iterator it=begin();
104                                 iterator lowest_hits_it=it;
105                                 unsigned lowest_hits=it->get_successful_hits();
106                                 ++it;
107                                 while (it!=end()) {
108                                         if (it->get_successful_hits()<lowest_hits) {
109                                                 lowest_hits=it->get_successful_hits();
110                                                 lowest_hits_it=it;
111                                         }
112                                         ++it;
113                                 }
114                                 erase(lowest_hits_it);
115                         }
116                         break;
117                 default:
118                         throw(std::logic_error("remember_table_list::add_entry(): invalid remember_strategy"));
119                 }
120                 GINAC_ASSERT(size()==max_assoc_size-1);
121         }
122         push_back(remember_table_entry(f,result));
123 }        
124
125 bool remember_table_list::lookup_entry(function const & f, ex & result) const
126 {
127         for (const_iterator cit=begin(); cit!=end(); ++cit) {
128                 if (cit->is_equal(f)) {
129                         result=cit->get_result();
130                         return true;
131                 }
132         }
133         return false;
134 }
135
136 //////////
137 // class remember_table
138 //////////
139
140 remember_table::remember_table()
141 {
142         table_size=0;
143         max_assoc_size=0;
144         remember_strategy=remember_strategies::delete_never;
145 }
146
147 remember_table::remember_table(unsigned s, unsigned as, unsigned strat)
148   : max_assoc_size(as), remember_strategy(strat)
149 {
150         // we keep max_assoc_size and remember_strategy if we need to clear
151         // all entries
152
153         // use some power of 2 next to s
154         table_size=1 << log2(s);
155         init_table();
156 }
157
158 bool remember_table::lookup_entry(function const & f, ex & result) const
159 {
160         unsigned entry=f.gethash() & (table_size-1);
161         GINAC_ASSERT(entry<size());
162         return operator[](entry).lookup_entry(f,result);
163 }
164
165 void remember_table::add_entry(function const & f, ex const & result)
166 {
167         unsigned entry=f.gethash() & (table_size-1);
168         GINAC_ASSERT(entry<size());
169         operator[](entry).add_entry(f,result);
170 }        
171
172 void remember_table::clear_all_entries(void)
173 {
174         clear();
175         init_table();
176 }
177
178 void remember_table::init_table(void)
179 {
180         reserve(table_size);
181         for (unsigned i=0; i<table_size; ++i) {
182                 push_back(remember_table_list(max_assoc_size,remember_strategy));
183         }
184 }
185
186 std::vector<remember_table> & remember_table::remember_tables(void)
187 {
188         static std::vector<remember_table> * rt = new std::vector<remember_table>;
189         return *rt;
190 }
191
192 #ifndef NO_NAMESPACE_GINAC
193 } // namespace GiNaC
194 #endif // ndef NO_NAMESPACE_GINAC