[mercury-users] Switching on strings

Ralph Becket rafe at cs.mu.OZ.AU
Thu Jul 24 11:18:04 AEST 2003


Michael Day, Thursday, 24 July 2003:
> 
> Hi,
> 
> Following on from the discussion of fact tables, how about strings?
> Specifically, considering these two cases:
> 
> func1(S) = X :-
>     ( if S = "one" then X = 1
>     else if S = "two" then X = 2
>     else if S = "three" then X = 3
>     ...
>     else fail ).
> 
> func2("one") = 1.
> func2("two") = 2.
> func2("three") = 3.
> 
> The first will require N/2 string comparisons on average, but will the
> second be implemented using a hash table instead?

A trie might be a better idea.

Looking at compiler/switch_gen.m, dense switches on ints, chars and enums are
by index into a jump table, unless the arms of the switch are
construction unifications with constants, in which case a lookup table
is used.

Switches on DU types is by if-then-else chain, dense jump table, or
binary search.

Switches on strings are by hash table.

All other cases are handled as if-then-else chains.

-- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list