lemongraph 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +8 -0
  3. data/LICENSE +674 -0
  4. data/README.md +6 -0
  5. data/ext/lemon-1.3.1/AUTHORS +26 -0
  6. data/ext/lemon-1.3.1/CMakeLists.txt +371 -0
  7. data/ext/lemon-1.3.1/INSTALL +167 -0
  8. data/ext/lemon-1.3.1/LICENSE +32 -0
  9. data/ext/lemon-1.3.1/NEWS +337 -0
  10. data/ext/lemon-1.3.1/README +50 -0
  11. data/ext/lemon-1.3.1/cmake/FindCOIN.cmake +110 -0
  12. data/ext/lemon-1.3.1/cmake/FindGLPK.cmake +55 -0
  13. data/ext/lemon-1.3.1/cmake/FindGhostscript.cmake +10 -0
  14. data/ext/lemon-1.3.1/cmake/FindILOG.cmake +102 -0
  15. data/ext/lemon-1.3.1/cmake/FindSOPLEX.cmake +23 -0
  16. data/ext/lemon-1.3.1/cmake/LEMONConfig.cmake.in +13 -0
  17. data/ext/lemon-1.3.1/cmake/nsis/lemon.ico +0 -0
  18. data/ext/lemon-1.3.1/cmake/nsis/uninstall.ico +0 -0
  19. data/ext/lemon-1.3.1/cmake/version.cmake +1 -0
  20. data/ext/lemon-1.3.1/cmake/version.cmake.in +1 -0
  21. data/ext/lemon-1.3.1/contrib/CMakeLists.txt +19 -0
  22. data/ext/lemon-1.3.1/lemon/CMakeLists.txt +91 -0
  23. data/ext/lemon-1.3.1/lemon/adaptors.h +3638 -0
  24. data/ext/lemon-1.3.1/lemon/arg_parser.cc +474 -0
  25. data/ext/lemon-1.3.1/lemon/arg_parser.h +440 -0
  26. data/ext/lemon-1.3.1/lemon/assert.h +214 -0
  27. data/ext/lemon-1.3.1/lemon/base.cc +37 -0
  28. data/ext/lemon-1.3.1/lemon/bellman_ford.h +1116 -0
  29. data/ext/lemon-1.3.1/lemon/bfs.h +1754 -0
  30. data/ext/lemon-1.3.1/lemon/bin_heap.h +347 -0
  31. data/ext/lemon-1.3.1/lemon/binomial_heap.h +445 -0
  32. data/ext/lemon-1.3.1/lemon/bits/alteration_notifier.h +472 -0
  33. data/ext/lemon-1.3.1/lemon/bits/array_map.h +351 -0
  34. data/ext/lemon-1.3.1/lemon/bits/bezier.h +174 -0
  35. data/ext/lemon-1.3.1/lemon/bits/default_map.h +182 -0
  36. data/ext/lemon-1.3.1/lemon/bits/edge_set_extender.h +627 -0
  37. data/ext/lemon-1.3.1/lemon/bits/enable_if.h +131 -0
  38. data/ext/lemon-1.3.1/lemon/bits/graph_adaptor_extender.h +401 -0
  39. data/ext/lemon-1.3.1/lemon/bits/graph_extender.h +1332 -0
  40. data/ext/lemon-1.3.1/lemon/bits/lock.h +65 -0
  41. data/ext/lemon-1.3.1/lemon/bits/map_extender.h +332 -0
  42. data/ext/lemon-1.3.1/lemon/bits/path_dump.h +177 -0
  43. data/ext/lemon-1.3.1/lemon/bits/solver_bits.h +194 -0
  44. data/ext/lemon-1.3.1/lemon/bits/traits.h +388 -0
  45. data/ext/lemon-1.3.1/lemon/bits/variant.h +494 -0
  46. data/ext/lemon-1.3.1/lemon/bits/vector_map.h +244 -0
  47. data/ext/lemon-1.3.1/lemon/bits/windows.cc +166 -0
  48. data/ext/lemon-1.3.1/lemon/bits/windows.h +44 -0
  49. data/ext/lemon-1.3.1/lemon/bucket_heap.h +594 -0
  50. data/ext/lemon-1.3.1/lemon/capacity_scaling.h +1014 -0
  51. data/ext/lemon-1.3.1/lemon/cbc.cc +460 -0
  52. data/ext/lemon-1.3.1/lemon/cbc.h +129 -0
  53. data/ext/lemon-1.3.1/lemon/christofides_tsp.h +254 -0
  54. data/ext/lemon-1.3.1/lemon/circulation.h +807 -0
  55. data/ext/lemon-1.3.1/lemon/clp.cc +464 -0
  56. data/ext/lemon-1.3.1/lemon/clp.h +164 -0
  57. data/ext/lemon-1.3.1/lemon/color.cc +44 -0
  58. data/ext/lemon-1.3.1/lemon/color.h +204 -0
  59. data/ext/lemon-1.3.1/lemon/concept_check.h +77 -0
  60. data/ext/lemon-1.3.1/lemon/concepts/bpgraph.h +1029 -0
  61. data/ext/lemon-1.3.1/lemon/concepts/digraph.h +491 -0
  62. data/ext/lemon-1.3.1/lemon/concepts/graph.h +788 -0
  63. data/ext/lemon-1.3.1/lemon/concepts/graph_components.h +2134 -0
  64. data/ext/lemon-1.3.1/lemon/concepts/heap.h +324 -0
  65. data/ext/lemon-1.3.1/lemon/concepts/maps.h +223 -0
  66. data/ext/lemon-1.3.1/lemon/concepts/path.h +312 -0
  67. data/ext/lemon-1.3.1/lemon/config.h.in +22 -0
  68. data/ext/lemon-1.3.1/lemon/connectivity.h +1688 -0
  69. data/ext/lemon-1.3.1/lemon/core.h +2506 -0
  70. data/ext/lemon-1.3.1/lemon/cost_scaling.h +1607 -0
  71. data/ext/lemon-1.3.1/lemon/counter.h +249 -0
  72. data/ext/lemon-1.3.1/lemon/cplex.cc +994 -0
  73. data/ext/lemon-1.3.1/lemon/cplex.h +292 -0
  74. data/ext/lemon-1.3.1/lemon/cycle_canceling.h +1230 -0
  75. data/ext/lemon-1.3.1/lemon/dfs.h +1637 -0
  76. data/ext/lemon-1.3.1/lemon/dheap.h +352 -0
  77. data/ext/lemon-1.3.1/lemon/dijkstra.h +1303 -0
  78. data/ext/lemon-1.3.1/lemon/dim2.h +726 -0
  79. data/ext/lemon-1.3.1/lemon/dimacs.h +448 -0
  80. data/ext/lemon-1.3.1/lemon/edge_set.h +1420 -0
  81. data/ext/lemon-1.3.1/lemon/edmonds_karp.h +556 -0
  82. data/ext/lemon-1.3.1/lemon/elevator.h +982 -0
  83. data/ext/lemon-1.3.1/lemon/error.h +276 -0
  84. data/ext/lemon-1.3.1/lemon/euler.h +287 -0
  85. data/ext/lemon-1.3.1/lemon/fib_heap.h +475 -0
  86. data/ext/lemon-1.3.1/lemon/fractional_matching.h +2139 -0
  87. data/ext/lemon-1.3.1/lemon/full_graph.h +1082 -0
  88. data/ext/lemon-1.3.1/lemon/glpk.cc +1012 -0
  89. data/ext/lemon-1.3.1/lemon/glpk.h +263 -0
  90. data/ext/lemon-1.3.1/lemon/gomory_hu.h +568 -0
  91. data/ext/lemon-1.3.1/lemon/graph_to_eps.h +1186 -0
  92. data/ext/lemon-1.3.1/lemon/greedy_tsp.h +251 -0
  93. data/ext/lemon-1.3.1/lemon/grid_graph.h +699 -0
  94. data/ext/lemon-1.3.1/lemon/grosso_locatelli_pullan_mc.h +840 -0
  95. data/ext/lemon-1.3.1/lemon/hao_orlin.h +1015 -0
  96. data/ext/lemon-1.3.1/lemon/hartmann_orlin_mmc.h +654 -0
  97. data/ext/lemon-1.3.1/lemon/howard_mmc.h +651 -0
  98. data/ext/lemon-1.3.1/lemon/hypercube_graph.h +459 -0
  99. data/ext/lemon-1.3.1/lemon/insertion_tsp.h +533 -0
  100. data/ext/lemon-1.3.1/lemon/karp_mmc.h +590 -0
  101. data/ext/lemon-1.3.1/lemon/kruskal.h +324 -0
  102. data/ext/lemon-1.3.1/lemon/lemon.pc.in +10 -0
  103. data/ext/lemon-1.3.1/lemon/lgf_reader.h +3854 -0
  104. data/ext/lemon-1.3.1/lemon/lgf_writer.h +2687 -0
  105. data/ext/lemon-1.3.1/lemon/list_graph.h +2510 -0
  106. data/ext/lemon-1.3.1/lemon/lp.h +95 -0
  107. data/ext/lemon-1.3.1/lemon/lp_base.cc +30 -0
  108. data/ext/lemon-1.3.1/lemon/lp_base.h +2147 -0
  109. data/ext/lemon-1.3.1/lemon/lp_skeleton.cc +143 -0
  110. data/ext/lemon-1.3.1/lemon/lp_skeleton.h +234 -0
  111. data/ext/lemon-1.3.1/lemon/maps.h +4057 -0
  112. data/ext/lemon-1.3.1/lemon/matching.h +3505 -0
  113. data/ext/lemon-1.3.1/lemon/math.h +77 -0
  114. data/ext/lemon-1.3.1/lemon/max_cardinality_search.h +794 -0
  115. data/ext/lemon-1.3.1/lemon/min_cost_arborescence.h +808 -0
  116. data/ext/lemon-1.3.1/lemon/nagamochi_ibaraki.h +702 -0
  117. data/ext/lemon-1.3.1/lemon/nauty_reader.h +113 -0
  118. data/ext/lemon-1.3.1/lemon/nearest_neighbor_tsp.h +238 -0
  119. data/ext/lemon-1.3.1/lemon/network_simplex.h +1659 -0
  120. data/ext/lemon-1.3.1/lemon/opt2_tsp.h +367 -0
  121. data/ext/lemon-1.3.1/lemon/pairing_heap.h +474 -0
  122. data/ext/lemon-1.3.1/lemon/path.h +1164 -0
  123. data/ext/lemon-1.3.1/lemon/planarity.h +2754 -0
  124. data/ext/lemon-1.3.1/lemon/preflow.h +985 -0
  125. data/ext/lemon-1.3.1/lemon/quad_heap.h +343 -0
  126. data/ext/lemon-1.3.1/lemon/radix_heap.h +438 -0
  127. data/ext/lemon-1.3.1/lemon/radix_sort.h +487 -0
  128. data/ext/lemon-1.3.1/lemon/random.cc +29 -0
  129. data/ext/lemon-1.3.1/lemon/random.h +1005 -0
  130. data/ext/lemon-1.3.1/lemon/smart_graph.h +1344 -0
  131. data/ext/lemon-1.3.1/lemon/soplex.cc +465 -0
  132. data/ext/lemon-1.3.1/lemon/soplex.h +158 -0
  133. data/ext/lemon-1.3.1/lemon/static_graph.h +476 -0
  134. data/ext/lemon-1.3.1/lemon/suurballe.h +776 -0
  135. data/ext/lemon-1.3.1/lemon/time_measure.h +610 -0
  136. data/ext/lemon-1.3.1/lemon/tolerance.h +242 -0
  137. data/ext/lemon-1.3.1/lemon/unionfind.h +1824 -0
  138. data/ext/lemon-1.3.1/scripts/unify-sources.sh +390 -0
  139. data/ext/lemon-1.3.1/scripts/valgrind-wrapper.sh +22 -0
  140. data/ext/lemongraph/arc_map.cc +1007 -0
  141. data/ext/lemongraph/digraph.cc +282 -0
  142. data/ext/lemongraph/digraph_arc.cc +153 -0
  143. data/ext/lemongraph/digraph_node.cc +277 -0
  144. data/ext/lemongraph/edge_map.cc +770 -0
  145. data/ext/lemongraph/extconf.rb +53 -0
  146. data/ext/lemongraph/graph.cc +351 -0
  147. data/ext/lemongraph/graph_arc.cc +95 -0
  148. data/ext/lemongraph/graph_edge.cc +153 -0
  149. data/ext/lemongraph/graph_item.cc +76 -0
  150. data/ext/lemongraph/graph_node.cc +321 -0
  151. data/ext/lemongraph/lemongraph.cc +260 -0
  152. data/ext/lemongraph/lemongraph.hh +295 -0
  153. data/ext/lemongraph/lemongraph.map +6 -0
  154. data/ext/lemongraph/lemongraph_export.hh +31 -0
  155. data/ext/lemongraph/node_map.cc +1011 -0
  156. data/lemongraph.gemspec +176 -0
  157. data/lib/lemongraph/graphviz.rb +240 -0
  158. data/lib/lemongraph/version.rb +4 -0
  159. data/lib/lemongraph.rb +21 -0
  160. data/samples/lemondeps.rb +38 -0
  161. metadata +202 -0
