I have some objects that I'd like to put into an associative container that groups them by a non-comparable field value. So if I had the following:
using System.Collections.Generic;
using Pair = System.Tuple<Penguin, int>;
class Penguin {}
class Whatever {
public static void Main(string[] args) {
Penguin emperor = new Penguin(),
adelie = new Penguin(),
batmanVillain = new Penguin();
var okay = new HypotheticalCollection<Pair>(5);
okay.Add(new Pair(emperor, 1) );
okay.Add(new Pair(adelie, 2) );
okay.Add(new Pair(batmanVillain, 3) );
okay.Add(new Pair(emperor, -500) );
okay.Add(new Pair(batmanVillain, 5) );
}
}
and I iterated over the HypotheticalCollection, I would want to see the two emperor
entries together and the two batmanVillain
entries together, but I don't care which one's first. However, I would like to be able to look up an entry in logarithmic time.
If I were using C++, I would just sort the Penguin
s by their address, but I don't have that luxury in C#. Right now I'm handling it by assigning a serial number to each of the objects, but that feels kind of hacky, and I'd prefer not to have the extra space overhead. Is there a better way, maybe a ReferenceLessThan
function or something?