-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespaces.cpp
125 lines (111 loc) · 3.74 KB
/
namespaces.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @file namespaces.cpp
* @author Eliot Hall
* @brief Introduction to namespaces
* @version 0.1
* @date 2023-07-21
*
* @copyright Copyright (c) 2023
*
* @details
* Namespaces are collections of functions, classes, and variables
* that have been grouped together. A good way to think of namespaces is to think
* of them as scopes. There's the global namespace (or global scope), and there
* can be custom namespaces in which variables, functions, etc exist.
*/
#include <iostream>
#include <unordered_map>
// here, we say that we want to use all the functions / classes / etc
// from the std namespace as if they were in the global namespace
using namespace std;
// (imaginary definition of cout and endl and iostream)
// this is would be what you'd do in a header file
// that way it doesn't bring the full namespace
// into any file that includes the header file
using std::cout;
using std::endl;
// inaccessible namespace
// since there's no name that you can use
// to access all the functions/variables w/i the namespace
namespace
{
string secretSauce = "secret!!";
};
// namespaces are declared with the keyword `namespace`. Their contents
// goes in the following brackets (which should be ended with a semicolon)
// this namespace contains multiple fibonacci functions, each
// with a different implementation
namespace fibonacci
{
// nested namespace
namespace fibonacciprivate
{
// cache that stores fibonacci numbers;
// this isn't actually a global variable, rather, it
// is a variable that exists inside the `fibonacci` namespace
unordered_map<int, int> fibonacciCache;
}
// basic recursive fibonacci
int fibonacciRecursive(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
// fibonacci, but it
// uses the cache to calculate fibonacci numbers
int fibonacciCached(int n)
{
// if n already is within the cache, just
// return that value
if (fibonacciprivate::fibonacciCache.find(n) != fibonacciprivate::fibonacciCache.end())
{
return fibonacciprivate::fibonacciCache[n];
}
int ret = 0;
if (n == 0)
{
ret = 0;
}
else if (n == 1)
{
ret = 1;
}
else
{
// calculate the value
ret = fibonacciCached(n - 1) + fibonacciCached(n - 2);
}
// store the calculated value
fibonacciprivate::fibonacciCache[n] = ret;
return ret;
}
}; // namespace fibonacci
int main()
{
int times = 35;
// :: is used to specify that we are referring to a member of the namespace
// equivalent to doing numpy.array
// namespace::variable / function
cout << fibonacci::fibonacciRecursive(times) << endl;
// using directives can go within functions too!!
// here, we say we want to use fibonacci's fibonacciCached as if it was "in the global namespace"
using fibonacci::fibonacciCached;
cout << fibonacciCached(times) << endl;
// std::cout and std::endl are what we are really
// using above, but because we used the `using` directive,
// we can use them as if they were in the global namespace
// trying to access the members of a namespace without either specifying the namespace
// or using a `using` directive would lead to the same type of error you would get
// if it wasn't defined at all --- because, for the compiler, it is undefined since
// the compiler doesn't know where that function comes from
// try it:
// cout << fibonacciRecursive(times) << endl; // compiler error
return 0;
}