home *** CD-ROM | disk | FTP | other *** search
- #include <alloc.h>
-
-
- void OutOfMemory();
-
- template <class Elem>
- class ARRAY {
- int UseDefault;
- Elem **A,def;
- unsigned long int size;
-
- public:
- Elem& operator [](unsigned long int i)
- {
- if (i<size) {
- if (size < 65535/sizeof(Elem)) {
- return A[0][i];
- }
- else {
- WORD pos= i % (65535/sizeof(Elem));
- WORD no = i / (65535/sizeof(Elem));
- return A[no][pos];
- }
- }
- else {
- MakeLonger(i);
- return operator[](i);
- }
- };
-
- void MakeLonger(unsigned long int i)
- {
- if (size < 65535 / sizeof(Elem)) {
- size = (i < 2*size) ? size*2 : (i+1);
- if (size < 65535 / sizeof(Elem)) {
- realloc(A[0],size*sizeof(Elem));
- }
- else {
- realloc(A[0],65535);
- for(long j=1;j <= size / (65535/sizeof(Elem));j++) {
- A[j]=(Elem*) calloc(65535,1);
- if (A[j] == NULL) OutOfMemory();
- }
- }
- if (A[0]==NULL) OutOfMemory();
-
- }
- else {
- i = (65535/sizeof(Elem))* (1+ i / (65535/sizeof(Elem)));
- cout << " Realloc: " << size << "\n";
- for(long pos=size / (65535 / sizeof(Elem))+1; pos<=i/(65535 / sizeof(Elem));pos++) {
- A[pos] = (Elem *) calloc(65535,1);
- if (A[pos] == NULL) OutOfMemory();
- }
- size = i;
- }
- }
-
- long int length() { return size; };
-
- ARRAY()
- {
- if (10*sizeof(Elem) > 65535) {
- }
- else {
- size = 10;
- UseDefault = 0;
- A = (Elem **) calloc(10,sizeof(void *));
- if (A == NULL) OutOfMemory();
- A[0] = (Elem *) calloc(10,sizeof(Elem));
- if (A[0] == NULL) OutOfMemory();
- }
- }
-
- ARRAY(unsigned long int i, Elem& E)
- {
- size = i;
- UseDefault = 1;
- def = E;
- A = (Elem **) calloc(10,sizeof(void *));
- if (A==NULL) OutOfMemory();
-
- if (i < 65535 / sizeof(Elem)) {
- A[0] = (Elem *) calloc(i, sizeof(Elem));
- if (A[0] == 0) OutOfMemory();
- for(long int j=0;j<size;j++) {
- A[0][j] = E;
- }
- }
- else {
- long j;
- for(j=0;j<= i/(65535/sizeof(Elem));i++) {
- A[j] = (Elem *) calloc(65535,1);
- if (A[j] == NULL) OutOfMemory();
- }
- for(j=0;j<size;j++) {
- A[j/(65535/sizeof(Elem))][j % (65535/sizeof(Elem))] = E;
- }
- }
- }
- };