home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / COWS / Code / COWSArrayNode.m < prev    next >
Encoding:
Text File  |  1994-03-19  |  1.4 KB  |  96 lines

  1. /*
  2.     Copyright (C) 1994 Sean Luke
  3.  
  4.     COWSArrayNode.m
  5.     Version 1.0
  6.     Sean Luke
  7.     
  8. */
  9.  
  10.  
  11.  
  12.  
  13. #import "COWSArrayNode.h"
  14.  
  15. @implementation COWSArrayNode
  16.  
  17. - initTable:(const int*) these_limits:(int) this_num_limits;
  18.     {
  19.     int x;
  20.     
  21.     // determine if limit number is valid
  22.     
  23.     size=0;
  24.     num_limits=this_num_limits;
  25.     if (num_limits<=0||num_limits>256) 
  26.         {
  27.         [self free];
  28.         return NULL;        // bad limits!
  29.         }
  30.     
  31.     // load limits and compute total size
  32.     
  33.     size=1;
  34.     for (x=0;x<num_limits;x++)
  35.         {
  36.         limits[x]=these_limits[x];
  37.         if (limits[x]<=0)                // any limit is 0 or lower
  38.             {
  39.             size=0;
  40.             [self free];
  41.             return NULL;
  42.             }
  43.         size*=limits[x];
  44.         }
  45.         
  46.     // allocate table
  47.     
  48.     table=calloc(size,sizeof(COWSStringNode*));
  49.     for (x=0;x<size;x++)
  50.         {
  51.         table[x]=[[COWSStringNode alloc] init];
  52.         }
  53.         
  54.     return self;
  55.     }
  56.     
  57. - setValue:(const int*) coords:(const char*) value
  58.     {
  59.     int offset=1;
  60.     int val=0;
  61.     int x;
  62.     
  63.     for (x=0;x<num_limits;x++)
  64.         {
  65.         if (coords[x]<=0||coords[x]>limits[x]) return NULL;
  66.         val+=((coords[x]-1)*offset);
  67.         offset*=limits[x];
  68.         }
  69.     [table[val] setString:value];
  70.     return self;
  71.     }
  72.     
  73. - (const char*) value:(const int*) coords
  74.     {
  75.     int offset=1;
  76.     int val=0;
  77.     int x;
  78.     
  79.     for (x=0;x<num_limits;x++)
  80.         {
  81.         if (coords[x]<=0||coords[x]>limits[x]) return NULL;
  82.         val+=((coords[x]-1)*offset);
  83.         offset*=limits[x];
  84.         }
  85.     return [table[val] string];
  86.     }
  87.     
  88. - free
  89.     {
  90.     int x;
  91.     for (x=0;x<size;x++) [table[x] free];
  92.     return [super free];
  93.     }
  94.  
  95.     
  96. @end