[m-dev.] for review: use bitsets in quantification

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Nov 8 19:38:14 AEDT 2000


On 08-Nov-2000, schachte at cs.mu.OZ.AU <schachte at cs.mu.OZ.AU> wrote:
> On  8 Nov, Fergus Henderson wrote:
> 
> > For a case like that, you could define `to_int' to return the log to
> > the base 2 of the value (using int__log2).  And you could then use
> > `dense_bitset' (or perhaps even `int_bitset', for bit sets that fit
> > in a single int), rather than `sparse_bitset'.
> 
> Take the base 2 log, and then shift 1 left that many places to get the
> value to OR into the bitset?
> That's a pretty long, inefficient way to compute an identity function.

Actually for a C enum you probably want to use an explicit lookup
table, rather than int__log2, since this will probably be a bit more
efficient:

	:- type my_enum --->
		my_enum(int). % the int is the C enum value

	:- func myenum_to_int(my_enum) = int.
	:- mode myenum_to_int(in) = out is semidet.
	:- mode myenum_to_int(out) = in is semidet.
	myenum_to_int(my_enum(0x0001)) = 1.
	myenum_to_int(my_enum(0x0002)) = 2.
	myenum_to_int(my_enum(0x0004)) = 3.
	myenum_to_int(my_enum(0x0008)) = 4.
	myenum_to_int(my_enum(0x0010)) = 5.
	myenum_to_int(my_enum(0x0020)) = 6.

	:- instance enum(my_enum) where [
		from_int(myenum_to_int(X)) = X,
		to_int(X) =
			( myenum_to_int(X) = Int ->
				Int
			;
				throw(software_error("myenum_to_int failed"))
			)
	].
				
I agree it's still somewhat inefficient, but your suggestion of
defining a class method which just returns the bitmask directly would
solve the efficiency problem.

> Also, this doesn't handle enumerated types where there are holes.

Using an explicit lookup table like the one above also has the
advantage that it handles enumerated types where there are holes.

	myenum_to_int(my_enum(0x0001)) = 1.
	myenum_to_int(my_enum(0x0002)) = 2.
	% holes for 0x0004 and 0x0008
	myenum_to_int(my_enum(0x0010)) = 3.
	myenum_to_int(my_enum(0x0020)) = 4.
	% holes for 0x0040 and 0x0080
	myenum_to_int(my_enum(0x0100)) = 5.
	myenum_to_int(my_enum(0x0200)) = 6.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list