Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 92263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:09:34+00:00 2026-05-10T23:09:34+00:00

I have a priority queue implementation in C# that I want to add a

  • 0

I have a priority queue implementation in C# that I want to add a .IndexOf method to.

However, since the priority queue doesn’t really concern itself with the order of the values themselves (that is, if I were to just grab all the values, disregarding their priorities, they wouldn’t necessarily have any order at all), only the priority of them, I don’t have any criteria for the generic type T of the priority queue, that is, I don’t specify that they need to have some intrinsic order, or comparability.

As such, when I came to implement .IndexOf(T value) I have a minor problem.

Is there a standard in what/how I should implement this? My initial thoughts was simply to use EqualityComparer<T>.Default to figure if I have found the value or not, but then there are so many similar such types these days.

For instance, here’s what I came up with to cover my basis, but this seems overkill:

  • public Int32 IndexOf(T value) (internally calls one of the others with ClassThatImplementsInterface.Default)
  • public Int32 IndexOf(T value, IComparer<T> comparer)
  • public Int32 IndexOf(T value, IEqualityComparer<T> comparer)
  • public Int32 IndexOf(T value, IEquatable<T> comparer)
  • public Int32 IndexOf(T value, Predicate<T> predicate)

What do you do? Marking this as both subjective and wiki as this is more of an opinion poll than anything else.

On re-reading my own question I guess I can just use the one without a comparer, and then add the predicate version, this way the user of this class can call just about anything.

Also note that I can also do pq[index] to get hold of a item that contains both the priority and the value itself, so I could also get by without IndexOf at all, but I’d also like to have methods that says change the priority of value X to priority P, which would necessitate some form of IndexOf/search internally. And thus I’d also like to avoid having to have umpteenth overloads of all these methods as well.


Response to comment: Yes, the priority queue is based on a heap.

Basically, the two classes are defined like this:

public class Heap<T> : IEnumerable<T>, ICloneable { ... } public class PriorityQueue<T> : Heap<PriorityQueueElement<T>> { ... } 

PriorityQueueElement is a simple immutable structure with Priority and Value properties.

Response to forthcoming comment: Since the priority queue is based on a heap, an ‘interesting property’ is that by changing the priority of a value through its index means that afterwards, the value won’t necessarily be at that index. I intend to just document this as in some cases I foresee a need for independent locate/change-priority operations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:09:35+00:00Added an answer on May 10, 2026 at 11:09 pm

    I would make the comparison an optional constructor parameter; this is comparable to how things like Dictionary<,>, SortedList<,> etc allow you to specify the comparison mechanism.

    Whether to accepts an IComparer<T> or an IEqualityComparer<T> depends on whether you are going to sort the data, or just look for an equality match; if the match, then you’ll need something like IEqualityComparer<T>. Unfortunately, since this has 2 methods (GetHashCode() and Equals()) there is no direct delegate version of this, except perhaps Predicate<T> or Func<T,T,bool>.

    For the default constructor, I’d pass in the [Equality]Comparer<T>.Default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Trigger are not asynch. Yes, they will increase the time… May 11, 2026 at 3:55 pm
  • added an answer You need to use the correct overload of the method.… May 11, 2026 at 3:55 pm
  • added an answer You can use the Windows Task Scheduler and set it… May 11, 2026 at 3:55 pm

Related Questions

I'm writing a data structure in C# (a priority queue using a fibonacci heap
I need a data structure that always holds the n largest items inserted so
I need to use a priority queue in my Python code, and: am looking
I have a priority_queue of some object: typedef priority_queue<Object> Queue; Queue queue; From time

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.