Source

orange / source / orange / maptemplates.hpp

Full commit
  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
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
#ifndef __MAPTEMPLATES_HPP
#define __MAPTEMPLATES_HPP

#ifdef _MSC_VER
  #pragma warning (disable : 4786 4114 4018 4267 4244)
#endif

#include "ormap.hpp"
#include "c2py.hpp"

template<class T>
static bool _orangeValueFromPython(PyObject *obj, T &val, PyTypeObject *PyKeyType)
{ if (!PyObject_TypeCheck(obj, PyKeyType)) {
    PyErr_Format(PyExc_TypeError, "invalid key: expected '%s', got '%s'", PyKeyType->tp_name, obj->ob_type->tp_name);
    return false;
  }
  val = PyOrange_AS_Orange(obj);
  return true;
}

template<class T>
static bool _nonOrangeValueFromPython(PyObject *obj, T &val, PyTypeObject *PyKeyType)
{ if (!convertFromPython(obj, val)) {
    PyErr_Format(PyExc_TypeError, "invalid key ('%s')", obj->ob_type->tp_name);
      return false;
  }
  
  return true;
}


template<class T>
static PyObject *_orangeValueToPython(const T &val)
{ return WrapOrange(const_cast<T &>(val)); }


template<class T>
static PyObject *_nonOrangeValueToPython(const T &val)
{ return convertToPython(const_cast<T &>(val)); }


#ifdef _MSC_VER
  #define INITIALIZE_MAPMETHODS(NAME, KEYTYPE, VALUETYPE, KFP, VFP, KTP, VTP) \
  TOrangeType *NAME::PyKeyType = KEYTYPE; \
  TOrangeType *NAME::PyValueType = VALUETYPE; \
  NAME::TKeyFromPython NAME::convertKeyFromPython = KFP; \
  NAME::TValueFromPython NAME::convertValueFromPython = VFP; \
  NAME::TKeyToPython NAME::convertKeyToPython = KTP; \
  NAME::TValueToPython NAME::convertValueToPython = VTP;
#else
  #define INITIALIZE_MAPMETHODS(NAME, KEYTYPE, VALUETYPE, KFP, VFP, KTP, VTP) \
  template <> TOrangeType *NAME::PyKeyType = KEYTYPE; \
  template <> TOrangeType *NAME::PyValueType = VALUETYPE; \
  template <> NAME::TKeyFromPython NAME::convertKeyFromPython = KFP; \
  template <> NAME::TValueFromPython NAME::convertValueFromPython = VFP; \
  template <> NAME::TKeyToPython NAME::convertKeyToPython = KTP; \
  template <> NAME::TValueToPython NAME::convertValueToPython = VTP;
#endif

template<class _WrappedMapType, class _MapType, class _Key, class _Value>
class MapMethods {
public:
  typedef pair<const _Key, _Value> _PairType;
  typedef map<_Key, _Value> mytype;
  typedef typename mytype::iterator iterator;
  typedef typename mytype::const_iterator const_iterator;

  typedef bool (*TKeyFromPython)(PyObject *, _Key &, PyTypeObject *);
  typedef bool (*TValueFromPython)(PyObject *, _Value &, PyTypeObject *);
  typedef PyObject *(*TKeyToPython)(const _Key &);
  typedef PyObject *(*TValueToPython)(const _Value &);

  static TOrangeType *PyKeyType;   // NULL for non-orange keys!
  static TOrangeType *PyValueType; // NULL for non-orange values!

  static TKeyFromPython convertKeyFromPython;
  static TValueFromPython convertValueFromPython;
  static TKeyToPython convertKeyToPython;
  static TValueToPython convertValueToPython;

  static bool _keyFromPython(PyObject *obj, _Key &key)
  { if (!obj) {
      PyErr_Format(PyExc_TypeError, "invalid key (NULL)");
      return false;
    }

    return convertKeyFromPython(obj, key, (PyTypeObject *)PyKeyType);
  }

  static bool _valueFromPython(PyObject *obj, _Value &value)
  { if (!obj) {
      PyErr_Format(PyExc_TypeError, "invalid value (NULL)");
      return false;
    }

    return convertValueFromPython(obj, value, (PyTypeObject *)PyValueType);
  }


