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,487 @@
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 RADIX_SORT_H
20
+ #define RADIX_SORT_H
21
+
22
+ /// \ingroup auxalg
23
+ /// \file
24
+ /// \brief Radix sort
25
+ ///
26
+ /// Linear time sorting algorithms
27
+
28
+ #include <vector>
29
+ #include <limits>
30
+ #include <iterator>
31
+ #include <algorithm>
32
+
33
+ namespace lemon {
34
+
35
+ namespace _radix_sort_bits {
36
+
37
+ template <typename Iterator>
38
+ bool unitRange(Iterator first, Iterator last) {
39
+ ++first;
40
+ return first == last;
41
+ }
42
+
43
+ template <typename Value>
44
+ struct Identity {
45
+ const Value& operator()(const Value& val) {
46
+ return val;
47
+ }
48
+ };
49
+
50
+
51
+ template <typename Value, typename Iterator, typename Functor>
52
+ Iterator radixSortPartition(Iterator first, Iterator last,
53
+ Functor functor, Value mask) {
54
+ while (first != last && !(functor(*first) & mask)) {
55
+ ++first;
56
+ }
57
+ if (first == last) {
58
+ return first;
59
+ }
60
+ --last;
61
+ while (first != last && (functor(*last) & mask)) {
62
+ --last;
63
+ }
64
+ if (first == last) {
65
+ return first;
66
+ }
67
+ std::iter_swap(first, last);
68
+ ++first;
69
+ while (true) {
70
+ while (!(functor(*first) & mask)) {
71
+ ++first;
72
+ }
73
+ --last;
74
+ while (functor(*last) & mask) {
75
+ --last;
76
+ }
77
+ if (unitRange(last, first)) {
78
+ return first;
79
+ }
80
+ std::iter_swap(first, last);
81
+ ++first;
82
+ }
83
+ }
84
+
85
+ template <typename Iterator, typename Functor>
86
+ Iterator radixSortSignPartition(Iterator first, Iterator last,
87
+ Functor functor) {
88
+ while (first != last && functor(*first) < 0) {
89
+ ++first;
90
+ }
91
+ if (first == last) {
92
+ return first;
93
+ }
94
+ --last;
95
+ while (first != last && functor(*last) >= 0) {
96
+ --last;
97
+ }
98
+ if (first == last) {
99
+ return first;
100
+ }
101
+ std::iter_swap(first, last);
102
+ ++first;
103
+ while (true) {
104
+ while (functor(*first) < 0) {
105
+ ++first;
106
+ }
107
+ --last;
108
+ while (functor(*last) >= 0) {
109
+ --last;
110
+ }
111
+ if (unitRange(last, first)) {
112
+ return first;
113
+ }
114
+ std::iter_swap(first, last);
115
+ ++first;
116
+ }
117
+ }
118
+
119
+ template <typename Value, typename Iterator, typename Functor>
120
+ void radixIntroSort(Iterator first, Iterator last,
121
+ Functor functor, Value mask) {
122
+ while (mask != 0 && first != last && !unitRange(first, last)) {
123
+ Iterator cut = radixSortPartition(first, last, functor, mask);
124
+ mask >>= 1;
125
+ radixIntroSort(first, cut, functor, mask);
126
+ first = cut;
127
+ }
128
+ }
129
+
130
+ template <typename Value, typename Iterator, typename Functor>
131
+ void radixSignedSort(Iterator first, Iterator last, Functor functor) {
132
+
133
+ Iterator cut = radixSortSignPartition(first, last, functor);
134
+
135
+ Value mask;
136
+ int max_digit;
137
+ Iterator it;
138
+
139
+ mask = ~0; max_digit = 0;
140
+ for (it = first; it != cut; ++it) {
141
+ while ((mask & functor(*it)) != mask) {
142
+ ++max_digit;
143
+ mask <<= 1;
144
+ }
145
+ }
146
+ radixIntroSort(first, cut, functor, 1 << max_digit);
147
+
148
+ mask = 0; max_digit = 0;
149
+ for (it = cut; it != last; ++it) {
150
+ while ((mask | functor(*it)) != mask) {
151
+ ++max_digit;
152
+ mask <<= 1; mask |= 1;
153
+ }
154
+ }
155
+ radixIntroSort(cut, last, functor, 1 << max_digit);
156
+ }
157
+
158
+ template <typename Value, typename Iterator, typename Functor>
159
+ void radixUnsignedSort(Iterator first, Iterator last, Functor functor) {
160
+
161
+ Value mask = 0;
162
+ int max_digit = 0;
163
+
164
+ Iterator it;
165
+ for (it = first; it != last; ++it) {
166
+ while ((mask | functor(*it)) != mask) {
167
+ ++max_digit;
168
+ mask <<= 1; mask |= 1;
169
+ }
170
+ }
171
+ radixIntroSort(first, last, functor, 1 << max_digit);
172
+ }
173
+
174
+
175
+ template <typename Value,
176
+ bool sign = std::numeric_limits<Value>::is_signed >
177
+ struct RadixSortSelector {
178
+ template <typename Iterator, typename Functor>
179
+ static void sort(Iterator first, Iterator last, Functor functor) {
180
+ radixSignedSort<Value>(first, last, functor);
181
+ }
182
+ };
183
+
184
+ template <typename Value>
185
+ struct RadixSortSelector<Value, false> {
186
+ template <typename Iterator, typename Functor>
187
+ static void sort(Iterator first, Iterator last, Functor functor) {
188
+ radixUnsignedSort<Value>(first, last, functor);
189
+ }
190
+ };
191
+
192
+ }
193
+
194
+ /// \ingroup auxalg
195
+ ///
196
+ /// \brief Sorts the STL compatible range into ascending order.
197
+ ///
198
+ /// The \c radixSort sorts an STL compatible range into ascending
199
+ /// order. The radix sort algorithm can sort items which are mapped
200
+ /// to integers with an adaptable unary function \c functor and the
201
+ /// order will be ascending according to these mapped values.
202
+ ///
203
+ /// It is also possible to use a normal function instead
204
+ /// of the functor object. If the functor is not given it will use
205
+ /// the identity function instead.
206
+ ///
207
+ /// This is a special quick sort algorithm where the pivot
208
+ /// values to split the items are choosen to be 2<sup>k</sup>
209
+ /// for each \c k.
210
+ /// Therefore, the time complexity of the algorithm is O(log(c)*n) and
211
+ /// it uses O(log(c)) additional space, where \c c is the maximal value
212
+ /// and \c n is the number of the items in the container.
213
+ ///
214
+ /// \param first The begin of the given range.
215
+ /// \param last The end of the given range.
216
+ /// \param functor An adaptible unary function or a normal function
217
+ /// which maps the items to any integer type which can be either
218
+ /// signed or unsigned.
219
+ ///
220
+ /// \sa stableRadixSort()
221
+ template <typename Iterator, typename Functor>
222
+ void radixSort(Iterator first, Iterator last, Functor functor) {
223
+ using namespace _radix_sort_bits;
224
+ typedef typename Functor::result_type Value;
225
+ RadixSortSelector<Value>::sort(first, last, functor);
226
+ }
227
+
228
+ template <typename Iterator, typename Value, typename Key>
229
+ void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
230
+ using namespace _radix_sort_bits;
231
+ RadixSortSelector<Value>::sort(first, last, functor);
232
+ }
233
+
234
+ template <typename Iterator, typename Value, typename Key>
235
+ void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
236
+ using namespace _radix_sort_bits;
237
+ RadixSortSelector<Value>::sort(first, last, functor);
238
+ }
239
+
240
+ template <typename Iterator, typename Value, typename Key>
241
+ void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
242
+ using namespace _radix_sort_bits;
243
+ RadixSortSelector<Value>::sort(first, last, functor);
244
+ }
245
+
246
+ template <typename Iterator, typename Value, typename Key>
247
+ void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
248
+ using namespace _radix_sort_bits;
249
+ RadixSortSelector<Value>::sort(first, last, functor);
250
+ }
251
+
252
+ template <typename Iterator>
253
+ void radixSort(Iterator first, Iterator last) {
254
+ using namespace _radix_sort_bits;
255
+ typedef typename std::iterator_traits<Iterator>::value_type Value;
256
+ RadixSortSelector<Value>::sort(first, last, Identity<Value>());
257
+ }
258
+
259
+ namespace _radix_sort_bits {
260
+
261
+ template <typename Value>
262
+ unsigned char valueByte(Value value, int byte) {
263
+ return value >> (std::numeric_limits<unsigned char>::digits * byte);
264
+ }
265
+
266
+ template <typename Functor, typename Key>
267
+ void stableRadixIntroSort(Key *first, Key *last, Key *target,
268
+ int byte, Functor functor) {
269
+ const int size =
270
+ unsigned(std::numeric_limits<unsigned char>::max()) + 1;
271
+ std::vector<int> counter(size);
272
+ for (int i = 0; i < size; ++i) {
273
+ counter[i] = 0;
274
+ }
275
+ Key *it = first;
276
+ while (first != last) {
277
+ ++counter[valueByte(functor(*first), byte)];
278
+ ++first;
279
+ }
280
+ int prev, num = 0;
281
+ for (int i = 0; i < size; ++i) {
282
+ prev = num;
283
+ num += counter[i];
284
+ counter[i] = prev;
285
+ }
286
+ while (it != last) {
287
+ target[counter[valueByte(functor(*it), byte)]++] = *it;
288
+ ++it;
289
+ }
290
+ }
291
+
292
+ template <typename Functor, typename Key>
293
+ void signedStableRadixIntroSort(Key *first, Key *last, Key *target,
294
+ int byte, Functor functor) {
295
+ const int size =
296
+ unsigned(std::numeric_limits<unsigned char>::max()) + 1;
297
+ std::vector<int> counter(size);
298
+ for (int i = 0; i < size; ++i) {
299
+ counter[i] = 0;
300
+ }
301
+ Key *it = first;
302
+ while (first != last) {
303
+ counter[valueByte(functor(*first), byte)]++;
304
+ ++first;
305
+ }
306
+ int prev, num = 0;
307
+ for (int i = size / 2; i < size; ++i) {
308
+ prev = num;
309
+ num += counter[i];
310
+ counter[i] = prev;
311
+ }
312
+ for (int i = 0; i < size / 2; ++i) {
313
+ prev = num;
314
+ num += counter[i];
315
+ counter[i] = prev;
316
+ }
317
+ while (it != last) {
318
+ target[counter[valueByte(functor(*it), byte)]++] = *it;
319
+ ++it;
320
+ }
321
+ }
322
+
323
+
324
+ template <typename Value, typename Iterator, typename Functor>
325
+ void stableRadixSignedSort(Iterator first, Iterator last, Functor functor) {
326
+ if (first == last) return;
327
+ typedef typename std::iterator_traits<Iterator>::value_type Key;
328
+ typedef std::allocator<Key> Allocator;
329
+ Allocator allocator;
330
+
331
+ int length = std::distance(first, last);
332
+ Key* buffer = allocator.allocate(2 * length);
333
+ try {
334
+ bool dir = true;
335
+ std::copy(first, last, buffer);
336
+ for (int i = 0; i < int(sizeof(Value)) - 1; ++i) {
337
+ if (dir) {
338
+ stableRadixIntroSort(buffer, buffer + length, buffer + length,
339
+ i, functor);
340
+ } else {
341
+ stableRadixIntroSort(buffer + length, buffer + 2 * length, buffer,
342
+ i, functor);
343
+ }
344
+ dir = !dir;
345
+ }
346
+ if (dir) {
347
+ signedStableRadixIntroSort(buffer, buffer + length, buffer + length,
348
+ sizeof(Value) - 1, functor);
349
+ std::copy(buffer + length, buffer + 2 * length, first);
350
+ } else {
351
+ signedStableRadixIntroSort(buffer + length, buffer + 2 * length,
352
+ buffer, sizeof(Value) - 1, functor);
353
+ std::copy(buffer, buffer + length, first);
354
+ }
355
+ } catch (...) {
356
+ allocator.deallocate(buffer, 2 * length);
357
+ throw;
358
+ }
359
+ allocator.deallocate(buffer, 2 * length);
360
+ }
361
+
362
+ template <typename Value, typename Iterator, typename Functor>
363
+ void stableRadixUnsignedSort(Iterator first, Iterator last,
364
+ Functor functor) {
365
+ if (first == last) return;
366
+ typedef typename std::iterator_traits<Iterator>::value_type Key;
367
+ typedef std::allocator<Key> Allocator;
368
+ Allocator allocator;
369
+
370
+ int length = std::distance(first, last);
371
+ Key *buffer = allocator.allocate(2 * length);
372
+ try {
373
+ bool dir = true;
374
+ std::copy(first, last, buffer);
375
+ for (int i = 0; i < int(sizeof(Value)); ++i) {
376
+ if (dir) {
377
+ stableRadixIntroSort(buffer, buffer + length,
378
+ buffer + length, i, functor);
379
+ } else {
380
+ stableRadixIntroSort(buffer + length, buffer + 2 * length,
381
+ buffer, i, functor);
382
+ }
383
+ dir = !dir;
384
+ }
385
+ if (dir) {
386
+ std::copy(buffer, buffer + length, first);
387
+ } else {
388
+ std::copy(buffer + length, buffer + 2 * length, first);
389
+ }
390
+ } catch (...) {
391
+ allocator.deallocate(buffer, 2 * length);
392
+ throw;
393
+ }
394
+ allocator.deallocate(buffer, 2 * length);
395
+ }
396
+
397
+
398
+
399
+ template <typename Value,
400
+ bool sign = std::numeric_limits<Value>::is_signed >
401
+ struct StableRadixSortSelector {
402
+ template <typename Iterator, typename Functor>
403
+ static void sort(Iterator first, Iterator last, Functor functor) {
404
+ stableRadixSignedSort<Value>(first, last, functor);
405
+ }
406
+ };
407
+
408
+ template <typename Value>
409
+ struct StableRadixSortSelector<Value, false> {
410
+ template <typename Iterator, typename Functor>
411
+ static void sort(Iterator first, Iterator last, Functor functor) {
412
+ stableRadixUnsignedSort<Value>(first, last, functor);
413
+ }
414
+ };
415
+
416
+ }
417
+
418
+ /// \ingroup auxalg
419
+ ///
420
+ /// \brief Sorts the STL compatible range into ascending order in a stable
421
+ /// way.
422
+ ///
423
+ /// This function sorts an STL compatible range into ascending
424
+ /// order according to an integer mapping in the same as radixSort() does.
425
+ ///
426
+ /// This sorting algorithm is stable, i.e. the order of two equal
427
+ /// elements remains the same after the sorting.
428
+ ///
429
+ /// This sort algorithm use a radix forward sort on the
430
+ /// bytes of the integer number. The algorithm sorts the items
431
+ /// byte-by-byte. First, it counts how many times a byte value occurs
432
+ /// in the container, then it copies the corresponding items to
433
+ /// another container in asceding order in O(n) time.
434
+ ///
435
+ /// The time complexity of the algorithm is O(log(c)*n) and
436
+ /// it uses O(n) additional space, where \c c is the
437
+ /// maximal value and \c n is the number of the items in the
438
+ /// container.
439
+ ///
440
+
441
+ /// \param first The begin of the given range.
442
+ /// \param last The end of the given range.
443
+ /// \param functor An adaptible unary function or a normal function
444
+ /// which maps the items to any integer type which can be either
445
+ /// signed or unsigned.
446
+ /// \sa radixSort()
447
+ template <typename Iterator, typename Functor>
448
+ void stableRadixSort(Iterator first, Iterator last, Functor functor) {
449
+ using namespace _radix_sort_bits;
450
+ typedef typename Functor::result_type Value;
451
+ StableRadixSortSelector<Value>::sort(first, last, functor);
452
+ }
453
+
454
+ template <typename Iterator, typename Value, typename Key>
455
+ void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
456
+ using namespace _radix_sort_bits;
457
+ StableRadixSortSelector<Value>::sort(first, last, functor);
458
+ }
459
+
460
+ template <typename Iterator, typename Value, typename Key>
461
+ void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
462
+ using namespace _radix_sort_bits;
463
+ StableRadixSortSelector<Value>::sort(first, last, functor);
464
+ }
465
+
466
+ template <typename Iterator, typename Value, typename Key>
467
+ void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
468
+ using namespace _radix_sort_bits;
469
+ StableRadixSortSelector<Value>::sort(first, last, functor);
470
+ }
471
+
472
+ template <typename Iterator, typename Value, typename Key>
473
+ void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
474
+ using namespace _radix_sort_bits;
475
+ StableRadixSortSelector<Value>::sort(first, last, functor);
476
+ }
477
+
478
+ template <typename Iterator>
479
+ void stableRadixSort(Iterator first, Iterator last) {
480
+ using namespace _radix_sort_bits;
481
+ typedef typename std::iterator_traits<Iterator>::value_type Value;
482
+ StableRadixSortSelector<Value>::sort(first, last, Identity<Value>());
483
+ }
484
+
485
+ }
486
+
487
+ #endif
@@ -0,0 +1,29 @@
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-2009
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
+ ///\file
20
+ ///\brief Instantiation of the Random class.
21
+
22
+ #include <lemon/random.h>
23
+
24
+ namespace lemon {
25
+ /// \brief Global random number generator instance
26
+ ///
27
+ /// A global Mersenne Twister random number generator instance.
28
+ Random rnd;
29
+ }