aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/kiwisolver/py2/py/constraint.cpp
blob: 2fbdd0aeb83e5a57ee0ea2c13a739acec119865a (plain) (blame)
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
45
46
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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
288
289
290
291
292
293
294
295
296
297
298
299
/*-----------------------------------------------------------------------------
| Copyright (c) 2013-2017, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file COPYING.txt, distributed with this software.
|----------------------------------------------------------------------------*/
#include <algorithm>
#include <sstream>
#include <Python.h>
#include <kiwi/kiwi.h>
#include "pythonhelpers.h"
#include "types.h"
#include "util.h"


using namespace PythonHelpers;


static PyObject*
Constraint_new( PyTypeObject* type, PyObject* args, PyObject* kwargs )
{
    static const char *kwlist[] = { "expression", "op", "strength", 0 };
    PyObject* pyexpr;
    PyObject* pyop;
    PyObject* pystrength = 0;
    if( !PyArg_ParseTupleAndKeywords(
        args, kwargs, "OO|O:__new__", const_cast<char**>( kwlist ),
        &pyexpr, &pyop, &pystrength ) )
        return 0;
    if( !Expression::TypeCheck( pyexpr ) )
        return py_expected_type_fail( pyexpr, "Expression" );
    kiwi::RelationalOperator op;
    if( !convert_to_relational_op( pyop, op ) )
        return 0;
    double strength = kiwi::strength::required;
    if( pystrength && !convert_to_strength( pystrength, strength ) )
        return 0;
    PyObjectPtr pycn( PyType_GenericNew( type, args, kwargs ) );
    if( !pycn )
        return 0;
    Constraint* cn = reinterpret_cast<Constraint*>( pycn.get() );
    cn->expression = reduce_expression( pyexpr );
    if( !cn->expression )
        return 0;
    kiwi::Expression expr( convert_to_kiwi_expression( cn->expression ) );
    new( &cn->constraint ) kiwi::Constraint( expr, op, strength );
    return pycn.release();
}


static void
Constraint_clear( Constraint* self )
{
    Py_CLEAR( self->expression );
}


static int
Constraint_traverse( Constraint* self, visitproc visit, void* arg )
{
    Py_VISIT( self->expression );
    return 0;
}


static void
Constraint_dealloc( Constraint* self )
{
    PyObject_GC_UnTrack( self );
    Constraint_clear( self );
    self->constraint.~Constraint();
    Py_TYPE( self )->tp_free( pyobject_cast( self ) );
}


static PyObject*
Constraint_repr( Constraint* self )
{
    std::stringstream stream;
    Expression* expr = reinterpret_cast<Expression*>( self->expression );
    Py_ssize_t size = PyTuple_GET_SIZE( expr->terms );
    for( Py_ssize_t i = 0; i < size; ++i )
    {
        PyObject* item = PyTuple_GET_ITEM( expr->terms, i );
        Term* term = reinterpret_cast<Term*>( item );
        stream << term->coefficient << " * ";
        stream << reinterpret_cast<Variable*>( term->variable )->variable.name();
        stream << " + ";
    }
    stream << expr->constant;
    switch( self->constraint.op() )
    {
        case kiwi::OP_EQ:
            stream << " == 0";
            break;
        case kiwi::OP_LE:
            stream << " <= 0";
            break;
        case kiwi::OP_GE:
            stream << " >= 0";
            break;
    }
    stream << " | strength = " << self->constraint.strength();
    return FROM_STRING( stream.str().c_str() );
}


static PyObject*
Constraint_expression( Constraint* self )
{
    return newref( self->expression );
}


static PyObject*
Constraint_op( Constraint* self )
{
    PyObject* res = 0;
    switch( self->constraint.op() )
    {
        case kiwi::OP_EQ:
            res = FROM_STRING( "==" );
            break;
        case kiwi::OP_LE:
            res = FROM_STRING( "<=" );
            break;
        case kiwi::OP_GE:
            res = FROM_STRING( ">=" );
            break;
    }
    return res;
}


static PyObject*
Constraint_strength( Constraint* self )
{
    return PyFloat_FromDouble( self->constraint.strength() );
}


static PyObject*
Constraint_or( PyObject* pyoldcn, PyObject* value )
{
    if( !Constraint::TypeCheck( pyoldcn ) )
        std::swap( pyoldcn, value );
    double strength;
    if( !convert_to_strength( value, strength ) )
        return 0;
    PyObject* pynewcn = PyType_GenericNew( &Constraint_Type, 0, 0 );
    if( !pynewcn )
        return 0;
    Constraint* oldcn = reinterpret_cast<Constraint*>( pyoldcn );
    Constraint* newcn = reinterpret_cast<Constraint*>( pynewcn );
    newcn->expression = newref( oldcn->expression );
    new( &newcn->constraint ) kiwi::Constraint( oldcn->constraint, strength );
    return pynewcn;
}


static PyMethodDef
Constraint_methods[] = {
    { "expression", ( PyCFunction )Constraint_expression, METH_NOARGS,
      "Get the expression object for the constraint." },
    { "op", ( PyCFunction )Constraint_op, METH_NOARGS,
      "Get the relational operator for the constraint." },
    { "strength", ( PyCFunction )Constraint_strength, METH_NOARGS,
      "Get the strength for the constraint." },
    { 0 } // sentinel
};


