cache2.h 23 KB
Newer Older
Damien Leroux's avatar
Damien Leroux committed
1
2
3
#ifndef _SPEL_CACHE_2_H_
#define _SPEL_CACHE_2_H_

4
5
#include "cache/base.h"
#include "disk_hashtable.h"
Damien Leroux's avatar
Damien Leroux committed
6
7


8
9
10
11
12
13
14
15
16
17
18
template <typename Ret, typename... Args>
computation_registry<value<Ret>, Ret (*) (Args...), value<typename clean_type<Args>::type>...>&
__get_registry()
{
    static computation_registry<value<Ret>, Ret (*) (Args...), value<typename clean_type<Args>::type>...> _reg_;
    /*MSG_DEBUG("Registry at " << (&_reg_));*/
    return _reg_;
}


template <typename Ret, typename... Args>
19
20
21
22
23
24
    struct async_computation<Ret(Args...)>;

template <typename Ret, typename... Args>
computation_registry<std::shared_ptr<async_computation<Ret(Args...)>>,
                     Ret (*) (Args...),
                     value<typename clean_type<Args>::type>...>&
25
26
__get_in_progress_registry()
{
27
28
29
    static computation_registry<std::shared_ptr<async_computation<Ret(Args...)>>,
                                Ret (*) (Args...),
                                value<typename clean_type<Args>::type>...> _reg_;
30
31
32
33
34
35
36
37
38
    /*MSG_DEBUG("Registry at " << (&_reg_));*/
    return _reg_;
}


template <typename Ret, typename... Args>
std::mutex& __get_in_progress_mutex() { static std::mutex _; return _; }

template <typename Ret, typename... Args>
39
40
41
void
unregister_task_in_progress(Ret (*f) (Args...),
                            const value<typename clean_type<Args>::type>&... args)
42
43
{
    __get_in_progress_mutex<Ret, Args...>().lock();
44
    __get_in_progress_registry<Ret, Args...>().remove(f, args...);
45
46
47
48
49
    __get_in_progress_mutex<Ret, Args...>().unlock();
}


template <typename Ret, typename... Args>
50
51
52
53
std::shared_ptr<async_computation<Ret(Args...)>>
register_task_in_progress(std::shared_ptr<async_computation<Ret(Args...)>> v,
                          Ret (*f) (Args...),
                          const value<typename clean_type<Args>::type>&... args)
54
{
Damien Leroux's avatar
Damien Leroux committed
55
    /*__get_in_progress_mutex<Ret, Args...>().lock();*/
56
    __get_in_progress_registry<Ret, Args...>().get(f, args...) = v;
Damien Leroux's avatar
Damien Leroux committed
57
    /*__get_in_progress_mutex<Ret, Args...>().unlock();*/
58
    return v;
59
60
}

Damien Leroux's avatar
Damien Leroux committed
61
62
63
64
65
66
67
68

