Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_LINKEDLISTPOINTER_H_INCLUDED
  21. #define WATER_LINKEDLISTPOINTER_H_INCLUDED
  22. #include "../water.h"
  23. namespace water {
  24. //==============================================================================
  25. /**
  26. Helps to manipulate singly-linked lists of objects.
  27. For objects that are designed to contain a pointer to the subsequent item in the
  28. list, this class contains methods to deal with the list. To use it, the ObjectType
  29. class that it points to must contain a LinkedListPointer called nextListItem, e.g.
  30. @code
  31. struct MyObject
  32. {
  33. int x, y, z;
  34. // A linkable object must contain a member with this name and type, which must be
  35. // accessible by the LinkedListPointer class. (This doesn't mean it has to be public -
  36. // you could make your class a friend of a LinkedListPointer<MyObject> instead).
  37. LinkedListPointer<MyObject> nextListItem;
  38. };
  39. LinkedListPointer<MyObject> myList;
  40. myList.append (new MyObject());
  41. myList.append (new MyObject());
  42. int numItems = myList.size(); // returns 2
  43. MyObject* lastInList = myList.getLast();
  44. @endcode
  45. */
  46. template <class ObjectType>
  47. class LinkedListPointer
  48. {
  49. public:
  50. //==============================================================================
  51. /** Creates a null pointer to an empty list. */
  52. LinkedListPointer() noexcept
  53. : item (nullptr)
  54. {
  55. }
  56. /** Creates a pointer to a list whose head is the item provided. */
  57. explicit LinkedListPointer (ObjectType* const headItem) noexcept
  58. : item (headItem)
  59. {
  60. }
  61. /** Sets this pointer to point to a new list. */
  62. LinkedListPointer& operator= (ObjectType* const newItem) noexcept
  63. {
  64. item = newItem;
  65. return *this;
  66. }
  67. //==============================================================================
  68. /** Returns the item which this pointer points to. */
  69. inline operator ObjectType*() const noexcept
  70. {
  71. return item;
  72. }
  73. /** Returns the item which this pointer points to. */
  74. inline ObjectType* get() const noexcept
  75. {
  76. return item;
  77. }
  78. /** Returns the last item in the list which this pointer points to.
  79. This will iterate the list and return the last item found. Obviously the speed
  80. of this operation will be proportional to the size of the list. If the list is
  81. empty the return value will be this object.
  82. If you're planning on appending a number of items to your list, it's much more
  83. efficient to use the Appender class than to repeatedly call getLast() to find the end.
  84. */
  85. LinkedListPointer& getLast() noexcept
  86. {
  87. LinkedListPointer* l = this;
  88. while (l->item != nullptr)
  89. l = &(l->item->nextListItem);
  90. return *l;
  91. }
  92. /** Returns the number of items in the list.
  93. Obviously with a simple linked list, getting the size involves iterating the list, so
  94. this can be a lengthy operation - be careful when using this method in your code.
  95. */
  96. int size() const noexcept
  97. {
  98. int total = 0;
  99. for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
  100. ++total;
  101. return total;
  102. }
  103. /** Returns the item at a given index in the list.
  104. Since the only way to find an item is to iterate the list, this operation can obviously
  105. be slow, depending on its size, so you should be careful when using this in algorithms.
  106. */
  107. LinkedListPointer& operator[] (int index) noexcept
  108. {
  109. LinkedListPointer* l = this;
  110. while (--index >= 0 && l->item != nullptr)
  111. l = &(l->item->nextListItem);
  112. return *l;
  113. }
  114. /** Returns the item at a given index in the list.
  115. Since the only way to find an item is to iterate the list, this operation can obviously
  116. be slow, depending on its size, so you should be careful when using this in algorithms.
  117. */
  118. const LinkedListPointer& operator[] (int index) const noexcept
  119. {
  120. const LinkedListPointer* l = this;
  121. while (--index >= 0 && l->item != nullptr)
  122. l = &(l->item->nextListItem);
  123. return *l;
  124. }
  125. /** Returns true if the list contains the given item. */
  126. bool contains (const ObjectType* const itemToLookFor) const noexcept
  127. {
  128. for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
  129. if (itemToLookFor == i)
  130. return true;
  131. return false;
  132. }
  133. //==============================================================================
  134. /** Inserts an item into the list, placing it before the item that this pointer
  135. currently points to.
  136. */
  137. void insertNext (ObjectType* const newItem)
  138. {
  139. wassert (newItem != nullptr);
  140. wassert (newItem->nextListItem == nullptr);
  141. newItem->nextListItem = item;
  142. item = newItem;
  143. }
  144. /** Inserts an item at a numeric index in the list.
  145. Obviously this will involve iterating the list to find the item at the given index,
  146. so be careful about the impact this may have on execution time.
  147. */
  148. void insertAtIndex (int index, ObjectType* newItem)
  149. {
  150. wassert (newItem != nullptr);
  151. LinkedListPointer* l = this;
  152. while (index != 0 && l->item != nullptr)
  153. {
  154. l = &(l->item->nextListItem);
  155. --index;
  156. }
  157. l->insertNext (newItem);
  158. }
  159. /** Replaces the object that this pointer points to, appending the rest of the list to
  160. the new object, and returning the old one.
  161. */
  162. ObjectType* replaceNext (ObjectType* const newItem) noexcept
  163. {
  164. wassert (newItem != nullptr);
  165. wassert (newItem->nextListItem == nullptr);
  166. ObjectType* const oldItem = item;
  167. item = newItem;
  168. item->nextListItem = oldItem->nextListItem.item;
  169. oldItem->nextListItem.item = nullptr;
  170. return oldItem;
  171. }
  172. /** Adds an item to the end of the list.
  173. This operation involves iterating the whole list, so can be slow - if you need to
  174. append a number of items to your list, it's much more efficient to use the Appender
  175. class than to repeatedly call append().
  176. */
  177. void append (ObjectType* const newItem)
  178. {
  179. getLast().item = newItem;
  180. }
  181. /** Creates copies of all the items in another list and adds them to this one.
  182. This will use the ObjectType's copy constructor to try to create copies of each
  183. item in the other list, and appends them to this list.
  184. */
  185. void addCopyOfList (const LinkedListPointer& other)
  186. {
  187. LinkedListPointer* insertPoint = this;
  188. for (ObjectType* i = other.item; i != nullptr; i = i->nextListItem)
  189. {
  190. insertPoint->insertNext (new ObjectType (*i));
  191. insertPoint = &(insertPoint->item->nextListItem);
  192. }
  193. }
  194. /** Removes the head item from the list.
  195. This won't delete the object that is removed, but returns it, so the caller can
  196. delete it if necessary.
  197. */
  198. ObjectType* removeNext() noexcept
  199. {
  200. ObjectType* const oldItem = item;
  201. if (oldItem != nullptr)
  202. {
  203. item = oldItem->nextListItem;
  204. oldItem->nextListItem.item = nullptr;
  205. }
  206. return oldItem;
  207. }
  208. /** Removes a specific item from the list.
  209. Note that this will not delete the item, it simply unlinks it from the list.
  210. */
  211. void remove (ObjectType* const itemToRemove)
  212. {
  213. if (LinkedListPointer* const l = findPointerTo (itemToRemove))
  214. l->removeNext();
  215. }
  216. /** Iterates the list, calling the delete operator on all of its elements and
  217. leaving this pointer empty.
  218. */
  219. void deleteAll()
  220. {
  221. while (item != nullptr)
  222. {
  223. ObjectType* const oldItem = item;
  224. item = oldItem->nextListItem;
  225. delete oldItem;
  226. }
  227. }
  228. /** Finds a pointer to a given item.
  229. If the item is found in the list, this returns the pointer that points to it. If
  230. the item isn't found, this returns null.
  231. */
  232. LinkedListPointer* findPointerTo (ObjectType* const itemToLookFor) noexcept
  233. {
  234. LinkedListPointer* l = this;
  235. while (l->item != nullptr)
  236. {
  237. if (l->item == itemToLookFor)
  238. return l;
  239. l = &(l->item->nextListItem);
  240. }
  241. return nullptr;
  242. }
  243. /** Copies the items in the list to an array.
  244. The destArray must contain enough elements to hold the entire list - no checks are
  245. made for this!
  246. */
  247. void copyToArray (ObjectType** destArray) const noexcept
  248. {
  249. wassert (destArray != nullptr);
  250. for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
  251. *destArray++ = i;
  252. }
  253. /** Swaps this pointer with another one */
  254. void swapWith (LinkedListPointer& other) noexcept
  255. {
  256. std::swap (item, other.item);
  257. }
  258. //==============================================================================
  259. /**
  260. Allows efficient repeated insertions into a list.
  261. You can create an Appender object which points to the last element in your
  262. list, and then repeatedly call Appender::append() to add items to the end
  263. of the list in O(1) time.
  264. */
  265. class Appender
  266. {
  267. public:
  268. /** Creates an appender which will add items to the given list.
  269. */
  270. Appender (LinkedListPointer& endOfListPointer) noexcept
  271. : endOfList (&endOfListPointer)
  272. {
  273. // This can only be used to add to the end of a list.
  274. wassert (endOfListPointer.item == nullptr);
  275. }
  276. /** Appends an item to the list. */
  277. void append (ObjectType* const newItem) noexcept
  278. {
  279. *endOfList = newItem;
  280. endOfList = &(newItem->nextListItem);
  281. }
  282. private:
  283. LinkedListPointer* endOfList;
  284. CARLA_DECLARE_NON_COPYABLE (Appender)
  285. };
  286. private:
  287. //==============================================================================
  288. ObjectType* item;
  289. CARLA_DECLARE_NON_COPYABLE (LinkedListPointer)
  290. };
  291. }
  292. #endif // WATER_LINKEDLISTPOINTER_H_INCLUDED