GiNaC 1.8.10
remember.cpp
Go to the documentation of this file.
1
6/*
7 * GiNaC Copyright (C) 1999-2026 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#include "function.h"
24#include "utils.h"
25#include "remember.h"
26
27#include <stdexcept>
28
29namespace GiNaC {
30
32// class remember_table_entry
34
36 : hashvalue(f.gethash()), seq(f.seq), result(r)
37{
40}
41
43{
44 GINAC_ASSERT(f.seq.size()==seq.size());
45 if (f.gethash()!=hashvalue) return false;
46 size_t num = seq.size();
47 for (size_t i=0; i<num; ++i)
48 if (!seq[i].is_equal(f.seq[i])) return false;
51 return true;
52}
53
55
57// class remember_table_list
59
60remember_table_list::remember_table_list(unsigned as, unsigned strat)
61{
62 max_assoc_size = as;
63 remember_strategy = strat;
64}
65
66
67void remember_table_list::add_entry(function const & f, ex const & result)
68{
69 if ((max_assoc_size!=0) &&
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) {
77 // delete oldest entry (first in list)
78 erase(begin());
79 break;
80 }
82 // delete least recently used entry
83 auto it = begin();
84 auto 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 }
98 // delete least frequently used entry
99 auto it = begin();
100 auto 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
121bool remember_table_list::lookup_entry(function const & f, ex & result) const
122{
123 auto i = begin(), iend = end();
124 while (i != iend) {
125 if (i->is_equal(f)) {
126 result = i->get_result();
127 return true;
128 }
129 ++i;
130 }
131 return false;
132}
133
135// class remember_table
137
144
145remember_table::remember_table(unsigned s, unsigned as, unsigned strat)
146 : max_assoc_size(as), remember_strategy(strat)
147{
148 // we keep max_assoc_size and remember_strategy if we need to clear
149 // all entries
150
151 // use some power of 2 next to s
152 table_size = 1 << log2(s);
153 init_table();
154}
155
156bool remember_table::lookup_entry(function const & f, ex & result) const
157{
158 unsigned entry = f.gethash() & (table_size-1);
159 GINAC_ASSERT(entry<size());
160 return operator[](entry).lookup_entry(f,result);
161}
162
163void remember_table::add_entry(function const & f, ex const & result)
164{
165 unsigned entry = f.gethash() & (table_size-1);
166 GINAC_ASSERT(entry<size());
167 operator[](entry).add_entry(f,result);
168}
169
171{
172 clear();
173 init_table();
174}
175
177{
178 reserve(table_size);
179 for (unsigned i=0; i<table_size; ++i)
181}
182
183std::vector<remember_table> & remember_table::remember_tables()
184{
185 static std::vector<remember_table> rt = std::vector<remember_table>();
186 return rt;
187}
188
189} // namespace GiNaC
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:32
unsigned gethash() const
Definition basic.h:271
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:673
@ delete_never
Let table grow undefinitely.
Definition flags.h:290
@ delete_cyclic
First (oldest) one in list.
Definition flags.h:293
@ delete_lfu
Least frequently used.
Definition flags.h:292
@ delete_lru
Least recently used.
Definition flags.h:291
A single entry in the remember table of a function.
Definition remember.h:39
exvector seq
Definition remember.h:49
bool is_equal(function const &f) const
Definition remember.cpp:42
unsigned hashvalue
Definition remember.h:48
remember_table_entry(function const &f, ex const &r)
Definition remember.cpp:35
unsigned successful_hits
Definition remember.h:52
unsigned long last_access
Definition remember.h:51
static unsigned long access_counter
Definition remember.h:53
A list of entries in the remember table having some least significant bits of the hashvalue in common...
Definition remember.h:58
remember_table_list(unsigned as, unsigned strat)
Definition remember.cpp:60
void add_entry(function const &f, ex const &result)
Definition remember.cpp:67
bool lookup_entry(function const &f, ex &result) const
Definition remember.cpp:121
static std::vector< remember_table > & remember_tables()
Definition remember.cpp:183
unsigned max_assoc_size
Definition remember.h:94
void add_entry(function const &f, ex const &result)
Definition remember.cpp:163
unsigned remember_strategy
Definition remember.h:95
bool lookup_entry(function const &f, ex &result) const
Definition remember.cpp:156
size_t r
Definition factor.cpp:756
Interface to class of symbolic functions.
Definition add.cpp:35
unsigned log2(unsigned n)
Integer binary logarithm.
Definition utils.cpp:47
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.