scm2cpp-learn 0.22.0__py3-none-macosx_15_0_arm64.whl

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 (71) hide show
  1. scm2cpp_learn/.dylibs/libgcc_s.1.1.dylib +0 -0
  2. scm2cpp_learn/.dylibs/libgfortran.5.dylib +0 -0
  3. scm2cpp_learn/.dylibs/libomp.dylib +0 -0
  4. scm2cpp_learn/.dylibs/libopenblasp-r0.3.34.dylib +0 -0
  5. scm2cpp_learn/.dylibs/libquadmath.0.dylib +0 -0
  6. scm2cpp_learn/__init__.py +4794 -0
  7. scm2cpp_learn/_classifiers.cpython-312-darwin.so +0 -0
  8. scm2cpp_learn/_compat.py +354 -0
  9. scm2cpp_learn/_generated/__init__.py +0 -0
  10. scm2cpp_learn/_generated/_loader.py +41 -0
  11. scm2cpp_learn/_generated/_loader_accel_cublas.py +185 -0
  12. scm2cpp_learn/_generated/_loader_classifiers.py +308 -0
  13. scm2cpp_learn/_generated/_loader_derived_blas.py +29 -0
  14. scm2cpp_learn/_generated/_loader_derived_cublas.py +29 -0
  15. scm2cpp_learn/_generated/_loader_derived_loops.py +29 -0
  16. scm2cpp_learn/_generated/_loader_gpu_cuda.py +313 -0
  17. scm2cpp_learn/_generated/_loader_gramops_blas.py +836 -0
  18. scm2cpp_learn/_generated/_loader_gramops_loops.py +855 -0
  19. scm2cpp_learn/_generated/_loader_nmf_blas.py +29 -0
  20. scm2cpp_learn/_generated/_loader_nmf_cublas.py +29 -0
  21. scm2cpp_learn/_generated/_loader_nmf_loops.py +29 -0
  22. scm2cpp_learn/_generated/accel_cublas/accel.cpp +26 -0
  23. scm2cpp_learn/_generated/accel_cublas/accel.hpp +1250 -0
  24. scm2cpp_learn/_generated/accel_cublas/accel_capi.cpp +82 -0
  25. scm2cpp_learn/_generated/classifiers.cpp +37 -0
  26. scm2cpp_learn/_generated/classifiers.hpp +3745 -0
  27. scm2cpp_learn/_generated/classifiers_capi.cpp +115 -0
  28. scm2cpp_learn/_generated/derived_blas/lasso_design.cpp +5 -0
  29. scm2cpp_learn/_generated/derived_blas/lasso_design.hpp +156 -0
  30. scm2cpp_learn/_generated/derived_blas/lasso_design_capi.cpp +19 -0
  31. scm2cpp_learn/_generated/derived_cublas/lasso_design.cpp +5 -0
  32. scm2cpp_learn/_generated/derived_cublas/lasso_design.hpp +158 -0
  33. scm2cpp_learn/_generated/derived_cublas/lasso_design_capi.cpp +19 -0
  34. scm2cpp_learn/_generated/derived_loops/lasso_design.cpp +5 -0
  35. scm2cpp_learn/_generated/derived_loops/lasso_design.hpp +179 -0
  36. scm2cpp_learn/_generated/derived_loops/lasso_design_capi.cpp +19 -0
  37. scm2cpp_learn/_generated/gpu_cuda/gpu.cpp +37 -0
  38. scm2cpp_learn/_generated/gpu_cuda/gpu.hpp +3846 -0
  39. scm2cpp_learn/_generated/gpu_cuda/gpu_capi.cpp +308 -0
  40. scm2cpp_learn/_generated/gramops_blas/gramops.cpp +99 -0
  41. scm2cpp_learn/_generated/gramops_blas/gramops.hpp +6655 -0
  42. scm2cpp_learn/_generated/gramops_blas/gramops_capi.cpp +295 -0
  43. scm2cpp_learn/_generated/gramops_loops/gramops.cpp +99 -0
  44. scm2cpp_learn/_generated/gramops_loops/gramops.hpp +7711 -0
  45. scm2cpp_learn/_generated/gramops_loops/gramops_capi.cpp +301 -0
  46. scm2cpp_learn/_generated/lasso_cov.cpp +7 -0
  47. scm2cpp_learn/_generated/lasso_cov.hpp +152 -0
  48. scm2cpp_learn/_generated/lasso_cov_capi.cpp +25 -0
  49. scm2cpp_learn/_generated/nmf_blas/nmf_cd.cpp +5 -0
  50. scm2cpp_learn/_generated/nmf_blas/nmf_cd.hpp +132 -0
  51. scm2cpp_learn/_generated/nmf_blas/nmf_cd_capi.cpp +19 -0
  52. scm2cpp_learn/_generated/nmf_cublas/nmf_cd.cpp +5 -0
  53. scm2cpp_learn/_generated/nmf_cublas/nmf_cd.hpp +136 -0
  54. scm2cpp_learn/_generated/nmf_cublas/nmf_cd_capi.cpp +19 -0
  55. scm2cpp_learn/_generated/nmf_loops/nmf_cd.cpp +5 -0
  56. scm2cpp_learn/_generated/nmf_loops/nmf_cd.hpp +167 -0
  57. scm2cpp_learn/_generated/nmf_loops/nmf_cd_capi.cpp +19 -0
  58. scm2cpp_learn/_generated/scm2cpp-blas.hpp +176 -0
  59. scm2cpp_learn/_generated/scm2cpp-cublas.hpp +155 -0
  60. scm2cpp_learn/_generated/scm2cpp-cuda.hpp +285 -0
  61. scm2cpp_learn/_generated/scm2cpp.hpp +1641 -0
  62. scm2cpp_learn/_gramops_blas.cpython-312-darwin.so +0 -0
  63. scm2cpp_learn/_lasso_cov.cpython-312-darwin.so +0 -0
  64. scm2cpp_learn/_lasso_derived_blas.cpython-312-darwin.so +0 -0
  65. scm2cpp_learn/_libfind.py +153 -0
  66. scm2cpp_learn/_nmf_blas.cpython-312-darwin.so +0 -0
  67. scm2cpp_learn/batch_capi.cu +431 -0
  68. scm2cpp_learn-0.22.0.dist-info/METADATA +1350 -0
  69. scm2cpp_learn-0.22.0.dist-info/RECORD +71 -0
  70. scm2cpp_learn-0.22.0.dist-info/WHEEL +6 -0
  71. scm2cpp_learn-0.22.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1641 @@