template <typename Ret, typename... Args>
    struct async_computation<Ret(Args...)> {
        typedef async_computation<Ret(Args...)> this_type;
        typedef Ret value_type;
        typedef Ret (*computation_function_pointer_type) (Args...);
        typedef std::packaged_task<Ret(Args...)> task_type;

69
        async_computation(CachingPolicy _Sync, computation_function_pointer_type func,
Damien Leroux's avatar
Damien Leroux committed
70
71
72
                          const value<typename clean_type<Args>::type>&... args)
            : dependencies(args...)
            , m_storage()
73
74
75
76
            , m_future(active_settings
                        ->enqueue(_Sync,
                            [=] (Args... args)
                            {
Damien Leroux's avatar
Damien Leroux committed
77
78
79
80
81
82
                                std::string func_name = get_func_name(func);
                                chrono_trace _(func_name);
                                std::thread::id this_id = std::this_thread::get_id();
                                active_settings->thread_stacks[this_id].push_back(func_name);
                                /*MSG_DEBUG("[" << this_id << ',' << (this_id == active_settings->main_thread) << "] ENTER " << func_name);*/
                                msg_handler_t::run_hooks();
83
                                Ret ret = func(args...);
Damien Leroux's avatar
Damien Leroux committed
84
85
86
                                /*MSG_DEBUG("[" << this_id << ',' << (this_id == active_settings->main_thread) << "] LEAVE " << func_name);*/
                                active_settings->thread_stacks[this_id].pop_back();
                                msg_handler_t::run_hooks();
87
                                unregister_task_in_progress(func, {args}...);
88
89
                                return ret;
                            }, *args...))
90
            , mutex()
91
        {  }
Damien Leroux's avatar
Damien Leroux committed
92

Damien Leroux's avatar
Damien Leroux committed
93
94
        async_computation(CachingPolicy _Sync, std::function<Ret(Args...)>& proxy,
                          computation_function_pointer_type func,
Damien Leroux's avatar
Damien Leroux committed
95
96
97
                          const value<typename clean_type<Args>::type>&... args)
            : dependencies(args...)
            , m_storage()
Damien Leroux's avatar
Damien Leroux committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
            /*, m_future(active_settings->enqueue(_Sync, func, *args...))*/
            , m_future(active_settings
                        ->enqueue(_Sync,
                            [=] (Args... args)
                            {
                                std::string func_name = get_func_name(func);
                                chrono_trace _(func_name);
                                std::thread::id this_id = std::this_thread::get_id();
                                active_settings->thread_stacks[this_id].push_back(func_name);
                                /*MSG_DEBUG("[" << this_id << ',' << (this_id == active_settings->main_thread) << "] ENTER " << func_name);*/
                                msg_handler_t::run_hooks();
                                Ret ret = proxy(args...);
                                /*MSG_DEBUG("[" << this_id << ',' << (this_id == active_settings->main_thread) << "] LEAVE " << func_name);*/
                                active_settings->thread_stacks[this_id].pop_back();
                                msg_handler_t::run_hooks();
                                unregister_task_in_progress(func, {args}...);
                                return ret;
                            }, *args...))
116
            /*, m_future(std::async(func, *args...))*/
117
            , mutex()
Damien Leroux's avatar
Damien Leroux committed
118
119
120
121
        {}

        value_type& __get_noconst()
        {
Damien Leroux's avatar
Damien Leroux committed
122
123
124
125
            /*std::lock_guard<decltype(mutex)> read_guard(mutex);*/
            /*if (m_future.valid()) {*/
                /*return m_storage = m_future.get();*/
            /*}*/
126
            mutex.lock();
Damien Leroux's avatar
Damien Leroux committed
127
            if (m_future.valid()) {
Damien Leroux's avatar
Damien Leroux committed
128
                m_storage = m_future.get();
Damien Leroux's avatar
Damien Leroux committed
129
            }
Damien Leroux's avatar
Damien Leroux committed
130
            mutex.unlock();
Damien Leroux's avatar
Damien Leroux committed
131
132
133
134
135
136
137
138
139
140
141
142
            return m_storage;
        }

        const value_type& __get_const() const
        {
            return const_cast<this_type*>(this)->__get_noconst();
        }

    protected:
        std::tuple<value<typename clean_type<Args>::type>...> dependencies;
        value_type m_storage;
        std::future<Ret> m_future;
Damien Leroux's avatar
Damien Leroux committed
143
        std::recursive_mutex mutex;
Damien Leroux's avatar
Damien Leroux committed
144
145
    };

146
147
148
149
150
151
template <typename Ret, typename... Args>
std::shared_ptr<async_computation<Ret(Args...)>>
    make_async_computation(CachingPolicy& _Sync,
                           Ret (*func) (Args...),
                           const value<typename clean_type<Args>::type>&... args)
    {
152
153
154
155
156
        /*struct _mac_guard {*/
            /*_mac_guard() { __get_in_progress_mutex<Ret, Args...>().lock(); }*/
            /*~_mac_guard() { __get_in_progress_mutex<Ret, Args...>().unlock(); }*/
        /*} _;*/
        std::lock_guard<std::mutex> scope_lock(__get_in_progress_mutex<Ret, Args...>());
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        auto& r = __get_in_progress_registry<Ret, Args...>();
        auto exists = r.find(func, args...);
        if (exists) {
            return *exists;
        } else {
            std::shared_ptr<async_computation<Ret(Args...)>>
                ac(new async_computation<Ret(Args...)>(_Sync, func, args...));
            return r.get(func, args...) = register_task_in_progress(ac, func, args...);
        }
    }


