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