  static bool updateLow(_MapType *uMap, PyObject *arg)
  {
    if (PyDict_Check(arg)) {
      Py_ssize_t pos=0;
      PyObject *pykey, *pyvalue;
      while (PyDict_Next(arg, &pos, &pykey, &pyvalue)) {
        if (_setitemlow(uMap, pykey, pyvalue)<0)
          return false;
      }
      return true;
    }


    PyObject *it = PyObject_GetIter(arg);
	  if (it == NULL)
      return _WrappedMapType();

	  for (int i = 0; ; ++i) {
		  PyObject *item = PyIter_Next(it);
		  if (item == NULL)
        if (!PyErr_Occurred()) {
          Py_DECREF(it);
          return true;
        }
			  else
          return false;

		  PyObject *fast = PySequence_Fast(item, "");
      Py_DECREF(item);

		  if (fast == NULL) {
			  if (PyErr_ExceptionMatches(PyExc_TypeError))
				  PyErr_Format(PyExc_TypeError,	"cannot convert dictionary update sequence element #%d to a sequence", i);
			  return false;
		  }

      const Py_ssize_t n = PySequence_Fast_GET_SIZE(fast);
		  if (n != 2) {
        PyErr_Format(PyExc_ValueError, "dictionary update sequence element #%d has length %d; 2 is required", i, n);
        Py_DECREF(fast);
        return false;
		  }

      PyObject *pykey = PySequence_Fast_GET_ITEM(fast, 0);
      PyObject *pyvalue = PySequence_Fast_GET_ITEM(fast, 1);
      Py_DECREF(fast);

      if (_setitemlow(uMap, pykey, pyvalue)<0)
        return false;
	  }

    Py_DECREF(it);
    return true;
  }


  static _WrappedMapType P_FromArguments(PyObject *arg, PyTypeObject *type = (PyTypeObject *)&PyOrOrange_Type)
  { 
    _MapType *uMap = mlnew _MapType();
    _WrappedMapType aMap = _WrappedMapType(uMap, type);

    return updateLow(uMap, arg) ? aMap : _WrappedMapType();
  }

  
  static PyObject *_FromArguments(PyTypeObject *type, PyObject *arg)
  { _WrappedMapType obj = P_FromArguments(arg, type);
    return obj ? WrapOrange(obj) : NULL;
  }


  static PyObject *_new(PyTypeObject *type, PyObject *args, PyObject *)
  { if (!args || (PySequence_Check(args) && !PySequence_Size(args)))
      return WrapNewOrange(mlnew _MapType(), type);

    if (PyTuple_Check(args) && PyTuple_Size(args)==1) {
      PyObject *arg = PyTuple_GetItem(args, 0);
      if (PySequence_Check(arg) || PyDict_Check(arg))
        return _FromArguments(type, arg);
    }

    return _FromArguments(type, args);
  }


  static bool findKey(_MapType *aMap, PyObject *pykey, iterator &fi, bool setError)
  {
    _Key key;
    if (!_keyFromPython(pykey, key))
      return false;

    fi = aMap->find(key);
    if (fi==aMap->end()) {
      if (setError) {
        PyObject *repred = PyObject_Str(pykey);
        PyErr_Format(PyExc_KeyError, PyString_AsString(repred));
        Py_DECREF(repred);
      }
      return false;
    }
    return true;
  }

  static PyObject *_getitem(TPyOrange *self, PyObject *pykey)
  { CAST_TO(_MapType, aMap)
    iterator fi;
    return findKey(aMap, pykey, fi, true) ? convertValueToPython((*fi).second) : PYNULL;
  }


  static int _setitemlow(_MapType *aMap, PyObject *pykey, PyObject *pyvalue)
  {
    _Key key;
    _Value value;
    if (!_keyFromPython(pykey, key) || !_valueFromPython(pyvalue, value))
      return -1;
  
      aMap->__ormap[key] = value;
    return 0;
  }


  static int _setitem(TPyOrange *self, PyObject *pykey, PyObject *pyvalue)
  { CAST_TO_err(_MapType, aMap, -1)

    if (pyvalue)
      return _setitemlow(aMap, pykey, pyvalue);
    
    iterator fi;
    if (!findKey(aMap, pykey, fi, true))
      return -1;

    aMap->erase(fi);
    return 0;
  }


  static PyObject *_str(TPyOrange *self)
  { 
    PyObject *result = callbackOutput((PyObject *)self, NULL, NULL, "str", "repr");
    if (result)
      return result;

    CAST_TO(_MapType, aMap);
    string res("{");
    for(const_iterator bi(aMap->begin()), ei(bi), ee(aMap->end()); ei!=ee; ei++) {
      if (ei!=bi)
        res += ", ";

      PyObject *obj = convertKeyToPython((*ei).first);
      PyObject *repred = PyObject_Str(obj);
      res += PyString_AsString(repred);
      res += ": ";
      Py_DECREF(obj);
      Py_DECREF(repred);

      obj = convertValueToPython((*ei).second);
      repred = PyObject_Str(obj);
      res += PyString_AsString(repred);
      Py_DECREF(obj);
      Py_DECREF(repred);
    }
    res += "}";
    return PyString_FromString(res.c_str());
  }

