BlueRaja / High-Speed-Priority-Queue-for-C-Sharp

A C# priority queue optimized for pathfinding applications
MIT License
1.15k stars 169 forks source link

priority with template type #14

Closed goldenbull closed 7 years ago

goldenbull commented 7 years ago

I have an event driven model in which each event is assigned with a timestamp in DateTime type, and smaller timestamp should dequeue first. There is data loss when converting long DateTime.Ticks to float priority. I understand in most cases float type is enough for priority, but in some cases it will cause trouble. How about leave the pritority comparison to user? This is the common pattern in most collections in .NET world.

public interface IPriorityQueue<T, P> : IEnumerable<T>
{
    void Enqueue(T node, P priority);
    ...
}

public sealed class SimplePriorityQueue<T, P> : IPriorityQueue<T, P>
{
    public SimplePriorityQueue( Comparer<P> comparer = Comparer<P>.Default )
    {
        ...
    }
}
BlueRaja commented 7 years ago

Hmm. This is a good idea, but I'm not sure how it could be implemented without completely rewriting SimplePriorityQueue. I don't want to add it to FastPriorityQueue too because of the performance hit. I'll have to think about it.

zijianhuang commented 7 years ago

I have just done those required by @goldenbull at https://github.com/zijianhuang/High-Speed-Priority-Queue-for-C-Sharp

now Priority could be generic, while backward compatibility with current HEAD in origin is kept.

When more stress tests are done, I will create a pull request.

Meanwhile, you @goldenbull may just use my fork in which currently the default Priority type is double. And I don't think you need to use DateTime for priority, since you could use DateTime.ToOATime().

goldenbull commented 7 years ago

@zijianhuang there is no ToOATime method in C# DateTime struct, I guess you meant to say ToOADate :) There is still data loss because I have some high frequence data which needs the precision to 1us

goldenbull commented 7 years ago

@BlueRaja yes, this will make a big change from the base of the whole building, I'm not sure if it's neccessary. Nevertheless, for my special case, I can modify several lines and change the float type to long in hard-code way. Glories to BlueRaja :)

BlueRaja commented 7 years ago

Completed in version 4.0. The new declaration for SimplePriorityQueue is SimplePriorityQueue<TItem, TPriority>

Generics won't be added to FastPriorityQueue - I found they cause a ~5% slowdown. If someone really needs a generic version and SimplePriorityQueue is too slow, there is an undocumented class named GenericPriorityQueue<TItem, TPriority> which they can use.

goldenbull commented 7 years ago

👍