Re: Hashtable Contains() - byte arrays as keys -

From: mikeb (mailbox.google_at_nospam.mailnull.com)
Date: 09/01/04


Date: Wed, 01 Sep 2004 09:55:56 -0700

Rakesh Rajan wrote:
> But that would again mean he will have to iterate and compare each entries
> right? Wonder whether there are any other efficient implementations?

Assuming I understand his problem (that he needs the byte arrays to
perform a 'deep' compare of the contents of the arrays), then yes -
these interface implementations would need to generate a hash value that
was dependent on the contents of the byte array and to compare the
entries of the arrays.

The check for equality could certainly have some short cuts so that a
member-by-member comparison might not always be needed:

   1) if there's reference equality, the arrays are by definition equal
   2) if the array lengths are different, then the arrays must not be
equal.

The hashcode might be cacheable if the byte arrays are not changed once
they are created. Indeed, once the byte arrays are added to the
hashtable, it would be a bug to modify them in such a way that the
hashcode (or equality test) would render different results.

There may be other techniques that can improve the efficiency
(particularly if unsafe code can be used). However, my main point was
that there's a way for the OP to get the behavior he needs without too
much work.

Performance is of no consequence if things don't even work correctly in
the first place.

>
>
> "mikeb" wrote:
>
>
>>Joseph Lee wrote:
>>
>>>ic, i will look into it. Thanks
>>
>>You can create your Hashtable with custom Hashcode provider and comparer.
>>
>>Look at the Hashtable( IHashCodeProvider, IComparer) constructor.
>>
>>With a Hashtable created with that constructor you can provide your own
>>methods for comparing the byte arrays instead of using the default
>>methods provided by the Array class.
>>
>>
>>>Joey
>>>
>>>"Rakesh Rajan" <RakeshRajan@discussions.microsoft.com> wrote in message
>>>news:8719FC6F-94E0-4217-8BC9-838FEEB1D665@microsoft.com...
>>>
>>>
>>>>As Bob said, it's the reference that is being tested and not the contents.
>>>
>>>If
>>>
>>>
>>>>I remember right, searching in the lines of overriding Equals and
>>>
>>>GetHashCode
>>>
>>>
>>>>might help. Sorry, I can't recollect them now...
>>>>
>>>>
>>>>"Joseph Lee" wrote:
>>>>
>>>>
>>>>
>>>>>Hi All,
>>>>>
>>>>>I am having problem when i am using hashtable to keep an array of bytes
>>>>>value as keys.
>>>>>
>>>>>Take a look at the code snippet below
>>>>>
>>>>>---------------------------------------------------
>>>>>
>>>>>ASCIIEncoding asciiEncoder = new ASCIIEncoding();
>>>>>byte[] bArray = asciiEncoder.GetBytes("Test");
>>>>>
>>>>>Hashtable ht = new Hashtable();
>>>>>ht.Add(bArray,"Some value");
>>>>>
>>>>>byte[] bArrayNew = asciiEncoder.GetBytes("Test");
>>>>>
>>>>>Console.WriteLine("Result : "+ ht.Contains(bArray)); //true
>>>>>Console.WriteLine("Result : "+ ht.Contains(bArrayNew)); //false
>>>>>Console.Read();
>>>>>
>>>>>--------------------------------------------------
>>>>>
>>>>>As seen here, by using the same object , the contains() will return
>>>
>>>true,
>>>
>>>
>>>>>while using a new object with same 'value' returns false. If I am
>>>
>>>correct
>>>
>>>
>>>>>the contains() command does not look at the values in an object. As for
>>>>>primitive types like normal string, int and etc, it does not have any
>>>>>problem.
>>>>>
>>>>>So I was wondering if there is anyway i can keep byte arrays as keys in
>>>>>hashtable and still find it in the time complexity of O(1). If i read
>>>
>>>all
>>>
>>>
>>>>>the keys out and make a byte comparison it would be O(n) time
>>>
>>>complexity,
>>>
>>>
>>>>>thus defeating the purpose of a hash table.
>>>>>
>>>>>Is there any other command in hashtable or some functions i can
>>>
>>>overwrite?
>>>
>>>
>>>>>Would prefer a much simpler way if possible.
>>>>>
>>>>>Thanks in advance
>>>>>
>>>>>Joseph Lee
>>>>>
>>>>>
>>>>>
>>>
>>>
>>>
>>
>>--
>>mikeb
>>

-- 
mikeb


Relevant Pages

  • Re: Array comparison
    ... And how do you compare two classes? ... does not explain why it can't compare two like-typed arrays. ... Free Pascal supports operator overloading. ... more inclined to say the reason is lack of customer demand for the feature. ...
    (alt.comp.lang.borland-delphi)
  • Re: Option Compare Statement
    ... both arrays are always the same type. ... compare text,. ... Your first post regarding this in the vb.controls newsgroup was, ... strings, let's say A and B. Then I want to compare their values. ...
    (microsoft.public.vb.general.discussion)
  • Re: Similarity searching
    ... a factor of N*log, if the arrays are big you won't in general have to compare to the last element so I don't see the factor of L**2, and I don't see where that C**2 factor is coming from at all. ... There are also sorting algorithms which don't compare each item to every other item, yet still generate a sorted result. ... If electricity comes from electrons, ...
    (comp.lang.fortran)
  • Re: Nearest Neighbour / Ternary Search Tree
    ... confirm that using Byte Arrays has produced significant speed ... With the dataset I am testing on, the entries in the array are not ... I only mention this as Larry indicated that I would ... probably only need to compare about 20% of the entires by eliminating ...
    (microsoft.public.vb.general.discussion)
  • Re: Similarity searching
    ... a factor of N*log, if the arrays are big you won't in general have to compare to the last element so I don't see the factor of L**2, and I don't see where that C**2 factor is coming from at all. ... There are also sorting algorithms which don't compare each item to every other item, yet still generate a sorted result. ... Similarity searching is still an active science, but there are a lot of useful algorithms out there now. ...
    (comp.lang.fortran)