tnxarray.hxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /****************************************************************************
  2. * apps/include/graphics/nxwidgets/tnxarray.hxx
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. *
  22. * Portions of this package derive from Woopsi (http://woopsi.org/) and
  23. * portions are original efforts. It is difficult to determine at this
  24. * point what parts are original efforts and which parts derive from Woopsi.
  25. * However, in any event, the work of Antony Dzeryn will be acknowledged
  26. * in most NxWidget files. Thanks Antony!
  27. *
  28. * Copyright (c) 2007-2011, Antony Dzeryn
  29. * All rights reserved.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions are met:
  33. *
  34. * * Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * * Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. * * Neither the names "Woopsi", "Simian Zombie" nor the
  40. * names of its contributors may be used to endorse or promote products
  41. * derived from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
  44. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  45. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
  47. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  48. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  49. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  50. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  51. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  52. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. *
  54. ****************************************************************************/
  55. #ifndef __APPS_INCLUDE_GRAPHICS_NXWIDGETS_TNXARRAY_HXX
  56. #define __APPS_INCLUDE_GRAPHICS_NXWIDGETS_TNXARRAY_HXX
  57. /****************************************************************************
  58. * Included Files
  59. ****************************************************************************/
  60. #include <nuttx/config.h>
  61. #include <stdint.h>
  62. #include <stdbool.h>
  63. #include "graphics/nxwidgets/nxconfig.hxx"
  64. /****************************************************************************
  65. * Pre-Processor Definitions
  66. ****************************************************************************/
  67. /****************************************************************************
  68. * Implementation Classes
  69. ****************************************************************************/
  70. #if defined(__cplusplus)
  71. /**
  72. * Class providing a dynamic array; that is, an array that will automatically
  73. * grow to accommodate new data. It provides a fast way to randomly access
  74. * a list of data. Essentially, it provides the most important functionality
  75. * of the STL vector class without any of the overhead of including an STL
  76. * class.
  77. *
  78. * If the data to be stored will store a lot of data that will predominantly
  79. * be read sequentially, consider using the LinkedList class instead. Resizing
  80. * the list is an expensive operation that will occur frequently when filling
  81. * the array with large amounts of data. Adding new data to the linked list is
  82. * very inexpensive.
  83. */
  84. template <class T>
  85. class TNxArray
  86. {
  87. private:
  88. T *m_data; /**< Internal array of data items */
  89. int m_size; /**< Number of items in the array */
  90. int m_reservedSize; /**< Total size of the array including unpopulated slots */
  91. /**
  92. * Re-allocate the array to this size;
  93. */
  94. void reallocate(const int newSize);
  95. /**
  96. * Resize the array if it is full.
  97. */
  98. void resize(void);
  99. public:
  100. /**
  101. * Constructor. Creates an un-allocated array. The array will
  102. * be allocated when items are added to it or when preallocate()
  103. * is called.
  104. */
  105. inline TNxArray();
  106. /**
  107. * Constructor. Creates an allocated array.
  108. *
  109. *@param initialSize The initial size of the array.
  110. */
  111. inline TNxArray(int initialSize);
  112. /**
  113. * Destructor.
  114. */
  115. inline ~TNxArray();
  116. /**
  117. * Set the initial size of the array. Normally, the array is
  118. * unallocated until the first data is pushed into the array.
  119. * That works great for stacks and lists. But if you want a
  120. * array of uninitialized elements, then this method will
  121. * preallocate the array for you.
  122. *
  123. * @return The size of the array.
  124. */
  125. void preallocate(void);
  126. /**
  127. * Get the size of the array.
  128. *
  129. * @return The size of the array.
  130. */
  131. inline const int size(void) const;
  132. /**
  133. * Add a value to the end of the array.
  134. *
  135. * @param value The value to add to the array.
  136. */
  137. void push_back(const T &value);
  138. /**
  139. * Insert a value into the array.
  140. *
  141. * @param index The index to insert into.
  142. * @param value The value to insert.
  143. */
  144. void insert(const int index, const T &value);
  145. /**
  146. * Remove the last element from the array.
  147. */
  148. void pop_back(void);
  149. /**
  150. * Erase a single value at the specified index
  151. */
  152. void erase(const int index);
  153. /**
  154. * Get a value at the specified location. Does not perform bounds checking.
  155. * @param index The index of the desired value.
  156. * @return The value at the specified index.
  157. */
  158. inline T &at(const int index) const;
  159. /**
  160. * Check if the array has any data.
  161. * @return True if the array is empty.
  162. */
  163. inline bool empty(void) const;
  164. /**
  165. * Remove all data.
  166. */
  167. void clear();
  168. /**
  169. * Overload the [] operator to allow array-style access.
  170. * @param index The index to retrieve.
  171. * @return The value at the specified index.
  172. */
  173. T& operator[](const int index) const;
  174. };
  175. template <class T>
  176. TNxArray<T>::TNxArray()
  177. {
  178. // Don't allocate anything until the first data is added to
  179. // the array
  180. m_size = 0; // Number of data items in use
  181. m_reservedSize = 0; // Number of data items allocated
  182. m_data = (T *)0; // Allocated memory for data items
  183. }
  184. template <class T>
  185. TNxArray<T>::TNxArray(int initialSize)
  186. {
  187. m_size = 0; // Number of data items in use
  188. m_reservedSize = 0; // Number of data items allocated
  189. m_data = (T *)0; // Allocated memory for data items
  190. preallocate(initialSize); // Allocate the initial array
  191. }
  192. template <class T>
  193. TNxArray<T>::~TNxArray()
  194. {
  195. if (m_data)
  196. {
  197. delete [] m_data;
  198. }
  199. }
  200. template <class T>
  201. const int TNxArray<T>::size(void) const
  202. {
  203. return m_size;
  204. }
  205. template <class T>
  206. void TNxArray<T>::push_back(const T &value)
  207. {
  208. // Ensure the array is large enough to hold one more data item
  209. resize();
  210. // Add data to array
  211. m_data[m_size] = value;
  212. // Remember we've filled a slot
  213. m_size++;
  214. }
  215. template <class T>
  216. void TNxArray<T>::pop_back(void)
  217. {
  218. if (m_size >= 1)
  219. {
  220. // We can just reduce the used size of the array, as the value
  221. // will get overwritten automatically
  222. m_size--;
  223. }
  224. }
  225. template <class T>
  226. void TNxArray<T>::insert(const int index, const T &value)
  227. {
  228. // Bounds check
  229. if ((index >= m_size) || (m_size == 0))
  230. {
  231. push_back(value);
  232. return;
  233. }
  234. // Ensure the array is large enough to hold one more data item
  235. resize();
  236. // Shift all of the data back one place to make a space for the new data
  237. for (int i = m_size; i > index; i--)
  238. {
  239. m_data[i] = m_data[i - 1];
  240. }
  241. // Add data to array
  242. m_data[index] = value;
  243. // Remember we've filled a slot
  244. m_size++;
  245. }
  246. template <class T>
  247. void TNxArray<T>::erase(const int index)
  248. {
  249. // Bounds check
  250. if (index >= m_size)
  251. {
  252. return;
  253. }
  254. // Shift all of the data back one place and overwrite the value
  255. for (int i = index; i < m_size - 1; i++)
  256. {
  257. m_data[i] = m_data[i + 1];
  258. }
  259. // Remember we've removed a slot
  260. m_size--;
  261. }
  262. template <class T>
  263. void TNxArray<T>::reallocate(const int newSize)
  264. {
  265. // Do we need to redim the array?
  266. if (m_reservedSize < newSize)
  267. {
  268. // Create the new array
  269. T *newData = new T[newSize];
  270. // Copy old array contents to new the new array
  271. for (int i = 0; i < m_reservedSize; i++)
  272. {
  273. newData[i] = m_data[i];
  274. }
  275. // Delete the old array (if there was one)
  276. if (m_data)
  277. {
  278. delete [] m_data;
  279. }
  280. // Update values
  281. m_data = newData;
  282. m_reservedSize = newSize;
  283. }
  284. }
  285. template <class T>
  286. void TNxArray<T>::resize(void)
  287. {
  288. // Do we need to redim the array in order to add one more entry?
  289. if (m_reservedSize == m_size)
  290. {
  291. // We have filled the array, so resize it
  292. int newSize = m_reservedSize;
  293. #if CONFIG_NXWIDGETS_TNXARRAY_INITIALSIZE != CONFIG_NXWIDGETS_TNXARRAY_SIZEINCREMENT
  294. newSize += m_reservedSize ?
  295. CONFIG_NXWIDGETS_TNXARRAY_SIZEINCREMENT :
  296. CONFIG_NXWIDGETS_TNXARRAY_INITIALSIZE;
  297. #else
  298. newSize += CONFIG_NXWIDGETS_TNXARRAY_SIZEINCREMENT;
  299. #endif
  300. // Re-allocate the array
  301. reallocate(newSize);
  302. }
  303. }
  304. template <class T>
  305. T& TNxArray<T>::at(const int index) const
  306. {
  307. // What if this is called with index > m_reservedSize? What if
  308. // this is called before m_data is allocated? Don't do that!
  309. return m_data[index];
  310. }
  311. template <class T>
  312. bool TNxArray<T>::empty() const
  313. {
  314. return (m_size == 0);
  315. }
  316. template <class T>
  317. T& TNxArray<T>::operator[](const int index) const
  318. {
  319. // What if this is called with index > m_reservedSize? What if
  320. // this is called before m_data is allocated? Don't do that!
  321. return m_data[index];
  322. }
  323. template <class T>
  324. void TNxArray<T>::clear()
  325. {
  326. // All we need to do is reset the size value
  327. m_size = 0;
  328. }
  329. #endif // __cplusplus
  330. #endif // __APPS_INCLUDE_GRAPHICS_NXWIDGETS_TNXARRAY_HXX