template <typename Ret, typename... Args>
std::shared_ptr<async_computation<Ret(Args...)>>
    make_async_computation(CachingPolicy& _Sync,
                           Ret (*func) (Args...),
                           std::function<Ret(Args...)>& proxy,
                           const value<typename clean_type<Args>::type>&... args)
    {
176
177
178
179
180
        /*struct _mac_guard {*/
            /*_mac_guard() { __get_in_progress_mutex<Ret, Args...>().lock(); }*/
            /*~_mac_guard() { __get_in_progress_mutex<Ret, Args...>().unlock(); }*/
        /*} _;*/
        std::lock_guard<std::mutex> scope_lock(__get_in_progress_mutex<Ret, Args...>());
181
182
183
184
185
186
        auto& r = __get_in_progress_registry<Ret, Args...>();
        auto exists = r.find(func, args...);
        if (exists) {
            return *exists;
        } else {
            std::shared_ptr<async_computation<Ret(Args...)>>
Damien Leroux's avatar
Damien Leroux committed
187
                ac(new async_computation<Ret(Args...)>(_Sync, proxy, func, args...));
188
189
190
191
            return r.get(func, args...) = register_task_in_progress(ac, func, args...);
        }
    }

Damien Leroux's avatar
Damien Leroux committed
192

193
194
195
196
197
198
#ifndef NDEBUG
#define THIS_ARG_MAY_BE_UNUSED(x) x
#else
#define THIS_ARG_MAY_BE_UNUSED(x)
#endif

Damien Leroux's avatar
Damien Leroux committed
199
200
201
202
template <typename Ret, typename... Args>
    struct cached_computation<Ret(Args...)> {
        typedef Ret value_type;

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
        cached_computation(const std::string& name, Ret (*f) (Args...), const value<typename clean_type<Args>::type>&... THIS_ARG_MAY_BE_UNUSED(args))
            : m_name(name)
            , m_func(f)
#ifndef NDEBUG
            , dump_call()
#endif
        {
#ifndef NDEBUG
            dump_call = [=] () {
                std::stringstream ss;
                ss << m_name << '(';
                std::vector<std::string> avec;
                do_with_arg_pack(avec.push_back(MESSAGE(args)));
                for (size_t i = 0; i < avec.size() - 1; ++i) {
                    ss << avec[i] << ", ";
                }
                ss << avec.back() << ')';
                return ss.str();
            };
#endif
            /*MSG_DEBUG("NEW CACHED_COMPUTATION WITH ARGS " << dump());*/
            /*MSG_DEBUG("MD5 (accum) = " << m_md5_hash.accum);*/
            /*MSG_DEBUG("MD5 (append) = " << m_md5_hash.append);*/
            /*std::cout << "NEW CACHED_COMPUTATION " << m_md5_hash.accum << ' ' << m_md5_hash.append << std::endl;*/
        }

        std::string
            dump()
            {
#ifndef NDEBUG
                return dump_call();
#else
                return m_name;
#endif
            }

        Ret operator () (Args... x)
        {
241
            static disk_hashtable<Ret(Args...)> dht(MESSAGE(active_settings->work_directory << "/cache/" << m_name));
242
243
244
245
246
247
248
249
250
251
252
253
            return dht.get_or_compute(m_func, std::forward<Args>(x)...);
        }

        std::string m_name;
        Ret (*m_func) (Args...);

#ifndef NDEBUG
        std::function<std::string()> dump_call;
#endif
    };


Damien Leroux's avatar
Damien Leroux committed
254
255
256
257
258
template <typename Ret, typename... Args>
    struct computed_value<Ret(Args...)> : generic_value_interface<Ret> {
        typedef Ret value_type;
        typedef std::function<Ret(Args...)> computation_type;

259
        computed_value(CachingPolicy _Sync, Ret (*func) (Args...), const value<typename clean_type<Args>::type>&... args)
Damien Leroux's avatar
Damien Leroux committed
260
            : m_hash(compute_hash(args...))
261
            , m_task(make_async_computation<Ret, Args...>(_Sync, func, args...))
Damien Leroux's avatar
Damien Leroux committed
262
263
264
265
        {}

        virtual
            value_type& operator * ()
266
            override { return m_task->__get_noconst(); }
Damien Leroux's avatar
Damien Leroux committed
267
268
        virtual
            value_type* operator -> ()
269
            override { return &m_task->__get_noconst(); }
Damien Leroux's avatar
Damien Leroux committed
270
271
        virtual
            const value_type& operator * () const
272
            override { return m_task->__get_const(); }
Damien Leroux's avatar
Damien Leroux committed
273
274
        virtual
            const value_type* operator -> () const
275
            override { return &m_task->__get_const(); }
Damien Leroux's avatar
Damien Leroux committed
276
277
278
279
280
281
282
283

        virtual
            size_t hash() const
            override { return m_hash; }
        virtual
            md5_digest& md5(md5_digest& md) const
            override { return md; }

284
    /*protected:*/
Damien Leroux's avatar
Damien Leroux committed
285
        size_t m_hash;
286
        std::shared_ptr<async_computation<Ret(Args...)>> m_task;
Damien Leroux's avatar
Damien Leroux committed
287
288
289
290
291
292
293
    };