  static Py_ssize_t _len(TPyOrange *self)
  { PyTRY
      CAST_TO_err(_MapType, aMap, -1);
      return aMap->size();
    PyCATCH_1
  }  


  static int _contains(TPyOrange *self, PyObject *pykey)
  { CAST_TO_err(_MapType, aMap, -1)
    iterator fi;
    return findKey(aMap, pykey, fi, false) ? 1 : 0;
  }

  
/* -------------------------------------------------------------------------------------------------- */

  static PyObject *_has_key(TPyOrange *self, PyObject *pykey)
  { const int cont = _contains(self, pykey);
    if (cont<0)
      return PYNULL;
    return PyInt_FromLong(cont);
  }


  static PyObject *_get(TPyOrange *self, PyObject *args)
  { PyObject *pykey;
    PyObject *deflt = Py_None;
    if (!PyArg_ParseTuple(args, "O|O:get", &pykey, &deflt))
		  return PYNULL;

    CAST_TO(_MapType, aMap)

    iterator fi;
    if (!findKey(aMap, pykey, fi, false)) {
      Py_INCREF(deflt);
      return deflt;
    }

    return convertValueToPython((*fi).second);
  }
      

  static PyObject *_setdefault(TPyOrange *self, PyObject *args)
  { PyObject *pykey;
    PyObject *deflt = Py_None;
    if (!PyArg_ParseTuple(args, "O|O:get", &pykey, &deflt))
		  return PYNULL;

    CAST_TO(_MapType, aMap)

    _Key key;
    if (!_keyFromPython(pykey, key))
      return PYNULL;

    iterator fi = aMap->find(key);
    if (fi==aMap->end()) {
      _Value value;
      if (!_valueFromPython(deflt, value))
        return PYNULL;
      aMap->__ormap[key] = value;
      Py_INCREF(deflt);
      return deflt;
    }

    return convertValueToPython((*fi).second);
  }


  static PyObject *_clear(TPyOrange *self)
  { CAST_TO(_MapType, aMap)
    aMap->clear();
    RETURN_NONE;
  }


  static PyObject *_keys(TPyOrange *self)
  { CAST_TO(_MapType, aMap)
    
    PyObject *res = PyList_New(aMap->size());
    Py_ssize_t i = 0;
    for(const_iterator ii(aMap->begin()), ie(aMap->end()); ii!=ie; ii++, i++) {
      PyObject *item = convertKeyToPython((*ii).first);
      if (!item) {
        Py_DECREF(res);
        return PYNULL;
      }

      PyList_SetItem(res, i, item);
    }

    return res;    
  }


  static PyObject *_values(TPyOrange *self)
  { CAST_TO(_MapType, aMap)
    
    PyObject *res = PyList_New(aMap->size());
    int i = 0;
    for(const_iterator ii(aMap->begin()), ie(aMap->end()); ii!=ie; ii++, i++) {
      PyObject *item = convertValueToPython((*ii).second);
      if (!item) {
        Py_DECREF(res);
        return PYNULL;
      }

      PyList_SetItem(res, i, item);
    }

    return res;    
  }


  static PyObject *_items(TPyOrange *self)
  { CAST_TO(_MapType, aMap)
    
    PyObject *res = PyList_New(aMap->size());
    Py_ssize_t i = 0;
    for(const_iterator ii(aMap->begin()), ie(aMap->end()); ii!=ie; ii++, i++) {
      PyObject *key = convertKeyToPython((*ii).first);
      PyObject *value = key ? convertValueToPython((*ii).second) : NULL;
      if (!value) {
        Py_DECREF(res);
        return PYNULL;
      }

      PyList_SetItem(res, i, Py_BuildValue("OO", key, value));
    }

    return res;    
  }

  static PyObject *_update(TPyOrange *self, PyObject *arg)
  { CAST_TO(_MapType, aMap)
    if (!updateLow(aMap, arg))
      return PYNULL;
    RETURN_NONE;    
  }


  static PyObject *_reduce(TPyOrange *self)
  { 
    PyTRY

      PyObject *res = Orange__reduce__((PyObject *)self, NULL, NULL);
      if (!res)
        return NULL;

      CAST_TO(_MapType, aMap)
      if (aMap->size()) {
        _PyTuple_Resize(&res, 5);

        Py_INCREF(Py_None);
        PyTuple_SET_ITEM(res, 3, Py_None);

        PyObject *items = _items(self);
        PyTuple_SET_ITEM(res, 4, PySeqIter_New(items));
        Py_DECREF(items);
      }

      return res;
    PyCATCH
  }

};

#endif