2

I am using the popular C5 library for C# (C5) and I'm trying to figure out how to implement the IPriorityQueue properly. Lets say I have the set A={5,4,1,2,3}. I want the priority to be the highest integer value. How do I implement this?

Could someone please give an example of where I add the elements in set A one by one to the queue? Where can you specify the priority property? Kinda lost on the implementation.

The ultimate goal by the way is to use this to make a binary heap.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
DrOmega
  • 31
  • 1
  • 3
  • 1
    I'd suggest looking at their [IntervalHeap](https://github.com/sestoft/C5/blob/master/C5/heaps/IntervalHeap.cs) implementation for an example of implementing `IProirityQueue`. You can use my [simple heap of integers](http://blog.mischel.com/2013/09/30/a-simple-heap-of-integers/) as an example of a heap implementation that you should be able to modify to implement their `IPriorityQueue`. I wrote quite a bit about binary (and d-ary) heaps a few years ago. See http://blog.mischel.com/2013/12/18/posting-the-heap-code/ for the whole series. – Jim Mischel May 02 '17 at 12:26
  • So do you use IPriorityQueue instead of a list? What does that syntax look like? public Mylist : IPriorityQueue ? – DrOmega May 02 '17 at 19:48
  • Are you familiar with implementing interfaces in C#? – Jim Mischel May 02 '17 at 20:39
  • still new to the language a bit, so not really haha. I would really appreciate a quick example of how to use this thing properly. I guess it's just an interface, so I should look that up more. Nice blog btw! – DrOmega May 02 '17 at 22:37
  • Here's my node code so far: https://pastebin.com/mbK8An5m . I guess to maintain the shape property of a heap I should add a left and right option :/ – DrOmega May 02 '17 at 22:41
  • Or you know... make a fib heap or pairing heap and to hell with structure :P – DrOmega May 02 '17 at 22:48

1 Answers1

1

I'm not familiar with that C5 library, but there's an easy-to-use, high-performance priority queue implementation here:

PriorityQueue source

ConcurrentPriorityQueue source (if you need thread safety)

Using one of those, you would construct it this way:

IPriorityQueue<object> queue = new PriorityQueue<object>(5, true);

where 5 is the number of priorities you want to manage, true/false specifies whether you want the priorities to be considered to be descending or ascending in priority, and <object> would be your generic type. Both of these support full list operations as well. There's also a project website.

Iucounu
  • 1,630
  • 1
  • 20
  • 31