home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dokpr1.zip / address.h < prev    next >
C/C++ Source or Header  |  1995-09-12  |  6KB  |  185 lines

  1. /**************************************************************************
  2.  *                                                                        *
  3.  *                                                                        *
  4.  *          This code is copyright (c)        1994                        *
  5.  *                     Athena Design, Inc.                                *
  6.  *                                                                        *
  7.  *                                                                        *
  8.  *                ALL RIGHTS RESERVED                                     *
  9.  *                                                                        *
  10.  *                                                                        *
  11.  *                                                                        *
  12.  *                                                                        *
  13.  *                                                                        *
  14.  **************************************************************************/
  15.  
  16. #ifndef _mh_TAG_ADDRESS__
  17.  
  18. #define _mh_TAG_ADDRESS__
  19.  
  20. /*
  21.     the address is a three dimensional coordinate
  22.     the theCol and theRow fields are the number of the row or column
  23.     the first 12 bits of theLayer contain the layer number
  24.     Bits in layer:
  25.     12 - relative row
  26.     13 - relative col
  27.     14 - relative layer
  28.     15 - null if set
  29. */
  30.  
  31. #define NULLADDRESS (1 << 15)
  32. #define ABSROW (1 << 12)
  33. #define ABSCOL (1 << 13)
  34. #define ABSLAYER (1 << 14)
  35. #define LAYERMASK (4095)
  36. #define LAYERBITMASK (0x7000)
  37. //#define MAXCOL (18277)
  38. //#define MAXROW (999999)
  39. //#define MAXROW (32766)
  40. //#define MAXLAYER (701)
  41. #define ROWMASK (0xffffff)
  42. #define COLMASK (0xffff)
  43.  
  44. extern int theMAXCOL,theMAXROW,theMAXLAYER;
  45.  
  46. #define MAXCOL (theMAXCOL)
  47. #define MAXROW (theMAXROW)
  48. #define MAXLAYER (theMAXLAYER)
  49.  
  50. class MStream;
  51.  
  52. class MAddress {
  53.     public:
  54.     // create a null value, initialize
  55.     void init() {theRow = 0; theCol = 0; theLayer = 0 | NULLADDRESS;};
  56.     void init(int r,int c,int l) {theRow = r & ROWMASK; theCol = c & COLMASK;
  57.                   theLayer = (l & LAYERMASK);};
  58.     void init(const MAddress *a) {*this = *a;};
  59.     void init(MStream *sp) {read(sp);};
  60.  
  61.     // set to null, cleanup
  62.     void free() {init();};
  63.     void zap() {theRow = 0; theCol = 0; theLayer = NULLADDRESS;};
  64.     void makeNull() {zap();};
  65.  
  66.     // get the column, row or layer
  67.     int getCol() const {return theCol;};
  68.     int getRow() const {return theRow;};
  69.     int getLayer() const {return theLayer & LAYERMASK;};
  70.  
  71.     // set address
  72.     void set(const MAddress *a1) {*this = *a1;};
  73.     void set(int r,int c,int l) {theRow = r & ROWMASK; theCol = c & COLMASK;
  74.                  theLayer = (l & LAYERMASK);};
  75.  
  76.     // set the row, col, or layer and make them relative
  77.     void setRow(int r) {theLayer &= ~ABSROW; theRow = r & ROWMASK;};
  78.     void setCol(int c) {theLayer &= ~ABSCOL; theCol = c & COLMASK;};
  79.     void setLayer(int l) {theLayer = (l & LAYERMASK) |
  80.               (theLayer & ~LAYERMASK & ~ABSLAYER);};
  81.  
  82.     // set the attributes without affecting the ABS values
  83.     void setRawRow(int r) {theRow = r & ROWMASK;};
  84.     void setRawCol(int c) {theCol = c & COLMASK;};
  85.     void setRawLayer(int l) {theLayer = (theLayer & ~LAYERMASK) | (l & LAYERMASK);};
  86.  
  87.     // get and set absolute values
  88.     void setAbsRow(int i) {if (i) theLayer |= ABSROW; else theLayer &= ~ABSROW;};
  89.     void setAbsCol(int i) {if (i) theLayer |= ABSCOL; else theLayer &= ~ABSCOL;};
  90.     void setAbsLayer(int i) {if (i) theLayer |= ABSLAYER; else theLayer &= ~ABSLAYER;};
  91.     void makeAbs() {theLayer |= ABSLAYER | ABSROW | ABSCOL;};
  92.  
  93.     int getAbsRow() const {return theLayer & ABSROW;};
  94.     int getAbsCol() const {return theLayer & ABSCOL;};
  95.     int getAbsLayer() const {return theLayer & ABSLAYER;};
  96.  
  97.     // swap values
  98.     void swapRowInfo(MAddress &);
  99.     void swapColInfo(MAddress &);
  100.     void swapLayerInfo(MAddress &);
  101.  
  102.     // offset values
  103.     void offset(int,int,int);
  104.     void offsetRel(int,int,int);
  105.     void offsetAbs(int,int,int);
  106.  
  107.     // operator overloading
  108.     int operator== (const MAddress &a1) const {
  109.         if (isNull() || a1.isNull()) return 0;
  110.         return (getRow() == a1.getRow() && getCol() == a1.getCol()
  111.         && getLayer() == a1.getLayer());
  112.         };
  113.             
  114.     int operator<= (const MAddress &a1) const;
  115.     
  116.  
  117.     /*
  118.         to preserve the correct ordering in the array, layer is the most
  119.         significant item, then row, and column is the least significant item.
  120.  
  121.         This allows keeping a pointer to a cell and incrementing the pointer
  122.         through the array and catching each cell in a given row.
  123.  
  124.         1/20/94 dpp
  125.     */
  126.  
  127.     int operator> (const MAddress &a1) const {
  128.         if (getLayer() < a1.getLayer()) return 0;
  129.         if (getLayer() > a1.getLayer()) return 1;
  130.         if (getRow() < a1.getRow()) return 0;
  131.         if (getRow() > a1.getRow()) return 1;
  132.         return getCol() > a1.getCol();
  133.         };
  134.  
  135.     int operator>=(const MAddress &a1) const {
  136.         if (a1 == *this) return 1;
  137.         if (getLayer() < a1.getLayer()) return 0;
  138.         if (getLayer() > a1.getLayer()) return 1;
  139.         if (getRow() < a1.getRow()) return 0;
  140.         if (getRow() > a1.getRow()) return 1;
  141.         return getCol() > a1.getCol();
  142.         };
  143.  
  144.     int operator< (const MAddress &a1) const {
  145.         if (getLayer() < a1.getLayer()) return 1;
  146.         if (getLayer() > a1.getLayer()) return 0;
  147.         if (getRow() < a1.getRow()) return 1;
  148.         if (getRow() > a1.getRow()) return 0;
  149.         return getCol() < a1.getCol();
  150.         };
  151.  
  152.     int operator!= (const MAddress &a1) const {
  153.         return (getRow() != a1.getRow() || getCol() != a1.getCol() || 
  154.         getLayer() != a1.getLayer());
  155.         };
  156.  
  157.     // utility and misc methods
  158.     int isNull() const {return theLayer & NULLADDRESS;};
  159.     int inRange(const MAddress *,const MAddress *) const;
  160.     void toStr(char *,int = -1) const;
  161.     void read(MStream *);
  162.     void write(MStream *) const;
  163.     void doMerge(const MAddress *);
  164.  
  165.     private:
  166.     unsigned short theLayer,theCol;
  167.     int theRow;
  168.     };
  169.  
  170. extern void sortAddresses(MAddress &,MAddress &);
  171.  
  172. #endif
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.