graph_state.hh 18.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2016 Tiago de Paula Peixoto <tiago@skewed.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_STATE_HH
#define GRAPH_STATE_HH

#include <boost/python.hpp>
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/size.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/arithmetic/dec.hpp>
#include <boost/preprocessor/seq/transform.hpp>
#include <boost/preprocessor/seq/filter.hpp>
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/comparison/not_equal.hpp>
#include <boost/preprocessor/punctuation/comma.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/preprocessor/seq/first_n.hpp>

#include "graph.hh"
#include "graph_selectors.hh"
#include "graph_properties.hh"
#include "graph_filtering.hh"
#include "graph_util.hh"

45
46
#include "numpy_bind.hh"

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "config.h"

namespace graph_tool
{
using namespace boost;

template <size_t N>
struct nth
{
    template <class T, class... Ts>
    auto&& operator()(T&&, Ts&&... as) const
    {
        static_assert(N < sizeof...(as) + 1, "wrong number of arguments");
        return nth<N-1>()(std::forward<Ts>(as)...);
    }
};

template <>
struct nth<0>
{
    template <class T, class... Ts>
    auto&& operator()(T&& a, Ts&&...) const
    {
        return a;
    }
};

template <size_t N, class T, class... Ts>
struct nth_t
{
    typedef typename std::conditional<N == 0,
                                      T,
                                      typename nth_t<N-1, Ts...>::type>::type
        type;
};

template <size_t N, class T>
struct nth_t<N, T>
{
    typedef T type;
};

template <template <class...> class State>
struct StateFactory
{
    template <class... Ts>
    struct apply
    {
        typedef State<Ts...> type;
    };
};


template <class Type, class Index>
auto uncheck(boost::checked_vector_property_map<Type,Index>*)
{
    boost::unchecked_vector_property_map<Type,Index>* na(nullptr);
    return na;
}

template <class Type, class Index>
auto uncheck(boost::checked_vector_property_map<Type,Index> p)
{
    return p.get_unchecked();
}

template <class T>
auto&& uncheck(T&& a)
{
    return a;
}


template <class Factory, class... TRS>
struct StateWrap
{
    template <class... Ts>
    struct FactoryWrap
    {
        typedef typename Factory::template apply
            <typename std::remove_reference<Ts>::type...>::type
            type;

        template <class... ATs>
        auto operator()(ATs&&... args) const
        {
            return type(std::forward<ATs>(args)...);
        };
    };

    template <class... TS, class F>
    static void dispatch(F&& f)
    {
        auto full_dispatch = [&](auto*... full_args)
            {
                typedef typename FactoryWrap
                    <typename std::remove_reference
                       <decltype(*full_args)>::type...>::type
                    state_t;
                state_t* ptr(nullptr);
                f(ptr);
            };
        auto partial_dispatch = [&](auto*... args)
            {
                full_dispatch(args...,
                              uncheck((typename std::add_pointer<TS>::type)(nullptr))...);
            };
        mpl::nested_for_each<TRS...>
            ([&](auto*... args){partial_dispatch(uncheck(args)...);});
    }

    template <class... TS, class F>
    static void dispatch(python::object& ostate, F&& f,
                         bool throw_not_found = true)
    {
        bool found = false;
        dispatch<TS...>
            ([&](auto* s)
             {
                 typedef typename std::remove_reference<decltype(*s)>::type
                     state_t;
                 python::extract<state_t&> get_state(ostate);
                 if (get_state.check())
                 {
                     state_t& state = get_state();
                     f(state);
                     found = true;
                 }
             });

        if (!found && throw_not_found)
            throw GraphException("dispatch not found for: " +
                                 name_demangle(typeid(StateWrap).name()));
    }


    template <class... Ts>
    struct make_dispatch
    {
        template <size_t N, size_t... Idx, size_t... FIdx, class... Extra,
                  class F>
        void operator()(python::object& ostate,
                        std::array<const char*, N>& names,
                        std::index_sequence<Idx...>,
                        std::index_sequence<FIdx...>,
                        F&& f, Extra&&... extra) const
        {
            static_assert(sizeof...(TRS)  == sizeof...(Idx),
                          "wrong number of argument names");
            static_assert(sizeof...(TRS) + sizeof...(FIdx) == N,
                          "wrong total number of argument names");

            auto full_f = [&](auto&&... full_args)
                {
                    auto state = FactoryWrap<decltype(full_args)...>()
                       (std::forward<Extra>(extra)...,
                        std::forward<decltype(full_args)>(full_args)...);
                    f(state);
                };
            auto partial_f = [&](auto&&... args)
                {
                    full_f(std::forward<decltype(args)>(args)...,
209
                           uncheck(Extract<Ts>()
210
211
212
213
214
215
216
217
                                   (ostate,
                                    names[FIdx + sizeof...(Idx)]))...);
                };
            gt_dispatch<>()(partial_f, TRS()...)
                (get_any(ostate, names[Idx], TRS())...);
        }

