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