GiNaC 1.8.7
remember.cpp
Go to the documentation of this file.
1
6/*
7 * GiNaC Copyright (C) 1999-2023 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
30namespace GiNaC {
31
33// class remember_table_entry
35
37 : hashvalue(f.gethash()), seq(f.seq), result(r)
38{
41}
42
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;
52 return true;
53}
54
56
58// class remember_table_list
60
61remember_table_list::remember_table_list(unsigned as, unsigned strat)
62{
63 max_assoc_size = as;
64 remember_strategy = strat;
65}
66
67
68void remember_table_list::add_entry(function const & f, ex const & result)
69{
70 if ((max_assoc_size!=0) &&
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) {
78 // delete oldest entry (first in list)
79 erase(begin());
80 break;
81 }
83 // delete least recently used entry
84 auto it = begin();
85 auto 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 }
99 // delete least frequently used entry
100 auto it = begin();
101 auto 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
122bool remember_table_list::lookup_entry(function const & f, ex & result) const
123{
124 auto 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
136// class remember_table
138
140{
141 table_size=0;
144}
145
146remember_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
157bool 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
164void 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
172{
173 clear();
174 init_table();
175}
176
178{
179 reserve(table_size);
180 for (unsigned i=0; i<table_size; ++i)
182}
183
184std::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
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition: assertion.h:33
unsigned gethash() const
Definition: basic.h:272
Lightweight wrapper for GiNaC's symbolic objects.
Definition: ex.h:72
The class function is used to implement builtin functions like sin, cos... and user defined functions...
Definition: function.h:674
@ delete_never
Let table grow undefinitely.
Definition: flags.h:291
@ delete_cyclic
First (oldest) one in list.
Definition: flags.h:294
@ delete_lfu
Least frequently used.
Definition: flags.h:293
@ delete_lru
Least recently used.
Definition: flags.h:292
A single entry in the remember table of a function.
Definition: remember.h:40
exvector seq
Definition: remember.h:50
bool is_equal(function const &f) const
Definition: remember.cpp:43
unsigned hashvalue
Definition: remember.h:49
remember_table_entry(function const &f, ex const &r)
Definition: remember.cpp:36
unsigned successful_hits
Definition: remember.h:53
unsigned long last_access
Definition: remember.h:52
static unsigned long access_counter
Definition: remember.h:54
A list of entries in the remember table having some least significant bits of the hashvalue in common...
Definition: remember.h:59
remember_table_list(unsigned as, unsigned strat)
Definition: remember.cpp:61
void add_entry(function const &f, ex const &result)
Definition: remember.cpp:68
bool lookup_entry(function const &f, ex &result) const
Definition: remember.cpp:122
static std::vector< remember_table > & remember_tables()
Definition: remember.cpp:184
unsigned max_assoc_size
Definition: remember.h:95
void add_entry(function const &f, ex const &result)
Definition: remember.cpp:164
unsigned remember_strategy
Definition: remember.h:96
unsigned table_size
Definition: remember.h:94
bool lookup_entry(function const &f, ex &result) const
Definition: remember.cpp:157
size_t r
Definition: factor.cpp:757
Interface to class of symbolic functions.
Definition: add.cpp:38
unsigned log2(unsigned n)
Integer binary logarithm.
Definition: utils.cpp:48
Interface to helper classes for using the remember option in GiNaC functions.
Interface to several small and furry utilities needed within GiNaC but not of any interest to the use...

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.