Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rochester!pt!ius2.cs.cmu.edu!edw From: edw@ius2.cs.cmu.edu (Eddie Wyatt) Newsgroups: comp.lang.c Subject: Re: enum - enum ? Message-ID: <1209@ius2.cs.cmu.edu> Date: Fri, 19-Jun-87 12:52:56 EDT Article-I.D.: ius2.1209 Posted: Fri Jun 19 12:52:56 1987 Date-Received: Tue, 23-Jun-87 01:11:43 EDT References: <7906@brl-adm.ARPA> Organization: Carnegie-Mellon University, CS/RI Lines: 106 Example of the pathological case for enums array indexes. (An implementors perspective) typedef enum {W = -10, X = -100, Y = 10, P = 0, Z = 0} PATHO ; int sumation(x) int x[PATHO]; { register PATH i; register int sum; for (i = W; i <= Z; i = successor(i)) sum += x[i]; return(sum); } The task here is to sum the over the first 5 indexes of array x. Lets consider the code that needs to be generated for the statements: for (i = W; i <= Z; i = successor(i)) sum += x[i]; first try for (i = -10, i <= 0; i = enum_get_next(i)) sum += *(x + lookup_hashtable(i)); execute the loop and we get : i = -10, -100, 10 - exit loop. Nope that can't be right. Assume we can fix the above problem somehow and lets consider the code generated for, lets say: sum += x[P]; try sum += *(x + lookup_hashtable(0)); Hmmm does lookup_hashtable return 3 or 4??? Lets consider an alternate solution enums as array indexes. Allow each enumeration element have two values associated with it - the place it holds in the enumeration (for W that would be the value 0) and the assigned value (for W that would be the value -10). From the context, one MIGHT be able to determine which is the correct value to use. In the above case. for (i = W; i <= Z; i = successor(i)) sum += x[i]; yields : for (i = 0; i <= 4; i++) sum += *(x+i); The above context prescribes that W,Z and i be intepreted as "place" values, not the "assigned" values. Look here too, no hashing function either - efficient code. The major question here is whether the correct interpretation can always be choosen given the context the variable is being used in. Example : typdef enum {RED = 30, GREEN = 40, BLUE = 50} COLORTABLE; ...... colortable[RED] = 64; ..... Are we indexing colortable[0] or colortable[30]?? We might say that if colortable is defined as int colortable[COLORTABLE] then the actual index is 0. If colortable is defined as int colartable[] or colortable[256] then we are refering to index 30. There are more things to be thought out, but this is a posible implementation alternative for enums in C. -- Eddie Wyatt e-mail: edw@ius2.cs.cmu.edu terrorist, cryptography, DES, drugs, cipher, secret, decode, NSA, CIA, NRO.