        template <class T>
218
        struct Extract
219
        {
220
            T operator()(python::object mobj, std::string name) const
221
            {
222
223
224
                python::object obj = mobj.attr(name.c_str());
                python::extract<T> extract(obj);
                if (extract.check())
225
                {
226
                    T val = extract();
227
228
                    return val;
                }
229
                else
230
                {
231
232
233
234
235
236
                    python::object aobj;
                    if (PyObject_HasAttrString(obj.ptr(), "_get_any"))
                        aobj = obj.attr("_get_any")();
                    else
                        aobj = obj;
                    python::extract<boost::any&> extract(aobj);
237
238
                    try
                    {
239
240
                        if (!extract.check())
                            throw boost::bad_any_cast();
241
                        boost::any& aval = extract();
242
243
                        T val = any_cast<T>(aval);
                        return val;
244
245
246
                    }
                    catch (boost::bad_any_cast)
                    {
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
                        try
                        {
                            typedef std::reference_wrapper
                                <typename std::remove_reference<T>::type>
                                ref_wrap_t;
                            boost::any& aval = extract();
                            auto val = any_cast<ref_wrap_t>(aval);
                            return val.get();
                        }
                        catch (boost::bad_any_cast)
                        {
                            throw ValueException("Cannot extract parameter '" + name +
                                                 "' of desired type: " +
                                                 name_demangle(typeid(T).name()));
                        }
262
263
264
                    }
                }
            }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
        };

        template <class Type, size_t Dim>
        struct Extract<multi_array_ref<Type, Dim>>
        {
            multi_array_ref<Type, Dim> operator()(python::object mobj,
                                                  std::string name) const
            {
                python::object obj = mobj.attr(name.c_str());
                try
                {
                    return get_array<Type, Dim>(obj);
                }
                catch (InvalidNumpyConversion& e)
                {
                    throw ValueException("Cannot extract parameter '" + name +
                                         "' of desired type: " +
                                         name_demangle(typeid(multi_array_ref<Type, Dim>).name())
                                         + ", reason: " +
                                         std::string(e.what()));
                }
            }
        };
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
    };

    template <class TR>
    static boost::any get_any(python::object mobj, string name, TR)
    {
        python::object obj = mobj.attr(name.c_str());
        if (PyObject_HasAttrString(obj.ptr(), "_get_any"))
        {
            return python::extract<boost::any&>(obj.attr("_get_any")())();
        }
        else
        {
            boost::any ret;
            bool found = false;
            mpl::nested_for_each<TR>
                ([&](auto* t)
                 {
                     typedef typename std::remove_reference<decltype(*t)>::type
                         val_t;
                     if (std::is_same<val_t, python::object>::value)
                     {
                         ret = obj;
                         found = true;
                     }
                     else
                     {
                         python::extract<val_t> extract(obj);
                         if (extract.check())
                         {
                             val_t val = extract();
                             ret = val;
                             found = true;
                         }
                     }
                 });
            if (!found)
                throw ValueException("Cannot extract parameter '" + name +
                                     "' of desired types: " +
                                     name_demangle(typeid(TR).name()));
            return ret;
        }
    }
};

#define _COMMA_CAT(r, data, pos, name) name,
#define GET_COMMA_LIST(seq)                                                    \
    BOOST_PP_SEQ_FOR_EACH_I                                                    \
        (_COMMA_CAT,,BOOST_PP_SEQ_FIRST_N(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(seq)),\
                                          seq))                                \
        BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(seq)), seq)

#define _PARAM_TYPEDEF(r, pack, pos, name)                                     \
    typedef typename nth_t<pos, pack...>::type                                 \
    BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(4, 0, name), _t);
#define GET_PARAMS_TYPEDEF(pack, names)                                        \
    BOOST_PP_SEQ_FOR_EACH_I(_PARAM_TYPEDEF, pack, names)

#define _PARAM_USING(r, base, pos, name)                                       \
    using base :: BOOST_PP_CAT(_,BOOST_PP_TUPLE_ELEM(4, 0, name));
#define GET_PARAMS_USING(base, names)                                          \
    BOOST_PP_SEQ_FOR_EACH_I(_PARAM_USING, base, names)

#define _PARAM_DEFINE(r, pack, pos, name)                                      \
    BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(4, 0, name), _t)                          \
    BOOST_PP_TUPLE_ELEM(2, 1, name)                                            \
    BOOST_PP_CAT(_, BOOST_PP_TUPLE_ELEM(4, 0, name));
#define GET_PARAMS_DEFINE(names)                                               \
    BOOST_PP_SEQ_FOR_EACH_I(_PARAM_DEFINE, r, names)

