Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!cmcl2!philabs!micomvax!musocs!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP (der Mouse) Newsgroups: net.lang.c Subject: Re: goto jumps into loops Message-ID: <412@mcgill-vision.UUCP> Date: Mon, 21-Apr-86 00:20:26 EST Article-I.D.: mcgill-v.412 Posted: Mon Apr 21 00:20:26 1986 Date-Received: Thu, 24-Apr-86 07:09:35 EST References: <69@brl-smoke.ARPA> Organization: McGill University, Montreal Lines: 91 Summary: goto-less way which is *shorter* and *cleaner* as well In article <69@brl-smoke.ARPA>, dmh@mit-borax.ARPA (David Harmon) writes: > [...] I need to print out all the elements of a set capable of containing > or not containing any value from 0..255. > [...] but I also wanted to contract adjacent members of a set into a range > syntax. > > /* Assume the existence of a type named "set", which is intended to simulate > the Pascal TYPE KeySet:SET OF 0..255; > Further assume the existence of a function > in(e,l); > set *l; > char e; > which returns a boolean 1 iff _e_ is an element of _*l_. > */ > [48-line example, using obscure goto, deleted] I once wrote some code which did something similar; adapting this to your problem.... As far as I can tell from your code (it's not the easiest thing in the world to understand), you want spaces instead of commas. For example, you want 1 3 5-10 not 1,3,5-10 I am also taking the liberty of producing a leading space, that is, this code produces " 1 3 5-10" instead of "1 3 5-10" This can be fixed with two more lines (a boolean variable and some code to use it). This is only 26 lines (saving of 22), and it is understandable (by comparison). Five more lines can be trimmed out if you are willing to sacrifice a smidgen of speed by calling in() more often than is really necessary: - delete the declarations of isin and oldin - delete the initialization of oldin - delete the oldin=isin assignment - change these three lines { isin = (i==MAX) ? 0 : in(listp,i); if (isin != oldin) { if (isin) to these two { if (((i==MAX)?0:in(listp,i)) != ((i==0)?0:in(listp,i-1))) { if ((i==MAX)?0:in(listp,i)) The messy `?:' operators in the last change above (and in the original assignment to isin) can be eliminated if in() is guaranteed to return 0 for out-of-bounds values of e. Just replace the entire `?:' expression with the call to in() contained in the `else' part. Without further ado, here is the code: #define MAX NUM_KEYWORDS /*The highest element in use by the application*/ wrtlist(listp) set *listp; { int i; int j; int isin; int oldin; oldin = 0; for (i=0;i<=MAX;i++) { isin = (i==MAX) ? 0 : in(listp,i); if (isin != oldin) { if (isin) { printf(" %d",i); j = i; } else { if (j < i-1) { printf("-%d",i-1); } } } oldin = isin; } } -- der Mouse USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse philabs!micomvax!musocs!mcgill-vision!mouse Europe: mcvax!decvax!utcsri!mcgill-vision!mouse mcvax!seismo!cmcl2!philabs!micomvax!musocs!mcgill-vision!mouse ARPAnet: utcsri!mcgill-vision!mouse@uw-beaver.arpa Wizard: One who can find and fix bugs in an emergency (such as during a site visit by funding agencies).