forked from LearningInfiniTensor/learning-cxx
-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathmain.cpp
More file actions
55 lines (44 loc) · 1.3 KB
/
main.cpp
File metadata and controls
55 lines (44 loc) · 1.3 KB
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
#include "../exercise.h"
#include <cstddef>
// READ: 复制构造函数 <https://zh.cppreference.com/w/cpp/language/copy_constructor>
// READ: 函数定义(显式弃置)<https://zh.cppreference.com/w/cpp/language/function>
class DynFibonacci {
size_t *cache;
int cached;
public:
DynFibonacci(int capacity): cache(new size_t[capacity]), cached(0) {
if (capacity > 0) {
cache[cached++] = 0;
}
if (capacity > 1) {
cache[cached++] = 1;
}
}
DynFibonacci(DynFibonacci const & other) : cache(new size_t[other.cached]), cached(other.cached) {
for (int i = 0; i < cached; ++i) {
cache[i] = other.cache[i];
}
}
~DynFibonacci() {
delete[] cache;
}
size_t get(int i) {
for (; cached <= i; ++cached) {
cache[cached] = cache[cached - 1] + cache[cached - 2];
}
return cache[i];
}
size_t get(int i) const {
if (i <= cached) {
return cache[i];
}
ASSERT(false, "i out of range");
}
};
int main(int argc, char **argv) {
DynFibonacci fib(12);
ASSERT(fib.get(10) == 55, "fibonacci(10) should be 55");
DynFibonacci const fib_ = fib;
ASSERT(fib_.get(10) == fib.get(10), "Object cloned");
return 0;
}