home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / oogl / util / vvec.c < prev   
Encoding:
C/C++ Source or Header  |  1993-09-23  |  2.4 KB  |  130 lines

  1. #include "ooglutil.h"
  2.  
  3. /*
  4.  * Variable-sized arrays ("vectors").
  5.  */
  6.  
  7. void
  8. vvinit(vvec *v, int elsize, int minelems)
  9. {
  10.     v->elsize = elsize;
  11.     v->count = 0;
  12.     v->malloced = 0;
  13.     v->dozero = 0;
  14.     v->allocated = -minelems;
  15.     v->base = NULL;
  16. }
  17.  
  18. void
  19. vvuse(vvec *v, void *buf, int allocated)
  20. {
  21.     vvfree(v);
  22.     v->base = buf;
  23.     v->allocated = allocated;
  24. }
  25.  
  26. void
  27. vvzero(vvec *v)
  28. {
  29.     v->dozero = 1;
  30.     if(v->allocated > v->count)
  31.     memset(v->base + v->elsize*v->count, 0,
  32.         v->elsize * (v->allocated - v->count));
  33. }
  34.  
  35. /*
  36.  * Trim vvec to minimum size; ensure its buffer is malloced if it wasn't.
  37.  */
  38. void
  39. vvtrim(vvec *v)
  40. {
  41.     int newalloc = (v->count > 0 ? v->count : 1);
  42.     int want = newalloc * v->elsize;
  43.     static char why[] = "trimming vvec";
  44.  
  45.     if(!v->malloced) {
  46.     void *base = OOGLNewNE(char, want, why);
  47.     memcpy(base, v->base, want);
  48.     v->base = base;
  49.     v->malloced = 1;
  50.     } else if(v->allocated > v->count) {
  51.     v->base = (void *)OOGLRenewNE(char, v->base, want, why);
  52.     } else
  53.     return;
  54.     v->allocated = newalloc;
  55. }
  56.  
  57. void
  58. vvfree(vvec *v)
  59. {
  60.     if(v->malloced) {
  61.     OOGLFree(v->base);
  62.     v->base = NULL;
  63.     v->malloced = 0;
  64.     }
  65. }
  66.  
  67. void
  68. vvneeds(register vvec *v, int needed)
  69. {
  70.     if(needed > v->allocated) {
  71.     int had = v->allocated;
  72.     int want = needed + (needed>>2) + 1;
  73.  
  74.     if(had < 0) {
  75.         if(want < -had)
  76.         want = -had;
  77.         had = 0;
  78.     } else {
  79.         int next = had + (had>>1) + 2;
  80.         if(next > needed)
  81.         want = next;
  82.     }
  83.  
  84.     if(v->malloced) {
  85.         v->base = OOGLRenewNE(char, v->base,
  86.             want * v->elsize, "extending vvec");
  87.         if(had > v->count) had = v->count;
  88.     } else {
  89.         void *was = v->base;
  90.         v->base = OOGLNewNE(char, want * v->elsize, "allocating vvec");
  91.         if(v->count > 0 && had > 0)
  92.         memcpy(v->base, was, (v->count<had ? v->count:had) * v->elsize);
  93.     }
  94.     v->allocated = want;
  95.     v->malloced = 1;
  96.     if(v->dozero)
  97.         memset(v->base + v->elsize * had, 0,
  98.         v->elsize * (want-had));
  99.     }
  100. }
  101.  
  102. void *
  103. vvindex(vvec *v, int index)
  104. {
  105.     if(index < 0) {
  106.     OOGLError(1, "negative array index: %d", index);
  107.     return v->base;
  108.     }
  109.     if(index >= v->allocated)
  110.     vvneeds(v, index+1);
  111.     if(index >= v->count)
  112.     v->count = index+1;
  113.     return v->base + index*v->elsize;
  114. }
  115.  
  116. void
  117. vvcopy(vvec *src, vvec *dest) 
  118. {
  119.   char *newbase;
  120.   if(src->base == NULL) {
  121.     *dest = *src;
  122.   } else {
  123.     vvneeds(dest, src->allocated);
  124.     newbase = dest->base;
  125.     *dest = *src;
  126.     dest->base = newbase;
  127.     bcopy(src->base, dest->base, dest->allocated * dest->elsize);
  128.   }
  129. }
  130.