home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / viewkit / xcontact / lib / OkArray.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.3 KB  |  278 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #pragma once
  18. #include "OkAddress.h"
  19.  
  20. //
  21. // There are five flavors of arrays:
  22. //   struct Arrays (in which the contents of the elements are not looked at)
  23. //   pointer Arrays (the elements are pointers to some memory)
  24. //   object Arrays (the elements are objects, which must beructed and
  25. //    destructed)
  26. //
  27. // Macros exist for each of these OkArray flavors:
  28. //   OkDECLARE_Array (same as OkDECLARE_StructArray)
  29. //   OkDECLARE_PtrArray
  30. //      (acts like OkDECLARE_Array, except that pointers are
  31. //       initialized to nil when new elements are added)
  32. //   OkDECLARE_ObjArray
  33.  
  34. class OkArray {
  35. public:
  36.     u_int length();
  37.     u_int elementSize()
  38.     { return elementsize; };
  39.     void resize(u_int length);
  40.     void setMaxLength(u_int maxlength);
  41.     void setIncrementSize(u_int insize);
  42.     void qsort(u_int posn, u_int len);
  43.     void qsort();
  44.  
  45.     void swap(u_int,u_int);
  46.  
  47.     virtual char *className() = 0;
  48.  
  49. protected:
  50.     OkArray(u_short esize, u_int initlength=0);
  51.     OkArray(u_int esize, u_int num, void *data);
  52.     OkArray(OkArray &);
  53.     virtual ~OkArray();
  54.  
  55.     void* operator[](u_int index);
  56.     void operator=(OkArray &);
  57.  
  58.     void append(void *item);
  59.     void append(OkArray &);
  60.     void remove(u_int start, u_int length=1);
  61.     void insert(OkArray &, u_int posn);
  62.     void insert(void *item, u_int posn);
  63.  
  64.     u_int find(void *, u_int start=0);
  65.  
  66.     // The objects in the array are stored sequentially at the
  67.     // location pointed to by data. The length of the known
  68.     // allocated segment is stored in maxi, in bytes. The
  69.     // length of the array is stored in num, in *bytes*. The
  70.     // size of an array element is stored in elementsize, in bytes.
  71.  
  72.     // data is allowed to be nil iff (maxi==0)
  73.     OkAddress data;
  74.  
  75.     // num <= maxi
  76.     u_int maxi,num;
  77.     u_short elementsize;
  78.     u_int   incrementsize;
  79.  
  80.     // These two methods control how the array class goes to
  81.     // fetch more memory.
  82.     virtual void getmem();
  83.     virtual void expand();
  84.  
  85.     // The raw methods are used to
  86.     // implement methods which return an OkArray type.
  87.     void * raw_copy();
  88.     void * raw_extract(u_int start, u_int length);
  89.     void * raw_cut(u_int start, u_int length);
  90.     void * raw_head(u_int);
  91.     void * raw_tail(u_int);
  92.  
  93.     void qsortInternal(u_int, u_int, void *);
  94.     void destroy();
  95.  
  96.     // These three methods can be overridden to properly copy, delete,
  97.     // and create new array elements in the desired manner. By default
  98.     // `create' and `destroy' do nothing, and `copy' is a simple bcopy.
  99.     //
  100.     // The job of create is to take an area of uninitialized memory and
  101.     // create a series of valid objects in it. The job of destroy is to
  102.     // take a series of valid objects and destroy any resources they
  103.     // consume. The status of the memory after the destroy is irrelevant.
  104.     // The job of copy is to take a source array of objects, and copy
  105.     // them to an area of *uninitialized* memory. There will not be any
  106.     // objects stored there previous to the copy.
  107.     virtual void createElements(void *, u_int numbytes);
  108.     virtual void destroyElements(void *, u_int numbytes);
  109.     virtual void copyElements(void *src, void *dst, u_int numbytes);
  110.     virtual int  compareElements(void *, void *) ;
  111. };
  112.  
  113. #define OkArrayHeader(ARRAY,ITEM)                    \
  114.     ARRAY();                            \
  115.     ARRAY(u_int size);                        \
  116.     ARRAY(ARRAY&a);                            \
  117.     ~ARRAY();                            \
  118.     virtual char* className();                    \
  119.     void operator=(ARRAY&a) {                    \
  120.         maxi = a.maxi; num = a.num; if (data) delete (void*)data;    \
  121.         data = a.raw_copy(); }                    \
  122.     ITEM & operator[](u_int index)                    \
  123.     { assert(index>=0 && index*sizeof(ITEM) < num);            \
  124.       return *(ITEM *)((char*)((void*)data) + index * sizeof(ITEM));} \
  125.     void append(ITEM & item) { OkArray::append(&item); }        \
  126.     void append(ARRAY & a) { OkArray::append(a); }            \
  127.     void remove(u_int start, u_int length=1)            \
  128.         { OkArray::remove(start,length); }                \
  129.     void removeAll()                                                \
  130.         { OkArray::remove(0, OkArray::length()); }            \
  131.     ARRAY cut(u_int start, u_int len = 1);                \
  132.     void insert(ARRAY & a, u_int p)                    \
  133.         { OkArray::insert(a,p); }                    \
  134.     void insert(ITEM & item, u_int p)                \
  135.         { OkArray::insert(&item,p);}                \
  136.     ARRAY extract(u_int start, u_int len);                \
  137.     ARRAY head(u_int len = 1);                    \
  138.     ARRAY tail(u_int len = 1);                    \
  139.     int find(ITEM& x, u_int start=0) {                \
  140.         return OkArray::find(&x,start);                \
  141.     }                                \
  142.     protected:                                \
  143.     ARRAY(u_int esize, u_int num, void *data);            \
  144.     public:                                \
  145. __enddef__
  146.  
  147. #define OkArrayVirtuals                            \
  148.     protected:                                \
  149.     virtual void createElements(void *,u_int);            \
  150.     virtual void destroyElements(void *,u_int);            \
  151.     virtual void copyElements(void *,void*,u_int) ;            \
  152.     virtual int compareElements(void *, void *) ;              \
  153. __enddef__
  154.  
  155.  
  156. //----------------------------------------------------------------------
  157. // Declare an array containing items of type ITEM.
  158.  
  159. #define OkDECLARE_Array(ARRAY,ITEM)                    \
  160. class ARRAY : public OkArray {                        \
  161. public:                                    \
  162.     OkArrayHeader(ARRAY,ITEM)                        \
  163. };                                    \
  164. __enddef__
  165.  
  166. #define OkDECLARE_StructArray(ARRAY,ITEM) OkDECLARE_Array(ARRAY,ITEM)
  167.  
  168. #define OkDECLARE_ObjArray(ARRAY,ITEM)                    \
  169. class ARRAY : public OkArray {                        \
  170. public:                                    \
  171.     OkArrayHeader(ARRAY,ITEM)                        \
  172.     OkArrayVirtuals                            \
  173. };                                    \
  174. __enddef__
  175.  
  176. #define OkDECLARE_PtrArray(ARRAY, POINTER)                \
  177. class ARRAY : public OkArray {                        \
  178. public:                                    \
  179.     OkArrayHeader(ARRAY,POINTER)                    \
  180. protected:                                \
  181.     virtual void createElements(void *, u_int);                \
  182. };                                    \
  183. __enddef__
  184.  
  185. //----------------------------------------------------------------------
  186. // Various method implementations
  187.  
  188. #define OkIMPLEMENT_ArrayMethods(ARRAY,ITEM)                \
  189.     ARRAY::ARRAY() : OkArray(sizeof(ITEM))                \
  190.     { if (data) createElements(data,num); }                \
  191.     ARRAY::ARRAY(ARRAY& a) : OkArray(a.elementsize)             \
  192.     { maxi = a.maxi; num = a.num; data = a.raw_copy(); }        \
  193.     ARRAY::ARRAY(u_int size) : OkArray(sizeof(ITEM),size)        \
  194.     { createElements(data,num); }                    \
  195.     ARRAY::~ARRAY() { destroy(); }                    \
  196.     char* ARRAY::className() { return "ARRAY"; }            \
  197.     ARRAY ARRAY::cut(u_int start, u_int len)                \
  198.     {return ARRAY(sizeof(ITEM), len*sizeof(ITEM),raw_cut(start,len));}\
  199.     ARRAY ARRAY::extract(u_int start, u_int len)            \
  200.     {return ARRAY(sizeof(ITEM), len*sizeof(ITEM),raw_extract(start,len));}\
  201.     ARRAY ARRAY::head(u_int len)                    \
  202.     {return ARRAY(sizeof(ITEM), len*sizeof(ITEM),raw_head(len));}   \
  203.     ARRAY ARRAY::tail(u_int len)                    \
  204.         {return ARRAY(sizeof(ITEM),len*sizeof(ITEM),raw_tail(len));}    \
  205.     ARRAY::ARRAY(u_int esize, u_int num, void * data)                \
  206.     : OkArray(esize,num,data) {}                    \
  207. __enddef__
  208.  
  209. #define OkIMPLEMENT_ObjArrayMethods(ARRAY,ITEM)                \
  210.     void ARRAY::createElements(void * start, u_int numbytes) {        \
  211.     ITEM * ptr = (ITEM *)start;                    \
  212.     for (;;) {                            \
  213.         if (numbytes == 0) break;                    \
  214.         numbytes -= elementsize;                    \
  215.         ITEM * obj = new(ptr) ITEM;                    \
  216.         ptr++;                             \
  217.     }                                \
  218.     }                                    \
  219.     void ARRAY::destroyElements(void * start, u_int numbytes) {        \
  220.     ITEM * ptr = (ITEM *)start;                    \
  221.     while (numbytes) {                        \
  222.         numbytes -= elementsize;                    \
  223.         ptr->ITEM::~ITEM();                        \
  224.         ptr++;                            \
  225.     }                                \
  226.     }                                    \
  227.     void ARRAY::copyElements(void * src, void * dst,            \
  228.         u_int numbytes) {                        \
  229.     if (src<dst) {                            \
  230.         src = (char*)src + numbytes;                \
  231.         dst = (char*)dst + numbytes;                \
  232.         ITEM * p = (ITEM *)src - 1;                    \
  233.         ITEM * q = (ITEM *)dst - 1;                    \
  234.         while (numbytes > 0) {                    \
  235.         ITEM * obj = new(q--) ITEM(*p--);            \
  236.         numbytes -= elementsize;                \
  237.         }                                \
  238.     } else {                            \
  239.         ITEM * p = (ITEM *)src;                    \
  240.         ITEM * q = (ITEM *)dst;                    \
  241.         while (numbytes > 0) {                    \
  242.         ITEM * obj = new(q++) ITEM(*p++);            \
  243.         numbytes -= elementsize;                \
  244.         }                                \
  245.     }                                \
  246.     }                                    \
  247.     int ARRAY::compareElements(void *o1, void *o2) {            \
  248.     return ((ITEM *)o1)->compare((ITEM *)o2);            \
  249.     }                                    \
  250. __enddef__
  251.  
  252. #define OkIMPLEMENT_PtrArrayMethods(ARRAY,POINTER)            \
  253.     void ARRAY::createElements(void * start, u_int numbytes) {        \
  254.     bzero(start,numbytes);                        \
  255.     }                                    \
  256. __enddef__
  257.  
  258. //----------------------------------------------------------------------
  259. // Implement various types of arrays
  260.  
  261. #define OkIMPLEMENT_Array(ARRAY,ITEM)                    \
  262.     OkIMPLEMENT_ArrayMethods(ARRAY,ITEM)                \
  263. __enddef__
  264.  
  265. #define OkIMPLEMENT_StructArray(ARRAY,ITEM)                 \
  266.     OkIMPLEMENT_ArrayMethods(ARRAY,ITEM)                \
  267. __enddef__
  268.  
  269. #define OkIMPLEMENT_ObjArray(ARRAY,ITEM)                \
  270.     OkIMPLEMENT_ArrayMethods(ARRAY,ITEM)                \
  271.     OkIMPLEMENT_ObjArrayMethods(ARRAY,ITEM)                \
  272. __enddef__
  273.  
  274. #define OkIMPLEMENT_PtrArray(ARRAY,POINTER)                \
  275.     OkIMPLEMENT_Array(ARRAY,POINTER)                    \
  276.     OkIMPLEMENT_PtrArrayMethods(ARRAY,POINTER)                \
  277. __enddef__
  278.