template <typename Ret, typename... Args>
    struct cached_computed_value<Ret(Args...)> : generic_value_interface<Ret> {
        typedef Ret value_type;
        typedef std::function<Ret(Args...)> computation_type;

294
        cached_computed_value(CachingPolicy _Sync, Ret (*func) (Args...), const value<typename clean_type<Args>::type>&... args)
295
            : m_comp(get_func_name(func), func, args...)
Damien Leroux's avatar
Damien Leroux committed
296
            , m_comp_proxy([this](Args... x) { return m_comp(x...); })
297
            , m_task(make_async_computation<Ret, Args...>(_Sync, func, m_comp_proxy, args...))
Damien Leroux's avatar
Damien Leroux committed
298
        {}
299
300
            /*m_hash = m_comp.m_md5_hash.md5.context;*/
        /*}*/
Damien Leroux's avatar
Damien Leroux committed
301
302
303

        virtual
            value_type& operator * ()
304
            override { return m_task->__get_noconst(); }
Damien Leroux's avatar
Damien Leroux committed
305
306
        virtual
            value_type* operator -> ()
307
            override { return &m_task->__get_noconst(); }
Damien Leroux's avatar
Damien Leroux committed
308
309
        virtual
            const value_type& operator * () const
310
            override { return m_task->__get_const(); }
Damien Leroux's avatar
Damien Leroux committed
311
312
        virtual
            const value_type* operator -> () const
313
            override { return &m_task->__get_const(); }
Damien Leroux's avatar
Damien Leroux committed
314
315

        virtual
316
317
318
            size_t hash() const override
            /*override { return m_hash; }*/
            {
319
                (void)m_task->__get_const();
320
321
322
                MSG_ERROR("hash is not implemented for cached_computed_value", "");
                return 0;
                /*return m_comp.m_md5_hash.md5.context;*/
323
            }
Damien Leroux's avatar
Damien Leroux committed
324
        virtual
325
326
            md5_digest& md5(md5_digest& md) const override
            {
327
                (void)m_task->__get_const();
328
329
330
331
                /*return md.blend(m_comp.m_md5_hash.md5.context);*/
                MSG_ERROR("md5 is not implemented for cached_computed_value", "");
                /*return md.blend(m_comp.m_md5_hash.md5.context);*/
                return md;
332
333
            }
            /*override { return md; }*/
Damien Leroux's avatar
Damien Leroux committed
334

335
    /*protected:*/
Damien Leroux's avatar
Damien Leroux committed
336
337
        cached_computation<Ret(Args...)> m_comp;
        std::function<Ret(Args...)> m_comp_proxy;
338
        std::shared_ptr<async_computation<Ret(Args...)>> m_task;
339
        /*size_t m_hash;*/
Damien Leroux's avatar
Damien Leroux committed
340
341
342
    };


Damien Leroux's avatar
Damien Leroux committed
343
344
345
/*template <typename X>*/
/*value<X> as_value(X&& x) { return {x}; }*/

Damien Leroux's avatar
Damien Leroux committed
346
347
348
349
350
351
352
353
354
355
356
#if 0
template <typename ValueType, typename... AllArgs> struct registry_impl_type;
template <typename ValueType>
    struct registry_impl_type<ValueType> { typedef ValueType type; };
template <typename ValueType, typename Arg0, typename... OtherArgs>
    struct registry_impl_type<ValueType, Arg0, OtherArgs...> {
        typedef std::unordered_map<Arg0, registry_impl_type<ValueType, OtherArgs...>::type> type;
    };
