#***************************************************************************** # Copyright (C) 2007 Carlo Hamalainen , # # Distributed under the terms of the GNU General Public License (GPL) # # This code 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. # # The full text of the GPL is available at: # # http://www.gnu.org/licenses/ #***************************************************************************** #clang c++ cdef extern from "stdlib.h": ctypedef unsigned long size_t cdef extern from "dancing_links.cpp": ctypedef struct vector_int "std::vector": void (* push_back)(int elem) void clear() int at(size_t loc) int size() ctypedef struct vector_vector_int "std::vector >": void (* push_back)(vector_int elem) ctypedef struct dancing_links: vector_int solution void add_rows(vector_vector_int rows) int search() cdef extern from "ccobject.h": # Some non-allocating versions dancing_links* dancing_links_construct "Construct"(void *mem) void dancing_links_destruct "Destruct"(dancing_links *mem) # some allocating versions for completeness dancing_links* dancing_links_new "New"() void dancing_links_delete "Delete"(dancing_links *mem) cdef class dancing_linksWrapper: cdef dancing_links x def __init__(self): pass # Note that the parameters to __new__ must be identical to __init__ # This is due to some Cython vagary def __new__(self): dancing_links_construct(&self.x) def __dealloc__(self): dancing_links_destruct(&self.x) def add_rows(self, rows): cdef vector_int v cdef vector_vector_int vv for row in rows: v.clear() for x in row: v.push_back(x) vv.push_back(v) self.x.add_rows(vv) def get_solution(self): s = [] for i in range(self.x.solution.size()): s.append(self.x.solution.at(i)) return s def search(self): x = self.x.search() return x def dlx_solver(rows): cdef dancing_linksWrapper dlw dlw = dancing_linksWrapper() dlw.add_rows(rows) return dlw