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 15KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. T& operator*() noexcept
  136. {
  137. static T& fallback(_getFallback());
  138. Data* const data(list_entry(fEntry, Data, siblings));
  139. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  140. return data->value;
  141. }
  142. const T& operator*() const noexcept
  143. {
  144. static const T& fallback(_getFallback());
  145. const Data* const data(list_entry_const(fEntry, Data, siblings));
  146. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  147. return data->value;
  148. }
  149. private:
  150. ListHead* fEntry;
  151. ListHead* fEntry2;
  152. static T& _getFallback()
  153. {
  154. static T data;
  155. carla_zeroStruct(data);
  156. return data;
  157. }
  158. };
  159. Itenerator begin2() const noexcept
  160. {
  161. return Itenerator(fQueue);
  162. }
  163. AutoItenerator begin() const noexcept
  164. {
  165. return AutoItenerator(fQueue.next);
  166. }
  167. AutoItenerator end() const noexcept
  168. {
  169. return AutoItenerator(&fQueue);
  170. }
  171. void clear() noexcept
  172. {
  173. if (fCount == 0)
  174. return;
  175. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  176. {
  177. Data* const data(list_entry(entry, Data, siblings));
  178. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  179. _deallocate(data);
  180. }
  181. _init();
  182. }
  183. std::size_t count() const noexcept
  184. {
  185. return fCount;
  186. }
  187. bool isEmpty() const noexcept
  188. {
  189. return (fCount == 0);
  190. }
  191. bool append(const T& value) noexcept
  192. {
  193. return _add(value, true, &fQueue);
  194. }
  195. bool appendAt(const T& value, const Itenerator& it) noexcept
  196. {
  197. return _add(value, true, it.fEntry->next);
  198. }
  199. bool insert(const T& value) noexcept
  200. {
  201. return _add(value, false, &fQueue);
  202. }
  203. bool insertAt(const T& value, const Itenerator& it) noexcept
  204. {
  205. return _add(value, false, it.fEntry->prev);
  206. }
  207. T getAt(const std::size_t index, T& fallback, const bool removeObj) noexcept
  208. {
  209. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  210. std::size_t i = 0;
  211. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  212. {
  213. if (index != i++)
  214. continue;
  215. return _get(entry, fallback, removeObj);
  216. }
  217. return fallback;
  218. }
  219. T& getAt(const std::size_t index, T& fallback) const noexcept
  220. {
  221. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  222. std::size_t i = 0;
  223. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  224. {
  225. if (index != i++)
  226. continue;
  227. return _get(entry, fallback);
  228. }
  229. return fallback;
  230. }
  231. const T& getAt(const std::size_t index, const T& fallback) const noexcept
  232. {
  233. CARLA_SAFE_ASSERT_RETURN(fCount > 0 && index < fCount, fallback);
  234. std::size_t i = 0;
  235. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  236. {
  237. if (index != i++)
  238. continue;
  239. return _get(entry, fallback);
  240. }
  241. return fallback;
  242. }
  243. T getFirst(T& fallback, const bool removeObj) noexcept
  244. {
  245. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  246. return _get(fQueue.next, fallback, removeObj);
  247. }
  248. T& getFirst(T& fallback) const noexcept
  249. {
  250. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  251. return _get(fQueue.next, fallback);
  252. }
  253. const T& getFirst(const T& fallback) const noexcept
  254. {
  255. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  256. return _get(fQueue.next, fallback);
  257. }
  258. T getLast(T& fallback, const bool removeObj) noexcept
  259. {
  260. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  261. return _get(fQueue.prev, fallback, removeObj);
  262. }
  263. T& getLast(T& fallback) const noexcept
  264. {
  265. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  266. return _get(fQueue.prev, fallback);
  267. }
  268. const T& getLast(const T& fallback) const noexcept
  269. {
  270. CARLA_SAFE_ASSERT_RETURN(fCount > 0, fallback);
  271. return _get(fQueue.prev, fallback);
  272. }
  273. void remove(Itenerator& it) noexcept
  274. {
  275. CARLA_SAFE_ASSERT_RETURN(it.fEntry != nullptr,);
  276. Data* const data(list_entry(it.fEntry, Data, siblings));
  277. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  278. _delete(it.fEntry, data);
  279. }
  280. bool removeOne(const T& value) noexcept
  281. {
  282. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  283. {
  284. Data* const data = list_entry(entry, Data, siblings);
  285. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  286. if (data->value != value)
  287. continue;
  288. _delete(entry, data);
  289. return true;
  290. }
  291. return false;
  292. }
  293. void removeAll(const T& value) noexcept
  294. {
  295. for (ListHead *entry = fQueue.next, *entry2 = entry->next; entry != &fQueue; entry = entry2, entry2 = entry->next)
  296. {
  297. Data* const data = list_entry(entry, Data, siblings);
  298. CARLA_SAFE_ASSERT_CONTINUE(data != nullptr);
  299. if (data->value != value)
  300. continue;
  301. _delete(entry, data);
  302. }
  303. }
  304. // move data to a new list, and clear ourselves
  305. bool moveTo(AbstractLinkedList<T>& list, const bool inTail = true) noexcept
  306. {
  307. CARLA_SAFE_ASSERT_RETURN(fCount > 0, false);
  308. if (inTail)
  309. __list_splice_tail(&fQueue, &list.fQueue);
  310. else
  311. __list_splice(&fQueue, &list.fQueue);
  312. //! @a list gets our items
  313. list.fCount += fCount;
  314. //! and we get nothing
  315. _init();
  316. return true;
  317. }
  318. protected:
  319. const std::size_t kDataSize;
  320. ListHead fQueue;
  321. std::size_t fCount;
  322. virtual Data* _allocate() noexcept = 0;
  323. virtual void _deallocate(Data* const dataPtr) noexcept = 0;
  324. private:
  325. void _init() noexcept
  326. {
  327. fCount = 0;
  328. fQueue.next = &fQueue;
  329. fQueue.prev = &fQueue;
  330. }
  331. bool _add(const T& value, const bool inTail, ListHead* const queue) noexcept
  332. {
  333. if (Data* const data = _allocate())
  334. return _add_internal(data, value, inTail, queue);
  335. return false;
  336. }
  337. bool _add_internal(Data* const data, const T& value, const bool inTail, ListHead* const queue) noexcept
  338. {
  339. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  340. CARLA_SAFE_ASSERT_RETURN(queue != nullptr, false);
  341. CARLA_SAFE_ASSERT_RETURN(queue->prev != nullptr, false);
  342. CARLA_SAFE_ASSERT_RETURN(queue->next != nullptr, false);
  343. data->value = value;
  344. ListHead* const siblings(&data->siblings);
  345. if (inTail)
  346. {
  347. siblings->prev = queue->prev;
  348. siblings->next = queue;
  349. queue->prev->next = siblings;
  350. queue->prev = siblings;
  351. }
  352. else
  353. {
  354. siblings->prev = queue;
  355. siblings->next = queue->next;
  356. queue->next->prev = siblings;
  357. queue->next = siblings;
  358. }
  359. ++fCount;
  360. return true;
  361. }
  362. void _delete(ListHead* const entry, Data* const data) noexcept
  363. {
  364. CARLA_SAFE_ASSERT_RETURN(entry != nullptr,);
  365. CARLA_SAFE_ASSERT_RETURN(entry->prev != nullptr,);
  366. CARLA_SAFE_ASSERT_RETURN(entry->next != nullptr,);
  367. --fCount;
  368. entry->next->prev = entry->prev;
  369. entry->prev->next = entry->next;
  370. entry->next = nullptr;
  371. entry->prev = nullptr;
  372. _deallocate(data);
  373. }
  374. T _get(ListHead* const entry, T& fallback, const bool removeObj) noexcept
  375. {
  376. Data* const data(list_entry(entry, Data, siblings));
  377. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  378. if (! removeObj)
  379. return data->value;
  380. const T value(data->value);
  381. _delete(entry, data);
  382. return value;
  383. }
  384. T& _get(ListHead* const entry, T& fallback) const noexcept
  385. {
  386. Data* const data(list_entry(entry, Data, siblings));
  387. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  388. return data->value;
  389. }
  390. const T& _get(ListHead* const entry, const T& fallback) const noexcept
  391. {
  392. const Data* const data(list_entry_const(entry, Data, siblings));
  393. CARLA_SAFE_ASSERT_RETURN(data != nullptr, fallback);
  394. return data->value;
  395. }
  396. static void __list_splice(ListHead* const list, ListHead* const head) noexcept
  397. {
  398. ListHead* const first = list->next;
  399. ListHead* const last = list->prev;
  400. ListHead* const at = head->next;
  401. first->prev = head;
  402. head->next = first;
  403. last->next = at;
  404. at->prev = last;
  405. }
  406. static void __list_splice_tail(ListHead* const list, ListHead* const head) noexcept
  407. {
  408. ListHead* const first = list->next;
  409. ListHead* const last = list->prev;
  410. ListHead* const at = head->prev;
  411. first->prev = at;
  412. at->next = first;
  413. last->next = head;
  414. head->prev = last;
  415. }
  416. template<typename> friend class RtLinkedList;
  417. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  418. CARLA_DECLARE_NON_COPY_CLASS(AbstractLinkedList)
  419. };
  420. // -----------------------------------------------------------------------
  421. // LinkedList
  422. template<typename T>
  423. class LinkedList : public AbstractLinkedList<T>
  424. {
  425. public:
  426. LinkedList() noexcept {}
  427. protected:
  428. typename AbstractLinkedList<T>::Data* _allocate() noexcept override
  429. {
  430. return (typename AbstractLinkedList<T>::Data*)std::malloc(this->kDataSize);
  431. }
  432. void _deallocate(typename AbstractLinkedList<T>::Data* const dataPtr) noexcept override
  433. {
  434. std::free(dataPtr);
  435. }
  436. CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
  437. CARLA_DECLARE_NON_COPY_CLASS(LinkedList)
  438. };
  439. // -----------------------------------------------------------------------
  440. #endif // LINKED_LIST_HPP_INCLUDED