IndexOf over-implementation question

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

However, since the priority queue doesn't really concern the very order of the values โ€‹โ€‹themselves (that is, if I should just grab all the values, regardless of their priorities, they won't necessarily have any order at all), only their priority, I have no criteria for the general type T of the priority queue, that is, I am not indicating that they need to have some kind of internal ordering or comparability.

As such, when I came to the implementation .IndexOf(T value)

I have a little problem.

Is there a standard on what / how should I implement this? My initial thoughts were just to use EqualityComparer<T>.Default

to determine if I found value

or not, but there are so many of these types these days.

For example, here's what I came up with to cover my base, but it 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? Noting this is both subjective and wiki as this is more of a public opinion poll than anything else.

On re-reading my own question, I think I can just use one without a comparator, and then add the predicate version so that the user of that class can invoke whatever.

Also note that I can also do pq[index]

to get an element that contains both priority and value, so I could do without IndexOf at all, but I would also like to have methods that say change the priority of the X value to the priority P. which would require some form of IndexOf / search inside. And thus, I would also like to avoid unnecessary overloads of all these methods.


Reply to comment : Yes, the priority queue is heap based.

Basically, the two classes are defined as follows:

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.

Reply to upcoming comment . Since the priority queue is heap-based, the "interesting property" is that changing the priority of a value through its index means that after that value, t must be at that index. I intend to simply document this, as in some cases I foresee the need for independent search / change priority operations.

+1


source to share


1 answer


I would make the comparison an optional constructor parameter; this is comparable to how things like Dictionary<,>

, SortedList<,>

etc. allow you to specify a comparison mechanism.

Whether IComparer<T>

or not to accept or IEqualityComparer<T>

depends on whether you are going to sort the data or just search for an equality match; if a match then you will need something like IEqualityComparer<T>

. Unfortunately, as it 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 would go to [Equality]Comparer<T>.Default

.

+2


source







All Articles