home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file19 / vecptr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  2.1 KB  |  64 lines

  1. /////////////////////////////////////////////////////////////
  2. // vecptr.h: Vector pointer class.
  3. // Copyright(c) 1993 Azarona Software. All rights reserved.
  4. /////////////////////////////////////////////////////////////
  5. #ifndef H_VECPTR
  6. #define H_VECPTR
  7.  
  8. // NOTE: You can define ELEMENT_STRIDE to get the non-optimized
  9. // version of the vector pointers, which don't use byte strides.
  10.  
  11. #ifndef ELEMENT_STRIDE
  12.  
  13. // This is the optimized version
  14.  
  15. template<class TYPE>
  16. class VecPtr { 
  17. private:
  18.   TYPE *p;
  19.   const unsigned stride;
  20. public:
  21.   VecPtr(TYPE *t, unsigned s=1)         : p(t), stride(s*sizeof(TYPE)) { }
  22.   VecPtr(const VecPtr<TYPE> &s)         : p(s.p), stride(s.stride) { }
  23.   void operator=(const VecPtr<TYPE> &s) { p = s.p; }
  24.   void operator=(TYPE *t)               { p = t; }
  25.   void operator++()                     { (char *)p += stride; }
  26.   void operator++(int)                  { (char *)p += stride; }
  27.   void operator+=(unsigned n)           { (char *)p += stride * n; }
  28.   TYPE &operator[](unsigned i) const    { return *(TYPE *)((char *)p + stride*i); }
  29.   operator TYPE *() const               { return p; }
  30.   TYPE &operator *() const              { return *p; }
  31. #ifdef TYPE_IS_STRUCTURE
  32.   TYPE *operator->() const              { return p; }
  33. #endif
  34. };
  35.  
  36. #else
  37.  
  38. // This is the unoptimized version
  39.  
  40. template<class TYPE>
  41. class VecPtr { 
  42. private:
  43.   TYPE *p;
  44.   const unsigned stride;
  45. public:
  46.   VecPtr(TYPE *t, unsigned s=1)         : p(t), stride(s) { }
  47.   VecPtr(const VecPtr<TYPE> &s)         : p(s.p), stride(s.stride) { } 
  48.   void operator=(const VecPtr<TYPE> &s) { p = s.p; }
  49.   void operator=(TYPE *t)               { p = t; }
  50.   void operator++()                     { p += stride; }
  51.   void operator++(int)                  { p += stride; }
  52.   void operator+=(unsigned n)           { p += stride * n; }
  53.   TYPE &operator[](unsigned i) const    { return p[stride*i]; }
  54.   operator TYPE *() const               { return p; }
  55.   TYPE &operator *() const              { return *p; }
  56. #ifdef TYPE_IS_STRUCTURE
  57.   TYPE *operator->() const              { return p; }
  58. #endif
  59. };
  60.  
  61. #endif
  62.  
  63. #endif
  64.