本文介绍了将自定义比较器传递到Cython中的优先级队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cython libcpp 模块包含 priority_queue 的模板,这很不错,除了以下几点:我不能向它传递一个自定义比较器(或者至少不知道如何操作)。

The Cython libcpp module contains a template for priority_queue, which is great, except for one thing: I cannot pass it a custom comparer (or, at least, I don't know how to).

我需要这个,因为我需要 priority_queue 进行排序的 argsort 而不是 sort (是的,优先级队列是最佳的

I need this because I need the priority_queue to do an argsort of sorts rather than a sort (yes, a priority queue is optimal for what I want to do), and I need it to be fast.

在Cython中是否可能做到这一点,也许是通过,还是根本不?

Is this possible within Cython, perhaps by wrapping a queue in a custom way, or not at all?

例如,假设我要按元素之一对 vector [int [:]] 进行排序以稳定的方式。实际的算法要复杂得多。

As an example, say I want to sort a vector[int[:]] by one of the elements in a stable way. The actual algorithm is much more complicated.

我决定通过将其逐个添加到 priority_queue 。但是,我不知道该怎么做。

I decide to sort it by adding it, element-by-element to the priority_queue. However, I don't know how to do that.

我的实际操作类似于,但是我在合并 int [:] s一维元素由一个特定元素组成,原始列表也由该元素排序。

My actual operation is something like this question, however I'm merging sorted int[:]s of 1-D elements by a particular element, where the original lists are also ordered by that element.

我不介意将对象转换为a缓冲区/指针。

I don't mind converting the objects to a buffer/pointer.

推荐答案

可以实现此功能,但我真的不建议这样做。主要问题是:

It is possible to make this work, but I real don't recommend it. The main problems are: