Hashtable Contains() - byte arrays as keys - | | |
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 | | | | re: Hashtable Contains() - byte arrays as keys -
You are correct; any array or collection object would test for the object
reference, not the contents. A match of an arbitrary number of items in a
collection would be cost-prohibitive.
Why do you need to match a byte array? What are you trying to accomplish?
Maybe there's a different approach.
--Bob
"Joseph Lee" <joeylta1979@hotmail.com> wrote in message
news:edH7agijEHA.632@TK2MSFTNGP12.phx.gbl...[color=blue]
> Hi All,
>
> I am having problem when i am using hashtable to keep an array of bytes
> value as keys.[/color] | | | | re: Hashtable Contains() - byte arrays as keys -
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:
[color=blue]
> 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
>
>
>[/color] | | | | re: Hashtable Contains() - byte arrays as keys -
I have a list of encrypted keywords and all of them are in an array of byte.
So I am trying to keep those keyword as keys in a hashtable for fast lookup,
then i will be able retrieve some information which is in the value part of
the hashtable
Joey
"Bob Grommes" <bob@bobgrommes.com> wrote in message
news:#VlDqjjjEHA.3944@tk2msftngp13.phx.gbl...[color=blue]
> You are correct; any array or collection object would test for the object
> reference, not the contents. A match of an arbitrary number of items in a
> collection would be cost-prohibitive.
>
> Why do you need to match a byte array? What are you trying to accomplish?
> Maybe there's a different approach.
>
> --Bob
>
> "Joseph Lee" <joeylta1979@hotmail.com> wrote in message
> news:edH7agijEHA.632@TK2MSFTNGP12.phx.gbl...[color=green]
> > Hi All,
> >
> > I am having problem when i am using hashtable to keep an array of bytes
> > value as keys.[/color]
>
>[/color] | | | | re: Hashtable Contains() - byte arrays as keys -
ic, i will look into it. Thanks
Joey
"Rakesh Rajan" <RakeshRajan@discussions.microsoft.com> wrote in message
news:8719FC6F-94E0-4217-8BC9-838FEEB1D665@microsoft.com...[color=blue]
> As Bob said, it's the reference that is being tested and not the contents.[/color]
If[color=blue]
> I remember right, searching in the lines of overriding Equals and[/color]
GetHashCode[color=blue]
> might help. Sorry, I can't recollect them now...
>
>
> "Joseph Lee" wrote:
>[color=green]
> > 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[/color][/color]
true,[color=blue][color=green]
> > while using a new object with same 'value' returns false. If I am[/color][/color]
correct[color=blue][color=green]
> > 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[/color][/color]
all[color=blue][color=green]
> > the keys out and make a byte comparison it would be O(n) time[/color][/color]
complexity,[color=blue][color=green]
> > thus defeating the purpose of a hash table.
> >
> > Is there any other command in hashtable or some functions i can[/color][/color]
overwrite?[color=blue][color=green]
> > Would prefer a much simpler way if possible.
> >
> > Thanks in advance
> >
> > Joseph Lee
> >
> >
> >[/color][/color] | | | | re: Hashtable Contains() - byte arrays as keys -
Joseph Lee wrote:[color=blue]
> ic, i will look into it. Thanks[/color]
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.
[color=blue]
>
> Joey
>
> "Rakesh Rajan" <RakeshRajan@discussions.microsoft.com> wrote in message
> news:8719FC6F-94E0-4217-8BC9-838FEEB1D665@microsoft.com...
>[color=green]
>>As Bob said, it's the reference that is being tested and not the contents.[/color]
>
> If
>[color=green]
>>I remember right, searching in the lines of overriding Equals and[/color]
>
> GetHashCode
>[color=green]
>>might help. Sorry, I can't recollect them now...
>>
>>
>>"Joseph Lee" wrote:
>>
>>[color=darkred]
>>>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[/color][/color]
>
> true,
>[color=green][color=darkred]
>>>while using a new object with same 'value' returns false. If I am[/color][/color]
>
> correct
>[color=green][color=darkred]
>>>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[/color][/color]
>
> all
>[color=green][color=darkred]
>>>the keys out and make a byte comparison it would be O(n) time[/color][/color]
>
> complexity,
>[color=green][color=darkred]
>>>thus defeating the purpose of a hash table.
>>>
>>>Is there any other command in hashtable or some functions i can[/color][/color]
>
> overwrite?
>[color=green][color=darkred]
>>>Would prefer a much simpler way if possible.
>>>
>>>Thanks in advance
>>>
>>>Joseph Lee
>>>
>>>
>>>[/color][/color]
>
>
>[/color]
--
mikeb | | | | re: Hashtable Contains() - byte arrays as keys -
But that would again mean he will have to iterate and compare each entries
right? Wonder whether there are any other efficient implementations?
"mikeb" wrote:
[color=blue]
> Joseph Lee wrote:[color=green]
> > ic, i will look into it. Thanks[/color]
>
> 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.
>[color=green]
> >
> > Joey
> >
> > "Rakesh Rajan" <RakeshRajan@discussions.microsoft.com> wrote in message
> > news:8719FC6F-94E0-4217-8BC9-838FEEB1D665@microsoft.com...
> >[color=darkred]
> >>As Bob said, it's the reference that is being tested and not the contents.[/color]
> >
> > If
> >[color=darkred]
> >>I remember right, searching in the lines of overriding Equals and[/color]
> >
> > GetHashCode
> >[color=darkred]
> >>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[/color]
> >
> > true,
> >[color=darkred]
> >>>while using a new object with same 'value' returns false. If I am[/color]
> >
> > correct
> >[color=darkred]
> >>>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[/color]
> >
> > all
> >[color=darkred]
> >>>the keys out and make a byte comparison it would be O(n) time[/color]
> >
> > complexity,
> >[color=darkred]
> >>>thus defeating the purpose of a hash table.
> >>>
> >>>Is there any other command in hashtable or some functions i can[/color]
> >
> > overwrite?
> >[color=darkred]
> >>>Would prefer a much simpler way if possible.
> >>>
> >>>Thanks in advance
> >>>
> >>>Joseph Lee
> >>>
> >>>
> >>>[/color]
> >
> >
> >[/color]
>
>
> --
> mikeb
>[/color] | | | | re: Hashtable Contains() - byte arrays as keys -
Rakesh Rajan wrote:[color=blue]
> But that would again mean he will have to iterate and compare each entries
> right? Wonder whether there are any other efficient implementations?[/color]
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.
[color=blue]
>
>
> "mikeb" wrote:
>
>[color=green]
>>Joseph Lee wrote:
>>[color=darkred]
>>>ic, i will look into it. Thanks[/color]
>>
>>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.
>>
>>[color=darkred]
>>>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
>>>>>
>>>>>
>>>>>
>>>
>>>
>>>[/color]
>>
>>--
>>mikeb
>>[/color][/color]
--
mikeb |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|