Re: What's the most efficient testing of variables?



Depending on the hashing function used, a hash table can usually work somewhere between
O(1) and O(2), that is, constant time. If there is a known set of values, an array of
pointers gives O(1) as well.

joe

On Wed, 04 Jan 2006 10:45:37 -0600, "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote:

>On Wed, 4 Jan 2006 08:26:04 -0600, "Jeff" <jbk@xxxxxxxxxxxx> wrote:
>
>>I need to quickly (efficiently) compare a DWORD with a list of DWORDs to
>>find the one and only match. I can guaranty that there is only one match.
>>switch won't work because both are variables. How about if...else if...else
>>if.... etc? Or is there a better way?
>>
>>
>>DWORD dwPassed, dwAssigned1, dwAssigned2, dwAssigned3;
>>
>>ideally I would use a switch like statement:
>>
>>switch(dwPassed)
>>{
>>case dwAssigned1
>> deal with it;:
>> break;
>>case dwAssigned2:
>> deal with it;:
>> break;
>>case dwAssigned3:
>> deal with it;:
>> break;
>>default:
>> deal with it;:
>> break;
>>}
>>
>>But since all are variables it won't work.
>
>There's nothing wrong with if/else if/else, except it doesn't scale too
>well. The general solution is to map these values to functions, so that you
>can look up a value and call the associated function. You can do this
>easily with std::map (O(log N)), though for relatively small data sets,
>linear search of an array (O(N)) will be faster.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Pinning Array
    ... Alex Yakhnin, .NET CF MVP ... parameter, which is een array, be pinned? ... since the CLR could move the array on the managed heap. ... DWORD WaitForMultipleObjects( ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Word 2007 is unacceptable and must be changed
    ... Doug Robbins - Word MVP ... there weren't enough persuading factors to make a version switch. ... plan for a poor take-up of Word 2007. ...
    (microsoft.public.word.newusers)
  • Re: [OT] Funny
    ... >> the NAG compiler does have a switch to allow you to tell it ... or the compiler simply isn't conforming. ... more arithmetic in computing addresses of array elements -- dis- ...
    (comp.lang.fortran)
  • SUMMARY: switch/hub for 3 T3 Raid devices
    ... > host and I want to use all 3 devices on the same host. ... Are there seperate hub and switch type solutions and is ... Loop from one array to another from the host to each array. ...
    (SunManagers)
  • Re: Need help to marshall Win32 DLL call to VB.net
    ... I allocate an array and I call GetDirInfo to fill the array ... DWORD WIMAAPI GetNbEntryCurDir; ... // LPDIRINFO: array of DIRINFO that will receive the info ... on C++ unmanager, I do: ...
    (microsoft.public.dotnet.languages.vb)