home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_07 / 8n07118a < prev    next >
Text File  |  1990-06-19  |  2KB  |  104 lines

  1. /* Listing 4 */
  2. /*----------------- ARRYLIST.H --------------------------*/
  3.  
  4. #include "list.h"
  5. #include <alloc.h>
  6. #define ARRAY_LIST_CLASS      LIST_CLASS \
  7. /* Index of current member */ int curr; \
  8. /* Total members in array */  int tot_members;
  9.  
  10. typedef struct array_list {
  11.   ARRAY_LIST_CLASS
  12. } ARRAY_LIST;
  13.  
  14. ARRAY_LIST *new_array_list();
  15. destroy_array_list(ARRAY_LIST *);
  16.  
  17. /*------------------ ARRYLIST.C -------------------------*/
  18.  
  19. #include "arrylist.h"
  20.  
  21. static long total_members(ARRAY_LIST *this) {
  22.     return((long) this->tot_members);
  23. }
  24.  
  25. static unsigned at_top(ARRAY_LIST *this) {
  26.     return(this->curr == 0);
  27. }
  28.  
  29. static unsigned at_end(ARRAY_LIST *this) {
  30.     return(this->curr == this->tot_members);
  31. }
  32.  
  33. static void prev(ARRAY_LIST *this) {
  34.   if (this->curr > 0)
  35.     --(this->curr);
  36. }
  37. static void next(ARRAY_LIST *this) {
  38.     if (this->curr < (this->tot_members))
  39.        ++(this->curr);
  40. }
  41.  
  42. static void seek(ARRAY_LIST *this, long where, int from) {
  43. switch(from) {
  44.     case SEEK_SET:
  45.         if (where < this->tot_members)
  46.             this->curr = (int) where;
  47.     break;
  48.     case SEEK_CUR:
  49.         if (where > 0) {
  50.             if ( (this->curr + (int) where) <
  51.                   this->tot_members ) {
  52.                 this->curr += (int) where;
  53.             }
  54.         }
  55.         else {
  56.             if ((this->curr - (int) where) > 0) {
  57.                 this->curr -= (int) where;
  58.             }
  59.         }
  60.     break;
  61.     case SEEK_END:
  62.        if (where <= this->tot_members) {
  63.             this->curr = this->tot_members - (int) where;
  64.        }
  65.     break;
  66.   }
  67. }
  68. static void top(ARRAY_LIST *this) {
  69.     this->curr = 0;
  70. }
  71. static void end(ARRAY_LIST *this) {
  72.     this->curr = this->tot_members - 1;
  73. }
  74. static long tell(ARRAY_LIST *this) {
  75.     return(this->curr);
  76. }
  77. ARRAY_LIST *new_array_list(void) {
  78.     ARRAY_LIST *this;
  79.     LIST *l;
  80.  
  81.     l = new_list();
  82.     if (l == NULL)
  83.        return(NULL);
  84.  
  85.     this = calloc(1,sizeof(ARRAY_LIST));
  86.     if (this == NULL) {
  87.         destroy_list(l);
  88.         return(NULL);
  89.     }
  90.     memmove(this,l,sizeof(LIST));
  91.     free(l);
  92.  
  93.     this->total_members = total_members;
  94.     this->at_top = at_top;  this->at_end = at_end;
  95.     this->prev = prev;      this->next = next;
  96.     this->seek = seek;      this->top = top;
  97.     this->end = end;        this->tell = tell;
  98.     return(this);
  99. }
  100.  
  101. destroy_array_list(ARRAY_LIST *this) {
  102.     destroy_list(this);
  103. }
  104.