@@ -0,0 +1,610 @@
1
+ /* -*- mode: C++; indent-tabs-mode: nil; -*-
2
+ *
3
+ * This file is a part of LEMON, a generic C++ optimization library.
4
+ *
5
+ * Copyright (C) 2003-2013
6
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
+ *
9
+ * Permission to use, modify and distribute this software is granted
10
+ * provided that this copyright notice appears in all copies. For
11
+ * precise terms see the accompanying LICENSE file.
12
+ *
13
+ * This software is provided "AS IS" with no warranty of any kind,
14
+ * express or implied, and with no claim as to its suitability for any
15
+ * purpose.
16
+ *
17
+ */
18
+
19
+ #ifndef LEMON_TIME_MEASURE_H
20
+ #define LEMON_TIME_MEASURE_H
21
+
22
+ ///\ingroup timecount
23
+ ///\file
24
+ ///\brief Tools for measuring cpu usage
25
+
26
+ #ifdef WIN32
27
+ #include <lemon/bits/windows.h>
28
+ #else
29
+ #include <unistd.h>
30
+ #include <sys/times.h>
31
+ #include <sys/time.h>
32
+ #endif
33
+
34
+ #include <string>
35
+ #include <fstream>
36
+ #include <iostream>
37
+ #include <lemon/math.h>
38
+
39
+ namespace lemon {
40
+
41
+ /// \addtogroup timecount
42
+ /// @{
43
+
44
+ /// A class to store (cpu)time instances.
45
+
46
+ /// This class stores five time values.
47
+ /// - a real time
48
+ /// - a user cpu time
49
+ /// - a system cpu time
50
+ /// - a user cpu time of children
51
+ /// - a system cpu time of children
52
+ ///
53
+ /// TimeStamp's can be added to or substracted from each other and
54
+ /// they can be pushed to a stream.
55
+ ///
56
+ /// In most cases, perhaps the \ref Timer or the \ref TimeReport
57
+ /// class is what you want to use instead.
58
+
59
+ class TimeStamp
60
+ {
61
+ double utime;
62
+ double stime;
63
+ double cutime;
64
+ double cstime;
65
+ double rtime;
66
+
67
+ public:
68
+ ///Display format specifier
69
+
70
+ ///\e
71
+ ///
72
+ enum Format {
73
+ /// Reports all measured values
74
+ NORMAL = 0,
75
+ /// Only real time and an error indicator is displayed
76
+ SHORT = 1
77
+ };
78
+
79
+ private:
80
+ static Format _format;
81
+
82
+ void _reset() {
83
+ utime = stime = cutime = cstime = rtime = 0;
84
+ }
85
+
86
+ public:
87
+
88
+ ///Set output format
89
+
90
+ ///Set output format.
91
+ ///
92
+ ///The output format is global for all timestamp instances.
93
+ static void format(Format f) { _format = f; }
94
+ ///Retrieve the current output format
95
+
96
+ ///Retrieve the current output format
97
+ ///
98
+ ///The output format is global for all timestamp instances.
99
+ static Format format() { return _format; }
100
+
101
+
102
+ ///Read the current time values of the process
103
+ void stamp()
104
+ {
105
+ #ifndef WIN32
106
+ timeval tv;
107
+ gettimeofday(&tv, 0);
108
+ rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
109
+
110
+ tms ts;
111
+ double tck=sysconf(_SC_CLK_TCK);
112
+ times(&ts);
113
+ utime=ts.tms_utime/tck;
114
+ stime=ts.tms_stime/tck;
115
+ cutime=ts.tms_cutime/tck;
116
+ cstime=ts.tms_cstime/tck;
117
+ #else
118
+ bits::getWinProcTimes(rtime, utime, stime, cutime, cstime);
119
+ #endif
120
+ }
121
+
122
+ /// Constructor initializing with zero
123
+ TimeStamp()
124
+ { _reset(); }
125
+ ///Constructor initializing with the current time values of the process
126
+ TimeStamp(void *) { stamp();}
127
+
128
+ ///Set every time value to zero
129
+ TimeStamp &reset() {_reset();return *this;}
130
+
131
+ ///\e
132
+ TimeStamp &operator+=(const TimeStamp &b)
133
+ {
134
+ utime+=b.utime;
135
+ stime+=b.stime;
136
+ cutime+=b.cutime;
137
+ cstime+=b.cstime;
138
+ rtime+=b.rtime;
139
+ return *this;
140
+ }
141
+ ///\e
142
+ TimeStamp operator+(const TimeStamp &b) const
143
+ {
144
+ TimeStamp t(*this);
145
+ return t+=b;
146
+ }
147
+ ///\e
148
+ TimeStamp &operator-=(const TimeStamp &b)
149
+ {
150
+ utime-=b.utime;
151
+ stime-=b.stime;
152
+ cutime-=b.cutime;
153
+ cstime-=b.cstime;
154
+ rtime-=b.rtime;
155
+ return *this;
156
+ }
157
+ ///\e
158
+ TimeStamp operator-(const TimeStamp &b) const
159
+ {
160
+ TimeStamp t(*this);
161
+ return t-=b;
162
+ }
163
+ ///\e
164
+ TimeStamp &operator*=(double b)
165
+ {
166
+ utime*=b;
167
+ stime*=b;
168
+ cutime*=b;
169
+ cstime*=b;
170
+ rtime*=b;
171
+ return *this;
172
+ }
173
+ ///\e
174
+ TimeStamp operator*(double b) const
175
+ {
176
+ TimeStamp t(*this);
177
+ return t*=b;
178
+ }
179
+ friend TimeStamp operator*(double b,const TimeStamp &t);
180
+ ///\e
181
+ TimeStamp &operator/=(double b)
182
+ {
183
+ utime/=b;
184
+ stime/=b;
185
+ cutime/=b;
186
+ cstime/=b;
187
+ rtime/=b;
188
+ return *this;
189
+ }
190
+ ///\e
191
+ TimeStamp operator/(double b) const
192
+ {
193
+ TimeStamp t(*this);
194
+ return t/=b;
195
+ }
196
+ ///The time ellapsed since the last call of stamp()
197
+ TimeStamp ellapsed() const
198
+ {
199
+ TimeStamp t(NULL);
200
+ return t-*this;
201
+ }
202
+
203
+ friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
204
+
205
+ ///Gives back the user time of the process
206
+ double userTime() const
207
+ {
208
+ return utime;
209
+ }
210
+ ///Gives back the system time of the process
211
+ double systemTime() const
212
+ {
213
+ return stime;
214
+ }
215
+ ///Gives back the user time of the process' children
216
+
217
+ ///\note On <tt>WIN32</tt> platform this value is not calculated.
218
+ ///
219
+ double cUserTime() const
220
+ {
221
+ return cutime;
222
+ }
223
+ ///Gives back the user time of the process' children
224
+
225
+ ///\note On <tt>WIN32</tt> platform this value is not calculated.
226
+ ///
227
+ double cSystemTime() const
228
+ {
229
+ return cstime;
230
+ }
231
+ ///Gives back the real time
232
+ double realTime() const {return rtime;}
233
+ };
234
+
235
+ inline TimeStamp operator*(double b,const TimeStamp &t)
236
+ {
237
+ return t*b;
238
+ }
239
+
240
+ ///Prints the time counters
241
+
242
+ ///Prints the time counters in the following form:
243
+ ///
244
+ /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
245
+ ///
246
+ /// where the values are the
247
+ /// \li \c u: user cpu time,
248
+ /// \li \c s: system cpu time,
249
+ /// \li \c cu: user cpu time of children,
250
+ /// \li \c cs: system cpu time of children,
251
+ /// \li \c real: real time.
252
+ /// \relates TimeStamp
253
+ /// \note On <tt>WIN32</tt> platform the cummulative values are not
254
+ /// calculated.
255
+ inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
256
+ {
257
+ switch(t._format)
258
+ {
259
+ case TimeStamp::NORMAL:
260
+ os << "u: " << t.userTime() <<
261
+ "s, s: " << t.systemTime() <<
262
+ "s, cu: " << t.cUserTime() <<
263
+ "s, cs: " << t.cSystemTime() <<
264
+ "s, real: " << t.realTime() << "s";
265
+ break;
266
+ case TimeStamp::SHORT:
267
+ double total = t.userTime()+t.systemTime()+
268
+ t.cUserTime()+t.cSystemTime();
269
+ os << t.realTime()
270
+ << "s (err: " << round((t.realTime()-total)/
271
+ t.realTime()*10000)/100
272
+ << "%)";
273
+ break;
274
+ }
275
+ return os;
276
+ }
277
+
278
+ ///Class for measuring the cpu time and real time usage of the process
279
+
280
+ ///Class for measuring the cpu time and real time usage of the process.
281
+ ///It is quite easy-to-use, here is a short example.
282
+ ///\code
283
+ /// #include<lemon/time_measure.h>
284
+ /// #include<iostream>
285
+ ///
286
+ /// int main()
287
+ /// {
288
+ ///
289
+ /// ...
290
+ ///
291
+ /// Timer t;
292
+ /// doSomething();
293
+ /// std::cout << t << '\n';
294
+ /// t.restart();
295
+ /// doSomethingElse();
296
+ /// std::cout << t << '\n';
297
+ ///
298
+ /// ...
299
+ ///
300
+ /// }
301
+ ///\endcode
302
+ ///
303
+ ///The \ref Timer can also be \ref stop() "stopped" and
304
+ ///\ref start() "started" again, so it is possible to compute collected
305
+ ///running times.
306
+ ///
307
+ ///\warning Depending on the operation system and its actual configuration
308
+ ///the time counters have a certain (10ms on a typical Linux system)
309
+ ///granularity.
310
+ ///Therefore this tool is not appropriate to measure very short times.
311
+ ///Also, if you start and stop the timer very frequently, it could lead to
312
+ ///distorted results.
313
+ ///
314
+ ///\note If you want to measure the running time of the execution of a certain
315
+ ///function, consider the usage of \ref TimeReport instead.
316
+ ///
317
+ ///\sa TimeReport
318
+ class Timer
319
+ {
320
+ int _running; //Timer is running iff _running>0; (_running>=0 always holds)
321
+ TimeStamp start_time; //This is the relativ start-time if the timer
322
+ //is _running, the collected _running time otherwise.
323
+
324
+ void _reset() {if(_running) start_time.stamp(); else start_time.reset();}
325
+
326
+ public:
327
+ ///Constructor.
328
+
329
+ ///\param run indicates whether or not the timer starts immediately.
330
+ ///
331
+ Timer(bool run=true) :_running(run) {_reset();}
332
+
333
+ ///\name Control the State of the Timer
334
+ ///Basically a Timer can be either running or stopped,
335
+ ///but it provides a bit finer control on the execution.
336
+ ///The \ref lemon::Timer "Timer" also counts the number of
337
+ ///\ref lemon::Timer::start() "start()" executions, and it stops
338
+ ///only after the same amount (or more) \ref lemon::Timer::stop()
339
+ ///"stop()"s. This can be useful e.g. to compute the running time
340
+ ///of recursive functions.
341
+
342
+ ///@{
343
+
344
+ ///Reset and stop the time counters
345
+
346
+ ///This function resets and stops the time counters
347
+ ///\sa restart()
348
+ void reset()
349
+ {
350
+ _running=0;
351
+ _reset();
352
+ }
353
+
354
+ ///Start the time counters
355
+
356
+ ///This function starts the time counters.
357
+ ///
358
+ ///If the timer is started more than ones, it will remain running
359
+ ///until the same amount of \ref stop() is called.
360
+ ///\sa stop()
361
+ void start()
362
+ {
363
+ if(_running) _running++;
364
+ else {
365
+ _running=1;
366
+ TimeStamp t;
367
+ t.stamp();
368
+ start_time=t-start_time;
369
+ }
370
+ }
371
+
372
+
373
+ ///Stop the time counters
374
+
375
+ ///This function stops the time counters. If start() was executed more than
376
+ ///once, then the same number of stop() execution is necessary the really
377
+ ///stop the timer.
378
+ ///
379
+ ///\sa halt()
380
+ ///\sa start()
381
+ ///\sa restart()
382
+ ///\sa reset()
383
+
384
+ void stop()
385
+ {
386
+ if(_running && !--_running) {
387
+ TimeStamp t;
388
+ t.stamp();
389
+ start_time=t-start_time;
390
+ }
391
+ }
392
+
393
+ ///Halt (i.e stop immediately) the time counters
394
+
395
+ ///This function stops immediately the time counters, i.e. <tt>t.halt()</tt>
396
+ ///is a faster
397
+ ///equivalent of the following.
398
+ ///\code
399
+ /// while(t.running()) t.stop()
400
+ ///\endcode
401
+ ///
402
+ ///
403
+ ///\sa stop()
404
+ ///\sa restart()
405
+ ///\sa reset()
406
+
407
+ void halt()
408
+ {
409
+ if(_running) {
410
+ _running=0;
411
+ TimeStamp t;
412
+ t.stamp();
413
+ start_time=t-start_time;
414
+ }
415
+ }
416
+
417
+ ///Returns the running state of the timer
418
+
419
+ ///This function returns the number of stop() exections that is
420
+ ///necessary to really stop the timer.
421
+ ///For example, the timer
422
+ ///is running if and only if the return value is \c true
423
+ ///(i.e. greater than
424
+ ///zero).
425
+ int running() { return _running; }
426
+
427
+
428
+ ///Restart the time counters
429
+
430
+ ///This function is a shorthand for
431
+ ///a reset() and a start() calls.
432
+ ///
433
+ void restart()
434
+ {
435
+ reset();
436
+ start();
437
+ }
438
+
439
+ ///@}
440
+
441
+ ///\name Query Functions for the Ellapsed Time
442
+
443
+ ///@{
444
+
445
+ ///Gives back the ellapsed user time of the process
446
+ double userTime() const
447
+ {
448
+ return operator TimeStamp().userTime();
449
+ }
450
+ ///Gives back the ellapsed system time of the process
451
+ double systemTime() const
452
+ {
453
+ return operator TimeStamp().systemTime();
454
+ }
455
+ ///Gives back the ellapsed user time of the process' children
456
+
457
+ ///\note On <tt>WIN32</tt> platform this value is not calculated.
458
+ ///
459
+ double cUserTime() const
460
+ {
461
+ return operator TimeStamp().cUserTime();
462
+ }
463
+ ///Gives back the ellapsed user time of the process' children
464
+
465
+ ///\note On <tt>WIN32</tt> platform this value is not calculated.
466
+ ///
467
+ double cSystemTime() const
468
+ {
469
+ return operator TimeStamp().cSystemTime();
470
+ }
471
+ ///Gives back the ellapsed real time
472
+ double realTime() const
473
+ {
474
+ return operator TimeStamp().realTime();
475
+ }
476
+ ///Computes the ellapsed time
477
+
478
+ ///This conversion computes the ellapsed time, therefore you can print
479
+ ///the ellapsed time like this.
480
+ ///\code
481
+ /// Timer t;
482
+ /// doSomething();
483
+ /// std::cout << t << '\n';
484
+ ///\endcode
485
+ operator TimeStamp () const
486
+ {
487
+ TimeStamp t;
488
+ t.stamp();
489
+ return _running?t-start_time:start_time;
490
+ }
491
+
492
+
493
+ ///@}
494
+ };
495
+
496
+ ///Same as Timer but prints a report on destruction.
497
+
498
+ ///Same as \ref Timer but prints a report on destruction.
499
+ ///This example shows its usage.
500
+ ///\code
501
+ /// void myAlg(ListGraph &g,int n)
502
+ /// {
503
+ /// TimeReport tr("Running time of myAlg: ");
504
+ /// ... //Here comes the algorithm
505
+ /// }
506
+ ///\endcode
507
+ ///
508
+ ///\sa Timer
509
+ ///\sa NoTimeReport
510
+ class TimeReport : public Timer
511
+ {
512
+ std::string _title;
513
+ std::ostream &_os;
514
+ bool _active;
515
+ public:
516
+ ///Constructor
517
+
518
+ ///Constructor.
519
+ ///\param title This text will be printed before the ellapsed time.
520
+ ///\param os The stream to print the report to.
521
+ ///\param run Sets whether the timer should start immediately.
522
+ ///\param active Sets whether the report should actually be printed
523
+ /// on destruction.
524
+ TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true,
525
+ bool active=true)
526
+ : Timer(run), _title(title), _os(os), _active(active) {}
527
+ ///Destructor that prints the ellapsed time
528
+ ~TimeReport()
529
+ {
530
+ if(_active) _os << _title << *this << std::endl;
531
+ }
532
+
533
+ ///Retrieve the activity status
534
+
535
+ ///\e
536
+ ///
537
+ bool active() const { return _active; }
538
+ ///Set the activity status
539
+
540
+ /// This function set whether the time report should actually be printed
541
+ /// on destruction.
542
+ void active(bool a) { _active=a; }
543
+ };
544
+
545
+ ///'Do nothing' version of TimeReport
546
+
547
+ ///\sa TimeReport
548
+ ///
549
+ class NoTimeReport
550
+ {
551
+ public:
552
+ ///\e
553
+ NoTimeReport(std::string,std::ostream &,bool) {}
554
+ ///\e
555
+ NoTimeReport(std::string,std::ostream &) {}
556
+ ///\e
557
+ NoTimeReport(std::string) {}
558
+ ///\e Do nothing.
559
+ ~NoTimeReport() {}
560
+
561
+ operator TimeStamp () const { return TimeStamp(); }
562
+ void reset() {}
563
+ void start() {}
564
+ void stop() {}
565
+ void halt() {}
566
+ int running() { return 0; }
567
+ void restart() {}
568
+ double userTime() const { return 0; }
569
+ double systemTime() const { return 0; }
570
+ double cUserTime() const { return 0; }
571
+ double cSystemTime() const { return 0; }
572
+ double realTime() const { return 0; }
573
+ };
574
+
575
+ ///Tool to measure the running time more exactly.
576
+
577
+ ///This function calls \c f several times and returns the average
578
+ ///running time. The number of the executions will be choosen in such a way
579
+ ///that the full real running time will be roughly between \c min_time
580
+ ///and <tt>2*min_time</tt>.
581
+ ///\param f the function object to be measured.
582
+ ///\param min_time the minimum total running time.
583
+ ///\retval num if it is not \c NULL, then the actual
584
+ /// number of execution of \c f will be written into <tt>*num</tt>.
585
+ ///\retval full_time if it is not \c NULL, then the actual
586
+ /// total running time will be written into <tt>*full_time</tt>.
587
+ ///\return The average running time of \c f.
588
+
589
+ template<class F>
590
+ TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL,
591
+ TimeStamp *full_time=NULL)
592
+ {
593
+ TimeStamp full;
594
+ unsigned int total=0;
595
+ Timer t;
596
+ for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) {
597
+ for(;total<tn;total++) f();
598
+ full=t;
599
+ }
600
+ if(num) *num=total;
601
+ if(full_time) *full_time=full;
602
+ return full/total;
603
+ }
604
+
605
+ /// @}
606
+
607
+
608
+ } //namespace lemon
609
+
610
+ #endif //LEMON_TIME_MEASURE_H