mem_root_array.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. #ifndef MEM_ROOT_ARRAY_INCLUDED
  19. #define MEM_ROOT_ARRAY_INCLUDED
  20. #include <algorithm>
  21. #include <type_traits>
  22. #include "my_alloc.h"
  23. #include "my_dbug.h"
  24. /**
  25. A typesafe replacement for DYNAMIC_ARRAY.
  26. We use MEM_ROOT for allocating storage, rather than the C++ heap.
  27. The interface is chosen to be similar to std::vector.
  28. @remark
  29. Mem_root_array_YY is constructor-less for use in the parser stack of unions.
  30. For other needs please use Mem_root_array.
  31. @remark
  32. Unlike DYNAMIC_ARRAY, elements are properly copied
  33. (rather than memcpy()d) if the underlying array needs to be expanded.
  34. @remark
  35. Unless Element_type's destructor is trivial, we destroy objects when they are
  36. removed from the array (including when the array object itself is destroyed).
  37. @remark
  38. Note that MEM_ROOT has no facility for reusing free space,
  39. so don't use this if multiple re-expansions are likely to happen.
  40. @tparam Element_type The type of the elements of the container.
  41. Elements must be copyable.
  42. */
  43. template <typename Element_type>
  44. class Mem_root_array_YY {
  45. /**
  46. Is Element_type trivially destructible? If it is, we don't destroy
  47. elements when they are removed from the array or when the array is
  48. destroyed.
  49. */
  50. static constexpr bool has_trivial_destructor =
  51. std::is_trivially_destructible<Element_type>::value;
  52. public:
  53. /// Convenience typedef, same typedef name as std::vector
  54. typedef Element_type value_type;
  55. void init(MEM_ROOT *root) {
  56. DBUG_ASSERT(root != NULL);
  57. m_root = root;
  58. m_array = NULL;
  59. m_size = 0;
  60. m_capacity = 0;
  61. }
  62. /// Initialize empty array that we aren't going to grow
  63. void init_empty_const() {
  64. m_root = NULL;
  65. m_array = NULL;
  66. m_size = 0;
  67. m_capacity = 0;
  68. }
  69. /**
  70. Switches mem-root, in case original mem-root was copied.
  71. NOTE: m_root should really be const, i.e. never change after initialization.
  72. */
  73. void set_mem_root(MEM_ROOT *new_root) {
  74. m_root = new_root;
  75. DBUG_ASSERT(m_root != NULL);
  76. }
  77. Element_type &at(size_t n) {
  78. DBUG_ASSERT(n < size());
  79. return m_array[n];
  80. }
  81. const Element_type &at(size_t n) const {
  82. DBUG_ASSERT(n < size());
  83. return m_array[n];
  84. }
  85. Element_type &operator[](size_t n) { return at(n); }
  86. const Element_type &operator[](size_t n) const { return at(n); }
  87. Element_type &back() { return at(size() - 1); }
  88. const Element_type &back() const { return at(size() - 1); }
  89. /// Random access iterators to value_type and const value_type.
  90. typedef Element_type *iterator;
  91. typedef const Element_type *const_iterator;
  92. /// Returns a pointer to the first element in the array.
  93. Element_type *begin() { return &m_array[0]; }
  94. const Element_type *begin() const { return &m_array[0]; }
  95. /// Returns a pointer to the past-the-end element in the array.
  96. Element_type *end() { return &m_array[size()]; }
  97. const Element_type *end() const { return &m_array[size()]; }
  98. /// Returns a constant pointer to the first element in the array.
  99. const_iterator cbegin() const { return begin(); }
  100. /// Returns a constant pointer to the past-the-end element in the array.
  101. const_iterator cend() const { return end(); }
  102. /// Erases all of the elements.
  103. void clear() {
  104. if (!empty()) chop(0);
  105. }
  106. /**
  107. Chops the tail off the array, erasing all tail elements.
  108. @param pos Index of first element to erase.
  109. */
  110. void chop(const size_t pos) {
  111. DBUG_ASSERT(pos < m_size);
  112. if (!has_trivial_destructor) {
  113. for (size_t ix = pos; ix < m_size; ++ix) {
  114. Element_type *p = &m_array[ix];
  115. p->~Element_type(); // Destroy discarded element.
  116. }
  117. }
  118. m_size = pos;
  119. }
  120. /**
  121. Reserves space for array elements.
  122. Copies over existing elements, in case we are re-expanding the array.
  123. @param n number of elements.
  124. @retval true if out-of-memory, false otherwise.
  125. */
  126. bool reserve(size_t n) {
  127. if (n <= m_capacity) return false;
  128. void *mem = m_root->Alloc(n * element_size());
  129. if (!mem) return true;
  130. Element_type *array = static_cast<Element_type *>(mem);
  131. // Copy all the existing elements into the new array.
  132. for (size_t ix = 0; ix < m_size; ++ix) {
  133. Element_type *new_p = &array[ix];
  134. Element_type *old_p = &m_array[ix];
  135. ::new (new_p)
  136. Element_type(std::move(*old_p)); // Copy or move into new location.
  137. if (!has_trivial_destructor)
  138. old_p->~Element_type(); // Destroy the old element.
  139. }
  140. // Forget the old array.
  141. m_array = array;
  142. m_capacity = n;
  143. return false;
  144. }
  145. /**
  146. Adds a new element at the end of the array, after its current last
  147. element. The content of this new element is initialized to a copy of
  148. the input argument.
  149. @param element Object to copy.
  150. @retval true if out-of-memory, false otherwise.
  151. */
  152. bool push_back(const Element_type &element) {
  153. const size_t min_capacity = 20;
  154. const size_t expansion_factor = 2;
  155. if (0 == m_capacity && reserve(min_capacity)) return true;
  156. if (m_size == m_capacity && reserve(m_capacity * expansion_factor))
  157. return true;
  158. Element_type *p = &m_array[m_size++];
  159. ::new (p) Element_type(element);
  160. return false;
  161. }
  162. /**
  163. Adds a new element at the end of the array, after its current last
  164. element. The content of this new element is initialized by moving
  165. the input element.
  166. @param element Object to move.
  167. @retval true if out-of-memory, false otherwise.
  168. */
  169. bool push_back(Element_type &&element) {
  170. const size_t min_capacity = 20;
  171. const size_t expansion_factor = 2;
  172. if (0 == m_capacity && reserve(min_capacity)) return true;
  173. if (m_size == m_capacity && reserve(m_capacity * expansion_factor))
  174. return true;
  175. Element_type *p = &m_array[m_size++];
  176. ::new (p) Element_type(std::move(element));
  177. return false;
  178. }
  179. /**
  180. Removes the last element in the array, effectively reducing the
  181. container size by one. This destroys the removed element.
  182. */
  183. void pop_back() {
  184. DBUG_ASSERT(!empty());
  185. if (!has_trivial_destructor) back().~Element_type();
  186. m_size -= 1;
  187. }
  188. /**
  189. Resizes the container so that it contains n elements.
  190. If n is smaller than the current container size, the content is
  191. reduced to its first n elements, removing those beyond (and
  192. destroying them).
  193. If n is greater than the current container size, the content is
  194. expanded by inserting at the end as many elements as needed to
  195. reach a size of n. If val is specified, the new elements are
  196. initialized as copies of val, otherwise, they are
  197. value-initialized.
  198. If n is also greater than the current container capacity, an automatic
  199. reallocation of the allocated storage space takes place.
  200. Notice that this function changes the actual content of the
  201. container by inserting or erasing elements from it.
  202. */
  203. void resize(size_t n, const value_type &val) {
  204. if (n == m_size) return;
  205. if (n > m_size) {
  206. if (!reserve(n)) {
  207. while (n != m_size) push_back(val);
  208. }
  209. return;
  210. }
  211. if (!has_trivial_destructor) {
  212. while (n != m_size) pop_back();
  213. }
  214. m_size = n;
  215. }
  216. /**
  217. Same as resize(size_t, const value_type &val), but default-constructs
  218. the new elements. This allows one to resize containers even if
  219. value_type is not copy-constructible.
  220. */
  221. void resize(size_t n) {
  222. if (n == m_size) return;
  223. if (n > m_size) {
  224. if (!reserve(n)) {
  225. while (n != m_size) push_back(value_type());
  226. }
  227. return;
  228. }
  229. if (!has_trivial_destructor) {
  230. while (n != m_size) pop_back();
  231. }
  232. m_size = n;
  233. }
  234. /**
  235. Erase all the elements in the specified range.
  236. @param first iterator that points to the first element to remove
  237. @param last iterator that points to the element after the
  238. last one to remove
  239. @return an iterator to the first element after the removed range
  240. */
  241. iterator erase(const_iterator first, const_iterator last) {
  242. iterator pos = begin() + (first - cbegin());
  243. if (first != last) {
  244. iterator new_end = std::move(last, cend(), pos);
  245. chop(new_end - begin());
  246. }
  247. return pos;
  248. }
  249. /**
  250. Removes a single element from the array.
  251. @param position iterator that points to the element to remove
  252. @return an iterator to the first element after the removed range
  253. */
  254. iterator erase(const_iterator position) {
  255. return erase(position, std::next(position));
  256. }
  257. /**
  258. Removes a single element from the array.
  259. @param ix zero-based number of the element to remove
  260. @return an iterator to the first element after the removed range
  261. */
  262. iterator erase(size_t ix) {
  263. DBUG_ASSERT(ix < size());
  264. return erase(std::next(this->cbegin(), ix));
  265. }
  266. /**
  267. Insert an element at a given position.
  268. @param pos the new element is inserted before the element
  269. at this position
  270. @param value the value of the new element
  271. @return an iterator that points to the inserted element
  272. */
  273. iterator insert(const_iterator pos, const Element_type &value) {
  274. ptrdiff_t idx = pos - cbegin();
  275. if (!push_back(value)) std::rotate(begin() + idx, end() - 1, end());
  276. return begin() + idx;
  277. }
  278. /**
  279. Removes a single element from the array by value.
  280. The removed element is destroyed. This effectively reduces the
  281. container size by one.
  282. Note that if there are multiple elements having the same
  283. value, only the first element is removed.
  284. This is generally an inefficient operation, since we need to copy
  285. elements to fill the "hole" in the array.
  286. We use std::copy to move objects, hence Element_type must be
  287. assignable.
  288. @retval number of elements removed, 0 or 1.
  289. */
  290. size_t erase_value(const value_type &val) {
  291. iterator position = std::find(begin(), end(), val);
  292. if (position != end()) {
  293. erase(position);
  294. return 1;
  295. }
  296. return 0; // Not found
  297. }
  298. /**
  299. Removes a single element from the array.
  300. The removed element is destroyed.
  301. This effectively reduces the container size by one.
  302. This is generally an inefficient operation, since we need to copy
  303. elements to fill the "hole" in the array.
  304. We use std::copy to move objects, hence Element_type must be assignable.
  305. */
  306. iterator erase(iterator position) {
  307. DBUG_ASSERT(position != end());
  308. if (position + 1 != end()) std::copy(position + 1, end(), position);
  309. this->pop_back();
  310. return position;
  311. }
  312. size_t capacity() const { return m_capacity; }
  313. size_t element_size() const { return sizeof(Element_type); }
  314. bool empty() const { return size() == 0; }
  315. size_t size() const { return m_size; }
  316. private:
  317. MEM_ROOT *m_root;
  318. Element_type *m_array;
  319. size_t m_size;
  320. size_t m_capacity;
  321. // No CTOR/DTOR for this class!
  322. // Mem_root_array_YY(const Mem_root_array_YY&);
  323. // Mem_root_array_YY &operator=(const Mem_root_array_YY&);
  324. };
  325. /**
  326. A typesafe replacement for DYNAMIC_ARRAY.
  327. @see Mem_root_array_YY.
  328. */
  329. template <typename Element_type>
  330. class Mem_root_array : public Mem_root_array_YY<Element_type> {
  331. typedef Mem_root_array_YY<Element_type> super;
  332. public:
  333. /// Convenience typedef, same typedef name as std::vector
  334. typedef Element_type value_type;
  335. typedef typename super::const_iterator const_iterator;
  336. explicit Mem_root_array(MEM_ROOT *root) { super::init(root); }
  337. Mem_root_array(MEM_ROOT *root, size_t n) {
  338. super::init(root);
  339. super::resize(n);
  340. }
  341. Mem_root_array(MEM_ROOT *root, size_t n, const value_type &val) {
  342. super::init(root);
  343. super::resize(n, val);
  344. }
  345. /**
  346. Range constructor.
  347. Constructs a container with as many elements as the range [first,last),
  348. with each element constructed from its corresponding element in that range,
  349. in the same order.
  350. @param root MEM_ROOT to use for memory allocation.
  351. @param first iterator that points to the first element to copy
  352. @param last iterator that points to the element after the
  353. last one to copy
  354. */
  355. Mem_root_array(MEM_ROOT *root, const_iterator first, const_iterator last) {
  356. super::init(root);
  357. if (this->reserve(last - first)) return;
  358. for (auto it = first; it != last; ++it) this->push_back(*it);
  359. }
  360. Mem_root_array(MEM_ROOT *root, const Mem_root_array &x)
  361. : Mem_root_array(root, x.cbegin(), x.cend()) {}
  362. ~Mem_root_array() { super::clear(); }
  363. private:
  364. // Not (yet) implemented.
  365. Mem_root_array(const Mem_root_array &);
  366. Mem_root_array &operator=(const Mem_root_array &);
  367. };
  368. #endif // MEM_ROOT_ARRAY_INCLUDED