Mercurial > octave
annotate scripts/miscellaneous/private/__memoize__.m @ 31245:a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
* scripts/miscellaneous/clearAllMemoizedCaches.m: new function.
* scripts/miscellaneous/memoize.m: new function.
* scripts/miscellaneous/private/__memoize__.m: new function.
* scripts/miscellaneous/module.mk: add new functions to build system.
* scripts/+matlab/+lang/MemoizedFunction.m: new function.
* scripts/+matlab/+lang/module.mk: add new functions to build system.
author | Guillaume Flandin <guillaume.offline@gmail.com> |
---|---|
date | Tue, 27 Sep 2022 16:12:45 -0400 |
parents | |
children | a40c0b7aa376 |
rev | line source |
---|---|
31245
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
1 ######################################################################## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
2 ## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
3 ## Copyright (C) 2021 The Octave Project Developers |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
4 ## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
7 ## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
8 ## This file is part of Octave. |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
9 ## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
11 ## under the terms of the GNU General Public License as published by |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
13 ## (at your option) any later version. |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
14 ## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
15 ## Octave is distributed in the hope that it will be useful, but |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
18 ## GNU General Public License for more details. |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
19 ## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
20 ## You should have received a copy of the GNU General Public License |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
21 ## along with Octave; see the file COPYING. If not, see |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
23 ## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
24 ######################################################################## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
25 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
26 ## -*- texinfo -*- |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
27 ## @deftypefn {} {@var{mem_fcn_handle} =} __memoize__ (@var{fcn_handle}) |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
28 ## @deftypefn {} {} __memoize__ () |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
29 ## Internal function used by memoize. |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
30 ## |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
31 ## @seealso{clearAllMemoizedCaches, memoize} |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
32 ## @end deftypefn |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
33 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
34 function mem_fcn_handle = __memoize__ (fcn_handle) |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
35 persistent cached_mem_fcn_handle |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
36 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
37 if (nargin) |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
38 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
39 for i=1:numel (cached_mem_fcn_handle) |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
40 if (isequal (cached_mem_fcn_handle{i}.Function, fcn_handle)) |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
41 mem_fcn_handle = cached_mem_fcn_handle{i}; |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
42 return; |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
43 endif |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
44 endfor |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
45 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
46 mem_fcn_handle = matlab.lang.MemoizedFunction (fcn_handle); |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
47 cached_mem_fcn_handle{end+1} = mem_fcn_handle; |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
48 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
49 else |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
50 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
51 for i=1:numel (cached_mem_fcn_handle) |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
52 clearCache (cached_mem_fcn_handle{i}); |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
53 endfor |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
54 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
55 endif |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
56 |
a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
Guillaume Flandin <guillaume.offline@gmail.com>
parents:
diff
changeset
|
57 endfunction |