Cython中带有自定义比较器的优先级队列

2024-10-03 11:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道已经有this个问题得到了回答,但我似乎无法让它起作用。你知道吗

我正在尝试使用pair[double,pair[int,int]]的PriorityQueue并使用pair的double(配对优先)自己分类。 如果这有帮助,方法是这样的:pathfind(pair[int,int]start,pair[int,int]goal,np.N阵列网格),第一个和第二个参数是从一个普通python方法中的元组传递的,网格只是一个2D数组。 所以。。。在Cython中创建pair[double,pair[int,int]]的PriorityQueue并使其与pair中的第一个东西(double)进行比较的最佳方法是什么?你知道吗

它输出的错误是:error C2955:'cpp\u pq':使用别名模板需要模板参数列表

cpp\U优先级_队列.hpp代码如下:

#include <functional>
#include <queue>
template <class T> //Had to use this since pair wasn't getting imported correctly with <utilities>

using cpp_pq = std::priority_queue<T,std::vector<T>,std::function<bool(T,T)>>;

.pyx代码的一部分是:

cdef extern from "cpp_priority_queue.hpp":
    cdef cppclass cpp_pq:
        cpp_pq(...) except +
        void push(pair[double,pair[int,int]])
        pair[double,pair[int,int]] top()
        void pop()
        bool empty()

cdef bool compare_element(pair[double,pair[int,int]] a, pair[double,pair[int,int]] b):
    return a.first < b.first

cpdef int pathfind(pair[int,int] start, pair[int,int] goal, np.ndarray grid): # it supposed to output a 
                                                                              # list of tupples, but its 
                                                                              # an int for now until this 
                                                                              # works
    cdef cpp_pq queue = cpp_pq(compare_element)
    cdef pair[double,pair[int,int]] p = (05.7,start) # some random stuff here for testing....
    queue.push(p)
    ##nothing here till this works...
    return 0

Tags: 方法queuethisstartcppintboolstd
1条回答
网友
1楼 · 发布于 2024-10-03 11:23:32

我终于解决了这个问题,我重新启动了项目,而不是复制.hpp文件,我重写了它,它就像一个魅力。 出于某种原因,一开始实用程序库显示错误(红色下划线的单词),所以我一直在重写代码,直到我自己设法打破它并添加了不必要的模板。 直到我放弃并重新开始一切工作。你知道吗

最终cpp\U优先级_队列.hpp文件如下:

#include <functional>
#include <queue>
#include <utility>

using cpp_pq = std::priority_queue<std::pair<double,std::pair<int,int>>,std::vector<std::pair<double,std::pair<int,int>>>,std::function<bool(std::pair<double,std::pair<int,int>>,std::pair<double,std::pair<int,int>>)>>;

.pyx文件格式正确,问题只是.hpp文件。你知道吗

相关问题 更多 >