FluVectorClass.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // $Id: FluVectorClass.h,v 1.2 2004/10/13 20:05:52 jbryan Exp $
  2. /***************************************************************
  3. * FLU - FLTK Utility Widgets
  4. * Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
  5. *
  6. * This file and its content is protected by a software license.
  7. * You should have received a copy of this license with this file.
  8. * If not, please contact the Ohio Supercomputer Center immediately:
  9. * Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
  10. *
  11. ***************************************************************/
  12. #ifndef _FLU_VECTOR_CLASS_H
  13. #define _FLU_VECTOR_CLASS_H
  14. // hackish macro way of making a template class for compilers that don't support templates well
  15. #define FluMakeVectorClass( T, C ) \
  16. class C \
  17. { \
  18. public: \
  19. \
  20. C() { _array = NULL; _size = 0; } \
  21. \
  22. ~C() { clear(); } \
  23. \
  24. inline void add( const T& item ) { insert( size(), item ); } \
  25. \
  26. inline T& operator [](int i) { return _array[i]; } \
  27. \
  28. inline T operator [](int i) const { return _array[i]; } \
  29. \
  30. inline unsigned int size() const { return _size; } \
  31. \
  32. C& operator =( const C &v ) \
  33. { \
  34. clear(); \
  35. if( v.size() ) \
  36. { \
  37. _array = new T[v.size()]; \
  38. for( unsigned int i = 0; i < v.size(); i++ ) \
  39. _array[i] = v._array[i]; \
  40. } \
  41. return *this; \
  42. } \
  43. \
  44. void insert( unsigned int pos, const T &item ) \
  45. { \
  46. if( pos > _size ) \
  47. pos = _size; \
  48. if( _size == 0 ) \
  49. { \
  50. _array = new T[1]; \
  51. } \
  52. else \
  53. { \
  54. if( !( _size & (_size-1) ) ) \
  55. { \
  56. T* temp = new T[_size*2]; \
  57. for( unsigned int i = 0; i < _size; i++ ) \
  58. temp[i] = _array[i]; \
  59. delete[] _array; \
  60. _array = temp; \
  61. } \
  62. for( unsigned int s = _size; s > pos; s-- ) \
  63. _array[s] = _array[s-1]; \
  64. } \
  65. _size++; \
  66. _array[pos] = item; \
  67. } \
  68. \
  69. void erase( unsigned int pos ) \
  70. { \
  71. if( pos >= _size ) \
  72. return; \
  73. _size--; \
  74. if( _size == 0 ) \
  75. { \
  76. delete[] _array; \
  77. _array = NULL; \
  78. } \
  79. else \
  80. { \
  81. for( ; pos < _size; pos++ ) \
  82. _array[pos] = _array[pos+1]; \
  83. } \
  84. } \
  85. \
  86. void clear() \
  87. { \
  88. if( _array ) \
  89. delete[] _array; \
  90. _array = NULL; \
  91. _size = 0; \
  92. } \
  93. \
  94. protected: \
  95. T *_array; \
  96. unsigned int _size; \
  97. }
  98. #endif