1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use TokenStream;
use ;
use LruArgs;
/// A macro for creating a static lazy LRU cache with a specified maximum size.
///
/// This macro can be applied to functions to enable memoization using an LRU (Least Recently Used)
/// cache. It will generate a static lazy LRU cache that stores function results based on
/// the provided maximum size. When the cache is full, the least recently used items will be
/// evicted.
///
/// # Example
/// ```ignore
/// #[lru_cache(max = 10)]
/// fn fib(n: u128) -> u128 {
/// if n < 2 {
/// return n;
/// }
/// fib(n - 1) + fib(n - 2)
/// }
///
/// let big_number = fib(186);
/// println!("{}", big_number);
/// // Output: 332825110087067562321196029789634457848
/// ```
/// A macro for automatically adding a memoization table for functions in an **explicit** way
///
/// Memoization is a technique that caches the results of expensive function calls
/// and reuses them when the same inputs occur again.
/// This can significantly improve the performance of recursive or repetitive functions.
///
/// # Usage
/// To use the memo macro, annotate a function with `#[memo]`.
/// The macro will generate code to handle memoization by adding a `&mut HashMap` argument to the function
/// and modifying the function's block to check the cache before performing the computation.
/// ```ignore
/// #[memo]
/// fn fib(n: u128) -> u128 {
/// if n < 2 {
/// return n;
/// }
/// fib(n - 1) + fib(n - 2)
/// }
///
/// let mut memo = HashMap::new();
/// let result = fib(186, &mut memo);
/// println!("{}", result);
/// // 332825110087067562321196029789634457848
/// ```