Array.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2001 Jani Kajala
  3. *
  4. * Permission to use, copy, modify, distribute and sell this
  5. * software and its documentation for any purpose is hereby
  6. * granted without fee, provided that the above copyright notice
  7. * appear in all copies and that both that copyright notice and
  8. * this permission notice appear in supporting documentation.
  9. * Jani Kajala makes no representations about the suitability
  10. * of this software for any purpose. It is provided "as is"
  11. * without express or implied warranty.
  12. */
  13. #ifndef _DEV_ARRAY_H
  14. #define _DEV_ARRAY_H
  15. namespace dev
  16. {
  17. /** Very simple dynamic array. */
  18. template <class T> class Array
  19. {
  20. public:
  21. /** Creates an empty array. */
  22. Array() :
  23. m_data(0), m_len(0), m_cap(0)
  24. {
  25. }
  26. /** Creates an array of specified size. */
  27. explicit Array( int size ) :
  28. m_data(0), m_len(0), m_cap(0)
  29. {
  30. setSize( size );
  31. }
  32. /***/
  33. ~Array()
  34. {
  35. delete[] m_data;
  36. }
  37. /** Appends an item at the end of the array. */
  38. void add( const T& item )
  39. {
  40. if ( m_len+1 > m_cap )
  41. setCapacity( m_len + 1 );
  42. m_data[m_len++] = item;
  43. }
  44. /** Resizes the array. */
  45. void setSize( int size )
  46. {
  47. if ( size > m_cap )
  48. setCapacity( size );
  49. m_len = size;
  50. }
  51. /** Returns ith item. */
  52. T& operator[]( int i )
  53. {
  54. return m_data[i];
  55. }
  56. /** Returns pointer to the first element in the vector. */
  57. T* begin()
  58. {
  59. return m_data;
  60. }
  61. /** Returns pointer to one beyond the last element in the vector. */
  62. T* end()
  63. {
  64. return m_data + m_len;
  65. }
  66. /** Returns number of items in the array. */
  67. int size() const
  68. {
  69. return m_len;
  70. }
  71. /** Returns ith item. */
  72. const T& operator[]( int i ) const
  73. {
  74. return m_data[i];
  75. }
  76. /** Returns pointer to the first element in the vector. */
  77. const T* begin() const
  78. {
  79. return m_data;
  80. }
  81. /** Returns pointer to one beyond the last element in the vector. */
  82. const T* end() const
  83. {
  84. return m_data + m_len;
  85. }
  86. private:
  87. T* m_data;
  88. int m_len;
  89. int m_cap;
  90. void setCapacity( int cap )
  91. {
  92. ++cap;
  93. if ( cap < 8 )
  94. cap = 8;
  95. else if ( cap < m_cap*2 )
  96. cap = m_cap*2;
  97. m_cap = cap;
  98. T* data = new T[cap];
  99. for ( int i = 0 ; i < m_len ; ++i )
  100. data[i] = m_data[i];
  101. delete[] m_data;
  102. m_data = data;
  103. }
  104. };
  105. } /* dev */
  106. #endif /* _DEV_ARRAY_H */