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.

LinkedList.hpp 14KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * High-level, templated, C++ doubly-linked list
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef LINKED_LIST_HPP_INCLUDED
  18. #define LINKED_LIST_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. // -----------------------------------------------------------------------
  21. // Define list_entry and list_entry_const
  22. #ifndef offsetof
  23. # define offsetof(TYPE, MEMBER) ((std::size_t) &((TYPE*)nullptr)->MEMBER)
  24. #endif
  25. #if (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__)
  26. # define list_entry(ptr, type, member) ({ \
  27. typeof( ((type*)nullptr)->member ) *__mptr = (ptr); \
  28. (type*)( (char*)__mptr - offsetof(type, member) );})
  29. # define list_entry_const(ptr, type, member) ({ \
  30. const typeof( ((type*)nullptr)->member ) *__mptr = (ptr); \
  31. (const type*)( (const char*)__mptr - offsetof(type, member) );})
  32. #else
  33. # define list_entry(ptr, type, member) \
  34. ((type*)((char*)(ptr)-offsetof(type, member)))
  35. # define list_entry_const(ptr, type, member) \
  36. ((const type*)((const char*)(ptr)-offsetof(type, member)))
  37. #endif
  38. // -----------------------------------------------------------------------
  39. // Abstract Linked List class
  40. // _allocate() and _deallocate are virtual calls provided by subclasses
  41. // NOTE: this class is meant for non-polymorphic data types only!
  42. template<typename T>
  43. class AbstractLinkedList
  44. {
  45. protected:
  46. struct ListHead {
  47. ListHead* next;
  48. ListHead* prev;
  49. };
  50. struct Data {
  51. T value;
  52. ListHead siblings;
  53. };
  54. AbstractLinkedList() noexcept
  55. : kDataSize(sizeof(Data)),
  56. #ifdef CARLA_PROPER_CPP11_SUPPORT
  57. fQueue({&fQueue, &fQueue}),
  58. #endif
  59. fCount(0)
  60. {
  61. #ifndef CARLA_PROPER_CPP11_SUPPORT
  62. fQueue.next = &fQueue;
  63. fQueue.prev = &fQueue;
  64. #endif
  65. }
  66. public:
  67. virtual ~AbstractLinkedList() noexcept
  68. {
  69. CARLA_SAFE_ASSERT(fCount == 0);
  70. }
  71. class Itenerator {
  72. public:
  73. Itenerator(const ListHead& queue) noexcept
  74. : fEntry(queue.next),
  75. fEntry2(fEntry->next),
  76. kQueue(queue)
  77. {
  78. CARLA_SAFE_ASSERT(fEntry != nullptr);
  79. CARLA_SAFE_ASSERT(fEntry2 != nullptr);
  80. }
  81. bool valid() const noexcept
  82. {
  83. return (fEntry != nullptr && fEntry != &kQueue);
  84. }
  85. void next() noexcept
  86. {
  87. fEntry = fEntry2;
  88. fEntry2 = (fEntry != nullptr) ? fEntry->next : nullptr;
  89. }
  90. T& getValue(T& fallback) const noexcept
  91. {
  92. Data* const data(list_entry(fEntry, Data, siblings));
  93. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  94. return data->value;
  95. }
  96. const T& getValue(const T& fallback) const noexcept
  97. {
  98. const Data* const data(list_entry_const(fEntry, Data, siblings));
  99. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  100. return data->value;
  101. }
  102. void setValue(const T& value) noexcept
  103. {
  104. Data* const data(list_entry(fEntry, Data, siblings));
  105. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  106. data->value = value;
  107. }
  108. private:
  109. ListHead* fEntry;
  110. ListHead* fEntry2;
  111. const ListHead& kQueue;
  112. friend class AbstractLinkedList;
  113. };
  114. class AutoItenerator {
  115. public:
  116. AutoItenerator(const ListHead* entry) noexcept
  117. : fEntry(entry),
  118. fEntry2(entry != nullptr ? entry->next : nullptr)
  119. {
  120. CARLA_SAFE_ASSERT(fEntry != nullptr);
  121. CARLA_SAFE_ASSERT(fEntry2 != nullptr);
  122. }
  123. bool operator!=(AutoItenerator& it) const noexcept
  124. {
  125. CARLA_SAFE_ASSERT_RETURN(fEntry != nullptr, false);
  126. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr, false);
  127. return fEntry != it.fEntry;
  128. }
  129. AutoItenerator& operator++() noexcept
  130. {
  131. fEntry = fEntry2;
  132. fEntry2 = (fEntry != nullptr) ? fEntry->next : nullptr;
  133. return *this;
  134. }
  135. #if 0
  136. T& operator*() noexcept
  137. {
  138. static T& fallback(_getFallback());
  139. Data* const data(list_entry(fEntry, Data, siblings));
  140. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  141. return data->value;
  142. }
  143. #endif
  144. const T& operator*() const noexcept
  145. {
  146. static const T& fallback(_getFallback());
  147. const Data* const data(list_entry_const(fEntry, Data, siblings));
  148. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  149. return data->value;
  150. }
  151. private:
  152. const ListHead* fEntry;
  153. const ListHead* fEntry2;
  154. static T& _getFallback()
  155. {
  156. static T data;
  157. carla_zeroStruct(data);
  158. return data;
  159. }
  160. };
  161. Itenerator begin2() const noexcept
  162. {
  163. return Itenerator(fQueue);
  164. }
  165. AutoItenerator begin() const noexcept
  166. {
  167. return AutoItenerator(fQueue.next);
  168. }
  169. AutoItenerator end() const noexcept
  170. {
  171. return AutoItenerator(&fQueue);
  172. }
  173. void clear() noexcept
  174. {
  175. if (fCount == 0)
  176. return;
  177. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  178. {
  179. Data* const data(list_entry(entry, Data, siblings));
  180. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  181. _deallocate(data);
  182. }
  183. _init();
  184. }
  185. std::size_t count() const noexcept
  186. {
  187. return fCount;
  188. }
  189. bool isEmpty() const noexcept
  190. {
  191. return (fCount == 0);
  192. }
  193. bool append(const T& value) noexcept
  194. {
  195. return _add(value, true, &fQueue);
  196. }
  197. bool appendAt(const T& value, const Itenerator& it) noexcept
  198. {
  199. return _add(value, true, it.fEntry->next);
  200. }
  201. bool insert(const T& value) noexcept
  202. {
  203. return _add(value, false, &fQueue);
  204. }
  205. bool insertAt(const T& value, const Itenerator& it) noexcept
  206. {
  207. return _add(value, false, it.fEntry->prev);
  208. }
  209. // NOTE: do not use this function unless strictly needed. it can be very expensive if the list is big
  210. const T& getAt(const std::size_t index, const T& fallback) const noexcept
  211. {
  212. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  213. std::size_t i = 0;
  214. ListHead* entry = fQueue.next;
  215. for (; i++ != index; entry = entry->next) {}
  216. return _get(entry, fallback);
  217. }
  218. T getFirst(T& fallback, const bool removeObj) noexcept
  219. {
  220. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  221. return _get(fQueue.next, fallback, removeObj);
  222. }
  223. T& getFirst(T& fallback) const noexcept
  224. {
  225. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  226. return _get(fQueue.next, fallback);
  227. }
  228. const T& getFirst(const T& fallback) const noexcept
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  231. return _get(fQueue.next, fallback);
  232. }
  233. T getLast(T& fallback, const bool removeObj) noexcept
  234. {
  235. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  236. return _get(fQueue.prev, fallback, removeObj);
  237. }
  238. T& getLast(T& fallback) const noexcept
  239. {
  240. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  241. return _get(fQueue.prev, fallback);
  242. }
  243. const T& getLast(const T& fallback) const noexcept
  244. {
  245. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  246. return _get(fQueue.prev, fallback);
  247. }
  248. void remove(Itenerator& it) noexcept
  249. {
  250. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr,);
  251. Data* const data(list_entry(it.fEntry, Data, siblings));
  252. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  253. _delete(it.fEntry, data);
  254. }
  255. bool removeOne(const T& value) noexcept
  256. {
  257. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  258. {
  259. Data* const data = list_entry(entry, Data, siblings);
  260. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  261. if (data->value != value)
  262. continue;
  263. _delete(entry, data);
  264. return true;
  265. }
  266. return false;
  267. }
  268. void removeAll(const T& value) noexcept
  269. {
  270. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  271. {
  272. Data* const data = list_entry(entry, Data, siblings);
  273. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  274. if (data->value != value)
  275. continue;
  276. _delete(entry, data);
  277. }
  278. }
  279. // move data to a new list, and clear ourselves
  280. bool moveTo(AbstractLinkedList<T>& list, const bool inTail = true) noexcept
  281. {
  282. CARLA_SAFE_ASSERT_RETURN(fCount > 0, false);
  283. if (inTail)
  284. __list_splice_tail(&fQueue, &list.fQueue);
  285. else
  286. __list_splice(&fQueue, &list.fQueue);
  287. //! @a list gets our items
  288. list.fCount += fCount;
  289. //! and we get nothing
  290. _init();
  291. return true;
  292. }
  293. protected:
  294. const std::size_t kDataSize;
  295. ListHead fQueue;
  296. std::size_t fCount;
  297. virtual Data* _allocate() noexcept = 0;
  298. virtual void _deallocate(Data* const dataPtr) noexcept = 0;
  299. private:
  300. void _init() noexcept
  301. {
  302. fCount = 0;
  303. fQueue.next = &fQueue;
  304. fQueue.prev = &fQueue;
  305. }
  306. bool _add(const T& value, const bool inTail, ListHead* const queue) noexcept
  307. {
  308. if (Data* const data = _allocate())
  309. return _add_internal(data, value, inTail, queue);
  310. return false;
  311. }
  312. bool _add_internal(Data* const data, const T& value, const bool inTail, ListHead* const queue) noexcept
  313. {
  314. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  315. CARLA_SAFE_ASSERT_RETURN(queue != nullptr, false);
  316. CARLA_SAFE_ASSERT_RETURN(queue->prev != nullptr, false);
  317. CARLA_SAFE_ASSERT_RETURN(queue->next != nullptr, false);
  318. data->value = value;
  319. ListHead* const siblings(&data->siblings);
  320. if (inTail)
  321. {
  322. siblings->prev = queue->prev;
  323. siblings->next = queue;
  324. queue->prev->next = siblings;
  325. queue->prev = siblings;
  326. }
  327. else
  328. {
  329. siblings->prev = queue;
  330. siblings->next = queue->next;
  331. queue->next->prev = siblings;
  332. queue->next = siblings;
  333. }
  334. ++fCount;
  335. return true;
  336. }
  337. void _delete(ListHead* const entry, Data* const data) noexcept
  338. {
  339. CARLA_SAFE_ASSERT_RETURN(entry != nullptr,);
  340. CARLA_SAFE_ASSERT_RETURN(entry->prev != nullptr,);
  341. CARLA_SAFE_ASSERT_RETURN(entry->next != nullptr,);
  342. --fCount;
  343. entry->next->prev = entry->prev;
  344. entry->prev->next = entry->next;
  345. entry->next = nullptr;
  346. entry->prev = nullptr;
  347. _deallocate(data);
  348. }
  349. T _get(ListHead* const entry, T& fallback, const bool removeObj) noexcept
  350. {
  351. Data* const data(list_entry(entry, Data, siblings));
  352. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  353. if (! removeObj)
  354. return data->value;
  355. const T value(data->value);
  356. _delete(entry, data);
  357. return value;
  358. }
  359. T& _get(ListHead* const entry, T& fallback) const noexcept
  360. {
  361. Data* const data(list_entry(entry, Data, siblings));
  362. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  363. return data->value;
  364. }
  365. const T& _get(const ListHead* const entry, const T& fallback) const noexcept
  366. {
  367. const Data* const data(list_entry_const(entry, Data, siblings));
  368. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  369. return data->value;
  370. }
  371. static void __list_splice(ListHead* const list, ListHead* const head) noexcept
  372. {
  373. ListHead* const first = list->next;
  374. ListHead* const last = list->prev;
  375. ListHead* const at = head->next;
  376. first->prev = head;
  377. head->next = first;
  378. last->next = at;
  379. at->prev = last;
  380. }
  381. static void __list_splice_tail(ListHead* const list, ListHead* const head) noexcept
  382. {
  383. ListHead* const first = list->next;
  384. ListHead* const last = list->prev;
  385. ListHead* const at = head->prev;
  386. first->prev = at;
  387. at->next = first;
  388. last->next = head;
  389. head->prev = last;
  390. }
  391. template<typename> friend class RtLinkedList;
  392. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  393. CARLA_DECLARE_NON_COPY_CLASS(AbstractLinkedList)
  394. };
  395. // -----------------------------------------------------------------------
  396. // LinkedList
  397. template<typename T>
  398. class LinkedList : public AbstractLinkedList<T>
  399. {
  400. public:
  401. LinkedList() noexcept {}
  402. protected:
  403. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  404. {
  405. return (typename AbstractLinkedList<T>::Data*)std::malloc(this->kDataSize);
  406. }
  407. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  408. {
  409. std::free(dataPtr);
  410. }
  411. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  412. CARLA_DECLARE_NON_COPY_CLASS(LinkedList)
  413. };
  414. // -----------------------------------------------------------------------
  415. #endif // LINKED_LIST_HPP_INCLUDED