Commit bfcec7e2 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

[lib] C api; rename variables; add doc; fix typos; update Clause attr list_of_literals to literals

parent d3033311
......@@ -224,9 +224,9 @@ PyDoc_STRVAR(forward_code_doc,
"forward_code(clause, var_code_table, shift_step)\n\
Translation from names to num codes. The translation depends on the shift direction.\n\
\n\
Numerically code a clause with the numeric code found in self.__var_code_table for\n\
Numerically code a clause with the numeric codes found in self.__var_code_table for\n\
a base variable x and numeric_code + shift_step for x'\n\
variable integer coding increases in futur steps\n\
variable integer coding increases in future steps.\n\
\n\
..note:: Future variables x' are noted \"name`\" in Literal names.\n\
Values of variables increases of __shift_step in future steps.\n\
......@@ -261,22 +261,22 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}
// Get attr list_of_literals of clause object
PyObject* literals = PyObject_GetAttrString(clause, "list_of_literals");
// Get attr literals of clause object
PyObject* literals = PyObject_GetAttrString(clause, "literals");
if (literals == NULL) {
PyErr_SetString(PyExc_SystemError, "failed get attribute 'list_of_literals' on clause object");
PyErr_SetString(PyExc_SystemError, "failed get attribute 'literals' on clause object");
return NULL;
}
PyObject *iterator = PyObject_GetIter(literals);
if (iterator == NULL) {
PyErr_SetString(PyExc_SystemError, "could not create iterator on 'list_of_literals' attribute");
PyObject *literals_iterator = PyObject_GetIter(literals);
if (literals_iterator == NULL) {
PyErr_SetString(PyExc_SystemError, "could not create iterator on 'literals' attribute");
return NULL;
}
// Declare a new list with the size of 'literals'
PyObject *num_clause = PyList_New(PySequence_Size(literals));
if (num_clause == NULL) {
PyObject *numeric_clause = PyList_New(PySequence_Size(literals));
if (numeric_clause == NULL) {
PyErr_SetString(PyExc_SystemError, "failed to create a list");
return NULL;
}
......@@ -290,7 +290,7 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *name_last_char;
PyObject *res;
PyObject *name_prefix;
PyObject *name_value;
PyObject *variable_value;
PyObject *multiply_result;
#ifdef IS_PY3K
PyObject *last_char = PyUnicode_FromString("`");
......@@ -302,7 +302,7 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *minus_one = PyInt_FromLong(-1);
#endif
Py_ssize_t i = 0;
while ((lit = PyIter_Next(iterator)) != NULL) {
while ((lit = PyIter_Next(literals_iterator)) != NULL) {
// get name and sign attributes of the literal in clause
lit_name = PyObject_GetAttrString(lit, "name");
......@@ -320,6 +320,8 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
// If last char is '`'
res = PyObject_RichCompare(name_last_char, last_char, Py_EQ);
if (res == Py_True) {
// t+1 variable
// Get prefix of the name
name_prefix = PySequence_GetSlice(lit_name, 0, name_size-1);
if (name_prefix == NULL) {
......@@ -329,20 +331,20 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
//std::cout << PyString_AsString(name_prefix) << std::endl;
// Borrowed reference
name_value = PyDict_GetItem(var_code_table, name_prefix);
if (name_value == NULL) {
PyErr_SetString(PyExc_KeyError, "name_value not found in dict");
variable_value = PyDict_GetItem(var_code_table, name_prefix);
if (variable_value == NULL) {
PyErr_SetString(PyExc_KeyError, "name_prefix not found in dict");
return NULL;
}
// Add shift_step to the value corresponding to the given name
lit_cod = PyNumber_Add(name_value, py_shift_step);
lit_cod = PyNumber_Add(variable_value, py_shift_step);
Py_DECREF(name_prefix);
// Because name_value reference IS borrowed (owned by PyDict_GetItem)
// we don't do Py_DECREF(name_value);
// Because variable_value reference IS borrowed (owned by PyDict_GetItem)
// we don't do Py_DECREF(variable_value);
// Add new lit_cod (use SET_ITEM instead of PyList_Append)
// => num_clause is a new list there will be no leak of previously inserted items
// => numeric_clause is a new list there will be no leak of previously inserted items
// Append does not steal a reference, so shifted_lit refcount = 2
// after Py_DECREF, shifted_lit refcount = 1, but stored in the list so the pointer var can be reused
// SET_ITEM steals a reference, so : no Py_DECREF on items !
......@@ -351,17 +353,18 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
if (PyObject_Not(lit_sign)) {
multiply_result = PyNumber_Multiply(lit_cod, minus_one);
PyList_SET_ITEM(num_clause, i, multiply_result);
PyList_SET_ITEM(numeric_clause, i, multiply_result);
Py_DECREF(lit_cod);
// Because PyList_SET_ITEM steals the reference of multiply_result, we don't do Py_DECREF(multiply_result);
} else {
PyList_SET_ITEM(num_clause, i, lit_cod);
PyList_SET_ITEM(numeric_clause, i, lit_cod);
// Because lit_cod is built in place (the reference is not borrowed)
// and because PyList_SET_ITEM steals the reference of lit_cod, we don't do Py_DECREF(lit_cod);
}
} else {
// t variable
// Borrowed reference
lit_cod = PyDict_GetItem(var_code_table, lit_name);
......@@ -374,11 +377,11 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
if (PyObject_Not(lit_sign)) {
multiply_result = PyNumber_Multiply(lit_cod, minus_one);
PyList_SET_ITEM(num_clause, i, multiply_result);
PyList_SET_ITEM(numeric_clause, i, multiply_result);
// Because PyList_SET_ITEM steals the reference of multiply_result, we don't do Py_DECREF(multiply_result);
} else {
PyList_SET_ITEM(num_clause, i, lit_cod);
PyList_SET_ITEM(numeric_clause, i, lit_cod);
Py_INCREF(lit_cod);
// Because lit_cod reference IS borrowed (owned by PyDict_GetItem),
// we don't do Py_DECREF(lit_cod) and because PyList_SET_ITEM steals the reference of lit_cod,
......@@ -397,7 +400,7 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
}
/* release reference when done */
Py_DECREF(iterator);
Py_DECREF(literals_iterator);
Py_DECREF(last_char);
Py_DECREF(py_shift_step);
Py_DECREF(minus_one);
......@@ -405,21 +408,25 @@ static PyObject* forward_code(PyObject *self, PyObject *args, PyObject *kwds)
if (PyErr_Occurred()) {
/* propagate error */
Py_DECREF(num_clause);
Py_DECREF(numeric_clause);
return PyErr_SetFromErrno(outofconflerr);
}
// Return list of all num_clause
return num_clause;
// Return list of all numeric_clause
return numeric_clause;
}
PyDoc_STRVAR(forward_init_dynamic_doc,
"forward_init_dynamic(clause, var_code_table, shift_step, [aux_clauses])\n\
"forward_init_dynamic(clauses, var_code_table, shift_step, [aux_clauses])\n\
Dynamics initialisations. Set dynamic constraints for a forward one step: X1 = f(X0)\n\
\n\
Numerically code a clause with the numeric code found in\n\
Numerically code clauses with the numeric codes found in\n\
self.__var_code_table for a base variable x,\n\
and numeric_code + shift_step for x' variable integer coding increases in futur steps\n\
and numeric_code + shift_step for x' variable integer coding increases in future steps.\n\
\n\
__dynamic_constraints is a list of lists of numeric clauses (lists of ints)\n\
Each sublist of __dynamic_constraints corresponds to a step in the unfolder;\n\
the last step is the last element.\n\
\n\
..note:: Future variables x' are noted \"name`\" in Literal names.\n\
Values of variables increases of __shift_step in future steps.\n\
......@@ -428,12 +435,12 @@ and numeric_code + shift_step for x' variable integer coding increases in futur
:param var_code_table: Var code table from the model\n\
:param shift_step: Shift step dependant on the run\n\
:param aux_clauses: List of auxiliary Clause objects\n\
:return: A list of lists of DIMACS coding of the forward shifted clause\n\
:type clauses: <list>\n\
:return: A list of lists of DIMACS coding of the forward shifted clauses\n\
:type clauses: <list <Clause>>\n\
:type var_code_table: <dict>\n\
:type shift_step: <int>\n\
:type aux_clauses: <list>\n\
:rtype: <list <list>>"
:type aux_clauses: <list <Clause>>\n\
:rtype: <list <list <list <int>>>>"
);
static PyObject* forward_init_dynamic(PyObject *self, PyObject *args, PyObject *kwds)
......@@ -460,16 +467,16 @@ static PyObject* forward_init_dynamic(PyObject *self, PyObject *args, PyObject *
return NULL;
}
PyObject *num_clause = PyList_New(PySequence_Size(clauses) + PySequence_Size(aux_clauses));
if (num_clause == NULL) {
PyObject *numeric_clauses = PyList_New(PySequence_Size(clauses) + PySequence_Size(aux_clauses));
if (numeric_clauses == NULL) {
PyErr_SetString(PyExc_SystemError, "failed to create a list");
return NULL;
}
// Default clauses ////////////////////////////////////////////////////////
PyObject *iterator = PyObject_GetIter(clauses);
if (iterator == NULL) {
PyObject *clauses_iterator = PyObject_GetIter(clauses);
if (clauses_iterator == NULL) {
/* propagate error */
PyErr_SetString(PyExc_SystemError, "could not create iterator on list");
return NULL;
......@@ -478,12 +485,12 @@ static PyObject* forward_init_dynamic(PyObject *self, PyObject *args, PyObject *
PyObject *clause;
PyObject *arglist;
Py_ssize_t i = 0;
while ((clause = PyIter_Next(iterator)) != NULL) {
while ((clause = PyIter_Next(clauses_iterator)) != NULL) {
// Build parameters of forward_code
// {"clause", "var_code_table", "shift_step", NULL};
arglist = Py_BuildValue("(OOi)", clause, var_code_table, shift_step);
PyList_SET_ITEM(num_clause, i, forward_code(self, arglist, NULL));
PyList_SET_ITEM(numeric_clauses, i, forward_code(self, arglist, NULL));
/* release reference when done */
// no DECREF on the return of forward_code, because PyList_SET_ITEM steals reference
......@@ -494,23 +501,23 @@ static PyObject* forward_init_dynamic(PyObject *self, PyObject *args, PyObject *
}
/* release reference when done */
Py_DECREF(iterator);
Py_DECREF(clauses_iterator);
// Auxiliary clauses //////////////////////////////////////////////////////
iterator = PyObject_GetIter(aux_clauses);
if (iterator == NULL) {
clauses_iterator = PyObject_GetIter(aux_clauses);
if (clauses_iterator == NULL) {
/* propagate error */
PyErr_SetString(PyExc_SystemError, "could not create iterator on list");
return NULL;
}
while ((clause = PyIter_Next(iterator)) != NULL) {
while ((clause = PyIter_Next(clauses_iterator)) != NULL) {
// Build parameters of forward_code
// {"clause", "var_code_table", "shift_step", NULL};
arglist = Py_BuildValue("(OOi)", clause, var_code_table, shift_step);
PyList_SET_ITEM(num_clause, i, forward_code(self, arglist, NULL));
PyList_SET_ITEM(numeric_clauses, i, forward_code(self, arglist, NULL));
/* release reference when done */
// no DECREF on the return of forward_code, because PyList_SET_ITEM steals reference
......@@ -521,16 +528,17 @@ static PyObject* forward_init_dynamic(PyObject *self, PyObject *args, PyObject *
}
/* release reference when done */
Py_DECREF(iterator);
Py_DECREF(clauses_iterator);
// Build a list and put in it the list of numeric clauses (lists of ints).
PyObject* dynamic_constraints = PyList_New(1);
// NULL on failure
PyList_SET_ITEM(dynamic_constraints, 0, num_clause);
PyList_SET_ITEM(dynamic_constraints, 0, numeric_clauses);
if (PyErr_Occurred()) {
/* propagate error */
Py_DECREF(dynamic_constraints);
// No decref (SET_ITEM) Py_DECREF(num_clause);
// No decref (SET_ITEM) Py_DECREF(numeric_clauses);
return PyErr_SetFromErrno(outofconflerr);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment