-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLazyTable.cpp.html
109 lines (80 loc) · 5.1 KB
/
LazyTable.cpp.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>/Volumes/Unix/unix-files.noindex/ntl-new/ntl-9.6.0/doc/LazyTable.cpp.html</title>
<meta name="Generator" content="Vim/7.3">
<meta name="plugin-version" content="vim7.3_v6">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css">
<style type="text/css">
<!--
pre { font-family: monospace; color: #000000; background-color: #ffffff; }
body { font-family: monospace; color: #000000; background-color: #ffffff; }
.Statement { color: #b03060; font-weight: bold; }
.Type { color: #008b00; font-weight: bold; }
.Comment { color: #0000ee; font-style: italic; }
-->
</style>
</head>
<body>
<pre>
<span class="Comment">/*</span><span class="Comment">**************************************************************************</span>
<span class="Comment">LazyTable<T,MAX>: template class for lazy initialization of objects whose</span>
<span class="Comment">values do not change after initialization. In a multi-threaded environment,</span>
<span class="Comment">this makes use of "double checked locking" for an efficient, thread-safe</span>
<span class="Comment">solution.</span>
<span class="Comment">Usage:</span>
<span class="Comment"> LazyTable<T,MAX> tab; // declaration of the lazy table, </span>
<span class="Comment"> // with max size == MAX</span>
<span class="Comment"> ...</span>
<span class="Comment"> do {</span>
<span class="Comment"> LazyTable<T,MAX>::Builder builder(tab, n); // request length n</span>
<span class="Comment"> long amt = builder.amt();</span>
<span class="Comment"> if (!amt) break; </span>
<span class="Comment"> ... initialize elements i = n-amt..n-1 </span>
<span class="Comment"> using builder.move(p), where p is a UnqiuePtr<T></span>
<span class="Comment"> note that each move application appends one element</span>
<span class="Comment"> </span>
<span class="Comment"> } while(0); // When this scope closes, </span>
<span class="Comment"> // the table is fully initialized to length n</span>
<span class="Comment"> const T* val = table[i]; // read-only access to table elements 0..n-1</span>
<span class="Comment"> </span>
<span class="Comment">It is important to follow this recipe carefully. In particular,</span>
<span class="Comment">the builder must be enclosed in a scope, as it's destructor</span>
<span class="Comment">plays a crucial role in finalizing the initialization.</span>
<span class="Comment">***************************************************************************</span><span class="Comment">*/</span>
<span class="Type">template</span><<span class="Type">class</span> T, <span class="Type">long</span> MAX>
<span class="Type">class</span> LazyTable {
<span class="Statement">public</span>:
LazyTable();
~LazyTable();
<span class="Type">const</span> T * <span class="Type">const</span> <span class="Statement">operator</span>[] (<span class="Type">long</span> i) <span class="Type">const</span>;
<span class="Comment">// element access -- currently no range checking</span>
<span class="Type">long</span> length() <span class="Type">const</span>;
<span class="Comment">// current table length</span>
<span class="Type">class</span> Builder {
Builder(<span class="Type">const</span> LazyTable&, <span class="Type">long</span> request);
<span class="Comment">// EXCEPTIONS: may throw an exception if request is out of range</span>
<span class="Comment">// or if alocation of table fails</span>
~Builder()
<span class="Type">long</span> amt() <span class="Type">const</span>;
<span class="Type">void</span> move(UniquePtr<T>& p);
<span class="Comment">// EXCEPTIONS: throws exception of move is not allowed.</span>
<span class="Comment">// Provides strong ES guarantee.</span>
};
<span class="Statement">private</span>:
LazyTable(<span class="Type">const</span> LazyTable&); <span class="Comment">// disabled</span>
LazyTable& <span class="Statement">operator</span>=(<span class="Type">const</span> LazyTable&);
};
<span class="Comment">// EXCEPTIONS: except where noted, no exceptions are thrown</span>
<span class="Comment">// NOTE: For more on double-checked locking, see</span>
<span class="Comment">// <a href="http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/">http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/</a></span>
<span class="Comment">// NOTE: when compiled with the NTL_THREADS option, the LazyTable</span>
<span class="Comment">// class may contain data members from the standard library</span>
<span class="Comment">// that may not satisfy the requirements of the Vec class</span>
<span class="Comment">// (i.e., relocatability). One can wrap it in a pointer </span>
<span class="Comment">// class (e.g., OptionalVal) to deal with this.</span>
</pre>
</body>
</html>