#endif


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#if 0
static inline
std::mutex& __get_lock(void* fptr)
{
    struct m_wrap { std::mutex mutex; m_wrap() : mutex() {} m_wrap(const m_wrap&) : mutex() {} };
    static std::unordered_map<void*, m_wrap> _;
    return _[fptr].mutex;
    /*auto it = _.find(fptr);*/
    /*if (it == _.end()) {*/
        /*bool discard;*/
        /*std::tie(it, discard) = _.insert({fptr, {}});*/
    /*}*/
    /*return it->second;*/
}
#endif
Damien Leroux's avatar
Damien Leroux committed
372
373
374


template <typename Ret, typename... Args>
375
376
377
struct with_disk_cache_traits {
	typedef cached_computed_value<Ret(Args...)> type;
};
Damien Leroux's avatar
Damien Leroux committed
378
379

template <typename Ret, typename... Args>
380
381
382
struct without_disk_cache_traits {
	typedef computed_value<Ret(Args...)> type;
};
Damien Leroux's avatar
Damien Leroux committed
383

384
385
template <int _Policy, typename Ret, typename... Args>
struct disk_cache_traits;
Damien Leroux's avatar
Damien Leroux committed
386

387
template <typename Ret, typename... Args>
388
struct disk_cache_traits<Oneshot, Ret, Args...> : without_disk_cache_traits<Ret, Args...> {};
389
390

template <typename Ret, typename... Args>
391
struct disk_cache_traits<Disk, Ret, Args...> : with_disk_cache_traits<Ret, Args...> {};
Damien Leroux's avatar
Damien Leroux committed
392

393
394
395
396
397
398
399
template <typename Ret, typename... Args>
struct with_mem_cache_traits {
	typedef value<Ret>& return_type;

	template <typename _Maker>
		static
		return_type
400
		create(CachingPolicy _Sync, Ret (&f) (Args...), const clean_value_type<Args>&... x)
401
		{
402
403
            static std::recursive_mutex _;
            std::lock_guard<decltype(_)> lock_guard(_);
404

405
            /*value<Ret>* ret_in_progress = __get_in_progress_registry<Ret, Args...>().find(&f, x...);*/
406

407
408
409
            /*if (ret_in_progress) {*/
                /*return *ret_in_progress;*/
            /*}*/
410

411
412
413
			/*MSG_DEBUG("new value with mem cache");*/
			return_type ret = __get_registry<Ret, Args...>().get(&f, x...);
			if (!ret.valid()) {
414
				ret = new _Maker(_Sync, f, x...);
415
416
417
418
419
420
421
422
423
424
425
426
			}
			return ret;
		}
};

template <typename Ret, typename... Args>
struct without_mem_cache_traits {
	typedef value<Ret> return_type;

	template <typename _Maker>
		static
		return_type
427
		create(CachingPolicy _Sync, Ret (&f) (Args...), const clean_value_type<Args>&... x)
428
		{
429
430
            static std::recursive_mutex _;
            std::lock_guard<decltype(_)> lock_guard(_);
431
            /*value<Ret>* ret_in_progress = __get_in_progress_registry<Ret, Args...>().find(&f, x...);*/
432

433
434
435
            /*if (ret_in_progress) {*/
                /*return *ret_in_progress;*/
            /*}*/
436

437
			/*MSG_DEBUG("new value without mem cache");*/
438
			return_type ret = new _Maker(_Sync, f, x...);
439
440
441
442
443
444
445
446
			return ret;
		}
};

template <int _Policy, typename Ret, typename... Args>
struct mem_cache_traits;

template <typename Ret, typename... Args>
447
struct mem_cache_traits<Oneshot, Ret, Args...> : without_mem_cache_traits<Ret, Args...> {};
448
449

template <typename Ret, typename... Args>
450
struct mem_cache_traits<Mem, Ret, Args...> : with_mem_cache_traits<Ret, Args...> {};
451

452
453
template <CachingPolicy _Policy = Oneshot, typename Ret, typename... Args>
typename mem_cache_traits<_Policy & Mem, Ret, Args...>::return_type
454
455
make_value(Ret (&f) (Args...), const clean_value_type<Args>&... x)
{
456
457
458
	typedef mem_cache_traits<_Policy & Mem, Ret, Args...> mem_policy;
	typedef disk_cache_traits<_Policy & Disk, Ret, Args...> disk_policy;
	return mem_policy::template create<typename disk_policy::type>(_Policy & Sync, f, x...);
459
}
Damien Leroux's avatar
Damien Leroux committed
460
461


