我的优先级队列声明为:
std::priority_queue<*MyClass> queue;
class MyClass {
bool operator<( const MyClass* m ) const;
}
Run Code Online (Sandbox Code Playgroud)
没有排序队列中的项目.
怎么了?我不想实现不同的(比较)类.
答案摘要:
问题是,指针地址是排序的.避免这种情况的唯一方法是"比较指针"的类.
现在实现为:
std::priority_queue<*MyClass, vector<*MyClass>, MyClass::CompStr > queue;
class MyClass {
struct CompStr {
bool operator()(MyClass* m1, MyClass* m2);
}
}
Run Code Online (Sandbox Code Playgroud) 对于不一致支持优先级消息(如AMQP)的面向消息的中间件,当队列只有FIFO语义时,实现优先级消耗的最佳方法是什么?一般用例是一种系统,其中当队列中存在大量积压的消息时,消费者在较低优先级的消息之前接收具有较高优先级的消息.
使用最小/最大堆算法时,优先级可能会发生变化.处理此问题的一种方法是删除并插入元素以更新队列顺序.
对于使用数组实现的优先级队列,这可能是一个似乎可以避免的性能瓶颈,特别是对于优先级变化较小的情况.
即使这不是优先级队列的标准操作,这也是一个可以根据我的需要进行修改的自定义实现.
是否有众所周知的最佳实践方法来更新min/max-heap中的元素?
背景信息:我不是二叉树专家,我继承了一些代码,这些代码在优先级队列中重新插入了元素.我为重新排序新元素的min-heap做了一个重新插入函数 - 这给了一个可测量的改进(删除和插入),但这似乎是其他人可能已经解决的更优雅的问题办法.
我可以链接到代码,如果它有所帮助,但宁愿不太关注实现细节 - 因为这个Q&A可能保持一般.
C是否有提供优先级队列的库?我对通常安装在Linux机器上的开源库感兴趣,这是一种glib,它提供了一些数据结构.
我需要它来实现Dijkstra的算法,我确实有自己的实现,但是使用java自己的类来记录我的代码会更容易.
我正在寻找一个通用的优先级队列R.R是否有任何通用优先级队列实现(包),如Java PriorityQueue类或Python heapq?
如果我有一个heapq,其中包含一些元素,如:
import heapq
class Element(object):
def __init__(self, name, val):
self.name = name
self.val = val
if __name__ == "__main__":
heap = []
e1 = Element('A', 1)
e2 = Element('B', 65)
e3 = Element('C', 53)
e4 = Element('D', 67)
...
heapq.heappush(heap, e1)
heapq.heappush(heap, e2)
heapq.heappush(heap, e3)
heapq.heappush(heap, e4)
...
#IF I want to take elements from the heap and print them I will call:
while heap:
new_e = heapq.heappop(heap)
print new_e.name + ' ' + str(new_e.val)
Run Code Online (Sandbox Code Playgroud)
假设我在堆上有50个元素.我想将元素e3的值从val = 53更改为val = 0.所以这不是堆的顶部元素.我也不想从堆中删除其他元素.我该怎么做这样的更新?
有点卡在这里,可能需要你的帮助.我想一次阅读几个BLE特征,有些人建议使用PriorityQueue.我已经知道所有的uuids等,只需要一种方法一次读几个.任何人都可以解释它究竟应该是什么样的?或者还有另一个更简单的解决方案?
在此先感谢,这是我的代码:
public static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
PriorityQueue<BluetoothGattCharacteristic> queue = new PriorityQueue<BluetoothGattCharacteristic>();
// When connection state changes
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.v(TAG, "Connected!");
gatt.discoverServices();
}
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.v(TAG, "Disconnected...");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
List<BluetoothGattService> services = gatt.getServices();
BluetoothGattService rightService = null;
for (int i = 0; i < services.size(); i++) {
if (services.get(i).getCharacteristics().size() > 8) { …Run Code Online (Sandbox Code Playgroud) java android priority-queue bluetooth-lowenergy android-bluetooth
我想比较以下数组的第二个元素:
int[][] intervals = new int[][]{new int[]{0, 30},new int[]{5, 10},new int[]{15, 20}};
Run Code Online (Sandbox Code Playgroud)
我的优先级队列与自定义比较器:
PriorityQueue<int[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);
Run Code Online (Sandbox Code Playgroud)
但我收到以下 2 个错误:
Line 8: error: array required, but Object found
PriorityQueue<Integer[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);
^
Line 8: error: array required, but Object found
PriorityQueue<Integer[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);
Run Code Online (Sandbox Code Playgroud) TL;DR:当有几个CompletableFutures 等待执行时,我如何优先考虑那些我感兴趣的值?
我有一个 10,000CompletableFuture秒的列表(计算产品数据库内部报告的数据行):
List<Product> products = ...;
List<CompletableFuture<DataRow>> dataRows = products
.stream()
.map(p -> CompletableFuture.supplyAsync(() -> calculateDataRowForProduct(p), singleThreadedExecutor))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
每个都需要大约50 毫秒才能完成,所以整个事情在500 秒内完成。(它们都共享相同的数据库连接,因此不能并行运行)。
假设我想访问第 9000 个产品的数据行:
dataRows.get(9000).join()
问题是,所有这些 CompletableFuture 都是按照它们被创建的顺序执行的,而不是按照它们被访问的顺序。这意味着我必须等待450 秒才能计算出我目前不关心的内容,最终到达我想要的数据行。
问:有什么办法改变这种行为,使期货我尝试访问GET优先对那些我不关心的时刻?
第一个想法:
我注意到 aThreadPoolExecutor使用 aBlockingQueue<Runnable>来排队等待可用线程的条目。
因此,我考虑使用PriorityBlockingQueue, 来更改Runnable访问它时的优先级,CompletableFuture但是:
PriorityBlockingQueue没有方法重新排列现有元素的优先级,并且java performance asynchronous priority-queue completable-future
priority-queue ×10
java ×4
heap ×2
min-heap ×2
algorithm ×1
amqp ×1
android ×1
asynchronous ×1
c ×1
c++ ×1
dijkstra ×1
linux ×1
messaging ×1
performance ×1
python ×1
python-2.7 ×1
queue ×1
r ×1
rabbitmq ×1