标签: priority-queue

创建python优先级队列

我想在python中构建一个优先级队列,其中队列包含不同的字典及其优先级编号.因此,当调用"get function"时,具有最高优先级(最低编号)的字典将被拉出队列,并且当调用"add function"时,新字典将被添加到队列中并基于其排序优先号码.

请帮忙...

提前致谢!

python priority-queue task-queue

0
推荐指数
1
解决办法
6143
查看次数

JAVA:可以将可运行的线程添加到队列中吗?

我最近开始使用Threads,我正在尝试在Android中完成Looper类的Java实现.基本上我正在创建一个Java类,它将线程放入一个队列,然后由Looper类执行.我已经完成了大部分代码但是在排队任务时遇到了问题.

在Looper类中,我声明了队列和我的入队方法:

List<Runnable> queue;   

public synchronized void enqueue(Runnable runnable) {
    queue.add(runnable);
    notify(); // signal a waiting thread
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了另一个名为TaskManager的类来将Tasks添加到队列中.我打电话时收到错误:

loop.enqueue(new Task());
Run Code Online (Sandbox Code Playgroud)

其中Task()实现了runnable,只是在run()方法中将两个整数相加......这只是一个测试.

我收到的错误是:

Exception in thread "Thread-0" java.lang.NullPointerException
at Looper.enqueue(Looper.java:20) (this is the queue.add(runnable))
at TaskMaker.run(TaskMaker.java:16) (this is the loop.enqueue(new Task())
Run Code Online (Sandbox Code Playgroud)

我显然做错了什么并没有实现这个......我该怎么做呢?我排队的任务是正确的吗?感谢您的帮助,非常感谢!

java queue multithreading priority-queue runnable

0
推荐指数
1
解决办法
1792
查看次数

Java标准集合中的最大优先级队列.有吗?

我需要一个Max-Priority Queue数据结构.

查看Java的优先级队列,我注意到它是一个Min-Priority Queue.

来自javadoc:

此队列的头部是指定排序的最小元素

我看到有提供自定义的选项,Comparator并查看一些建议使用一个帖子并进行反向比较以实现a的结果Max Priority Queue.
这在我看来虽然是"丑陋的黑客",也许并不直观.

这是Max-Priority Queue获得Java标准集合的唯一方法吗?
是否有一个我错过的更合适的对象?(比如前一阵子我没意识到那StackDeque...... 替换了......我的坏)

java queue collections priority-queue

0
推荐指数
1
解决办法
1287
查看次数

C++ 11 - 如何使用shared_ptr向量将此对象推入priority_queue?

我有base class一个priority_queue这样的:

class base
{
   //...
   std::priority_queue<std::shared_ptr<Obj>, std::vector<std::shared_ptr<Obj>>, obj_less> obj_queue;
   //...
}
Run Code Online (Sandbox Code Playgroud)

在我Obj class,我有一个方法,应该将此对象推入priority_queue:

void Obj::set ()
{
    BaseServer& myObj = BaseFactory::getBase();
    myObj.set(this); //<------ won't compile :(
}
Run Code Online (Sandbox Code Playgroud)

set()会打电话给set()base class:

void base::set(const Obj& o)
{
    obj_queue.push(o);
}
Run Code Online (Sandbox Code Playgroud)

我想使用this,获取指向同样的指针Obj,然后将它推入我的vector内部priority_queue....

但它甚至不会编译,我有点迷失...

我在这里缺少什么想法?

c++ vector priority-queue shared-ptr c++11

0
推荐指数
1
解决办法
307
查看次数

返回ac#字典中的键,其值(类)符合条件

给定以下代码,从字典中返回具有最低年龄的密钥的最佳方法是什么.

 public class Person
    {
        public int age {get; set;}
        public string name {get; set;}

        public Person(int Age, string Name)
        {
            age = Age;
            name = Name;
        }
    }


    public Dictionary<int, Person> people = new Dictionary<int, Person>();

    public int idNumber = // Key of person with lowest age inside Dictionary ?????
Run Code Online (Sandbox Code Playgroud)

我已经调查了优先级队列,但这一切似乎都有些过分.我觉得必须有一个简单的方法来告诉我年龄最小的关键.

c# dictionary priority-queue

0
推荐指数
2
解决办法
72
查看次数

多个类C++的优先级队列

class Event{
public:
      enum EventType { A_1, A_2, A_3, A_4};

      Event(EvtType type = A_1, double etime = 0.0)
           : _type(type)
           , _etime(etime)
      {}

      EventType get_Type() const { return _type; };
      double get_Time() const { return _etime; }

protected:
      EventType _type;
      double _etime;
};

struct EventLess{

    bool operator()(const Event& lhs, const Event& rhs) const
        {
         return (lhs.get_Time() > rhs.get_Time());
        }
};
Run Code Online (Sandbox Code Playgroud)

我可以创建最小优先级队列,如下所示

 priority_queue<Event, std::vector<Event>, EventLess> q;
Run Code Online (Sandbox Code Playgroud)

但是,如果我有另一个类,如:

class Event1
{
public:
     enum EventType { disruption_1, disruption_2};

     Event(EvtType type = disruption_1, double …
Run Code Online (Sandbox Code Playgroud)

c++ priority-queue

0
推荐指数
1
解决办法
289
查看次数

索引优先级队列的混淆

我已经了解了优先级队列.但是当涉及到索引优先级队列时,我对某些方法的实现有点困惑,例如change(int k,Item item)delete(int i).

change(int k,Item item)是将与k关联的项目更改为item

delete(int i)是删除k及其关联项

public void changeKey(int i, Key key) {
        if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
        if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
        keys[i] = key;
        swim(qp[i]);
        sink(qp[i]);
    }

public void delete(int i) {
        if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
        if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
        int index = qp[i];
        exch(index, …
Run Code Online (Sandbox Code Playgroud)

java algorithm priority-queue

0
推荐指数
1
解决办法
3030
查看次数

为什么我会收到ConcurrentModificationException?

为什么我在代码中的指定位置获得ConcurrentModificationException?我无法弄清楚我做错了什么...该removeMin()方法用于定位列表中的min pq,删除它,并返回其值

import java.util.Iterator;
import java.util.LinkedList;

public class test1 {

    static LinkedList<Integer> list = new LinkedList<Integer>();

    public static void main(String[] args) {
        list.add(10);
        list.add(4);
        list.add(12);
        list.add(3);
        list.add(7);

        System.out.println(removeMin());
    }

    public static Integer removeMin() {
        LinkedList<Integer> pq = new LinkedList<Integer>();
        Iterator<Integer> itPQ = pq.iterator();

        // Put contents of list into pq
        for (int i = 0; i < list.size(); i++) {
            pq.add(list.removeFirst());
        }

        int min = Integer.MAX_VALUE;
        int pos = 0;
        int remPos = 0;

        while (itPQ.hasNext()) { …
Run Code Online (Sandbox Code Playgroud)

java linked-list priority-queue

0
推荐指数
1
解决办法
393
查看次数

扩展Ordering以容纳类对象时,PriorityQueue varargs错误

我的目标是在初始化包含所述对象的PriorityQueue时创建一个捕获2个或更多对象的varargs的函数.

相关代码是:

case class Topic(topic: String, usageFrequency: Long = 1)  

object FreqOrdering extends Ordering[Topic] {
  def compare(a: Topic, b:Topic) = -(a.usageFrequency compare b.usageFrequency)}  

def initPriQu(a : Topic, b: Topic, c: Topic*): PriorityQueue[Topic] = {
return PriorityQueue(a,b,c)(FreqOrdering)}
Run Code Online (Sandbox Code Playgroud)

sbt(Scala 2)中的错误:

[错误]实测值:TopicTrenderInit.FreqOrdering.type
[错误]需要:scala.math.Ordering [等于]
[错误]注:TopicTrenderInit.Topic <:等于(和TopicTrenderInit.FreqOrdering.type <:scala.math.Ordering [TopicTrenderInit .Topic]),但特性排序在类型T中是不变的.
[错误]您可能希望调查通配符类型,如_ <: Equals.(SLS 3.2.10)
[错误]返回PriorityQueue中(A,B,C)(FreqOrdering)
[错误] ^
[错误] /home/aaron-laptop/Documents/Scala/topic_trender100/src/main/scala/main.scala :48:25:类型不匹配;
[错误]实测值:scala.collection.mutable.PriorityQueue [等于]
[错误]需要:scala.collection.mutable.PriorityQueue [TopicTrenderInit.Topic]
[错误]注:等于>:TopicTrenderInit.Topic,但类PriorityQueue是不变类型A.
[错误]您可能希望调查通配符类型,例如_ >: TopicTrenderInit.Topic.(SLS 3.2.10)
[错误]返回PriorityQueue(a,b,c)(FreqOrdering)

没有'*'表示vararg一切正常,没有错误.我认为最糟糕的是困扰我所需要的:scala.math.Ordering [Equals]错误我看到了.我还阅读了一篇关于模式匹配的文章,但我觉得我必须阅读更多内容才能理解实现.这里发生了什么?
谢谢.

scala priority-queue variadic-functions

0
推荐指数
1
解决办法
50
查看次数

为什么我的队列排序输入字符串与先进先出基础不同?

我正在尝试利用队列,但队列正在重新组织,我不知道为什么

使用队列的每个人似乎都工作得很好,似乎没有人遇到这个问题

public static void main(String[] args){
    PriorityQueue<String> cue = new PriorityQueue<String>();
    cue.offer("this");
    cue.offer("that");
    cue.offer("then");
    System.out.printf("%s \n", cue);
    System.out.printf("%s \n", cue.peek());
    cue.poll();
    System.out.printf("%s \n", cue);
}
Run Code Online (Sandbox Code Playgroud)

我希望它打印:

[this, that, then]
this
[that, then]
Run Code Online (Sandbox Code Playgroud)

但相反它打印:

[that, this, then]
that
[then, this]
Run Code Online (Sandbox Code Playgroud)

我不知道为什么

java priority-queue

0
推荐指数
1
解决办法
50
查看次数