462
463
template <typename... X> struct tuple;

464
template <CachingPolicy C, typename F, typename R, typename P> struct make_coll_impl;
Damien Leroux's avatar
Damien Leroux committed
465
466
467
468
469
470
471
472
473
474

template <typename A, typename B> struct types_must_be_identical : std::integral_constant<bool, false> {};
template <> struct types_must_be_identical<int, double> : std::integral_constant<bool, false> {};
template <> struct types_must_be_identical<double, int> : std::integral_constant<bool, false> {};
template <typename A> struct types_must_be_identical<A, A> : std::integral_constant<bool, true> {};
template <typename A> struct types_must_be_identical<const A&, A> : std::integral_constant<bool, true> {};
template <typename A> struct types_must_be_identical<A&, A> : std::integral_constant<bool, true> {};
template <typename A> struct types_must_be_identical<const A, A> : std::integral_constant<bool, true> {};

template <typename LA, typename LB> struct type_lists_must_be_identical;
475
template <> struct type_lists_must_be_identical<tuple<>, tuple<>> {};
Damien Leroux's avatar
Damien Leroux committed
476
477

template <typename A0, typename... A, typename B0, typename... B>
478
479
    struct type_lists_must_be_identical<tuple<A0, A...>, tuple<B0, B...>>
            : type_lists_must_be_identical<tuple<A...>, tuple<B...>> {
Damien Leroux's avatar
Damien Leroux committed
480
481
482
        static_assert(types_must_be_identical<A0, B0>::value, "INVALID PARAMETER TYPE.");
    };

483
template <CachingPolicy _Policy, typename Ret, typename... FuncArgs, typename... ErrArgs>
484
struct make_coll_impl<_Policy, Ret(FuncArgs...), tuple<>, tuple<ErrArgs...>> {
Damien Leroux's avatar
Damien Leroux committed
485
486
487
488
    void operator () (collection<Ret>& coll,
                      Ret (&f) (FuncArgs...),
                      ErrArgs... errargs)
    {
489
        type_lists_must_be_identical<tuple<FuncArgs...>, tuple<typename ErrArgs::value_type...>>();
490
        coll.push_back(make_value<_Policy>(f, errargs...));
Damien Leroux's avatar
Damien Leroux committed
491
492
493
    }
};

494
template <CachingPolicy _Policy, typename Ret, typename... FuncArgs>
495
struct make_coll_impl<_Policy, Ret(FuncArgs...), tuple<>, tuple<clean_value_type<FuncArgs>...>> {
Damien Leroux's avatar
Damien Leroux committed
496
497
498
499
    void operator () (collection<Ret>& coll,
                      Ret (&f) (FuncArgs...),
                      const value<typename clean_type<FuncArgs>::type>&... pargs)
    {
500
        coll.push_back(make_value<_Policy>(f, pargs...));
Damien Leroux's avatar
Damien Leroux committed
501
502
503
504
    }
};


505
template <CachingPolicy _Policy, typename Ret, typename... FuncArgs, typename VT, typename... ArgsRemaining, typename... PreviousArgs>
506
struct make_coll_impl<_Policy, Ret(FuncArgs...), tuple<value<VT>, ArgsRemaining...>, tuple<PreviousArgs...>> {
Damien Leroux's avatar
Damien Leroux committed
507
508
509
510
511
512
513
    void operator () (collection<Ret>& coll,
                      Ret (&f) (FuncArgs...),
                      const value<VT>& car,
                      const ArgsRemaining&... cdr,
                      const PreviousArgs&... pargs)
    {
        make_coll_impl<
514
			_Policy,
Damien Leroux's avatar
Damien Leroux committed
515
            Ret(FuncArgs...),
516
517
            tuple<ArgsRemaining...>,
            tuple<PreviousArgs..., value<VT>>>() (coll, f, cdr..., pargs..., car);
Damien Leroux's avatar
Damien Leroux committed
518
519
520
    }
};

