Array.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. #pragma once
  2. #include <functional>
  3. #include <stdexcept>
  4. #include "Errors.h"
  5. #include "ReferenceCounter.h"
  6. #include "Stream.h"
  7. #include "Supplier.h"
  8. #include "Text.h"
  9. namespace Framework
  10. {
  11. template<class TYP>
  12. //! An entry in a linked list
  13. struct ArrayEntry
  14. {
  15. TYP var;
  16. bool set;
  17. ArrayEntry<TYP>* next;
  18. ArrayEntry()
  19. : var(),
  20. set(0),
  21. next(0)
  22. {}
  23. //! Sets the entry to the values of the other entry
  24. ArrayEntry& operator=(ArrayEntry& r)
  25. {
  26. var = r.var;
  27. set = r.set;
  28. next = r.next;
  29. return *this;
  30. }
  31. //! Returns the currently stored value
  32. operator TYP()
  33. {
  34. if (!set)
  35. {
  36. Text err = "Index out of Range Exception File: ";
  37. err += __FILE__;
  38. err += " Line: ";
  39. err += __LINE__;
  40. throw std::out_of_range(err);
  41. }
  42. return var;
  43. }
  44. //! Increments through the linked list
  45. ArrayEntry<TYP>& operator++() //! prefix
  46. {
  47. if (!next)
  48. {
  49. ArrayEntry<TYP> tmp;
  50. tmp.set = 0;
  51. tmp.next = 0;
  52. *this = tmp;
  53. return *this;
  54. }
  55. *this = *next;
  56. return *next;
  57. }
  58. //! Increments through the linked list
  59. ArrayEntry<TYP>& operator++(int) //! postfix
  60. {
  61. if (!next)
  62. {
  63. ArrayEntry<TYP> tmp;
  64. tmp.set = 0;
  65. tmp.next = 0;
  66. *this = tmp;
  67. return *this;
  68. }
  69. *this = *next;
  70. return *next;
  71. }
  72. #ifdef WIN32
  73. # pragma warning(once : 26495)
  74. };
  75. #else
  76. };
  77. #endif
  78. template<class TYP> class ArrayIterator
  79. : public Iterator<TYP, ArrayIterator<TYP>>
  80. {
  81. private:
  82. ArrayEntry<TYP>* current;
  83. const std::function<void(TYP& removed)>* onRemove;
  84. const std::function<void(int change)>* onSizeChanged;
  85. const std::function<void(ArrayEntry<TYP>* last)>* onLastChanged;
  86. public:
  87. ArrayIterator(ArrayEntry<TYP>* start,
  88. const std::function<void(TYP& removed)>* onRemove,
  89. const std::function<void(int change)>* onSizeChanged,
  90. const std::function<void(ArrayEntry<TYP>* last)>* onLastChanged)
  91. {
  92. this->onRemove = onRemove;
  93. this->onSizeChanged = onSizeChanged;
  94. this->onLastChanged = onLastChanged;
  95. current = start;
  96. while (current && !current->set)
  97. {
  98. current = current->next;
  99. }
  100. }
  101. ArrayIterator(const ArrayIterator& it)
  102. {
  103. onRemove = it.onRemove;
  104. onSizeChanged = it.onSizeChanged;
  105. onLastChanged = it.onLastChanged;
  106. current = it.current;
  107. }
  108. virtual ~ArrayIterator()
  109. {
  110. current = 0;
  111. }
  112. ArrayIterator<TYP>& operator=(ArrayIterator<TYP> r)
  113. {
  114. onRemove = r.onRemove;
  115. onSizeChanged = r.onSizeChanged;
  116. onLastChanged = r.onLastChanged;
  117. current = r.current;
  118. return *this;
  119. }
  120. bool hasNext() override
  121. {
  122. ArrayEntry<TYP>* next = current->next;
  123. while (next && !next->set)
  124. {
  125. next = next->next;
  126. }
  127. return next != 0;
  128. }
  129. ArrayIterator<TYP> next() override
  130. {
  131. if (!current)
  132. {
  133. Text err = "Index out of Range Exception File: ";
  134. err += __FILE__;
  135. err += " Line: ";
  136. err += __LINE__;
  137. throw std::out_of_range(err);
  138. }
  139. return ArrayIterator(
  140. current->next, onRemove, onSizeChanged, onLastChanged);
  141. }
  142. operator bool() override
  143. {
  144. return current != 0 && current->set;
  145. }
  146. ArrayIterator<TYP>& operator++() override //! prefix
  147. {
  148. do
  149. {
  150. if (current) current = current->next;
  151. } while (current && !current->set);
  152. return *this;
  153. }
  154. ArrayIterator<TYP> operator++(int) override //! postfix
  155. {
  156. ArrayIterator<TYP> temp(*this);
  157. do
  158. {
  159. if (current) current = current->next;
  160. } while (current && !current->set);
  161. return temp;
  162. }
  163. TYP val() override
  164. {
  165. #ifdef _DEBUG
  166. if (!current || !current->set)
  167. {
  168. Text err = "Index out of Range Exception File: ";
  169. err += __FILE__;
  170. err += " Line: ";
  171. err += __LINE__;
  172. throw std::out_of_range(err);
  173. }
  174. #endif
  175. return current->var;
  176. }
  177. void addBefore(TYP val) override
  178. {
  179. if (current)
  180. {
  181. ArrayEntry<TYP>* newEntry = new ArrayEntry<TYP>();
  182. newEntry->var = current->var;
  183. newEntry->set = current->set;
  184. newEntry->next = current->next;
  185. if (!current->next && onLastChanged)
  186. {
  187. (*onLastChanged)(newEntry);
  188. }
  189. current->var = val;
  190. current->set = true;
  191. current->next = newEntry;
  192. if (onSizeChanged)
  193. {
  194. (*onSizeChanged)(1);
  195. }
  196. }
  197. else
  198. {
  199. Text err = "Index out of Range Exception File: ";
  200. err += __FILE__;
  201. err += " Line: ";
  202. err += __LINE__;
  203. throw std::out_of_range(err);
  204. }
  205. }
  206. void set(TYP val) override
  207. {
  208. if (current)
  209. {
  210. if (onRemove) (*onRemove)(current->var);
  211. current->var = val;
  212. current->set = true;
  213. }
  214. else
  215. {
  216. Text err = "Index out of Range Exception File: ";
  217. err += __FILE__;
  218. err += " Line: ";
  219. err += __LINE__;
  220. throw std::out_of_range(err);
  221. }
  222. }
  223. bool operator!=(ArrayIterator<TYP>& r)
  224. {
  225. return current != r.current;
  226. }
  227. bool operator==(ArrayIterator<TYP>& r)
  228. {
  229. return current == r.current;
  230. }
  231. void remove() override
  232. {
  233. if (!current)
  234. {
  235. Text err = "Index out of Range Exception File: ";
  236. err += __FILE__;
  237. err += " Line: ";
  238. err += __LINE__;
  239. throw std::out_of_range(err);
  240. }
  241. if (current->next)
  242. {
  243. if (onRemove) (*onRemove)(current->var);
  244. current->var = current->next->var;
  245. current->set = current->next->set;
  246. }
  247. else
  248. {
  249. if (onRemove) (*onRemove)(current->var);
  250. current->set = 0;
  251. }
  252. ArrayEntry<TYP>* del = current->next;
  253. if (current->next)
  254. current->next = current->next->next;
  255. else
  256. {
  257. if (onLastChanged)
  258. {
  259. (*onLastChanged)(current);
  260. }
  261. current->next = 0;
  262. }
  263. if (del)
  264. {
  265. del->set = 0;
  266. del->next = 0;
  267. delete del;
  268. }
  269. if (onSizeChanged)
  270. {
  271. (*onSizeChanged)(-1);
  272. }
  273. }
  274. };
  275. #define _ val()
  276. template<class TYP>
  277. //! A linked list of classes that do not use reference counting
  278. class Array : public virtual ReferenceCounter
  279. {
  280. private:
  281. ArrayEntry<TYP>* entries;
  282. ArrayEntry<TYP>* last;
  283. int count;
  284. std::function<void(int change)> onSizeChanged;
  285. std::function<void(ArrayEntry<TYP>* last)> onLastChanged;
  286. public:
  287. //! Creates a new linked list
  288. Array() noexcept
  289. : ReferenceCounter()
  290. {
  291. entries = new ArrayEntry<TYP>();
  292. entries->set = 0;
  293. entries->next = 0;
  294. last = entries;
  295. count = 0;
  296. onSizeChanged = [this](int change) { this->count += change; };
  297. onLastChanged
  298. = [this](ArrayEntry<TYP>* last) { this->last = last; };
  299. }
  300. //! Copies a linked list
  301. //!
  302. Array(const Array& arr)
  303. : Array()
  304. {
  305. int anz = arr.getEntryCount();
  306. for (int i = 0; i < anz; i++)
  307. add(arr.get(i));
  308. }
  309. //! Clears and deletes the linked list
  310. ~Array()
  311. {
  312. clear();
  313. delete entries;
  314. }
  315. //! Appends an element to the end of the list
  316. //! \param t The new element
  317. void add(TYP t)
  318. {
  319. if (!last->set)
  320. {
  321. last->var = t;
  322. last->set = 1;
  323. count++;
  324. return;
  325. }
  326. last->next = new ArrayEntry<TYP>();
  327. last = last->next;
  328. last->set = 1;
  329. last->var = t;
  330. count++;
  331. }
  332. //! Inserts an element at a specific position in the list
  333. //! \param t The new element
  334. //! \param i The position where the element is inserted (afterwards the
  335. //! index of the new element)
  336. void add(TYP t, int i)
  337. {
  338. if (i < 0 || i > count)
  339. throwOutOfRange(__FILE__, __LINE__, i, count);
  340. if (i == count)
  341. {
  342. add(t);
  343. return;
  344. }
  345. ArrayEntry<TYP>* e = entries;
  346. for (int a = 0; a < i; ++a)
  347. e = e->next;
  348. ArrayEntry<TYP>* ne = new ArrayEntry<TYP>();
  349. ne->var = e->var;
  350. ne->set = e->set;
  351. ne->next = e->next;
  352. e->next = ne;
  353. e->var = t;
  354. e->set = 1;
  355. if (last->next) last = last->next;
  356. count++;
  357. }
  358. //! Sets the value of the i-th entry
  359. //! \param t The new value
  360. //! \param i The index of the entry to be set
  361. void set(TYP t, int i)
  362. {
  363. if (i < 0 || i >= count)
  364. throwOutOfRange(__FILE__, __LINE__, i, count);
  365. ArrayEntry<TYP>* e = entries;
  366. for (int a = 0; a < i; ++a)
  367. e = e->next;
  368. e->var = t;
  369. e->set = 1;
  370. }
  371. //! Changes the position of the i-th element in the list
  372. //! \param i The index of the element to be moved
  373. //! \param p The target position of the element (afterwards the new
  374. //! index of the element)
  375. void setPosition(int i, int p)
  376. {
  377. if (i == p) return;
  378. if (i < 0 || p < 0 || i >= count || p >= count)
  379. throwOutOfRange(__FILE__, __LINE__, i, count);
  380. TYP t = get(i);
  381. remove(i);
  382. add(t, p);
  383. }
  384. //! Deletes a specific element
  385. //! \param i The index of the element to be deleted
  386. void remove(int i)
  387. {
  388. if (i < 0 || i >= count)
  389. throwOutOfRange(__FILE__, __LINE__, i, count);
  390. ArrayEntry<TYP>* e = entries;
  391. for (int a = 0; a < i; ++a)
  392. e = e->next;
  393. if (e->next)
  394. {
  395. e->var = e->next->var;
  396. e->set = e->next->set;
  397. }
  398. else
  399. e->set = 0;
  400. ArrayEntry<TYP>* del = e->next;
  401. if (e->next)
  402. e->next = e->next->next;
  403. else
  404. e->next = 0;
  405. if (del)
  406. {
  407. del->set = 0;
  408. del->next = 0;
  409. if (last == del) last = e;
  410. delete del;
  411. }
  412. count--;
  413. }
  414. //! Deletes a specific element by value
  415. //! \param value The value of the element to be deleted
  416. void removeValue(TYP value)
  417. {
  418. ArrayEntry<TYP>* e = entries;
  419. while (e->var != value)
  420. {
  421. if (!e->next) return;
  422. e = e->next;
  423. }
  424. if (e->next)
  425. {
  426. e->var = e->next->var;
  427. e->set = e->next->set;
  428. }
  429. else
  430. e->set = 0;
  431. ArrayEntry<TYP>* del = e->next;
  432. if (e->next)
  433. e->next = e->next->next;
  434. else
  435. e->next = 0;
  436. if (del)
  437. {
  438. del->set = 0;
  439. del->next = 0;
  440. if (last == del) last = e;
  441. delete del;
  442. }
  443. count--;
  444. }
  445. //! Swaps two elements in the list
  446. //! \param vi The index of the first element
  447. //! \param ni The index of the second element
  448. void swap(int vi, int ni)
  449. {
  450. TYP tmp = get(ni);
  451. set(get(vi), ni);
  452. set(tmp, vi);
  453. }
  454. //! Deletes all elements of the list
  455. void clear()
  456. {
  457. ArrayEntry<TYP>* e2 = 0;
  458. for (ArrayEntry<TYP>* e = entries->next; e; e = e->next)
  459. {
  460. delete e2;
  461. e2 = e;
  462. }
  463. delete e2;
  464. entries->set = 0;
  465. entries->next = 0;
  466. last = entries;
  467. count = 0;
  468. }
  469. //! Returns an iterator.
  470. //! Use ++ to iterate through the list
  471. ArrayIterator<TYP> begin() const
  472. {
  473. return ArrayIterator<TYP>(
  474. entries, 0, &onSizeChanged, &onLastChanged);
  475. }
  476. ArrayIterator<TYP> end() const
  477. {
  478. return ArrayIterator<TYP>(0, 0, &onSizeChanged, &onLastChanged);
  479. }
  480. //! Returns how many elements are in the list
  481. int getEntryCount() const
  482. {
  483. return count;
  484. }
  485. //! Returns the value of the i-th element
  486. //! \param i The index of the sought element
  487. //! throws:
  488. //! \param std:out_of_range if i < 0 or i >= getEntryCount()
  489. TYP get(int i) const
  490. {
  491. if (i < 0 || i >= count)
  492. throwOutOfRange(__FILE__, __LINE__, i, count);
  493. ArrayEntry<TYP>* e = entries;
  494. for (int a = 0; a < i && e; ++a)
  495. e = e->next;
  496. return e->var;
  497. }
  498. //! Checks whether an element exists in the list
  499. //! \param i The index of the sought element
  500. //! \return (true) if the index exists. (false) otherwise
  501. bool has(int i) const
  502. {
  503. return i >= 0 && i < count;
  504. }
  505. //! Returns the index of a value
  506. //! \param t The value to search for
  507. int getValueIndex(TYP t) const
  508. {
  509. int ret = 0;
  510. for (ArrayEntry<TYP>* e = entries; e; e = e->next)
  511. {
  512. if (e->set && e->var == t) return ret;
  513. ++ret;
  514. }
  515. return -1;
  516. }
  517. bool anyMatch(std::function<bool(const TYP element)> predicate) const
  518. {
  519. for (TYP t : *this)
  520. {
  521. if (predicate(t)) return 1;
  522. }
  523. return 0;
  524. }
  525. bool allMatch(std::function<bool(const TYP element)> predicate) const
  526. {
  527. for (TYP t : *this)
  528. {
  529. if (!predicate(t)) return 0;
  530. }
  531. return 1;
  532. }
  533. int findIndex(std::function<bool(const TYP element)> predicate) const
  534. {
  535. int index = 0;
  536. for (TYP t : *this)
  537. {
  538. if (predicate(t)) return index;
  539. index++;
  540. }
  541. return -1;
  542. }
  543. Array& operator=(const Array& arr)
  544. {
  545. clear();
  546. int anz = arr.getEntryCount();
  547. for (int i = 0; i < anz; i++)
  548. add(arr.get(i));
  549. return *this;
  550. }
  551. Stream<TYP> stream()
  552. {
  553. return Stream<TYP>(new IteratorSupplier<TYP>(new ArrayIterator<TYP>(
  554. entries, 0, &onSizeChanged, &onLastChanged)));
  555. }
  556. };
  557. template<class TYP>
  558. //! A linked list of pointers to objects that use reference counting
  559. class RCArray : public virtual ReferenceCounter
  560. {
  561. private:
  562. ArrayEntry<TYP*>* entries;
  563. ArrayEntry<TYP*>* last;
  564. int count;
  565. std::function<void(TYP*& removed)> onRemove;
  566. std::function<void(int change)> onSizeChanged;
  567. std::function<void(ArrayEntry<TYP*>* last)> onLastChanged;
  568. public:
  569. //! Creates a new linked list
  570. RCArray() noexcept
  571. : ReferenceCounter()
  572. {
  573. entries = new ArrayEntry<TYP*>();
  574. entries->var = 0;
  575. entries->set = 0;
  576. entries->next = 0;
  577. last = entries;
  578. count = 0;
  579. onRemove = [](TYP*& removed) { removed->release(); };
  580. onSizeChanged = [this](int change) { this->count += change; };
  581. onLastChanged
  582. = [this](ArrayEntry<TYP*>* last) { this->last = last; };
  583. }
  584. //! Copies a linked list
  585. RCArray(const RCArray& arr)
  586. : RCArray()
  587. {
  588. int anz = arr.getEntryCount();
  589. for (int i = 0; i < anz; i++)
  590. add(arr.get(i));
  591. }
  592. //! Clears and deletes the linked list
  593. ~RCArray()
  594. {
  595. clear();
  596. delete entries;
  597. }
  598. //! Appends an element to the end of the list
  599. //! \param t The new element
  600. void add(TYP* t)
  601. {
  602. count++;
  603. if (!last->set)
  604. {
  605. last->var = t;
  606. last->set = 1;
  607. return;
  608. }
  609. last->next = new ArrayEntry<TYP*>();
  610. last = last->next;
  611. last->var = t;
  612. last->set = 1;
  613. }
  614. //! Inserts an element at a specific position in the list
  615. //! \param t The new element
  616. //! \param i The position where the element is inserted (afterwards the
  617. //! index of the new element)
  618. void add(TYP* t, int i)
  619. {
  620. if (i < 0 || i > count)
  621. throwOutOfRange(__FILE__, __LINE__, i, count);
  622. if (i == count)
  623. {
  624. add(t);
  625. return;
  626. }
  627. ArrayEntry<TYP*>* e = entries;
  628. for (int a = 0; a < i; ++a)
  629. e = e->next;
  630. ArrayEntry<TYP*>* ne = new ArrayEntry<TYP*>();
  631. ne->var = e->var;
  632. ne->set = e->set;
  633. ne->next = e->next;
  634. e->next = ne;
  635. e->var = t;
  636. e->set = 1;
  637. if (last->next) last = last->next;
  638. count++;
  639. }
  640. //! Sets the value of the i-th entry
  641. //! \param t The new value
  642. //! \param i The index of the entry to be set
  643. void set(TYP* t, int i)
  644. {
  645. if (i < 0 || i >= count)
  646. throwOutOfRange(__FILE__, __LINE__, i, count);
  647. ArrayEntry<TYP*>* e = entries;
  648. for (int a = 0; a < i; ++a)
  649. e = e->next;
  650. if (e->set && e->var) e->var->release();
  651. e->var = t;
  652. e->set = 1;
  653. }
  654. //! Changes the position of the i-th element in the list
  655. //! \param i The index of the element to be moved
  656. //! \param p The target position of the element (afterwards the new
  657. //! index of the element)
  658. void setPosition(int i, int p)
  659. {
  660. if (i == p) return;
  661. if (i < 0 || p < 0 || i >= count || p >= count)
  662. throwOutOfRange(__FILE__, __LINE__, i, count);
  663. TYP* t = get(i);
  664. remove(i);
  665. add(t, p);
  666. }
  667. //! Deletes a specific element
  668. //! \param i The index of the element to be deleted
  669. void remove(int i)
  670. {
  671. if (i < 0 || i >= count)
  672. throwOutOfRange(__FILE__, __LINE__, i, count);
  673. ArrayEntry<TYP*>* e = entries;
  674. for (int a = 0; a < i; ++a)
  675. e = e->next;
  676. if (e->next)
  677. {
  678. if (e->set && e->var) e->var->release();
  679. e->var = e->next->var;
  680. e->set = e->next->set;
  681. }
  682. else
  683. {
  684. if (e->set && e->var) e->var->release();
  685. e->set = 0;
  686. }
  687. ArrayEntry<TYP*>* del = e->next;
  688. if (e->next)
  689. e->next = e->next->next;
  690. else
  691. e->next = 0;
  692. if (del)
  693. {
  694. del->set = 0;
  695. del->next = 0;
  696. if (last == del) last = e;
  697. delete del;
  698. }
  699. count--;
  700. }
  701. //! Swaps two elements in the list
  702. //! \param vi The index of the first element
  703. //! \param ni The index of the second element
  704. void swap(int vi, int ni)
  705. {
  706. if (vi < 0 || ni < 0) return;
  707. TYP* tmp = get(ni);
  708. set(get(vi), ni);
  709. set(tmp, vi);
  710. }
  711. //! Deletes all elements of the list
  712. void clear()
  713. {
  714. for (ArrayEntry<TYP*>* e = entries->next; e;)
  715. {
  716. if (e && e->var && e->set) e->var->release();
  717. auto tmp = e->next;
  718. delete e;
  719. e = tmp;
  720. }
  721. if (entries->var && entries->set) entries->var->release();
  722. entries->set = 0;
  723. entries->next = 0;
  724. last = entries;
  725. count = 0;
  726. }
  727. //! Returns an iterator.
  728. //! Use ++ to iterate through the list
  729. ArrayIterator<TYP*> begin() const
  730. {
  731. return ArrayIterator<TYP*>(
  732. entries, &onRemove, &onSizeChanged, &onLastChanged);
  733. }
  734. ArrayIterator<TYP*> end() const
  735. {
  736. return ArrayIterator<TYP*>(
  737. 0, &onRemove, &onSizeChanged, &onLastChanged);
  738. }
  739. //! Returns how many elements are in the list
  740. int getEntryCount() const
  741. {
  742. return count;
  743. }
  744. int getLastIndex() const
  745. {
  746. return count - 1;
  747. }
  748. //! Returns the value of the i-th element with increased reference
  749. //! counter \param i The index of the sought element, (0) if the
  750. //! index does not exist
  751. TYP* get(int i) const
  752. {
  753. if (i < 0 || i >= count)
  754. throwOutOfRange(__FILE__, __LINE__, i, count);
  755. ArrayEntry<TYP*>* e = entries;
  756. for (int a = 0; a < i && e; ++a)
  757. e = e->next;
  758. if (e && e->set && e->var)
  759. return dynamic_cast<TYP*>(e->var->getThis());
  760. return (TYP*)0;
  761. }
  762. //! Returns the value of the i-th element without increased reference
  763. //! counter \param i The index of the sought element, (0) if the
  764. //! index does not exist
  765. TYP* z(int i) const //! returns the index-th T
  766. {
  767. if (i < 0 || i >= count)
  768. throwOutOfRange(__FILE__, __LINE__, i, count);
  769. ArrayEntry<TYP*>* e = entries;
  770. for (int a = 0; a < i && e; ++a)
  771. e = e->next;
  772. if (e && e->set && e->var) return (TYP*)e->var;
  773. return (TYP*)0;
  774. }
  775. //! Checks whether an element exists in the list
  776. //! \param i The index of the sought element
  777. //! \return (true) if the index exists. (false) otherwise
  778. bool has(int i) const
  779. {
  780. return i >= 0 && i < count;
  781. }
  782. //! returns the index of the first element that matches zT or -1 if not
  783. //! found
  784. int indexOf(TYP* zT) const
  785. {
  786. int i = 0;
  787. for (TYP* t : *this)
  788. {
  789. if (t == zT) return i;
  790. ++i;
  791. }
  792. return -1;
  793. }
  794. bool anyMatch(std::function<bool(const TYP* zElement)> predicate) const
  795. {
  796. for (TYP* t : *this)
  797. {
  798. if (predicate(t)) return 1;
  799. }
  800. return 0;
  801. }
  802. bool allMatch(std::function<bool(const TYP* zElement)> predicate) const
  803. {
  804. for (TYP* t : *this)
  805. {
  806. if (!predicate(t)) return 0;
  807. }
  808. return 1;
  809. }
  810. int findIndex(std::function<bool(const TYP* zElement)> predicate) const
  811. {
  812. int index = 0;
  813. for (TYP* t : *this)
  814. {
  815. if (predicate(t)) return index;
  816. index++;
  817. }
  818. return -1;
  819. }
  820. RCArray& operator=(const RCArray& arr)
  821. {
  822. clear();
  823. int anz = arr.getEntryCount();
  824. for (int i = 0; i < anz; i++)
  825. add(arr.get(i));
  826. return *this;
  827. }
  828. Stream<TYP*> stream()
  829. {
  830. return Stream<TYP*>(
  831. new IteratorSupplier<TYP*>(new ArrayIterator<TYP*>(
  832. entries, &onRemove, &onSizeChanged, &onLastChanged)));
  833. }
  834. };
  835. } // namespace Framework