1
+ #ifndef SCM2CPP
2
+ #define SCM2CPP
3
+
4
+ #include <iostream>
5
+ #include <vector>
6
+ #include <string>
7
+ #include <list>
8
+ #include <map>
9
+ #include <algorithm>
10
+ #include <limits>
11
+ #include <array>
12
+ #include <cmath>
13
+ #include <cstdarg>
14
+
15
+ // Marks a function compilable for both host and device. Under nvcc it
16
+ // expands to the CUDA qualifiers; everywhere else to nothing, so the
17
+ // same generated text serves both toolchains. The translator applies
18
+ // it only to functions whose bodies stay inside the device-safe subset.
19
+ #if defined(__CUDACC__)
20
+ #define SCM2CPP_FN __host__ __device__
21
+ #else
22
+ #define SCM2CPP_FN
23
+ #endif
24
+
25
+ #include <type_traits>
26
+ #include <cstdint>
27
+ #include <functional>
28
+
29
+ // min and max of two numbers of possibly different types: an integer
30
+ // scalar is std::int64_t and an element of an integer array int, and
31
+ // std::min of the two does not deduce. The common type is C++'s usual
32
+ // arithmetic conversion, what Scheme's min would give.
33
+ namespace scm2cpp {
34
+ template <class A, class B>
35
+ SCM2CPP_FN inline typename std::common_type<A, B>::type min2(A a, B b)
36
+ { typedef typename std::common_type<A, B>::type T; return T(a) < T(b) ? T(a) : T(b); }
37
+ template <class A, class B>
38
+ SCM2CPP_FN inline typename std::common_type<A, B>::type max2(A a, B b)
39
+ { typedef typename std::common_type<A, B>::type T; return T(a) < T(b) ? T(b) : T(a); }
40
+ }
41
+
42
+ #ifndef SCM2CPP_MINIMAL // the full-feature runtime: everything a numeric kernel never touches
43
+
44
+
45
+ #include <boost/function.hpp>
46
+ #include <boost/functional.hpp>
47
+
48
+ //#include <boost/thread.hpp>
49
+
50
+ #include <boost/typeof/typeof.hpp>
51
+ #include <boost/type_traits.hpp>
52
+ #include <boost/utility/result_of.hpp>
53
+ #include <boost/function_types/result_type.hpp>
54
+ #include <boost/function_types/parameter_types.hpp>
55
+ #include <boost/function_types/function_type.hpp>
56
+
57
+ #include <boost/preprocessor.hpp>
58
+
59
+
60
+ #include <boost/utility/enable_if.hpp>
61
+
62
+ #include <boost/mpl/vector.hpp>
63
+ #include <boost/mpl/contains.hpp>
64
+ #include <boost/mpl/transform.hpp>
65
+ #include <boost/mpl/insert_range.hpp>
66
+ #include <boost/mpl/identity.hpp>
67
+ #include <boost/mpl/logical.hpp>
68
+
69
+ #include <boost/mpl/less.hpp>
70
+ #include <boost/mpl/comparison.hpp>
71
+ #include <boost/mpl/filter_view.hpp>
72
+ #include <boost/mpl/sizeof.hpp>
73
+ #include <boost/mpl/vector.hpp>
74
+ #include <boost/mpl/sort.hpp>
75
+ #include <boost/mpl/equal.hpp>
76
+ #include <boost/mpl/assert.hpp>
77
+ #include <boost/mpl/if.hpp>
78
+ #include <boost/mpl/set.hpp>
79
+ #include <boost/mpl/empty.hpp>
80
+ #include <boost/mpl/unique.hpp>
81
+
82
+ //#include <boost/fusion/iterator.hpp>
83
+ //#include <boost/fusion/include/iterator.hpp>
84
+
85
+
86
+ #include <boost/fusion/container/vector.hpp>
87
+ #include <boost/fusion/algorithm.hpp>
88
+ #include <boost/fusion/sequence.hpp>
89
+ #include <boost/fusion/include/list.hpp>
90
+ #include <boost/fusion/include/vector.hpp>
91
+
92
+ #include <boost/fusion/container/list/cons.hpp>
93
+ #include <boost/fusion/include/cons.hpp>
94
+ #include <boost/fusion/include/make_cons.hpp>
95
+ #include <boost/fusion/container/generation/make_cons.hpp>
96
+ #include <boost/fusion/container/generation/make_list.hpp>
97
+ #include <boost/fusion/include/make_list.hpp>
98
+
99
+
100
+ #include <boost/fusion/iterator/iterator_facade.hpp>
101
+ #include <boost/fusion/include/iterator_facade.hpp>
102
+
103
+ #include <boost/fusion/algorithm/auxiliary/copy.hpp>
104
+ #include <boost/fusion/include/copy.hpp>
105
+
106
+ #include <boost/fusion/algorithm/transformation/push_front.hpp>
107
+ #include <boost/fusion/include/push_front.hpp>
108
+
109
+ #include <boost/fusion/algorithm/transformation/push_back.hpp>
110
+ #include <boost/fusion/include/push_back.hpp>
111
+
112
+
113
+ #include <boost/variant.hpp>
114
+
115
+
116
+ #define null_P scm2cpp::is_null
117
+ #define eq_P scm2cpp::is_eq
118
+ #define eqv_P scm2cpp::is_eqv
119
+ #define equal_P scm2cpp::is_equal
120
+
121
+
122
+
123
+
124
+ #define CONSED_AS_STD_PAIR 1
125
+ #if CONSED_AS_STD_PAIR
126
+ #define consd std::pair
127
+ #endif
128
+
129
+ // #include <boost/intrusive/slist.hpp>
130
+ // #include <boost/intrusive/list.hpp>
131
+ // #include <boost/intrusive/list_hook.hpp>
132
+
133
+ namespace scm2cpp
134
+ {
135
+
136
+
137
+
138
+
139
+
140
+ typedef boost::fusion::nil nil_type;
141
+ typedef boost::fusion::nil nil;
142
+ typedef boost::fusion::nil null_type;
143
+
144
+ //nil_type null;
145
+ nil_type null();
146
+ //nil_type nil();
147
+ //nil_type & nil = null;
148
+
149
+ typedef std::string string_type;
150
+
151
+ struct symbol_type : public string_type
152
+ { symbol_type(string_type & st) : string_type(st) {}
153
+ symbol_type(char *c) : string_type(c) {} };
154
+
155
+ inline symbol_type string_to_symbol(string_type &st){return symbol_type(st);}
156
+ inline string_type symbol_to_string(symbol_type &st){return st;}
157
+
158
+
159
+ typedef boost::mpl::vector<char,int,short,long,float,double,bool> c_number_types;
160
+ // Exact rationals, replacing rational: the four
161
+ // operations, normalization with the sign on the numerator, and
162
+ // ordering by cross-multiplication -- what the number-type order
163
+ // and eqv need, and nothing else.
164
+ struct rational {
165
+ int num, den;
166
+ static int gcd_(int a, int b)
167
+ { while (b) { int t = a % b; a = b; b = t; } return a < 0 ? -a : a; }
168
+ rational(int n = 0, int d = 1) : num(n), den(d) {
169
+ if (den < 0) { num = -num; den = -den; }
170
+ int g = gcd_(num, den); if (g > 1) { num /= g; den /= g; }
171
+ }
172
+ rational operator+(const rational& o) const
173
+ { return rational(num*o.den + o.num*den, den*o.den); }
174
+ rational operator-(const rational& o) const
175
+ { return rational(num*o.den - o.num*den, den*o.den); }
176
+ rational operator*(const rational& o) const
177
+ { return rational(num*o.num, den*o.den); }
178
+ rational operator/(const rational& o) const
179
+ { return rational(num*o.den, den*o.num); }
180
+ bool operator==(const rational& o) const
181
+ { return num == o.num && den == o.den; }
182
+ bool operator<(const rational& o) const
183
+ { return (long long)num*o.den < (long long)o.num*den; }
184
+ explicit operator double() const { return (double)num / den; }
185
+ };
186
+ inline std::ostream& operator<<(std::ostream& os, const rational& r)
187
+ { return os << r.num << "/" << r.den; }
188
+
189
+ typedef boost::mpl::vector<rational,std::complex<double> > not_c_number_types;
190
+ typedef boost::mpl::insert_range<c_number_types,boost::mpl::end<c_number_types>::type,not_c_number_types>::type number_types;
191
+
192
+ typedef boost::mpl::push_back<number_types,string_type>::type number_string_types;
193
+ typedef boost::mpl::push_back<number_types,symbol_type>::type number_symbol_types;
194
+ typedef boost::mpl::push_back<number_string_types,symbol_type>::type number_string_symbol_types;
195
+ typedef boost::mpl::push_back<not_c_number_types,string_type>::type not_c_number_string_types;
196
+ typedef boost::mpl::push_back<not_c_number_string_types,symbol_type>::type not_c_number_string_symbol_types;
197
+
198
+
199
+ template<typename Base
200
+ , class Enable = void
201
+ >
202
+ struct quoted
203
+ : Base
204
+ { Base & value; quoted(Base & base):Base(base),value(base) {} };
205
+
206
+
207
+ template<typename Base
208
+ //, class Enable = void
209
+ //,typename boost::disable_if<boost::is_arithmetic<Base> >::type
210
+ >
211
+ //struct quoted
212
+ struct quoted<Base
213
+ ,typename boost::disable_if<boost::is_arithmetic<Base> >::type>
214
+ : Base
215
+ { Base & value; quoted(Base & base):Base(base),value(base) {} };
216
+
217
+ template<typename Base>
218
+ struct quoted<Base
219
+ ,typename boost::enable_if<boost::is_arithmetic<Base> >::type>
220
+ #if 0
221
+ :
222
+ less_than_comparable < quoted<Base>,
223
+ equality_comparable < quoted<Base>,
224
+ less_than_comparable2 < quoted<Base>, Base,
225
+ equality_comparable2 < quoted<Base>, Base,
226
+ addable < quoted<Base>,
227
+ subtractable < quoted<Base>,
228
+ multipliable < quoted<Base>,
229
+ dividable < quoted<Base>,
230
+ addable2 < quoted<Base>, Base,
231
+ subtractable2 < quoted<Base>, Base,
232
+ subtractable2_left < quoted<Base>, Base,
233
+ multipliable2 < quoted<Base>, Base,
234
+ dividable2 < quoted<Base>, Base,
235
+ dividable2_left < quoted<Base>, Base,
236
+ incrementable < quoted<Base>,
237
+ decrementable < quoted<Base>
238
+ > > > > > > > > > > > > > > > >
239
+ #endif
240
+ : boost::operators<quoted<Base> >
241
+ {
242
+ Base value;
243
+ quoted(Base base):value(base) {}
244
+ bool operator<(const quoted<Base>& x) const { return value < x.value; }
245
+ bool operator==(const quoted<Base>& x) const { return value == x.value; }
246
+ quoted<Base>& operator+=(const quoted<Base>& x) { value+=x.value; return *this; }
247
+ quoted<Base>& operator-=(const quoted<Base>& x) { value-=x.value; return *this; }
248
+ quoted<Base>& operator*=(const quoted<Base>& x) { value*=x.value; return *this; }
249
+ quoted<Base>& operator/=(const quoted<Base>& x) { value/=x.value; return *this; }
250
+ quoted<Base>& operator%=(const quoted<Base>& x) { value%=x.value; return *this; }
251
+ quoted<Base>& operator|=(const quoted<Base>& x) { value|=x.value; return *this; }
252
+ quoted<Base>& operator&=(const quoted<Base>& x) { value&=x.value; return *this; }
253
+ quoted<Base>& operator^=(const quoted<Base>& x) { value^=x.value; return *this; }
254
+ quoted<Base>& operator++() { ++value; return *this; }
255
+ quoted<Base>& operator--() { --value; return *this; }
256
+
257
+ };
258
+
259
+
260
+
261
+ template<typename Base> quoted<Base> quote(Base &base){return quoted<Base>(base); }
262
+
263
+ template<typename Base> quoted<Base>
264
+ quote(Base base,typename boost::enable_if<boost::is_arithmetic<Base>, Base>::type* = 0)
265
+ {return quoted<Base>(base);}
266
+
267
+
268
+
269
+ template<typename Base>
270
+ struct unquoted
271
+ { Base & value;
272
+ unquoted(Base &base):value(base)
273
+ {} };
274
+
275
+ template<typename Base> unquoted<Base> unquote(Base &base){return unquoted<Base>(base); }
276
+
277
+ template<typename Base>
278
+ struct quasiquoted
279
+ { Base & value;
280
+ quasiquoted(Base &base):value(base)
281
+ {} };
282
+
283
+ template<typename Base> quasiquoted <Base> quasiquote(Base &base){return quasiquoted<Base>(base); }
284
+
285
+
286
+ #if 1
287
+ typedef boost::mpl::transform< number_string_types,quoted<boost::mpl::_1> >::type quoted_number_string_types ;
288
+ typedef boost::mpl::insert_range<quoted_number_string_types,
289
+ boost::mpl::end<quoted_number_string_types>::type,
290
+ number_string_symbol_types>::type value_types;
291
+
292
+
293
+ typedef boost::mpl::transform< number_types,quoted<boost::mpl::_1> >::type quoted_number_types ;
294
+ typedef boost::mpl::insert_range<quoted_number_types,
295
+ boost::mpl::end<quoted_number_types>::type,
296
+ number_symbol_types>::type scalar_types;
297
+
298
+ typedef boost::mpl::vector<char,int,short,long> int_types;
299
+ typedef boost::mpl::transform<int_types, quoted<boost::mpl::_1> >::type quoted_int_types ;
300
+ typedef boost::mpl::insert_range<quoted_int_types,
301
+ boost::mpl::end<quoted_int_types>::type,
302
+ int_types>::type int_quote_types;
303
+ #endif
304
+
305
+
306
+
307
+ /////////////////////////list begin///////////////////////////////////
308
+
309
+ #if CONSED_AS_STD_PAIR
310
+ #else
311
+ template<typename C ,typename D> struct
312
+ consed : std::pair<C,D> {};
313
+ #endif
314
+ #if 0
315
+ template<typename Seqs ,typename Last> struct
316
+ appended : std::pair<Seqs,Last> {};
317
+ #else
318
+ template<typename Seqs> struct
319
+ appended
320
+ {
321
+ Seqs &value;
322
+ appended(const Seqs & seqs):value(seqs){}
323
+ };
324
+
325
+ #endif
326
+
327
+ //s8vector signed exact integer in the range -(2^7) to (2^7)-1
328
+ // u8vector unsigned exact integer in the range 0 to (2^8)-1
329
+ // s16vector signed exact integer in the range -(2^15) to (2^15)-1
330
+ // u16vector unsigned exact integer in the range 0 to (2^16)-1
331
+ // s32vector signed exact integer in the range -(2^31) to (2^31)-1
332
+ // u32vector unsigned exact integer in the range 0 to (2^32)-1
333
+ // s64vector signed exact integer in the range -(2^63) to (2^63)-1
334
+ // u64vector unsigned exact integer in the range 0 to (2^64)-1
335
+
336
+
337
+
338
+
339
+
340
+ template<typename T> struct is_uniform_sequence :public boost::mpl::false_ {};
341
+ template<> struct is_uniform_sequence<std::string > :public boost::mpl::true_ {};
342
+ template<typename T> struct is_uniform_sequence<std::vector<T> > :public boost::mpl::true_ {};
343
+ template<typename T> struct is_uniform_sequence<std::list<T> > :public boost::mpl::true_ {};
344
+ template<typename T,std::size_t N> struct is_uniform_sequence<std::array<T,N> > :public boost::mpl::true_ {};
345
+
346
+
347
+ template<typename T,typename E> struct is_uniform_sequence_of :public boost::mpl::false_ {};
348
+ template<> struct is_uniform_sequence_of<std::string,char> :public boost::mpl::true_ {};
349
+ template<typename T> struct is_uniform_sequence_of<std::vector<T>,T> :public boost::mpl::true_ {};
350
+ template<typename T> struct is_uniform_sequence_of<std::list<T>,T> :public boost::mpl::true_ {};
351
+ template<typename T,std::size_t N> struct is_uniform_sequence_of<std::array<T,N>,T> :public boost::mpl::true_ {};
352
+
353
+ // cons and cdr on a uniform sequence used to answer with a
354
+ // boost::ptr_container view sharing the caller's storage; without
355
+ // boost they answer with a std::list copy, which is the persistent
356
+ // semantics Scheme meant anyway.
357
+ template<typename T> struct uniform_sequence_to_boost_ptr_sequence_view {typedef T type;};
358
+ template<typename T> struct uniform_sequence_to_boost_ptr_sequence_view<std::vector<T> >
359
+ {typedef std::list<T> type; };
360
+ template<typename T,std::size_t N> struct uniform_sequence_to_boost_ptr_sequence_view<std::array<T,N> >
361
+ {typedef std::list<T> type; };
362
+
363
+ template<typename T> struct
364
+ uniform_sequence_value_type
365
+ {typedef typename T::value_type type;};
366
+ template<typename C,typename D> struct
367
+ uniform_sequence_value_type<std::pair<C,D> >
368
+ {typedef C type;};
369
+
370
+
371
+ template<typename T> struct
372
+ is_sequence : boost::mpl::or_<
373
+ boost::fusion::traits::is_sequence<T>
374
+ ,is_uniform_sequence<T>
375
+ >{};
376
+
377
+ template<typename T> struct is_std_pair : boost::mpl::false_ {};
378
+ template<typename C,typename D> struct is_std_pair<std::pair<C,D> > : boost::mpl::true_ {};
379
+
380
+
381
+ template<typename T> struct
382
+ is_pair_type : boost::mpl::or_<
383
+ is_sequence<T>
384
+ ,is_std_pair<T>
385
+ >{};
386
+ template<> struct
387
+ is_pair_type<nil_type> : boost::mpl::false_ {};
388
+ template<typename T> bool is_pair(const T& dummy){return is_pair_type<T>::value; }
389
+
390
+
391
+ template<typename T> struct
392
+ is_list_type : is_sequence<T> {};
393
+ template<typename C,typename D> struct
394
+ is_list_type<std::pair<C,D> > : is_list_type<D> {};
395
+ template<typename T> bool is_list(const T& dummy){return is_list_type<T>::value;}
396
+
397
+
398
+
399
+
400
+ template<typename T> struct
401
+ is_subst_ref_object :public
402
+ boost::mpl::or_<
403
+ boost::is_array<T>
404
+ ,boost::fusion::traits::is_sequence<T>
405
+ ,is_uniform_sequence<T>
406
+ >{};
407
+ template<typename C,typename D> struct
408
+ is_subst_ref_object<std::pair<C,D> > :public boost::mpl::true_ {};
409
+ template<typename C,typename D> struct
410
+ is_subst_ref_object<std::map<C,D> > :public boost::mpl::true_ {};
411
+
412
+ template<typename T> struct
413
+ is_subst_copy_object :public boost::mpl::not_<is_subst_ref_object<T> > {};
414
+
415
+ template<typename T> struct to_subst_type : boost::mpl::if_<
416
+ is_subst_copy_object<T>
417
+ ,boost::mpl::identity<T>
418
+ ,boost::add_reference<T>
419
+ >::type {};
420
+
421
+
422
+ inline bool is_null(const nil_type & dummy){return true;}
423
+ template<typename T> bool is_null(const std::list<T> & l){return (l.size()==0);}
424
+ template<typename T> bool is_null(const std::vector<T> & l){return (l.size()==0);}
425
+ template<typename T> bool is_null(const T & dummy){return false;}
426
+
427
+
428
+
429
+ template<typename C, typename D> std::pair<
430
+ typename to_subst_type<C>::type
431
+ ,typename to_subst_type<D>::type>
432
+ cons(const C & c,const D & d
433
+ ,typename boost::disable_if<boost::mpl::or_<
434
+ boost::fusion::traits::is_sequence<D>
435
+ ,is_uniform_sequence_of<D,C>
436
+ > > ::type* =0
437
+ ){return std::pair<
438
+ typename to_subst_type<C>::type
439
+ ,typename to_subst_type<D>::type
440
+ > (const_cast<C &>(c),const_cast<D &>(d)); }
441
+ #if 1
442
+ template <typename T,typename Sequence>
443
+ boost::fusion::cons<typename to_subst_type<T>::type,Sequence >
444
+ cons(const T & e,
445
+ const Sequence & seq
446
+ ,typename boost::enable_if<
447
+ boost::fusion::traits::is_sequence<Sequence>
448
+ > ::type* = 0
449
+ ){ return boost::fusion::cons<typename to_subst_type<T>::type,Sequence >(e,seq);}
450
+ #else
451
+ template <typename T,typename Sequence>
452
+ typename boost::fusion::result_of::as_list<
453
+ typename boost::fusion::result_of::push_front<
454
+ Sequence,
455
+ typename to_subst_type<T>::type
456
+ >::type
457
+ >::type
458
+ cons(T & e,
459
+ Sequence &seq
460
+ ,typename boost::enable_if<
461
+ boost::fusion::traits::is_sequence<Sequence>
462
+ > ::type* = 0
463
+ ){ return boost::fusion::push_front<
464
+ Sequence,
465
+ typename to_subst_type<T>::type
466
+ >(seq, e);}
467
+ #endif
468
+
469
+ template <typename T>
470
+ std::list<T>
471
+ cons(const T & e,const nil_type & seq){
472
+ std::list<T> l;
473
+ l.push_front(e);
474
+ return l;}
475
+
476
+ template<typename C, typename D>
477
+ typename uniform_sequence_to_boost_ptr_sequence_view<D>::type
478
+ cons(C e
479
+ ,const D & d
480
+ ,typename boost::enable_if<boost::mpl::and_<
481
+ is_subst_copy_object<C>
482
+ ,is_uniform_sequence_of<D,C>
483
+ > >::type* = 0
484
+ ){typename uniform_sequence_to_boost_ptr_sequence_view<D>::type
485
+ l(d.begin(),d.end()); l.push_front(e); return l;}
486
+
487
+ template<typename C, typename D>
488
+ typename uniform_sequence_to_boost_ptr_sequence_view<D>::type
489
+ cons(C & e
490
+ ,const D & d
491
+ ,typename boost::enable_if<boost::mpl::and_<
492
+ is_subst_ref_object<C>
493
+ ,is_uniform_sequence_of<D,C>
494
+ > >::type* = 0
495
+ ){typename uniform_sequence_to_boost_ptr_sequence_view<D>::type
496
+ l(d.begin(),d.end()); l.push_front(e); return l;}
497
+
498
+
499
+ template<typename T> std::list<T>
500
+ make_uniform_list(const int n,const T & x)
501
+ {return std::list<T>(n,x);}
502
+
503
+ template<typename T> std::vector<T>
504
+ make_uniform_vector(const int n,const T & x)
505
+ {return std::vector<T>(n,x) ;}
506
+
507
+
508
+ template<typename T> const T* cdr(const T l[]){return l+1;}
509
+
510
+ template<typename C, typename D> D
511
+ cdr(const std::pair<C,D> & seq){return seq.second;}
512
+
513
+ template<typename Seq>
514
+ typename uniform_sequence_to_boost_ptr_sequence_view<Seq>::type
515
+ cdr(const Seq & seq
516
+ ,typename boost::enable_if<is_uniform_sequence<Seq> >::type* = 0 ){
517
+ typename Seq::const_iterator i=seq.begin();i++;
518
+ return
519
+ typename uniform_sequence_to_boost_ptr_sequence_view<Seq>::type(i,seq.end());
520
+ }
521
+
522
+
523
+ template <typename Sequence>
524
+ typename Sequence::cdr_type
525
+ //boost::fusion::cons<Sequence>::cdr_type
526
+ cdr( Sequence & seq
527
+ ,typename boost::enable_if<boost::fusion::traits::is_sequence<Sequence> >::type* = 0
528
+ ){return seq.cdr; }
529
+
530
+ #if 0
531
+ ///////dummy for list_ref
532
+ template <typename Sequence> Sequence
533
+ cdr(const Sequence & seq
534
+ ,typename boost::disable_if<boost::mpl::or_<
535
+ boost::fusion::traits::is_sequence<Sequence>
536
+ ,is_uniform_sequence<Sequence> >
537
+ >::type* = 0
538
+ ){ return seq;}
539
+ #endif
540
+
541
+
542
+ template<typename L, typename T> void
543
+ set_cdr(const L & l, const T & t){&cdr(l)=t;}
544
+
545
+
546
+ template<typename T> const T* car_pointer(const T l[]){return l;}
547
+ template<typename C, typename D> const C* car_pointer(const std::pair<C,D> & seq){return &seq.first;}
548
+ template <typename Sequence> typename Sequence::const_iterator
549
+ //typename boost::enable_if< is_uniform_sequence<Sequence>, typename Sequence::iterator>::type
550
+ car_pointer(const Sequence & seq
551
+ ,typename boost::enable_if<is_uniform_sequence<Sequence> >::type* = 0
552
+ ){return seq.begin();}
553
+ template <typename Sequence> typename boost::fusion::result_of::begin<Sequence const>::type
554
+ car_pointer(const Sequence & seq
555
+ ,typename boost::enable_if<boost::fusion::traits::is_sequence<Sequence> >::type* = 0
556
+ ){return boost::fusion::begin(seq);}
557
+
558
+ template<typename T> T car(const T l[]){return l[0];}
559
+ template<typename C, typename D> const C car(const std::pair<C,D> & seq){return seq.first;}
560
+ template <typename Sequence> typename Sequence::iterator::value_type
561
+ car(Sequence & seq
562
+ ,typename boost::enable_if<is_uniform_sequence<Sequence> >::type* = 0
563
+ ){return *seq.begin();}
564
+ // A const-reference overload so that temporaries can be passed. ptr_list
565
+ // has value_type T*, so use iterator::value_type as the non-const one does.
566
+ template <typename Sequence> typename Sequence::iterator::value_type
567
+ car(const Sequence & seq
568
+ ,typename boost::enable_if<is_uniform_sequence<Sequence> >::type* = 0
569
+ ){return *seq.begin();}
570
+ template <typename Sequence>
571
+ //typename Sequence::car_type
572
+ typename boost::fusion::result_of::value_of<typename boost::fusion::result_of::begin<Sequence const>::type>::type
573
+ car(const Sequence & seq
574
+ ,typename boost::enable_if<boost::fusion::traits::is_sequence<Sequence> >::type* = 0
575
+ ){
576
+ //return seq.car;
577
+ return boost::fusion::deref(boost::fusion::begin(seq));
578
+ }
579
+
580
+ #if 0
581
+ ///////dummy for list_ref
582
+ template <typename Sequence> Sequence
583
+ car(const Sequence & seq
584
+ ,typename boost::disable_if<boost::mpl::or_<
585
+ boost::fusion::traits::is_sequence<Sequence>
586
+ ,is_uniform_sequence<Sequence> >
587
+ >::type* = 0
588
+ ){ return seq;}
589
+ #endif
590
+
591
+
592
+ template<typename L, typename T> void
593
+ set_car(const L & l, const T & t){*car_pointer(l)=t;}
594
+
595
+ template<typename T> const T* list_ref_pointer(const T l[] ,int n ){return l+n;}
596
+ template<typename T> T list_ref(const T l[] ,int n ){return l[n];}
597
+ template<typename T> T list_ref(const std::vector<T> & l ,const int &n ){ return l[n];}
598
+ #if 0
599
+ template <typename Sequence> typename Sequence::iterator
600
+ list_ref_pointer(Sequence & seq,std::size_t n
601
+ ,typename boost::enable_if<is_uniform_sequence<Sequence> >::type* = 0){
602
+ typename Sequence ::iterator it = seq.begin();
603
+ std::advance(it, n);
604
+ return it;}
605
+ template <typename Sequence> typename Sequence::iterator::value_type
606
+ list_ref(Sequence & seq,std::size_t n
607
+ ,typename boost::enable_if<is_uniform_sequence<Sequence> >::type* = 0)
608
+ {return *list_ref_pointer(seq,n);}
609
+ #else
610
+ template<typename T>
611
+ typename std::list<T>::const_iterator
612
+ list_ref_pointer(const std::list<T> & l ,const int & n ){
613
+ typename std::list<T>::const_iterator it = l.begin();
614
+ std::advance(it, n);return it;}
615
+ template<typename T> T
616
+ list_ref(const std::list<T> & l ,const int &n ){
617
+ return *list_ref_pointer(l ,n );}
618
+
619
+
620
+ template<typename T>
621
+ typename std::vector<T>::const_iterator
622
+ list_ref_pointer(const std::vector<T> & l ,const int & n ){
623
+ typename std::vector<T>::const_iterator it = l.begin();
624
+ std::advance(it, n);
625
+ return it;}
626
+ #endif
627
+
628
+
629
+
630
+ #if 0
631
+ #if 0
632
+ template <typename Ret,typename Sequence> Ret
633
+ car_ret_type_chk(const Sequence & seq
634
+ ,typename boost::enable_if<boost::is_same<Ret, BOOST_TYPEOF_TML(car(seq)) > >::type* = 0
635
+ ){return car(seq);}
636
+ template <typename Ret,typename Sequence> Ret
637
+ car_ret_type_chk(const Sequence & seq
638
+ ,typename boost::disable_if<boost::is_same<Ret, BOOST_TYPEOF_TML(car(seq)) > >::type* = 0
639
+ ){return Ret();}
640
+ #endif
641
+
642
+ template <typename Ret,typename Sequence> Ret
643
+ list_ref(const Sequence & seq, int n
644
+ //,typename boost::disable_if<boost::fusion::traits::is_sequence<Sequence> >::type* = 0
645
+ ,typename boost::disable_if<boost::mpl::or_<
646
+ boost::fusion::traits::is_sequence<Sequence>
647
+ ,is_uniform_sequence<Sequence> >
648
+ >::type* = 0
649
+ ){
650
+ if(n<=0){ return
651
+ car(seq)
652
+ //car_ret_type_chk<Ret>(seq)
653
+ ;}
654
+ else{ return list_ref<Ret>(cdr(seq), n-1);}}
655
+ #endif
656
+
657
+ template<typename V>
658
+ struct fusion_at_n_functor
659
+ {
660
+ mutable int i;
661
+ int n;
662
+ mutable V value;
663
+ fusion_at_n_functor(int _n):i(0),n(_n){}
664
+ void operator()(const V & t) const
665
+ { if(i==n){value=t;} i++;}
666
+ template<typename T>
667
+ void operator()(const T & t) const
668
+ { i++;}
669
+ };
670
+
671
+ template <typename First,typename Last,typename AtN > void
672
+ list_ref_fusion_impl(First i,Last last,AtN &atn,boost::mpl::true_ ){}
673
+ template <typename First,typename Last,typename AtN > void
674
+ list_ref_fusion_impl(First i,Last last,AtN &atn,boost::mpl::false_ ){
675
+ //std::cout << atn.i << "," <<atn.n << ":" ;
676
+ if(atn.i == atn.n ){atn(boost::fusion::deref(i));}
677
+ else{
678
+ atn(boost::fusion::deref(i));
679
+ list_ref_fusion_impl(boost::fusion::next(i),last,atn,
680
+ boost::fusion::result_of::equal_to<
681
+ typename boost::fusion::result_of::next<First>::type,Last>());}}
682
+
683
+ template <typename Ret,typename Sequence> Ret
684
+ list_ref(Sequence & seq, int n
685
+ ,typename boost::enable_if<boost::fusion::traits::is_sequence<Sequence> >::type* = 0
686
+ ){
687
+ fusion_at_n_functor<Ret> atn(n);
688
+ #if 0
689
+ //dbug
690
+ boost::fusion::for_each(seq, atn);
691
+ #else
692
+ list_ref_fusion_impl(boost::fusion::begin(seq),boost::fusion::end(seq) ,atn,
693
+ boost::fusion::result_of::equal_to<
694
+ typename boost::fusion::result_of::begin<Sequence>::type,
695
+ typename boost::fusion::result_of::end<Sequence>::type>());
696
+ #endif
697
+ return atn.value;}
698
+
699
+
700
+ #if 1
701
+ //////////////////////////////////
702
+ template <typename Ret,typename C,typename D> Ret
703
+ list_ref(std::pair<C,D> & seq,int n
704
+ //,typename boost::enable_if<typename boost::mpl::contains <value_types,D>::type >::type* =0
705
+ ,typename boost::disable_if<boost::mpl::or_<
706
+ is_sequence<D>
707
+ ,is_std_pair<D>
708
+ > >::type* =0
709
+ ){
710
+ if(n<=0){ return
711
+ car(seq);
712
+ }else{
713
+ return seq.second;
714
+ }}
715
+ //////////////////////////////////
716
+
717
+ template <typename Ret,typename C,typename D> Ret
718
+ list_ref(const std::pair<C,D> & seq, int n)
719
+ {
720
+ if(n<=0){ return
721
+ car(seq)
722
+ ;}
723
+ else{ return list_ref<Ret>(cdr(seq), n-1);}}
724
+ #else
725
+
726
+ template <typename Ret,typename AtN> void
727
+ list_ref_pair_impl(std::pair<double,double> & seq,AtN &atn
728
+ ){
729
+ if(atn.i == atn.n ){
730
+ atn(seq.first);
731
+ }
732
+ else{
733
+ atn(seq.second);
734
+ }}
735
+
736
+
737
+ template <typename Ret,typename C,typename D,typename AtN> void
738
+ list_ref_pair_impl(std::pair<C,D> & seq,AtN &atn
739
+ //,typename boost::enable_if<typename boost::mpl::contains <value_types,D>::type >::type* =0
740
+ ){
741
+ if(atn.i == atn.n ){
742
+ atn(seq.first);
743
+ }
744
+ else{
745
+ atn(seq.second);
746
+ }}
747
+
748
+ template <typename Ret,typename C,typename D,typename AtN> void
749
+ list_ref_pair_impl(std::pair<C,D> & seq,AtN &atn
750
+ ,typename boost::enable_if<is_sequence<D> >::type* = 0
751
+ ){
752
+ if(atn.i == atn.n ){
753
+ atn(seq.first);
754
+ }
755
+ else{
756
+ atn(seq.first);
757
+ atn.value=list_ref<Ret>(seq.second,atn.n - atn.i );
758
+ }}
759
+
760
+ template <typename Ret,typename C,typename D,typename AtN> void
761
+ list_ref_pair_impl(std::pair<C,D> & seq,AtN &atn
762
+ ,typename boost::enable_if<is_std_pair<D> >::type* = 0
763
+ ){
764
+ if(atn.i == atn.n ){
765
+ atn(seq.first);
766
+ }
767
+ else{
768
+ atn(seq.first);
769
+ list_ref_pair_impl<Ret>(seq.second,atn);
770
+ }}
771
+
772
+ template <typename Ret,typename C,typename D> Ret
773
+ list_ref(const std::pair<C,D> & seq, int n)
774
+ {
775
+ fusion_at_n_functor<Ret> atn(n);
776
+ list_ref_pair_impl<Ret>(seq,atn);
777
+ return atn.value;
778
+ }
779
+
780
+ #endif
781
+
782
+ #if 0
783
+ template<typename C, typename D>
784
+ std::map<C,D>
785
+ append(const std::pair<C,D> &l1,const std::pair<C,D> &l2)
786
+ {
787
+ std::map<C,D> ll;
788
+ ll.insert(l1);
789
+ ll.insert(l2);
790
+ return ll;
791
+ }
792
+ #endif
793
+
794
+ #if 1
795
+ template<typename Seq>
796
+ typename uniform_sequence_to_boost_ptr_sequence_view<Seq>::type
797
+ append(const Seq & l1, const Seq & l2
798
+ ,typename boost::enable_if<is_uniform_sequence<Seq> >::type* = 0 ){
799
+ typedef typename Seq::iterator::value_type value_type;
800
+ typename Seq::const_reverse_iterator ir=l1.rbegin();
801
+ typename uniform_sequence_to_boost_ptr_sequence_view<Seq>::type ll(l2.begin(),l2.end());
802
+ for(std::size_t i=0;i<l1.size();i++){ll.push_front(new value_type(*ir++));}
803
+ return ll;
804
+ }
805
+ #else
806
+ template<typename Seq>
807
+ typename uniform_sequence_to_boost_ptr_sequence_view<Seq>::type
808
+ append(const Seq & l1, const Seq & l2
809
+ ,typename boost::enable_if<is_uniform_sequence<Seq> >::type* = 0 ){
810
+ Seq ll(l1);
811
+ ll.insert(l1.end(), l2.begin(),l2.end() );
812
+ return ll;
813
+ }
814
+ #endif
815
+
816
+
817
+ #if 1
818
+ template<typename Seq1,typename Seq2>
819
+ boost::fusion::joint_view<Seq1,Seq2>
820
+ append(Seq1 & l1, Seq2 & l2
821
+ ,typename boost::enable_if<boost::mpl::and_<
822
+ boost::fusion::traits::is_sequence<Seq1>
823
+ ,boost::fusion::traits::is_sequence<Seq2> >
824
+ >::type* = 0
825
+ ){
826
+ Seq1 l1b;
827
+ boost::fusion::copy(l1b,l1);
828
+ boost::fusion::joint_view<Seq1,Seq2> ll(l1b,l2);
829
+ return ll;
830
+ }
831
+ #endif
832
+
833
+
834
+ #if 0
835
+ template<typename Seq1,typename Seq2>
836
+ typename boost::fusion::result_of::as_list<
837
+ typename boost::fusion::result_of::push_back<
838
+ //seq1a_type
839
+ typename boost::fusion::result_of::as_list<
840
+ typename boost::fusion::result_of::erase<
841
+ Seq1,
842
+ typename boost::fusion::result_of::advance<
843
+ typename boost::fusion::result_of::begin<Seq1>::type,
844
+ boost::mpl::plus<
845
+ boost::fusion::result_of::size<Seq1>
846
+ ,boost::mpl::int_<-1> > >::type
847
+ >::type
848
+ >::type
849
+ ,
850
+ //result_back_type &
851
+ //Seq2
852
+ BOOST_TYPEOF(cons(boost::fusion::back(Seq1()),Seq2())) &
853
+ //BOOST_TYPEOF(cons(Seq1(),Seq2()))
854
+ >::type
855
+ >::type
856
+ append(Seq1 & seq1, Seq2 & seq2
857
+ ,typename boost::enable_if<
858
+ boost::mpl::and_<
859
+ boost::fusion::traits::is_sequence<Seq1>
860
+ ,boost::mpl::not_<
861
+ boost::fusion::traits::is_sequence<Seq2> > > >::type* = 0
862
+ ){
863
+ //Seq1 ll;boost::fusion::copy(ll,seq1);//Seq1 ll(seq1);
864
+ typedef
865
+ #if 0
866
+ boost::fusion::result_of::as_list<
867
+ boost::fusion::result_of::erase<
868
+ Seq1,
869
+ boost::fusion::result_of::advance<
870
+ boost::fusion::result_of::begin<Seq1>::type,
871
+ boost::mpl::plus<
872
+ boost::fusion::result_of::size<Seq1>
873
+ ,boost::mpl::int_<-1> > >::type
874
+ >::type>::type
875
+ #else
876
+ typename boost::fusion::result_of::as_list<
877
+ typename boost::fusion::result_of::erase<
878
+ Seq1,
879
+ typename boost::fusion::result_of::advance<
880
+ typename boost::fusion::result_of::begin<Seq1>::type,
881
+ boost::mpl::plus<
882
+ boost::fusion::result_of::size<Seq1>
883
+ ,boost::mpl::int_<-1> > >::type >::type>::type
884
+ #endif
885
+ seq1a_type;
886
+ seq1a_type ll(seq1);
887
+ //typedef boost::fusion::result_of::back<Seq1> seq1_back_type;
888
+ //typedef boost::result_of<cons(seq1_back_type,Seq2)>::type result_back_type;
889
+ //BOOST_TYPEOF(cons(boost::fusion::back(seq1),seq2));
890
+ //BOOST_TYPEOF(cons(seq1_back_type(),Seq2()));
891
+ //boost::type_of
892
+ //boost::fusion::erase(ll, back(vec));
893
+ //boost::fusion::back(ll)=cons(boost::fusion::back(seq1),seq2 );
894
+ return
895
+ boost::fusion::push_back(ll,cons(boost::fusion::back(seq1),seq2 ) );
896
+ //return ll;
897
+ }
898
+ #endif
899
+
900
+
901
+ #if 0
902
+ template<typename Seq1,typename Seq2>
903
+ appended<Seq1,Seq2>
904
+ append(Seq1 & l1, Seq2 & l2
905
+ //,typename boost::enable_if<boost::mpl::and_<
906
+ //boost::fusion::traits::is_sequence<Seq1>
907
+ //,is_uniform_sequence<Seq2> >
908
+ //>::type* = 0
909
+ ){
910
+ return appended<Seq1,Seq2>(l1,l2);
911
+ }
912
+ #endif
913
+
914
+ #if 0
915
+ template<typename Seq>
916
+ typename uniform_sequence_to_boost_ptr_sequence_view<
917
+ typename Seq::iterator::value_type
918
+ >::type
919
+ //typename Seq::iterator::value_type
920
+ append(Seq & ll
921
+ ,typename boost::enable_if<
922
+ boost::mpl::and_<
923
+ is_uniform_sequence<Seq>
924
+ ,is_uniform_sequence<
925
+ typename Seq::iterator::value_type
926
+ > >
927
+ >::type* = 0
928
+ )
929
+ {
930
+ //typedef typename Seq::iterator::value_type value_type;
931
+ typename Seq::const_reverse_iterator ir=ll.rbegin();
932
+ typename uniform_sequence_to_boost_ptr_sequence_view<
933
+ typename Seq::iterator::value_type
934
+ >::type
935
+ l((*ir).begin(),(*ir).end());
936
+ ir++;
937
+ for(std::size_t i=1;i<ll.size();i++){
938
+ l=append(*ir++,l);
939
+ }
940
+ return l;
941
+ }
942
+ #endif
943
+
944
+
945
+ #if 0
946
+ template<typename T>
947
+ boost::ptr_list<T, boost::view_clone_allocator>
948
+ append(std::list<T> &l1,std::list<T> &l2)
949
+ {
950
+ boost::ptr_list<T, boost::view_clone_allocator> ll(l2.begin(),l2.end());
951
+ //std::list<T>::const_iterator i
952
+ typename std::list<T>::reverse_iterator ir=l1.rbegin();
953
+ for(int i=0;i<l1.size();i++){
954
+ ll.push_front(
955
+ //&(*ir++)
956
+ //*ir++
957
+ //std::auto_ptr<T>(&(*ir++))
958
+ new T(*ir++)
959
+ //ptr_r2lvalue(*ir++)
960
+ );
961
+ }
962
+ return ll;
963
+ }
964
+ #endif
965
+
966
+
967
+ template<typename Seq>
968
+ boost::fusion::reverse_view<Seq>
969
+ reverse(Seq & seq
970
+ ,typename boost::enable_if<boost::fusion::traits::is_sequence<Seq> >::type* = 0){
971
+ return boost::fusion::reverse(seq) ;
972
+ }
973
+
974
+
975
+
976
+
977
+
978
+ /////////////////////////list end ///////////////////////////////////
979
+
980
+
981
+
982
+
983
+
984
+
985
+ //template<typename Base> quoted<Base> quote(Base &base){return quoted<Base>(base); }
986
+
987
+
988
+
989
+
990
+
991
+ typedef std::basic_ofstream<char> stream_type;
992
+
993
+ typedef boost::mpl::vector<std::basic_ofstream<char>
994
+ ,std::streambuf
995
+ ,std::ostream,std::istream
996
+ ,std::ifstream,std::ofstream
997
+ ,std::stringstream
998
+ ,std::istream,std::ostream
999
+ ,std::string
1000
+ > stream_types;
1001
+
1002
+ #if 0
1003
+ typedef boost::mpl::transform< number_string_types,quoted<boost::mpl::_1> >::type quoted_number_string_types ;
1004
+ typedef boost::mpl::insert_range<quoted_number_string_types,
1005
+ boost::mpl::end<quoted_number_string_types>::type,
1006
+ number_string_symbol_types>::type value_types;
1007
+
1008
+
1009
+ typedef boost::mpl::transform< number_types,quoted<boost::mpl::_1> >::type quoted_number_types ;
1010
+ typedef boost::mpl::insert_range<quoted_number_types,
1011
+ boost::mpl::end<quoted_number_types>::type,
1012
+ number_symbol_types>::type scalar_types;
1013
+
1014
+ typedef boost::mpl::vector<char,int,short,long> int_types;
1015
+ typedef boost::mpl::transform<int_types, quoted<boost::mpl::_1> >::type quoted_int_types ;
1016
+ typedef boost::mpl::insert_range<quoted_int_types,
1017
+ boost::mpl::end<quoted_int_types>::type,
1018
+ int_types>::type int_quote_types;
1019
+ #endif
1020
+
1021
+ inline bool is_eq(symbol_type & x, symbol_type & y) {return x==y;}
1022
+ //bool is_eq(string_type & x, string_type & y) {return (& x)==(& y);}
1023
+ template<typename T> bool is_eq(T x, T y,typename boost::enable_if<boost::is_integral<T>, T>::type* = 0) {return x==y;}
1024
+ template<typename T> bool is_eq(quoted<T> &x, quoted<T> &y,typename boost::enable_if<boost::is_integral<T>, T>::type* = 0) {return x.value==y.value;}
1025
+ template<typename T> bool is_eq(quoted<T> &x, T y,typename boost::enable_if<boost::is_integral<T>, T>::type* = 0) {return x.value==y;}
1026
+ template<typename T> bool is_eq(T x, quoted<T> &y,typename boost::enable_if<boost::is_integral<T>, T>::type* = 0) {return x==y.value;}
1027
+ template<typename T> bool is_eq(T & x, T & y,typename boost::disable_if<typename boost::mpl::contains <int_quote_types,T>::type >::type* =0) {return (&x)==(&y);}
1028
+ template<typename T1,typename T2> bool is_eq(T1 x, T2 y){return false;}
1029
+
1030
+
1031
+ //bool is_eqv(string_type & x, string_type & y) {return x==y;}
1032
+ inline bool is_eqv(symbol_type & x, symbol_type & y) {return x==y;}
1033
+ inline bool is_eqv(rational & x, rational & y) {return x==y;}
1034
+ inline bool is_eqv(quoted<rational > & x, rational & y) {return x.value==y;}
1035
+ inline bool is_eqv(rational & x, quoted<rational > & y) {return x==y.value;}
1036
+ inline bool is_eqv(quoted<rational > & x, quoted<rational > & y) {return x.value==y.value;}
1037
+ template<typename T> bool is_eqv(T x, T y,typename boost::enable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x==y;}
1038
+ template<typename T> bool is_eqv(quoted<T> &x, quoted<T> &y ,typename boost::enable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x.value==y.value;}
1039
+ template<typename T> bool is_eqv(quoted<T> &x, T y,typename boost::enable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x.value==y;}
1040
+ template<typename T> bool is_eqv(T x, quoted<T> &y,typename boost::enable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x==y.value;}
1041
+ template<typename T> bool is_eqv(T & x, T & y,typename boost::disable_if<typename boost::mpl::contains <scalar_types,T>::type >::type* =0) {return (&x)==(&y);}
1042
+ template<typename T1,typename T2> bool is_eqv(T1 x, T2 y){return false;}
1043
+
1044
+
1045
+ template<typename T1,typename T2> bool is_equal(const T1 & x, const T2 & y){return false;}
1046
+ template<typename T> bool is_equal(const T & x,const T & y){return ( ( x ) == ( y ) );}
1047
+ template<typename T> bool is_equal(const quoted<T> &x, const quoted<T> &y) {return is_equal(x.value,y.value);} ////////////
1048
+ //template<> bool is_equal(string_type & x, string_type & y) {return x==y;}
1049
+ //template<> bool is_equal(symbol_type & x, symbol_type & y) {return x==y;}
1050
+
1051
+ // //bool is_equal(quoted<symbol_type> & x, quoted<symbol_type> & y) {return x.value==y.value;}
1052
+ // template<typename T> bool is_equal(T x, T y,typename boost::enable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x==y;}
1053
+
1054
+ // //template<typename T> bool is_equal(T &x, T &y,typename boost::disable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x==y;}
1055
+ //template<typename T> bool is_equal(quoted<T> &x, T y,typename boost::enable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x.value==y;}
1056
+ //template<typename T> bool is_equal(T x, quoted<T> &y,typename boost::enable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x==y.value;}
1057
+ // //template<typename T> bool is_equal(quoted<T> &x, quoted<T> &y,typename boost::enable_if<boost::is_arithmetic<T>, T>::type* = 0) {return x.value==y.value;}
1058
+ template<typename T> bool is_equal(const quoted<T> &x,const T & y
1059
+ ,typename boost::enable_if<typename boost::mpl::contains <
1060
+ //not_c_number_string_types
1061
+ number_string_types
1062
+ ,T>::type >::type* =0) {return x.value==y;}
1063
+ template<typename T> bool is_equal(const T & x,const quoted<T> &y
1064
+ ,typename boost::enable_if<typename boost::mpl::contains <
1065
+ //not_c_number_string_types
1066
+ number_string_types
1067
+ ,T>::type >::type* =0) {return x==y.value;}
1068
+ // // template<typename T> bool is_equal(quoted<T> & x, quoted<T> &y
1069
+ // // ,typename boost::enable_if<typename boost::mpl::contains <not_c_number_string_types,T>::type >::type* =0) {return x.value==y.value;}
1070
+
1071
+
1072
+ // // template<typename T> bool is_equal(quoted<T> &x, quoted<T> &y
1073
+ // // ,typename boost::disable_if<typename boost::mpl::contains <value_types,T>::type >::type* =0)
1074
+ // // {return is_equal(x.value,y.value);}
1075
+ // // template<typename T> bool is_equal(T &x, quoted<T> &y
1076
+ // // ,typename boost::disable_if<typename boost::mpl::contains <value_types,T>::type >::type* =0)
1077
+ // // {return is_equal(x,y.value);}
1078
+
1079
+ // // template<typename T> bool is_equal(quoted<T> &x, T &y
1080
+ // // ,typename boost::disable_if<typename boost::mpl::contains <value_types,T>::type >::type* =0)
1081
+ // // {return is_equal(x.value,y);}
1082
+ // // template<typename T1,typename T2> bool is_equal(quoted<T1> &x, quoted<T2> &y
1083
+ // // ,typename boost::disable_if<typename boost::mpl::contains <value_types,T1>::type >::type* =0
1084
+ // // ,typename boost::disable_if<typename boost::mpl::contains <value_types,T2>::type >::type* =0
1085
+ // // )
1086
+ // // {return is_equal(x.value,y.value);}
1087
+ // template<typename T> bool is_equal(T & x, T & y
1088
+ // ,typename boost::disable_if<typename boost::mpl::contains <value_types,T>::type >::type* =0)
1089
+ // {return x==y;}
1090
+ // //number_string_symbol_types
1091
+
1092
+
1093
+
1094
+
1095
+ ///////variant shrink begin
1096
+
1097
+ //typedef boost::mpl::vector<int,rational,float,double,long double,std::complex<double> >::type scheme_ordered_number_types;
1098
+ //typedef scheme_ordered_number_types ordered_number_types;
1099
+ typedef boost::mpl::vector<int,rational,float,double,std::complex<double> >::type default_type_order;
1100
+
1101
+
1102
+
1103
+ ////////////mpl general util begin
1104
+
1105
+ template<typename TypeVec>
1106
+ struct is_mpl_sequence_single :
1107
+ boost::is_same<
1108
+ typename boost::mpl::size<TypeVec>::type
1109
+ ,typename boost::mpl::long_<1> >
1110
+ {};
1111
+
1112
+ template<typename TypeVec>
1113
+ struct mpl_sequence_if_single_then_element_else_variant : public
1114
+ boost::mpl::if_<
1115
+ typename is_mpl_sequence_single<TypeVec>::type
1116
+ ,
1117
+ typename
1118
+ boost::mpl::front<TypeVec>//::type
1119
+ //,typename TypeVec::type
1120
+ //,TypeVec
1121
+ ,typename boost::make_variant_over<TypeVec>
1122
+ >::type
1123
+ {};
1124
+
1125
+ template<typename TypeVec,typename IsLess>
1126
+ struct max_in_mpl_sequence_given_is_less_lambda : public
1127
+ boost::mpl::fold<
1128
+ TypeVec
1129
+ ,typename boost::mpl::front<TypeVec>::type
1130
+ ,boost::mpl::if_<
1131
+ boost::mpl::bind2<IsLess,boost::mpl::_1 , boost::mpl::_2 >
1132
+ //typename boost::mpl::apply<IsLess,boost::mpl::_1 , boost::mpl::_2 >//::type
1133
+ //typename IsLess::apply< boost::mpl::_1 , boost::mpl::_2 >
1134
+ //boost::mpl::true_
1135
+ ,boost::mpl::_2 , boost::mpl::_1 >
1136
+ >
1137
+ {};
1138
+
1139
+
1140
+ template<typename TypeVec,typename TypeOrder>
1141
+ struct mpl_vector_contains_filter : boost::mpl::filter_view<
1142
+ TypeVec
1143
+ ,boost::mpl::contains<TypeOrder,boost::mpl::_1>
1144
+ >
1145
+ {};
1146
+
1147
+ template<typename TypeVec,typename TypeOrder>
1148
+ struct mpl_vector_not_contains_filter : boost::mpl::filter_view<
1149
+ TypeVec
1150
+ ,boost::mpl::not_<boost::mpl::contains<TypeOrder,boost::mpl::_1> >
1151
+ >
1152
+ {};
1153
+
1154
+
1155
+
1156
+ ////////////mpl general util end
1157
+
1158
+
1159
+
1160
+ template<typename T1,typename T2,typename TypeOrder>
1161
+ struct is_less_in_orderd_mpl_sequence :
1162
+ boost::is_same <
1163
+ typename boost::mpl::find<
1164
+ typename boost::mpl::iterator_range<
1165
+ typename boost::mpl::find<TypeOrder,T2>::type ,
1166
+ typename boost::mpl::end<TypeOrder>::type
1167
+ >::type ,
1168
+ T1 >::type,
1169
+ typename boost::mpl::end<TypeOrder>::type >
1170
+ {};
1171
+
1172
+ typedef boost::mpl::lambda<is_less_in_orderd_mpl_sequence<boost::mpl::_1 , boost::mpl::_2 , default_type_order> >::type is_less_over_default_type_order_mpl_lambda;
1173
+
1174
+
1175
+ template<typename T1,typename TBase,typename TypeOrder>
1176
+ struct is_generalizable_to_using_generaize_order_sequence :
1177
+ boost::mpl::if_<
1178
+ typename
1179
+ boost::mpl::contains<TypeOrder,T1>::type
1180
+ ,
1181
+ typename
1182
+ boost::mpl::if_<
1183
+ typename
1184
+ boost::mpl::contains<TypeOrder,TBase>::type
1185
+ ,
1186
+ //typename
1187
+ is_less_in_orderd_mpl_sequence<T1,TBase,TypeOrder> //::type
1188
+ ,
1189
+ //typename
1190
+ boost::mpl::false_ //::type
1191
+ >::type
1192
+ ,
1193
+ //typename
1194
+ boost::mpl::false_ //::type
1195
+ > ::type
1196
+ {};
1197
+
1198
+ template<typename TypeOrder>
1199
+ struct generate_mpl_lambda_is_generalizable_to_from_type_order_sequence
1200
+ { typedef typename boost::mpl::lambda<is_generalizable_to_using_generaize_order_sequence<boost::mpl::_1 , boost::mpl::_2 , TypeOrder> >::type type;
1201
+ };
1202
+
1203
+ //typedef boost::mpl::lambda<is_generalizable_to_using_generaize_order_sequence<boost::mpl::_1 , boost::mpl::_2 , default_type_order> >::type is_generalizable_using_default_type_order_mpl_lambda;
1204
+
1205
+ typedef generate_mpl_lambda_is_generalizable_to_from_type_order_sequence<default_type_order>::type is_generalizable_using_default_type_order_mpl_lambda;
1206
+
1207
+
1208
+ //template<typename TypeVec,typename Greater>
1209
+ template<typename TypeVec,typename TypeBase,typename IsGeneralizable>
1210
+ struct generalize_type_sequence_to_base :
1211
+ boost::mpl::transform<
1212
+ TypeVec
1213
+ ,boost::mpl::if_<
1214
+ boost::mpl::bind2<IsGeneralizable,boost::mpl::_1 , TypeBase >
1215
+ ,TypeBase
1216
+ ,boost::mpl::_1>
1217
+ >
1218
+ {};
1219
+
1220
+
1221
+ template<typename TypeVec,typename IsGeneralizable>
1222
+ struct generaize_type_sequence_using_is_generalizable_to :
1223
+ boost::mpl::fold<
1224
+ TypeVec
1225
+ ,TypeVec
1226
+ ,
1227
+ //typename
1228
+ generalize_type_sequence_to_base<boost::mpl::_1,boost::mpl::_2,IsGeneralizable > //::type
1229
+ >::type
1230
+ {};
1231
+
1232
+
1233
+ template<typename TypeVec,typename IsGeneralizable=is_generalizable_using_default_type_order_mpl_lambda >
1234
+ struct make_variant_shrink_over : public
1235
+ //mpl_sequence_if_single_to_element
1236
+ mpl_sequence_if_single_then_element_else_variant<
1237
+ typename
1238
+ boost::mpl::unique<
1239
+ typename
1240
+ generaize_type_sequence_using_is_generalizable_to<TypeVec,IsGeneralizable>::type
1241
+ ,boost::is_same<boost::mpl::_1,boost::mpl::_2>
1242
+ >::type
1243
+ >//::type
1244
+ {};
1245
+
1246
+
1247
+
1248
+ template < BOOST_VARIANT_ENUM_PARAMS(typename T) >
1249
+ typename max_in_mpl_sequence_given_is_less_lambda<
1250
+ typename
1251
+ boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >::types
1252
+ ,is_generalizable_using_default_type_order_mpl_lambda>::type
1253
+ get_number(boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& x)
1254
+ {
1255
+ return boost::get<
1256
+ typename max_in_mpl_sequence_given_is_less_lambda<
1257
+ typename
1258
+ boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >::types
1259
+ ,is_generalizable_using_default_type_order_mpl_lambda>::type
1260
+ >(x);
1261
+ }
1262
+
1263
+
1264
+
1265
+ template<typename T>
1266
+ T & get_number(boost::optional<T &> x) { return *x;}
1267
+
1268
+ template<typename T>
1269
+ T & get_number(boost::optional<const T &> x) { return *x;}
1270
+
1271
+
1272
+ template<typename T>
1273
+ T get_number(boost::optional<T > x) { return *x;}
1274
+
1275
+ template<typename T>
1276
+ T get_number(T x)
1277
+ {
1278
+ return x;
1279
+ }
1280
+
1281
+
1282
+
1283
+ template<typename T> boost::optional<T &>
1284
+ optional_attach(T &x) { boost::optional<T &> y(x); return y; }
1285
+
1286
+ template<typename T> boost::optional<T &>
1287
+ optional_attach(const T &x)
1288
+ { boost::optional<T &> y(const_cast<T &>(x)); return y; }
1289
+
1290
+
1291
+
1292
+
1293
+
1294
+ // template<typename Seq,typename X>
1295
+ // struct mpl_adjoin_copy_vector : public
1296
+ // boost::mpl::fold<
1297
+ // //typename TypeVec::type ,
1298
+ // Seq ,
1299
+ // //typename Seq::type ,
1300
+ // boost::mpl::vector1<X> ,
1301
+ // //boost::mpl::insert<boost::mpl::_1,boost::mpl::_2>
1302
+ // boost::mpl::push_back<boost::mpl::_1,boost::mpl::_2> //::type
1303
+ // //boost::mpl::push_front<boost::mpl::_1 , boost::mpl::_2 >
1304
+ // >
1305
+ // {};
1306
+
1307
+
1308
+ ///////variant shrink end
1309
+
1310
+
1311
+
1312
+
1313
+
1314
+
1315
+
1316
+
1317
+ #if CONSED_AS_STD_PAIR
1318
+ #undef consd
1319
+ #endif
1320
+
1321
+
1322
+
1323
+ }
1324
+
1325
+
1326
+ //template<class T>void Func(T arg1, typename ::boost::enable_if_c< ::boost::is_same<T, int>::value >::type* =0) { ... }
1327
+
1328
+ //template<class T>void Func(T arg1, typename ::boost::enable_if_c< ::boost::is_same<T, string>::value >::type* =0) { ... }
1329
+
1330
+ template<typename F,typename ResultType>
1331
+ struct promise
1332
+ {
1333
+ //need partial specialization or enable_if
1334
+ //typedef typename boost::function_types::result_type<F>::type result_type;
1335
+ //typedef typename F::result_type result_type;
1336
+ typedef ResultType result_type;
1337
+ result_type result;
1338
+ bool is_evaled;
1339
+ F f;
1340
+ promise(F & _f):f(_f),is_evaled(false){}
1341
+ inline result_type operator()(){
1342
+ if(is_evaled)
1343
+ return result;
1344
+ else{
1345
+ result=f();
1346
+ is_evaled=true;
1347
+ return result;
1348
+ }}
1349
+ };
1350
+
1351
+
1352
+
1353
+
1354
+ #ifdef BOOST_NO_DECLTYPE
1355
+
1356
+ template<typename F> promise<F,typename boost::function_types::result_type<F>::type> make_promise(F &f){ return promise<F,typename boost::function_types::result_type<F>::type>(f);}
1357
+ template<typename ResultType,typename F> promise<F,ResultType> make_promise(F f){ return promise<F,ResultType>(f);}
1358
+ #else
1359
+
1360
+ template<typename F> auto make_promise(F f) -> promise<F,decltype(f())>{return promise<F,decltype(f())>(f);}
1361
+
1362
+ #endif
1363
+
1364
+ template<typename F> promise<boost::function<F>,typename boost::function<F>::result_type> make_promise(boost::function<F> &f){ return promise<boost::function<F>,typename boost::function<F>::result_type>(f);}
1365
+
1366
+
1367
+ #ifdef BOOST_NO_DECLTYPE
1368
+ #else
1369
+ // By reference: a promise memoises into itself, so forcing a copy computes
1370
+ // the value again and leaves the original still unevaluated. Taking it by
1371
+ // value made (force p) twice run the body twice, which is exactly what a
1372
+ // promise is for not doing. The const overload serves promises reached
1373
+ // through a const reference; those cannot memoise, but they still yield
1374
+ // the right value.
1375
+ template<typename PromiseType>
1376
+ auto force(PromiseType & promise) -> decltype(promise())
1377
+ {
1378
+ return promise();
1379
+ }
1380
+ template<typename PromiseType>
1381
+ auto force(const PromiseType & promise) -> decltype(PromiseType(promise)())
1382
+ {
1383
+ PromiseType copy(promise);
1384
+ return copy();
1385
+ }
1386
+ #endif
1387
+
1388
+ //////////////////////////////////////////////////////////////////////////
1389
+ // Delayed streams as a nominal type whose recursion is erased through
1390
+ // std::function. Expanding the type structurally yields an infinite type and
1391
+ // the occurs check rejects it; naming it lets unification compare element
1392
+ // types in a single step, so inference terminates.
1393
+ namespace scm2cpp {
1394
+
1395
+ template<typename T> struct stream_cell {
1396
+ typedef T value_type;
1397
+ T head;
1398
+ std::function<stream_cell<T>()> tail;
1399
+ stream_cell(){}
1400
+ stream_cell(const T& h, const std::function<stream_cell<T>()>& t)
1401
+ : head(h), tail(t) {}
1402
+ };
1403
+
1404
+ // Corresponds to (cons a (delay b)); wraps b without evaluating it.
1405
+ template<typename T, typename F>
1406
+ stream_cell<T> make_stream(const T& h, F f) {
1407
+ return stream_cell<T>(h, std::function<stream_cell<T>()>(f));
1408
+ }
1409
+
1410
+ template<typename T> T car(const stream_cell<T>& s) { return s.head; }
1411
+ template<typename T> std::function<stream_cell<T>()> cdr(const stream_cell<T>& s) { return s.tail; }
1412
+
1413
+ }
1414
+
1415
+ //////////////////////////////////////////////////////////////////////////
1416
+ #endif // SCM2CPP_MINIMAL
1417
+
1418
+ // An n-dimensional summed-area table. Built once over a flat row-major
1419
+ // array, it answers the sum over any axis-aligned box in O(2^n) additions
1420
+ // by inclusion-exclusion, instead of O(volume) by looping. The table is a
1421
+ // representation an inferencer may choose for an array whose only reads
1422
+ // are box sums; the choice is valid only if every write precedes the
1423
+ // first query, which build() makes explicit.
1424
+ namespace scm2cpp {
1425
+
1426
+ // A (vector e ...) literal: the arity is the array's extent, so the
1427
+ // whole thing is one fixed-size value. Elements are cast to the first
1428
+ // element's type, mirroring the inference rule that types the literal
1429
+ // from its first element.
1430
+ template<typename T>
1431
+ inline std::array<T, 1> make_array(T e0)
1432
+ { std::array<T, 1> a = {{ e0 }}; return a; }
1433
+ template<typename T, typename... Es>
1434
+ inline std::array<T, 1 + sizeof...(Es)> make_array(T e0, Es... es)
1435
+ { std::array<T, 1 + sizeof...(Es)> a = {{ e0, static_cast<T>(es)... }}; return a; }
1436
+
1437
+ // (make-vector N v) with a literal extent: one fixed-size value with
1438
+ // every element v. What boost::assign::list_of used to spell.
1439
+ template<typename T, std::size_t N>
1440
+ inline std::array<T, N> filled_array(T v)
1441
+ { std::array<T, N> a; a.fill(v); return a; }
1442
+
1443
+ // Dynamic-extent array parameters as views. A span is one pointer:
1444
+ // host code converts to it implicitly from std::vector or std::array,
1445
+ // device code from a raw device pointer, and the callee indexes it
1446
+ // the same either way. Writes go to the caller's memory -- what the
1447
+ // reference parameter meant, without the container crossing into
1448
+ // device code. The generated subset never resizes or measures a
1449
+ // parameter, so a pointer is the whole interface.
1450
+ // n is the length when the span was made from a container (or given
1451
+ // one), 0 when only a pointer was; the --cuda back end needs it to copy
1452
+ // the elements, nothing else reads it.
1453
+ template<typename T> struct span {
1454
+ T* p;
1455
+ std::size_t n;
1456
+ SCM2CPP_FN span(T* p) : p(p), n(0) {}
1457
+ SCM2CPP_FN span(T* p, std::size_t n) : p(p), n(n) {}
1458
+ span(std::vector<T>& v) : p(v.data()), n(v.size()) {}
1459
+ template<std::size_t N> span(std::array<T, N>& a) : p(a.data()), n(N) {}
1460
+ SCM2CPP_FN T& operator[](std::size_t i) const { return p[i]; }
1461
+ T* data() const { return p; }
1462
+ std::size_t size() const { return n; }
1463
+ };
1464
+ template<typename T> struct cspan {
1465
+ const T* p;
1466
+ std::size_t n;
1467
+ SCM2CPP_FN cspan(const T* p) : p(p), n(0) {}
1468
+ SCM2CPP_FN cspan(const T* p, std::size_t n) : p(p), n(n) {}
1469
+ cspan(span<T> s) : p(s.p), n(s.n) {}
1470
+ cspan(const std::vector<T>& v) : p(v.data()), n(v.size()) {}
1471
+ template<std::size_t N> cspan(const std::array<T, N>& a) : p(a.data()), n(N) {}
1472
+ SCM2CPP_FN const T& operator[](std::size_t i) const { return p[i]; }
1473
+ const T* data() const { return p; }
1474
+ std::size_t size() const { return n; }
1475
+ };
1476
+
1477
+ // A memo for a pure call: (memo-call M E KEY ...) is
1478
+ // memo_get(M, memo_key().add(KEY)...str(), [&]() { return E; })
1479
+ // M a hash table from the key's bytes to E's value (the translator's
1480
+ // (make-hash), an std::unordered_map<std::string, V>), so E runs once
1481
+ // per distinct key. A key is scalars by value and, through
1482
+ // add_range, the first N elements of an array -- the index set a
1483
+ // gather read, say. The bytes are the values' object representation,
1484
+ // so 0.0 and -0.0 are different keys (a miss, never a wrong hit).
1485
+ struct memo_key {
1486
+ std::string b;
1487
+ template<typename T> memo_key& add(const T& v) {
1488
+ b.append(reinterpret_cast<const char*>(&v), sizeof(T));
1489
+ return *this;
1490
+ }
1491
+ template<typename V> memo_key& add_range(const V& v, long n) {
1492
+ add(n);
1493
+ for (long i = 0; i < n; ++i) add(v[i]);
1494
+ return *this;
1495
+ }
1496
+ const std::string& str() const { return b; }
1497
+ };
1498
+ template<typename M, typename F>
1499
+ inline typename M::mapped_type memo_get(M& m, const std::string& k, F f) {
1500
+ auto it = m.find(k);
1501
+ if (it != m.end()) return it->second;
1502
+ typename M::mapped_type v = f();
1503
+ m.emplace(k, v);
1504
+ return v;
1505
+ }
1506
+
1507
+ template<typename T, int N> struct integral_image {
1508
+ std::vector<T> table; // padded with a zero border: dim[k]+1 per axis
1509
+ int dim[N]; // extents of the source array
1510
+ int stride[N]; // strides of the padded table, innermost last
1511
+
1512
+ integral_image() { for (int k = 0; k < N; ++k) { dim[k] = 0; stride[k] = 0; } }
1513
+
1514
+ // src is the flat row-major source; dims its extents, slowest axis first.
1515
+ template<typename Src>
1516
+ void build(const Src& src, const int (&dims)[N]) {
1517
+ std::size_t total = 1;
1518
+ for (int k = N; k-- > 0; ) {
1519
+ dim[k] = dims[k];
1520
+ stride[k] = int(total);
1521
+ total *= std::size_t(dims[k] + 1);
1522
+ }
1523
+ table.assign(total, T(0));
1524
+ // Fill: table[i0+1,..] = src[i0,..], then prefix-sum along each axis.
1525
+ std::size_t count = 1;
1526
+ for (int k = 0; k < N; ++k) count *= std::size_t(dim[k]);
1527
+ for (std::size_t s = 0; s < count; ++s) {
1528
+ std::size_t rem = s, t = 0;
1529
+ for (int k = N; k-- > 0; ) {
1530
+ int i = int(rem % std::size_t(dim[k])); rem /= std::size_t(dim[k]);
1531
+ t += std::size_t(i + 1) * std::size_t(stride[k]);
1532
+ }
1533
+ table[t] = src[s];
1534
+ }
1535
+ for (int k = 0; k < N; ++k)
1536
+ for (std::size_t t = 0; t < total; ++t)
1537
+ if (int(t / std::size_t(stride[k])) % (dim[k] + 1) != 0)
1538
+ table[t] += table[t - std::size_t(stride[k])];
1539
+ }
1540
+
1541
+ // Sum of the source over the closed box [lo[k], hi[k]] on every axis.
1542
+ T query(const int (&lo)[N], const int (&hi)[N]) const {
1543
+ T acc = T(0);
1544
+ for (unsigned corner = 0; corner < (1u << N); ++corner) {
1545
+ std::size_t t = 0; int sign = 1;
1546
+ for (int k = 0; k < N; ++k) {
1547
+ int edge = (corner >> k) & 1 ? lo[k] : hi[k] + 1; // 0..dim[k]
1548
+ if ((corner >> k) & 1) sign = -sign;
1549
+ t += std::size_t(edge) * std::size_t(stride[k]);
1550
+ }
1551
+ acc += sign > 0 ? table[t] : T(0) - table[t];
1552
+ }
1553
+ return acc;
1554
+ }
1555
+
1556
+ // The two-dimensional case, spelled out for the generated code.
1557
+ T query2(int i0, int i1, int j0, int j1) const {
1558
+ const int lo[2] = { i0, j0 }, hi[2] = { i1, j1 };
1559
+ return query(lo, hi);
1560
+ }
1561
+ };
1562
+
1563
+
1564
+ // Operator tags for monoid_table: the fold's operation as a type, so
1565
+ // the loop bodies inline with no function-pointer indirection.
1566
+ template<typename T> struct mt_add { static T op(T a, T b) { return a + b; } };
1567
+ template<typename T> struct mt_mul { static T op(T a, T b) { return a * b; } };
1568
+ template<typename T> struct mt_min { static T op(T a, T b) { return a < b ? a : b; } };
1569
+ template<typename T> struct mt_max { static T op(T a, T b) { return a < b ? b : a; } };
1570
+
1571
+ // The integral image generalised to a commutative monoid: the same
1572
+ // padded table, the borders holding the identity instead of zero, the
1573
+ // axis scans folding with OP. What is answerable shrinks with the
1574
+ // structure: prefix() reads an origin-anchored box straight out of the
1575
+ // table and needs nothing but associativity and an identity, which is
1576
+ // why -I can take any catalogued operator through here; the
1577
+ // inclusion-exclusion query of integral_image needs subtraction and
1578
+ // stays with the additive group.
1579
+ template<typename T, int N, typename OP> struct monoid_table {
1580
+ std::vector<T> table;
1581
+ int dim[N];
1582
+ int stride[N];
1583
+
1584
+ monoid_table() { for (int k = 0; k < N; ++k) { dim[k] = 0; stride[k] = 0; } }
1585
+
1586
+ template<typename Src>
1587
+ void build(const Src& src, const int (&dims)[N], T id) {
1588
+ std::size_t total = 1;
1589
+ for (int k = N; k-- > 0; ) {
1590
+ dim[k] = dims[k];
1591
+ stride[k] = int(total);
1592
+ total *= std::size_t(dims[k] + 1);
1593
+ }
1594
+ table.assign(total, id);
1595
+ std::size_t count = 1;
1596
+ for (int k = 0; k < N; ++k) count *= std::size_t(dim[k]);
1597
+ for (std::size_t s = 0; s < count; ++s) {
1598
+ std::size_t rem = s, t = 0;
1599
+ for (int k = N; k-- > 0; ) {
1600
+ int i = int(rem % std::size_t(dim[k])); rem /= std::size_t(dim[k]);
1601
+ t += std::size_t(i + 1) * std::size_t(stride[k]);
1602
+ }
1603
+ table[t] = src[s];
1604
+ }
1605
+ for (int k = 0; k < N; ++k)
1606
+ for (std::size_t t = 0; t < total; ++t)
1607
+ if (int(t / std::size_t(stride[k])) % (dim[k] + 1) != 0)
1608
+ table[t] = OP::op(table[t - std::size_t(stride[k])], table[t]);
1609
+ }
1610
+
1611
+ // The fold of the source over the origin-anchored box [0, hi[k]] on
1612
+ // every axis: one table read, no inverse anywhere.
1613
+ T prefix(const int (&hi)[N]) const {
1614
+ std::size_t t = 0;
1615
+ for (int k = 0; k < N; ++k)
1616
+ t += std::size_t(hi[k] + 1) * std::size_t(stride[k]);
1617
+ return table[t];
1618
+ }
1619
+ };
1620
+
1621
+ // (integral-image v n m) in the generated code: build a rank-2 table.
1622
+ template<typename T, typename Src>
1623
+ integral_image<T,2> make_integral_image2(const Src& src, int n, int m) {
1624
+ integral_image<T,2> ii;
1625
+ const int dims[2] = { n, m };
1626
+ ii.build(src, dims);
1627
+ return ii;
1628
+ }
1629
+
1630
+ }
1631
+
1632
+ #endif
1633
+
1634
+ //Copyright (C) 2011-2026 Hirotaka Niitsuma
1635
+ //
1636
+ //This header is part of Scm2Cpp and is distributed under the MIT License.
1637
+ //See the LICENSE file in the project root for the full text.
1638
+ //
1639
+ //Note: this header is included into the C++ that Scm2Cpp generates, and so
1640
+ //becomes part of the user's program. The MIT terms were chosen so that this
1641
+ //imposes no obligation on that program beyond preserving this notice.