521
template <CachingPolicy _Policy, typename Ret, typename... FuncArgs, typename T, typename... ArgsRemaining, typename... PreviousArgs>
522
struct make_coll_impl<_Policy, Ret(FuncArgs...), tuple<range<T>, ArgsRemaining...>, tuple<PreviousArgs...>> {
Damien Leroux's avatar
Damien Leroux committed
523
524
525
526
527
528
529
530
531
    void operator () (collection<Ret>& coll,
                      Ret (&f) (FuncArgs...),
                      const range<T>& car,
                      const ArgsRemaining&... cdr,
                      const PreviousArgs&... pargs)
    {
        typedef value<T> vtype;
        for (const vtype& v: car) {
            make_coll_impl<
532
				_Policy,
Damien Leroux's avatar
Damien Leroux committed
533
                Ret(FuncArgs...),
534
535
                tuple<ArgsRemaining...>,
                tuple<PreviousArgs..., vtype>>() (coll, f, cdr..., pargs..., v);
Damien Leroux's avatar
Damien Leroux committed
536
537
538
539
        }
    }
};

540
template <CachingPolicy _Policy, typename Ret, typename... FuncArgs, typename T, typename... ArgsRemaining, typename... PreviousArgs>
541
struct make_coll_impl<_Policy, Ret(FuncArgs...), tuple<collection<T>, ArgsRemaining...>, tuple<PreviousArgs...>> {
Damien Leroux's avatar
Damien Leroux committed
542
543
544
545
546
547
548
549
550
    void operator () (collection<Ret>& coll,
                      Ret (&f) (FuncArgs...),
                      const collection<T>& car,
                      const ArgsRemaining&... cdr,
                      const PreviousArgs&... pargs)
    {
        typedef value<T> vtype;
        for (const vtype& v: car) {
            make_coll_impl<
551
				_Policy,
Damien Leroux's avatar
Damien Leroux committed
552
                Ret(FuncArgs...),
553
554
                tuple<ArgsRemaining...>,
                tuple<PreviousArgs..., vtype>>() (coll, f, cdr..., pargs..., v);
Damien Leroux's avatar
Damien Leroux committed
555
556
557
558
        }
    }
};

559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
template <CachingPolicy _Policy, typename Ret, typename... FuncArgs, typename T, typename... ArgsRemaining, typename... PreviousArgs>
struct make_coll_impl<_Policy, Ret(FuncArgs...), tuple<as_collection<T>, ArgsRemaining...>, tuple<PreviousArgs...>> {
    void operator () (collection<Ret>& coll,
                      Ret (&f) (FuncArgs...),
                      const as_collection<T>& car,
                      const ArgsRemaining&... cdr,
                      const PreviousArgs&... pargs)
    {
        typedef value<typename T::value_type> vtype;
        for (const vtype& v: car) {
            make_coll_impl<
				_Policy,
                Ret(FuncArgs...),
                tuple<ArgsRemaining...>,
                tuple<PreviousArgs..., vtype>>() (coll, f, cdr..., pargs..., v);
        }
    }
};

Damien Leroux's avatar
Damien Leroux committed
578
579


580
template <CachingPolicy _Policy = Oneshot, typename Ret, typename... Args, typename... CollArgs>
Damien Leroux's avatar
Damien Leroux committed
581
582
583
584
collection<Ret>
make_collection(Ret (&f) (Args...), CollArgs... args)
{
    collection<Ret> ret;
585
    make_coll_impl<_Policy, Ret(Args...), tuple<CollArgs...>, tuple<>>() (ret, f, args...);
Damien Leroux's avatar
Damien Leroux committed
586
587
588
589
    return ret;
}


590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629

template <typename T1, typename T2>
std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << p.first << ':' << p.second; }

template <typename T1, typename T2>
std::ostream& operator << (std::ostream& os, const std::map<T1, T2>& m)
{
    auto i = m.begin(), j = m.end();
    os << '{';
    if (i != j) {
        os << (*i);
        for (++i; i != j; ++i) {
            os << ' ' << (*i);
        }
    }
    return os << '}';
}



template <typename T>
std::ostream&
operator << (std::ostream& os, const collection<T>& coll)
{
    for (const auto& k: coll) {
        os << '\t' << k << std::endl;
    }
#if 0
    auto i = coll.begin(), j = coll.end();
    if (i == j) {
        return os;
    }
    os << (**i);
    for (++i; i != j; ++i) {
        os << ' ' << (**i);
    }
#endif
    return os;
}

Damien Leroux's avatar
Damien Leroux committed
630
631
#endif