/* -*- Mode: C -*-
 *
 * $Header: /home/gene/library/website/docsrc/cloop/src/RCS/teq.c,v 395.1 2008/04/20 17:25:50 gene Exp $
 *
 * Copyright (c) 2001 Gene Michael Stover.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "this.h"

/*
 */
static char S_tag[] = __FILE__;

/*
 * Compare two Time Events.
 *
 * If A should occur first, then A is less than B.
 *
 * If A should occur at the same time as B, but A has a smaller
 * sequence number (A's N), then A is less than B.
 *
 * Otherwise, B must occur first, or B must occur at the same time
 * & has a samller sequence number, so B is less than A.
 */
static int
S_Cmp (const void *va, const void *vb)
{
  int cmp;
  TimeEvent *a, *b;

  assert (va != NULL);
  assert (vb != NULL);
  a = (TimeEvent *) va; b = (TimeEvent *) vb;
  if (a->when > b->when) cmp = -1;
  else if (a->when == b->when) {
    if (a->n > b->n) cmp = -1;
    else if (a->n == b->n) {
      fprintf (stderr, "\n%s:%d: A's N == B's N.  This should never happen.",
               __FILE__, __LINE__);
      cmp = 0;
      abort ();
    } else {
      cmp = 1;
    }
  } else {
    assert ((b->when > a->when) || (b->when == a->when && b->n > a->n));
    cmp = 1;
  }
  return cmp;
}

/*
 * Return the number of items in the TimeEventQueue.
 */
C_API int
TEQ_Count (TimeEventQueue *teq)
{
  assert (teq != NULL);
  return teq->count;
}

/*
 * Destroy a TimeEventQueue.  Return NULL as convenience to caller.
 */
C_API TimeEventQueue *
TEQ_Destroy (TimeEventQueue *teq)
{
  if (teq != NULL) {
    TEQ_Uninit (teq);
    xfree (teq);
  }
  return NULL;
}

/*
 */
C_API void
TEQ_Dump (TimeEventQueue *teq, FILE *strm)
{
  assert (teq != NULL);
  assert (strm != NULL);
  fprintf (strm, "\n#<TEQ %p", teq);
  fprintf (strm, "\n  (tag . \"%s\")", teq->tag);
  fprintf (strm, "\n  (lst . ...)");
  fprintf (strm, "\n  (count . %d)", teq->count);
  fprintf (strm, "\n  (n . %lu)", teq->n);
  fprintf (strm, "\n  (isUnsorted . %s)", teq->isUnsorted ? "T" : "NIL");
  fprintf (strm, ">");
}

/*
 */
C_API void
TEQ_Init (TimeEventQueue *teq)
{
  assert (teq != NULL);
  bzero (teq, sizeof *teq);
  teq->tag = S_tag;
}

/*
 * Return true if the TimeEventQueue is empty.
 */
C_API int
TEQ_IsEmpty (TimeEventQueue *teq)
{
  assert (teq != NULL);
  return teq->count == 0;
}

/*
 * Return a pointer to a dynamically allocated & initialized
 * TimeEventQueue.
 */
C_API TimeEventQueue *
TEQ_New ()
{
  TimeEventQueue *teq;

  teq = (TimeEventQueue *) xmalloc (sizeof *teq);
  if (teq != NULL) {
    TEQ_Init (teq);
  }
  return teq;
}

/*
 */
C_API void
TEQ_Pop (TimeEventQueue *teq)
{
  assert (teq != NULL);
  if (teq->isUnsorted) {
    qsort (teq->lst, teq->count, sizeof teq->lst[0], &S_Cmp);
    teq->isUnsorted = False;
    fprintf (stderr, "\n%s:%d: TEQ_Pop", __FILE__, __LINE__);
    TEQ_Dump (teq, stdout);
  }
  if (teq->count > 0) {
    --teq->count;
  }
}

/*
 */
C_API void
TEQ_Push (int delay, CLOOP_TimeFnType fn, void *p, TimeEventQueue *teq)
{
  int i;

  assert (teq != NULL);
  assert (teq->count < TEQ_LEN);
  if (fn != NULL) {
    teq->isUnsorted = True;
    i = teq->count++;
    if (delay < 0) delay = 0;
    teq->lst[i].when = time(NULL) + delay;
    teq->lst[i].delay = delay;
    teq->lst[i].n = teq->n++;
    teq->lst[i].fn = fn;
    teq->lst[i].p = p;
  }
}

/*
 * Return a pointer to the first item in the queue, if there is
 * one.  Return NULL if there isn't.
 * Assumes the queue is not empty.
 * Before calling this, you should ensure that the queue is not
 * empty by calling TEQ_IsEmpty or TEQ_Count.
 */
C_API TimeEvent *
TEQ_Top (TimeEventQueue *teq)
{
  assert (teq != NULL);
  if (teq->isUnsorted) {
    qsort (teq->lst, teq->count, sizeof teq->lst[0], &S_Cmp);
    teq->isUnsorted = False;
  }
  return teq->count == 0 ? NULL : &teq->lst[teq->count-1];
}

/*
 */
C_API void
TEQ_Uninit (TimeEventQueue *teq)
{
  assert (teq != NULL);
  teq->tag = NULL;
}

/* --- end of file --- */
