3 * Implementation of helper classes for using the remember option
4 * in GiNaC functions */
7 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
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.
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.
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
33 // class remember_table_entry
36 remember_table_entry::remember_table_entry(function const & f, ex const & r)
37 : hashvalue(f.gethash()), seq(f.seq), result(r)
39 ++last_access = access_counter;
43 bool remember_table_entry::is_equal(function const & f) const
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;
55 unsigned long remember_table_entry::access_counter = 0;
58 // class remember_table_list
61 remember_table_list::remember_table_list(unsigned as, unsigned strat)
64 remember_strategy = strat;
68 void remember_table_list::add_entry(function const & f, ex const & result)
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
76 switch (remember_strategy) {
77 case remember_strategies::delete_cyclic: {
78 // delete oldest entry (first in list)
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();
89 if ((*it).get_last_access()<lowest_access) {
90 lowest_access = (*it).get_last_access();
91 lowest_access_it = it;
95 erase(lowest_access_it);
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();
105 if ((*it).get_successful_hits()<lowest_hits) {
106 lowest_hits = (*it).get_successful_hits();
111 erase(lowest_hits_it);
115 throw(std::logic_error("remember_table_list::add_entry(): invalid remember_strategy"));
117 GINAC_ASSERT(size()==max_assoc_size-1);
119 push_back(remember_table_entry(f,result));
122 bool remember_table_list::lookup_entry(function const & f, ex & result) const
124 const_iterator i = begin(), iend = end();
126 if (i->is_equal(f)) {
127 result = i->get_result();
136 // class remember_table
139 remember_table::remember_table()
143 remember_strategy=remember_strategies::delete_never;
146 remember_table::remember_table(unsigned s, unsigned as, unsigned strat)
147 : max_assoc_size(as), remember_strategy(strat)
149 // we keep max_assoc_size and remember_strategy if we need to clear
152 // use some power of 2 next to s
153 table_size = 1 << log2(s);
157 bool remember_table::lookup_entry(function const & f, ex & result) const
159 unsigned entry = f.gethash() & (table_size-1);
160 GINAC_ASSERT(entry<size());
161 return operator[](entry).lookup_entry(f,result);
164 void remember_table::add_entry(function const & f, ex const & result)
166 unsigned entry = f.gethash() & (table_size-1);
167 GINAC_ASSERT(entry<size());
168 operator[](entry).add_entry(f,result);
171 void remember_table::clear_all_entries()
177 void remember_table::init_table()
180 for (unsigned i=0; i<table_size; ++i)
181 push_back(remember_table_list(max_assoc_size,remember_strategy));
184 std::vector<remember_table> & remember_table::remember_tables()
186 static std::vector<remember_table> * rt = new std::vector<remember_table>;