home *** CD-ROM | disk | FTP | other *** search
/ Quake++ for Quake / Quake++.iso / quake / qkview / array.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-10  |  2.1 KB  |  101 lines

  1. #include <alloc.h>
  2.  
  3.  
  4. void OutOfMemory();
  5.  
  6. template <class Elem>
  7. class ARRAY {
  8.     int UseDefault;
  9.     Elem **A,def;
  10.     unsigned long int size;
  11.  
  12. public:
  13.     Elem& operator [](unsigned long int i)
  14.     {
  15.         if (i<size) {
  16.             if (size < 65535/sizeof(Elem)) {
  17.                 return A[0][i];
  18.             }
  19.             else {
  20.                 WORD pos= i % (65535/sizeof(Elem));
  21.                 WORD no = i / (65535/sizeof(Elem));
  22.                 return A[no][pos];
  23.             }
  24.         }
  25.         else {
  26.             MakeLonger(i);
  27.             return operator[](i);
  28.         }
  29.     };
  30.  
  31.     void MakeLonger(unsigned long int i)
  32.     {
  33.         if (size < 65535 / sizeof(Elem)) {
  34.             size = (i < 2*size) ? size*2 : (i+1);
  35.             if (size < 65535 / sizeof(Elem)) {
  36.                 realloc(A[0],size*sizeof(Elem));
  37.             }
  38.             else {
  39.                 realloc(A[0],65535);
  40.                 for(long j=1;j <= size / (65535/sizeof(Elem));j++) {
  41.                     A[j]=(Elem*) calloc(65535,1);
  42.                     if (A[j] == NULL) OutOfMemory();
  43.                 }
  44.             }
  45.             if (A[0]==NULL) OutOfMemory();
  46.  
  47.         }
  48.         else {
  49.             i = (65535/sizeof(Elem))* (1+ i / (65535/sizeof(Elem)));
  50.             cout << "   Realloc: " << size << "\n";
  51.             for(long pos=size / (65535 / sizeof(Elem))+1; pos<=i/(65535 / sizeof(Elem));pos++) {
  52.                 A[pos] = (Elem *) calloc(65535,1);
  53.                 if (A[pos] == NULL) OutOfMemory();
  54.             }
  55.             size = i;
  56.         }
  57.     }
  58.  
  59.     long int length() { return size; };
  60.  
  61.     ARRAY()
  62.     {
  63.         if (10*sizeof(Elem) > 65535) {
  64.         }
  65.         else {
  66.             size = 10;
  67.             UseDefault = 0;
  68.             A = (Elem **) calloc(10,sizeof(void *));
  69.             if (A == NULL) OutOfMemory();
  70.             A[0] = (Elem *) calloc(10,sizeof(Elem));
  71.             if (A[0] == NULL) OutOfMemory();
  72.         }
  73.     }
  74.  
  75.     ARRAY(unsigned long int i, Elem& E)
  76.     {
  77.         size = i;
  78.         UseDefault = 1;
  79.         def = E;
  80.         A = (Elem **) calloc(10,sizeof(void *));
  81.         if (A==NULL) OutOfMemory();
  82.  
  83.         if (i < 65535 / sizeof(Elem)) {
  84.             A[0] = (Elem *) calloc(i, sizeof(Elem));
  85.             if (A[0] == 0) OutOfMemory();
  86.             for(long int j=0;j<size;j++) {
  87.                 A[0][j] = E;
  88.             }
  89.         }
  90.         else {
  91.             long j;
  92.             for(j=0;j<= i/(65535/sizeof(Elem));i++) {
  93.                 A[j] = (Elem *) calloc(65535,1);
  94.                 if (A[j] == NULL) OutOfMemory();
  95.             }
  96.             for(j=0;j<size;j++) {
  97.                 A[j/(65535/sizeof(Elem))][j % (65535/sizeof(Elem))] = E;
  98.             }
  99.         }
  100.     }
  101. };