wbooze

problem

Mar 14th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. // $Id$ -*- C++ -*-
  2. // Dynamic array -- generates the referenced entries
  3.  
  4. // Copyright (C) 1995 Technische Universitaet Braunschweig, Germany.
  5. // Written by Andreas Zeller <[email protected]>.
  6. //
  7. // This file is part of DDD.
  8. //
  9. // DDD is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 3 of the License, or (at your option) any later version.
  13. //
  14. // DDD is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. // See the GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with DDD -- see the file COPYING.
  21. // If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. // DDD is the data display debugger.
  24. // For details, see the DDD World-Wide-Web page,
  25. // `http://www.gnu.org/software/ddd/',
  26. // or send a mail to the DDD developers <[email protected]>.
  27.  
  28. #ifndef _DDD_DynArray_h
  29. #define _DDD_DynArray_h
  30.  
  31. #include "assert.h"
  32. #include "bool.h"
  33. #include <new>
  34.  
  35. // A DynArray acts like an ordinary array,
  36. // but it automatically expands to hold the referenced entry.
  37. // Thus, if you say DynArray<int> d; d[100] = 10,
  38. // it automatically expands to 100 elements.
  39. // Note that a const DynArray will not grow.
  40.  
  41. // If you prefer sparse storage, see the Assoc template.
  42.  
  43. template<class T>
  44. class DynArray {
  45. private:
  46. int _allocated_size; // size of _values
  47. T *_values; // values
  48.  
  49. static int max(int a, int b) { return (a > b) ? a : b; }
  50.  
  51. // Grow by half the current size, at least to new_size_min
  52. void grow(int new_size_min = 0)
  53. {
  54. int new_size = max(_allocated_size + _allocated_size / 2 + 1,
  55. new_size_min);
  56. T *new_values = new T[new_size];
  57. for (int i = 0; i < _allocated_size; i++)
  58. new_values[i] = _values[i];
  59.  
  60. delete[] _values;
  61. _values = new_values;
  62. _allocated_size = new_size;
  63. }
  64.  
  65. protected:
  66. T& value(int i)
  67. {
  68. assert(i >= 0);
  69. if (i <= _allocated_size)
  70. grow(i + 1);
  71. return _values[i];
  72. }
  73. const T& _value(int i) const
  74. {
  75. assert(i >= 0 && i < size());
  76. return _values[i];
  77. }
  78. T& _value(int i)
  79. {
  80. assert(i >= 0 && i < size());
  81. return _values[i];
  82. }
  83.  
  84. public:
  85. // Resources
  86. virtual int size() const { return _allocated_size; }
  87. const T& operator[](int i) const { return _value(i); }
  88. T& operator[](int i) { return value(i); }
  89. const T* values() const { return _values; }
  90. T* values() { return _values; }
  91. #if 0
  92. operator const T*() const { return values(); }
  93. operator T*() const { return values(); }
  94. #endif
  95.  
  96. // Constructors
  97. DynArray(int initial_size = 0):
  98. _allocated_size(initial_size),
  99. _values(new T[initial_size])
  100. {}
  101.  
  102. DynArray(T *v, int n):
  103. _allocated_size(n),
  104. _values(new T[n])
  105. {
  106. for (int i = 0; i < n; i++)
  107. _values[i] = v[i];
  108. }
  109.  
  110. // Copy constructor
  111. DynArray(const DynArray<T>& m):
  112. _allocated_size(m.size()),
  113. _values(new T[m.size()])
  114. {
  115. for (int i = 0; i < _allocated_size; i++)
  116. _values[i] = m._values[i];
  117. }
  118.  
  119. // Destructor
  120. virtual ~DynArray()
  121. {
  122. delete[] _values;
  123. }
  124.  
  125. // Assignment
  126. DynArray<T>& operator = (const DynArray<T>& m)
  127. {
  128. // Make sure self-assignment A = A works
  129. if (this != &m)
  130. {
  131. T *old_values = _values;
  132.  
  133. _allocated_size = m.size();
  134. _values = new T[_allocated_size];
  135.  
  136. for (int i = 0; i < _allocated_size; i++)
  137. _values[i] = m._values[i];
  138.  
  139. delete[] old_values;
  140. }
  141.  
  142. return *this;
  143. }
  144.  
  145. // Comparison
  146. bool operator == (const DynArray<T>& m) const
  147. {
  148. if (this == &m)
  149. return true; // THIS == THIS
  150.  
  151. if (size() != m.size())
  152. return false;
  153.  
  154. for (int i = size() - 1; i >= 0; i--)
  155. {
  156. if (!(_values[i] == m._values[i]))
  157. return false;
  158. }
  159.  
  160. return true;
  161. }
  162.  
  163. bool operator != (const DynArray<T>& m) const
  164. {
  165. return !(operator == (m));
  166. }
  167. };
  168.  
  169. #endif // _DDD_DynArray_h
  170. // DON'T ADD ANYTHING BEHIND THIS #endif
Advertisement
Add Comment
Please, Sign In to add comment