#define _PARAM_INIT(r, pack, pos, name)                                        \
    BOOST_PP_CAT(_, BOOST_PP_TUPLE_ELEM(4, 0, name))(nth<pos>()(pack...)),
#define GET_PARAMS_INIT(pack, names)                                           \
    BOOST_PP_SEQ_FOR_EACH_I                                                    \
       (_PARAM_INIT, pack,                                                     \
        BOOST_PP_SEQ_FIRST_N(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(names)),           \
                             names))                                           \
   BOOST_PP_CAT(_, BOOST_PP_TUPLE_ELEM                                         \
                (4, 0,                                                         \
                 BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(names)),     \
                                   names)))                                    \
        (nth<BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(names))>()(pack...))

#define _PARAM_STRING(r, data, pos, name)                                      \
    BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(4, 0, name)),
#define GET_PARAMS_NAMES(names)                                                \
    BOOST_PP_SEQ_FOR_EACH_I                                                    \
       (_PARAM_STRING,,                                                        \
        BOOST_PP_SEQ_FIRST_N(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(names)),           \
                             names))                                           \
    BOOST_PP_STRINGIZE                                                         \
       (BOOST_PP_TUPLE_ELEM                                                    \
        (4, 0, BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(names)),       \
                                 names)))


#define GEN_STATE_BASE(base_name, state_params)                                \
    template <class... Ts>                                                     \
    class base_name                                                            \
    {                                                                          \
    public:                                                                    \
        GET_PARAMS_TYPEDEF(Ts, state_params)                                   \
        template <class... ATs,                                                \
                  typename std::enable_if_t                                    \
                      <!std::is_same<typename nth_t<0, ATs...>::type,          \
                                     base_name<Ts...>>::value>* = nullptr>     \
        base_name(ATs&&... args) : GET_PARAMS_INIT(args, state_params) {}      \
        base_name(const base_name<Ts...>&) = default;                          \
    protected:                                                                 \
        GET_PARAMS_DEFINE(state_params)                                        \
    };



#define _GET_T(r, data, elem) BOOST_PP_TUPLE_ELEM(4, 2, elem)
#define _IS_TR(s, data, elem) BOOST_PP_EQUAL(BOOST_PP_TUPLE_ELEM(4, 3, elem),1)
#define GET_PARAMS_TRS(params)                                                 \
    BOOST_PP_SEQ_TRANSFORM(_GET_T,,BOOST_PP_SEQ_FILTER(_IS_TR,,params))

#define IS_TYPE(s, data, elem) BOOST_PP_EQUAL(BOOST_PP_TUPLE_ELEM(4, 3, elem),0)
#define GET_PARAMS_TYPES(params)                                               \
    BOOST_PP_SEQ_TRANSFORM(_GET_T,,BOOST_PP_SEQ_FILTER(IS_TYPE,,params))

#define GEN_DISPATCH(gen_name, state_name, params)                             \
    struct gen_name                                                            \
    {                                                                          \
        typedef StateWrap<StateFactory<state_name>,                            \
                          GET_COMMA_LIST(GET_PARAMS_TRS(params))>              \
            state_wrap_t;                                                      \
        template <class F, class... Extra>                                     \
        static void make_dispatch(python::object ostate, F&& f,                \
                                  Extra&&... args)                             \
        {                                                                      \
            std::array<const char*, BOOST_PP_SEQ_SIZE(params)> names =         \
                {{GET_PARAMS_NAMES(params)}};                                  \
            typedef typename state_wrap_t::template make_dispatch              \
                <GET_COMMA_LIST(GET_PARAMS_TYPES(params))> dispatch_t;         \
            dispatch_t()                                                       \
                (ostate, names,                                                \
                 std::make_index_sequence                                      \
                     <BOOST_PP_SEQ_SIZE(GET_PARAMS_TRS(params))>(),            \
                 std::make_index_sequence                                      \
                     <BOOST_PP_SEQ_SIZE(GET_PARAMS_TYPES(params))>(),          \
                 std::forward<F>(f), std::forward<Extra>(args)...);            \
        }                                                                      \
        template <class F>                                                     \
        static void dispatch(F&& f)                                            \
        {                                                                      \
            state_wrap_t::template dispatch                                    \
                <GET_COMMA_LIST(GET_PARAMS_TYPES(params))>(std::forward<F>(f));\
        }                                                                      \
        template <class F>                                                     \
        static void dispatch(python::object state, F&& f,                      \
                             bool throw_not_found = true)                      \
        {                                                                      \
            state_wrap_t::template dispatch                                    \
                <GET_COMMA_LIST(GET_PARAMS_TYPES(params))>(state,              \
                                                           std::forward<F>(f), \
                                                           throw_not_found);   \
        }                                                                      \
    };

} // namespace graph_tool

#endif // GRAPH_STATE_HH