Re: Help! Need to sorted collection accessible by key




KH wrote:
Create your own type for the key that implements IComparable, and override
Equals() and GetHashCode(), then return the hashcode of the key you want
access with, and compare (sort) by the one you want to order by, and put it
in SortedList; quickly:

class MyKey : IComparable
{
MyKey(string comparer, string hasher)
{
this._comparer = comparer;
this._hasher = hasher;
}

override bool Equals(MyKey other)
{
return (this._comparer == other._comparer);
}

override int GetHashCode()
{
return this._hasher.GetHashCode();
}

int CompareTo(MyKey other)
{
return this._comparer.CompareTo(other._comparer);
}
};

This is unlikely to work, or at least will work under restricted
circumstances.

Hashtable uses the Equals method to decide if two items are equal,
since GetHashCode may return the same code for more than one item.

So, this will work ONLY if the _comparer and the _hasher are such that
if two items compare as equal by the _comparer key then they also
compare equal by the _hasher key. Let's look at what happens if this is
not true.

If two items that compare equal using the _hasher key do not compare
equal with the _comparer key, then Hashtable will put duplicate entries
in the table for that item, since the second item to be hashed will
hash to the same bucket in the hash table but will not be equal to
anything in the chain. So this:

MyKey key1 = new MyKey("A", "A");
MyKey key2 = new MyKey("B", "A");
myHashtable[key1] = obj1;
myHashtable[key2] = obj2;

may result in two entries in the hash table even though the "hasher"
key of both key1 and key2 are the same.

Conversely, if two items that compare equal using the _comparer key do
not compare equal with the _hasher key, then they may result in only
one entry in the Hashtable if by chance the two _hasher keys hash to
the same code (and thus the same hash table bucket), since the second
object to be hashed will be equal to an item already in the chain.

I would suggest instead that you create your own data structure backed
by a Hashtable and either a SortedList or your own sorted list
implementation. Here's a rough idea:

public class MyCollection
{
private Hashtable _hash;
private SortedList _list;

public MyCollection(int capacity)
{
this._hash = new Hashtable(capacity);
this._list = new SortedList(capacity);
}

public MyCollection() : this(10) { }

public object this[string hashKey]
{
get { return this._hash[hashKey]; }
}

public object this[int index]
{
get { return this._list[index]; }
}

public IDictionaryEnumerator GetEnumerator() { return
this._list.GetEnumerator(); }

public object this[string hashKey, string listKey]
{
get
{
object hashObj = this._hash[hashKey];
object listObj = this._list[listKey];
if (hashObj != listObj) { throw new
ArgumentException(String.Format("Hash key '{0}' and list key '{1}'
indicate different elements.", hashKey, listKey), "listKey"); }
return hashObj;
}

set
{
this._hash[hashKey] = value;
this._list[listKey] = value;
}
}
}

Of course, you can define more methods, and the semantics are really up
to you. (For example, if you say myStructure["key"] do you mean the
hash table key or the list key? Or perhaps the caller must always
specify both keys? Or perhaps you supply functions, not indexers, to
allow the caller to look in either table?) This was just a quick sketch
of what is possible.

.



Relevant Pages

  • Re: Interesting list Validity (True/False)
    ... Python to compare those strings equal. ... "a equals d" is true. ... There may or may not be any coercion involved.) ... And if you ask any mathematician, he'll say that is equal to. ...
    (comp.lang.python)
  • Re: Reading specific memory address into variable
    ... cons_pi and it equals 3.14159265358979. ... for how many times they want to do that calculation to figure out pi (4/1 - ... which is where I was stuck trying to figure out a way to compare the two ... incrementing the accuracy variable by 1 and incrementing the multiply ...
    (comp.lang.cpp)
  • Re: do I need to override the equals() method?
    ... Make sure they are consistent with each other, i.e., if two instances compare equal, they must have the same hash code. ... It occurs to me that something I'd like in an IDE is for it to let me mark the fields that consititute my unique key, and then it can generate equals() and hashCode(). ... lists the local variables with check boxes, and also gives an option to ...
    (comp.lang.java.programmer)
  • Re: Interesting list Validity (True/False)
    ... The exceptions you mean are not exceptions to "'X==Y' means 'X equals ... "Obbjects of different types never compare equal". ... The exceptions being different numeric types and different string types, that have a special treatment; see section 5.9 in the Reference Manual for details." ...
    (comp.lang.python)
  • Re: Suggestions for double-hashing scheme
    ... >> secondary hash function to 1/4 of the table size, ... > Hsieh is considered a laughing stock in many circles, ... Thats more typedefs than in all of bstrlib already. ... (Compare this to Bstrlib, whose influence ...
    (comp.programming)