lynx-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: lynx-dev lynx2.8.2dev.18


From: Mike Castle
Subject: Re: lynx-dev lynx2.8.2dev.18
Date: Fri, 19 Mar 1999 13:03:58 -0600 (CST)

[Still way behind in email, these issues have probably already been worked out]

Amazingly enough address@hidden said:
> > There is a similar pair of command tables in "mutt", but the process 
> > of keeping them lined up has been automated by gen'ing the entry pairs 
> > from a single text file at make time. 
> 
> we do that in vile and tin (hadn't noticed that mutt does)
> -- but it's a PITA to port to non-Unix boxes.

I often use parallel enums and arrays based upon enums in my code.

Rather than trying to worry about them being in sync, I create an array of 
structs that has the enums as one member, then loop over that array, match
the enum, then pull that value out.  Performance is neglible for what I use
it for.  If performace becomes an issue, could be processed at first call
into a tree of some sort that is walked from then on.  Hardcoded arrays
are O(1), my method O(n), tree O(log n).  For small arrarys, O(n) isn't
bad, and a lot easier to maintain than other methods.

Example:

typedef enum 
{
  sd_import, sd_export, sd_aom, sd_data, sd_unused
} directories_t;

typedef struct
{
  directories_t sd;
  char * name;
} directory_map_t;

directory_map_t map[]=
{
  sd_import, "import",
  sd_export, "export",
  sd_aom, "aom",
  sd_data, "data"
};

char * get_dir(directories_t d)
{
  int i;

  for (i=0; sizeof(map)/sizeof(directory_map_t); i++)
  {
    if (d == map[i].sd)
    {
      return map[i].name;
    }
  }
  log unhandled request here
  return NULL;
}

Now, in this example, the things are trivial, and could have been used in
line.

Granted, the contents must still have the same ifdef's, but at least the
order no longer matters.

And if you get paranoid, you could always build the exectuble to, in the
initialization routines, to loop through from 0 to sd_unused and make sure
that each one returns a value to make sure they're all mapped. (You don't
have to worry about the other way, because if an ifdef removes and enum but
forgets to touch the map side, the enum won't be defined and won't
compile).

That should, at least, be portable across all systems.

mrc
-- 
       Mike Castle       Life is like a clock:  You can work constantly
  address@hidden  and be right all the time, or not work at all
www.netcom.com/~dalgoda/ and be right at least twice a day.  -- mrc
    We are all of us living in the shadow of Manhattan.  -- Watchmen

reply via email to

[Prev in Thread] Current Thread [Next in Thread]