/************************************************************************
*                                                                       *
*   Name:       D2ARRAY.T (Version 1.0)                                 *
*                                                                       *
*   Function:   Simulates a two dimensional array using a list.         *
*                                                                       *
*   Author:     John Baker ([email protected])                      *
*               (if address becomes invalid in the future,              *
*                post to rec.arts.int-fiction)                          *
*                                                                       *
*   Notes:      This code is public domain.  Do with it as you see      *
*               fit.  No warranties whatsoever apply.                  *
*                                                                       *
*               This wasn't especially hard to write, nor did it take   *
*               very long, but hey if it's already done you may as well *
*               save yourself some time and use it. :)                  *
*                                                                       *
************************************************************************/

d2Array: object
   myContents = []
   maxX = 0
   maxY = 0

   dimension (x, y) =
       {
       local scratch, i, j;

       /* Array must have positive size */
       if ((x <= 0) or (y <= 0))
           return (nil);

       /* If this is an existing array, all previous information is lost */
       while (car (self.myContents))
           self.myContents := cdr (self.myContents);

       /* Set dimensions */
       self.maxX := x;
       self.maxY := y;

       /* Initialize all array postions to nil */
       for (i := 1; i <= x; i++)
           for (j := 1; j <= y; j++)
               self.myContents := self.myContents + nil;

       /* Let calling function know we completed happily */
       return (true);
       }

   isDimensioned () =
       {
       if (self.maxX = 0)
           return (nil);
       return (true);
       }

   /* If we were being strict about OOP, we'd include GetMaxX and      */
   /* GetMaxY functions, but we'll let other routines just access the  */
   /* properties to get these values.                                  */

   get (x, y) =
       {
       /* Make sure we have happy params */
       if ((x <= 0) or (x > self.maxX) or (y <= 0) or (y > self.maxY))
           return (nil);

       return (self.myContents [((x - 1) * self.maxY) + y]);
       }

   put (x, y, source) =
       {
       /* Make sure we have happy params */
       if ((x <= 0) or (x > self.maxX) or (y <= 0) or (y > self.maxY))
           return (nil);

       self.myContents [((x - 1) * self.maxY) + y] := source;
       return (true);
       }
;