Fast IFS using RPN notation
python
c
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_if.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "python_if.h"
  2. PyMethodDef RPNIterExpr_methods[] = {
  3. {"__getstate__", (PyCFunction)rpnif_getstate, METH_NOARGS,
  4. "Pickling method. Return a bytes repr of tokenized expression \
  5. and the stack state."},
  6. {"__setstate__", (PyCFunction)rpnif_setstate, METH_O,
  7. "Unpickling method"},
  8. {NULL} //Sentinel
  9. };
  10. PyMemberDef RPNIterExpr_members[] = {
  11. {NULL}
  12. };
  13. PyGetSetDef RPNIterExpr_getset[] = {
  14. {NULL}
  15. };
  16. PyTypeObject RPNIterExprType = {
  17. PyVarObject_HEAD_INIT(NULL, 0)
  18. "pyrpn.RPNIterExpr", /* tp_name */
  19. sizeof(PyRPNIterExpr_t), /* tp_basicsize */
  20. 0, /* tp_itemsize */
  21. (destructor)rpnif_del, /* tp_dealloc */
  22. 0, /* tp_print */
  23. 0, /* tp_getattr */
  24. 0, /* tp_setattr */
  25. 0, /* tp_reserved */
  26. rpnif_repr, /* tp_repr */
  27. 0, /* tp_as_number */
  28. 0, /* tp_as_sequence */
  29. 0, /* tp_as_mapping */
  30. 0, /* tp_hash */
  31. 0, /* tp_call */
  32. rpnif_str, /* tp_str */
  33. 0, /* tp_getattro */
  34. 0, /* tp_setattro */
  35. 0, /* tp_as_buffer */
  36. Py_TPFLAGS_DEFAULT |
  37. Py_TPFLAGS_BASETYPE, /* tp_flags */
  38. "RPN expression evaluator", /* tp_doc */
  39. 0, /* tp_traverse */
  40. 0, /* tp_clear */
  41. 0, /* tp_richcompare */
  42. 0, /* tp_weaklistoffset */
  43. 0, /* tp_iter */
  44. 0, /* tp_iternext */
  45. RPNIterExpr_methods, /* tp_methods */
  46. RPNIterExpr_members, /* tp_members */
  47. RPNIterExpr_getset, /* tp_getset */
  48. 0, /* tp_base */
  49. 0, /* tp_dict */
  50. 0, /* tp_descr_get */
  51. 0, /* tp_descr_set */
  52. 0, /* tp_dictoffset */
  53. rpnif_init, /* tp_init */
  54. 0, /* tp_alloc */
  55. rpnif_new, /* tp_new */
  56. };
  57. PyObject* rpnif_new(PyTypeObject *subtype, PyObject *args, PyObject* kwds)
  58. {
  59. PyObject *ret, *err;
  60. PyRPNIterExpr_t *expr;
  61. ret = PyType_GenericNew(subtype, args, kwds);
  62. if((err = PyErr_Occurred()))
  63. {
  64. Py_DECREF(err);
  65. return ret;
  66. }
  67. expr = (PyRPNIterExpr_t*)ret;
  68. expr->rif = NULL;
  69. return ret;
  70. }
  71. int rpnif_init(PyObject *self, PyObject *args, PyObject *kwds)
  72. {
  73. /**@todo TODO write the function */
  74. PyRPNIterExpr_t *expr_self;
  75. char *names[] = {"pos_flag", "res_flag", "lim", "value", "stack_size", NULL};
  76. unsigned short pos_flag, res_flag, stack_size;
  77. PyObject *lim, *value;
  78. char err_str[256];
  79. expr_self = (PyRPNIterExpr_t*)self;
  80. stack_size = 16;
  81. value = lim = NULL;
  82. expr_self->rif = NULL;
  83. if(!PyArg_ParseTupleAndKeywords(args, kwds, "HHO|OH:RPNIterExpr.__init__", names,
  84. &pos_flag, &res_flag, lim, value, &stack_size))
  85. {
  86. return -1;
  87. }
  88. // Args checking
  89. if(stack_size < 4 || stack_size > 255)
  90. {
  91. snprintf(err_str, 128,
  92. "Stack size should be in [0..255] but %u given",
  93. stack_size);
  94. PyErr_SetString(PyExc_ValueError, err_str);
  95. return -1;
  96. }
  97. //Check & convert lim
  98. //Check & convert value
  99. return 0;
  100. }
  101. void rpnif_del(PyObject *self)
  102. {
  103. PyRPNIterExpr_t *expr_self;
  104. expr_self = (PyRPNIterExpr_t*)self;
  105. if(expr_self->rif)
  106. {
  107. rpn_if_free(expr_self->rif);
  108. expr_self->rif = NULL;
  109. }
  110. }
  111. PyObject* rpnif_str(PyObject *self)
  112. {
  113. /**@todo TODO write the function */
  114. Py_RETURN_NONE;
  115. }
  116. PyObject* rpnif_repr(PyObject *self)
  117. {
  118. /**@todo TODO write the function */
  119. Py_RETURN_NONE;
  120. }
  121. PyObject* rpnif_getstate(PyObject *self, PyObject *noargs)
  122. {
  123. /**@todo TODO write the function */
  124. Py_RETURN_NONE;
  125. }
  126. PyObject* rpnif_setstate(PyObject *self, PyObject *state_bytes)
  127. {
  128. /**@todo TODO write the function */
  129. Py_RETURN_NONE;
  130. }