static PyNumberMethods
Constraint_as_number = {
    0,                          /* nb_add */
    0,                          /* nb_subtract */
    0,                          /* nb_multiply */
#if PY_MAJOR_VERSION < 3
    0,                          /* nb_divide */
#endif
    0,                          /* nb_remainder */
    0,                          /* nb_divmod */
    0,                          /* nb_power */
    0,                          /* nb_negative */
    0,                          /* nb_positive */
    0,                          /* nb_absolute */
#if PY_MAJOR_VERSION >= 3
    0,                          /* nb_bool */
#else
    0,                          /* nb_nonzero */
#endif
    0,                          /* nb_invert */
    0,                          /* nb_lshift */
    0,                          /* nb_rshift */
    0,                          /* nb_and */
    0,                          /* nb_xor */
    (binaryfunc)Constraint_or,  /* nb_or */
#if PY_MAJOR_VERSION < 3
    0,                          /* nb_coerce */
#endif
    0,                          /* nb_int */
    0,                          /* nb_long */
    0,                          /* nb_float */
#if PY_MAJOR_VERSION < 3
    0,                          /* nb_oct */
    0,                          /* nb_hex */
#endif
    0,                          /* nb_inplace_add */
    0,                          /* nb_inplace_subtract */
    0,                          /* nb_inplace_multiply */
#if PY_MAJOR_VERSION < 3
    0,                          /* nb_inplace_divide */
#endif
    0,                          /* nb_inplace_remainder */
    0,                          /* nb_inplace_power */
    0,                          /* nb_inplace_lshift */
    0,                          /* nb_inplace_rshift */
    0,                          /* nb_inplace_and */
    0,                          /* nb_inplace_xor */
    0,                          /* nb_inplace_or */
    (binaryfunc)0,              /* nb_floor_divide */
    (binaryfunc)0,              /* nb_true_divide */
    0,                          /* nb_inplace_floor_divide */
    0,                          /* nb_inplace_true_divide */
#if PY_VERSION_HEX >= 0x02050000
    (unaryfunc)0,               /* nb_index */
#endif
#if PY_VERSION_HEX >= 0x03050000
    (binaryfunc)0,              /* nb_matrix_multiply */
    (binaryfunc)0,              /* nb_inplace_matrix_multiply */
#endif
};


PyTypeObject Constraint_Type = {
    PyVarObject_HEAD_INIT( &PyType_Type, 0 )
    "kiwisolver.Constraint",                /* tp_name */
    sizeof( Constraint ),                   /* tp_basicsize */
    0,                                      /* tp_itemsize */
    (destructor)Constraint_dealloc,         /* tp_dealloc */
    (printfunc)0,                           /* tp_print */
    (getattrfunc)0,                         /* tp_getattr */
    (setattrfunc)0,                         /* tp_setattr */
#if PY_VERSION_HEX >= 0x03050000
    ( PyAsyncMethods* )0,                   /* tp_as_async */
#elif PY_VERSION_HEX >= 0x03000000
    ( void* ) 0,                            /* tp_reserved */
#else
    ( cmpfunc )0,                           /* tp_compare */
#endif
    (reprfunc)Constraint_repr,              /* tp_repr */
    (PyNumberMethods*)&Constraint_as_number,/* tp_as_number */
    (PySequenceMethods*)0,                  /* tp_as_sequence */
    (PyMappingMethods*)0,                   /* tp_as_mapping */
    (hashfunc)0,                            /* tp_hash */
    (ternaryfunc)0,                         /* tp_call */
    (reprfunc)0,                            /* tp_str */
    (getattrofunc)0,                        /* tp_getattro */
    (setattrofunc)0,                        /* tp_setattro */
    (PyBufferProcs*)0,                      /* tp_as_buffer */
#if PY_MAJOR_VERSION >= 3
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE, /* tp_flags */
#else
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_CHECKTYPES, /* tp_flags */
#endif
    0,                                      /* Documentation string */
    (traverseproc)Constraint_traverse,      /* tp_traverse */
    (inquiry)Constraint_clear,              /* tp_clear */
    (richcmpfunc)0,                         /* tp_richcompare */
    0,                                      /* tp_weaklistoffset */
    (getiterfunc)0,                         /* tp_iter */
    (iternextfunc)0,                        /* tp_iternext */
    (struct PyMethodDef*)Constraint_methods,/* tp_methods */
    (struct PyMemberDef*)0,                 /* tp_members */
    0,                                      /* tp_getset */
    0,                                      /* tp_base */
    0,                                      /* tp_dict */
    (descrgetfunc)0,                        /* tp_descr_get */
    (descrsetfunc)0,                        /* tp_descr_set */
    0,                                      /* tp_dictoffset */
    (initproc)0,                            /* tp_init */
    (allocfunc)PyType_GenericAlloc,         /* tp_alloc */
    (newfunc)Constraint_new,                /* tp_new */
    (freefunc)PyObject_GC_Del,              /* tp_free */
    (inquiry)0,                             /* tp_is_gc */
    0,                                      /* tp_bases */
    0,                                      /* tp_mro */
    0,                                      /* tp_cache */
    0,                                      /* tp_subclasses */
    0,                                      /* tp_weaklist */
    (destructor)0                           /* tp_del */
};


int import_constraint()
{
    return PyType_Ready( &Constraint_Type );
}