/** @file remember.cpp * * Implementation of helper classes for using the remember option * in GiNaC functions */ /* * GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include "function.h" #include "utils.h" #include "remember.h" namespace GiNaC { ////////// // class remember_table_entry ////////// remember_table_entry::remember_table_entry(function const & f, ex const & r) : hashvalue(f.gethash()), seq(f.seq), result(r) { ++last_access = access_counter; successful_hits = 0; } bool remember_table_entry::is_equal(function const & f) const { GINAC_ASSERT(f.seq.size()==seq.size()); if (f.gethash()!=hashvalue) return false; size_t num = seq.size(); for (size_t i=0; i=max_assoc_size)) { // table is full, we must delete an older entry GINAC_ASSERT(size()>0); // there must be at least one entry switch (remember_strategy) { case remember_strategies::delete_cyclic: { // delete oldest entry (first in list) erase(begin()); break; } case remember_strategies::delete_lru: { // delete least recently used entry iterator it = begin(); iterator lowest_access_it = it; unsigned long lowest_access = (*it).get_last_access(); ++it; while (it!=end()) { if ((*it).get_last_access()is_equal(f)) { result = i->get_result(); return true; } ++i; } return false; } ////////// // class remember_table ////////// remember_table::remember_table() { table_size=0; max_assoc_size=0; remember_strategy=remember_strategies::delete_never; } remember_table::remember_table(unsigned s, unsigned as, unsigned strat) : max_assoc_size(as), remember_strategy(strat) { // we keep max_assoc_size and remember_strategy if we need to clear // all entries // use some power of 2 next to s table_size = 1 << log2(s); init_table(); } bool remember_table::lookup_entry(function const & f, ex & result) const { unsigned entry = f.gethash() & (table_size-1); GINAC_ASSERT(entry & remember_table::remember_tables() { static std::vector * rt = new std::vector; return *rt; } } // namespace GiNaC