我想在python中构建一个优先级队列,其中队列包含不同的字典及其优先级编号.因此,当调用"get function"时,具有最高优先级(最低编号)的字典将被拉出队列,并且当调用"add function"时,新字典将被添加到队列中并基于其排序优先号码.
请帮忙...
提前致谢!
我最近开始使用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)
我显然做错了什么并没有实现这个......我该怎么做呢?我排队的任务是正确的吗?感谢您的帮助,非常感谢!
我需要一个Max-Priority Queue数据结构.
查看Java的优先级队列,我注意到它是一个Min-Priority Queue.
来自javadoc:
此队列的头部是指定排序的最小元素
我看到有提供自定义的选项,Comparator并查看一些建议使用一个帖子并进行反向比较以实现a的结果Max Priority Queue.
这在我看来虽然是"丑陋的黑客",也许并不直观.
这是Max-Priority Queue获得Java标准集合的唯一方法吗?
是否有一个我错过的更合适的对象?(比如前一阵子我没意识到那Stack被Deque...... 替换了......我的坏)
我有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....
但它甚至不会编译,我有点迷失...
我在这里缺少什么想法?
给定以下代码,从字典中返回具有最低年龄的密钥的最佳方法是什么.
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)
我已经调查了优先级队列,但这一切似乎都有些过分.我觉得必须有一个简单的方法来告诉我年龄最小的关键.
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) 我已经了解了优先级队列.但是当涉及到索引优先级队列时,我对某些方法的实现有点困惑,例如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) 为什么我在代码中的指定位置获得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) 我的目标是在初始化包含所述对象的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]错误我看到了.我还阅读了一篇关于模式匹配的文章,但我觉得我必须阅读更多内容才能理解实现.这里发生了什么?
谢谢.
我正在尝试利用队列,但队列正在重新组织,我不知道为什么
使用队列的每个人似乎都工作得很好,似乎没有人遇到这个问题
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)
我不知道为什么
priority-queue ×10
java ×5
c++ ×2
queue ×2
algorithm ×1
c# ×1
c++11 ×1
collections ×1
dictionary ×1
linked-list ×1
python ×1
runnable ×1
scala ×1
shared-ptr ×1
task-queue ×1
vector ×1