Re: [CODE] Level-sorted do_who

From: Daniel A. Koepke (dkoepke@california.com)
Date: 04/07/99


On Tue, 6 Apr 1999, Richard P. Bandura wrote:

> My coders are attempting to order out wholist by level, higher levs
> being on top, sorting down to lower levs. We're unsure of how to do it
> really, and we're just screwing around with stuff that works,
> forgetting about efficiency (yes, i know, *shudder*). It's not my
> project, really, so I can't modify the files, but the constant
> crashing of the mud is beginnning to annoy me greatly...so I'm trying
> to help out. Here it is, hacky as can be.

Ugh.  That's a headache.  I would just build a sorted array from the
linked list using qsort().

  int level_comp(const void * a, const void * b)
  {
    struct descriptor_data * ap = (struct descriptor_data *) a;
    struct descriptor_data * bp = (struct descriptor_data *) b;
    struct char_data * char_a = ap->character;
    struct char_data * char_b = bp->character;

    /* Sorts from lowest -> highest, make this "<" to reverse */
    if (GET_LEVEL(char_a) > GET_LEVEL(char_b))
      return 1;

    return -1;
  }

  .
  .
  .

    int num_connections = 0, i;
    struct descriptor_data * d, * array;
    int (*sort_func)(const void *, const void *);

    for (d = descriptor_list; d; d = d->next)
      if (!d->connected) /* if they show-up on the who list */
        num_connections++;

    CREATE(array, struct descriptor_data, num_connections);

    for (d = descriptor_list, i = 0; d; d = d->next, i++)
      if (!d->connected) /* if they show-up on the who list */
        array[i] = *d;

    if (sort_func)
      qsort(array, num_connections, sizeof(struct descriptor_list),
            sort_func);

    /* display who list from array[] */
    .
    .
    .

This is entirely ugly mailer code, of course.  The sort_func variable is
used to point to a function we want to use to sort.  This allows us to
VERY easily change how we sort.  For instance, if we had the following
sort functions:

  int who_sort_alphabetical(const void *, const void *);
  int who_sort_alphabetical_reverse(const void *, const void *);
  int who_sort_level(const void *, const void *);
  int who_sort_level_reverse(const void *, const void *);
  int who_sort_class(const void *, const void *);
  int who_sort_class_reverse(const void *, const void *);

and we wanted to change how we sort based upon arguments given to do_who()
we might have,

  .
  .
  .
  case 's': /* sort */
    char blah = *(arg + 2);
    switch (blah) {
    case 'a':
      sort_func = who_sort_alphabetical;
      break;
    case 'A':
      sort_func = who_sort_alphabetical_reverse;
      break;
    case 'l':
      sort_func = who_sort_level;
      break;
    case 'L':
      sort_func = who_sort_level_reverse;
      break;
    case 'c':
      sort_func = who_sort_class;
      break;
    case 'C':
      sort_func = who_sort_class_reverse;
      break;
    default:
      /* report usage message/error */
      return;
    }
    break;

All of this is 100% untested.  Use at your own risk.  No need to credit
me, just please don't take discredit me (that is, don't claim you wrote
it).

-dak


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST