Array.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. && ((!current || !current->set)
  227. != (!r.current || !r.current->set));
  228. }
  229. bool operator==(ArrayIterator<TYP>& r)
  230. {
  231. return current == r.current
  232. || ((!current || !current->set)
  233. == (!r.current || !r.current->set));
  234. }
  235. void remove() override
  236. {
  237. if (!current)
  238. {
  239. Text err = "Index out of Range Exception File: ";
  240. err += __FILE__;
  241. err += " Line: ";
  242. err += __LINE__;
  243. throw std::out_of_range(err);
  244. }
  245. if (current->next)
  246. {
  247. if (onRemove) (*onRemove)(current->var);
  248. current->var = current->next->var;
  249. current->set = current->next->set;
  250. }
  251. else
  252. {
  253. if (onRemove) (*onRemove)(current->var);
  254. current->set = 0;
  255. }
  256. ArrayEntry<TYP>* del = current->next;
  257. if (current->next)
  258. {
  259. if (current->next->next)
  260. current->next = current->next->next;
  261. else
  262. {
  263. if (onLastChanged)
  264. {
  265. (*onLastChanged)(current);
  266. }
  267. current->next = 0;
  268. }
  269. }
  270. if (del)
  271. {
  272. del->set = 0;
  273. del->next = 0;
  274. delete del;
  275. }
  276. if (onSizeChanged)
  277. {
  278. (*onSizeChanged)(-1);
  279. }
  280. }
  281. };
  282. #define _ val()
  283. template<class TYP>
  284. //! A linked list of classes that do not use reference counting
  285. class Array : public virtual ReferenceCounter
  286. {
  287. private:
  288. ArrayEntry<TYP>* entries;
  289. ArrayEntry<TYP>* last;
  290. int count;
  291. std::function<void(int change)> onSizeChanged;
  292. std::function<void(ArrayEntry<TYP>* last)> onLastChanged;
  293. public:
  294. //! Creates a new linked list
  295. Array() noexcept
  296. : ReferenceCounter()
  297. {
  298. entries = new ArrayEntry<TYP>();
  299. entries->set = 0;
  300. entries->next = 0;
  301. last = entries;
  302. count = 0;
  303. onSizeChanged = [this](int change) { this->count += change; };
  304. onLastChanged
  305. = [this](ArrayEntry<TYP>* last) { this->last = last; };
  306. }
  307. //! Copies a linked list
  308. //!
  309. Array(const Array& arr)
  310. : Array()
  311. {
  312. int anz = arr.getEntryCount();
  313. for (int i = 0; i < anz; i++)
  314. add(arr.get(i));
  315. }
  316. //! Clears and deletes the linked list
  317. ~Array()
  318. {
  319. clear();
  320. delete entries;
  321. }
  322. //! Appends an element to the end of the list
  323. //! \param t The new element
  324. void add(TYP t)
  325. {
  326. if (!last->set)
  327. {
  328. last->var = t;
  329. last->set = 1;
  330. count++;
  331. return;
  332. }
  333. last->next = new ArrayEntry<TYP>();
  334. last = last->next;
  335. last->set = 1;
  336. last->var = t;
  337. count++;
  338. }
  339. //! Inserts an element at a specific position in the list
  340. //! \param t The new element
  341. //! \param i The position where the element is inserted (afterwards the
  342. //! index of the new element)
  343. void add(TYP t, int i)
  344. {
  345. if (i < 0 || i > count)
  346. throwOutOfRange(__FILE__, __LINE__, i, count);
  347. if (i == count)
  348. {
  349. add(t);
  350. return;
  351. }
  352. ArrayEntry<TYP>* e = entries;
  353. for (int a = 0; a < i; ++a)
  354. e = e->next;
  355. ArrayEntry<TYP>* ne = new ArrayEntry<TYP>();
  356. ne->var = e->var;
  357. ne->set = e->set;
  358. ne->next = e->next;
  359. e->next = ne;
  360. e->var = t;
  361. e->set = 1;
  362. if (last->next) last = last->next;
  363. count++;
  364. }
  365. //! Sets the value of the i-th entry
  366. //! \param t The new value
  367. //! \param i The index of the entry to be set
  368. void set(TYP t, int i)
  369. {
  370. if (i < 0 || i >= count)
  371. throwOutOfRange(__FILE__, __LINE__, i, count);
  372. ArrayEntry<TYP>* e = entries;
  373. for (int a = 0; a < i; ++a)
  374. e = e->next;
  375. e->var = t;
  376. e->set = 1;
  377. }
  378. //! Changes the position of the i-th element in the list
  379. //! \param i The index of the element to be moved
  380. //! \param p The target position of the element (afterwards the new
  381. //! index of the element)
  382. void setPosition(int i, int p)
  383. {
  384. if (i == p) return;
  385. if (i < 0 || p < 0 || i >= count || p >= count)
  386. throwOutOfRange(__FILE__, __LINE__, i, count);
  387. TYP t = get(i);
  388. remove(i);
  389. add(t, p);
  390. }
  391. //! Deletes a specific element
  392. //! \param i The index of the element to be deleted
  393. void remove(int i)
  394. {
  395. if (i < 0 || i >= count)
  396. throwOutOfRange(__FILE__, __LINE__, i, count);
  397. ArrayEntry<TYP>* e = entries;
  398. for (int a = 0; a < i; ++a)
  399. e = e->next;
  400. if (e->next)
  401. {
  402. e->var = e->next->var;
  403. e->set = e->next->set;
  404. }
  405. else
  406. {
  407. e->set = 0;
  408. }
  409. ArrayEntry<TYP>* del = e->next;
  410. if (e->next)
  411. e->next = e->next->next;
  412. else
  413. e->next = 0;
  414. if (del)
  415. {
  416. del->set = 0;
  417. del->next = 0;
  418. if (last == del) last = e;
  419. delete del;
  420. }
  421. count--;
  422. }
  423. //! Deletes a specific element by value
  424. //! \param value The value of the element to be deleted
  425. void removeValue(TYP value)
  426. {
  427. ArrayEntry<TYP>* e = entries;
  428. while (e->var != value)
  429. {
  430. if (!e->next) return;
  431. e = e->next;
  432. }
  433. if (e->next)
  434. {
  435. e->var = e->next->var;
  436. e->set = e->next->set;
  437. }
  438. else
  439. e->set = 0;
  440. ArrayEntry<TYP>* del = e->next;
  441. if (e->next)
  442. e->next = e->next->next;
  443. else
  444. e->next = 0;
  445. if (del)
  446. {
  447. del->set = 0;
  448. del->next = 0;
  449. if (last == del) last = e;
  450. delete del;
  451. }
  452. count--;
  453. }
  454. //! Swaps two elements in the list
  455. //! \param vi The index of the first element
  456. //! \param ni The index of the second element
  457. void swap(int vi, int ni)
  458. {
  459. TYP tmp = get(ni);
  460. set(get(vi), ni);
  461. set(tmp, vi);
  462. }
  463. //! Deletes all elements of the list
  464. void clear()
  465. {
  466. ArrayEntry<TYP>* e2 = 0;
  467. for (ArrayEntry<TYP>* e = entries->next; e; e = e->next)
  468. {
  469. delete e2;
  470. e2 = e;
  471. }
  472. delete e2;
  473. entries->set = 0;
  474. entries->next = 0;
  475. last = entries;
  476. count = 0;
  477. }
  478. //! Returns an iterator.
  479. //! Use ++ to iterate through the list
  480. ArrayIterator<TYP> begin() const
  481. {
  482. return ArrayIterator<TYP>(
  483. entries, 0, &onSizeChanged, &onLastChanged);
  484. }
  485. ArrayIterator<TYP> end() const
  486. {
  487. return ArrayIterator<TYP>(0, 0, &onSizeChanged, &onLastChanged);
  488. }
  489. //! Returns how many elements are in the list
  490. int getEntryCount() const
  491. {
  492. return count;
  493. }
  494. //! Returns the value of the i-th element
  495. //! \param i The index of the sought element
  496. //! throws:
  497. //! \param std:out_of_range if i < 0 or i >= getEntryCount()
  498. TYP get(int i) const
  499. {
  500. if (i < 0 || i >= count)
  501. throwOutOfRange(__FILE__, __LINE__, i, count);
  502. ArrayEntry<TYP>* e = entries;
  503. for (int a = 0; a < i && e; ++a)
  504. e = e->next;
  505. return e->var;
  506. }
  507. //! Checks whether an element exists in the list
  508. //! \param i The index of the sought element
  509. //! \return (true) if the index exists. (false) otherwise
  510. bool has(int i) const
  511. {
  512. return i >= 0 && i < count;
  513. }
  514. //! Returns the index of a value
  515. //! \param t The value to search for
  516. int getValueIndex(TYP t) const
  517. {
  518. int ret = 0;
  519. for (ArrayEntry<TYP>* e = entries; e; e = e->next)
  520. {
  521. if (e->set && e->var == t) return ret;
  522. ++ret;
  523. }
  524. return -1;
  525. }
  526. bool anyMatch(std::function<bool(const TYP element)> predicate) const
  527. {
  528. for (TYP t : *this)
  529. {
  530. if (predicate(t)) return 1;
  531. }
  532. return 0;
  533. }
  534. bool allMatch(std::function<bool(const TYP element)> predicate) const
  535. {
  536. for (TYP t : *this)
  537. {
  538. if (!predicate(t)) return 0;
  539. }
  540. return 1;
  541. }
  542. int findIndex(std::function<bool(const TYP element)> predicate) const
  543. {
  544. int index = 0;
  545. for (TYP t : *this)
  546. {
  547. if (predicate(t)) return index;
  548. index++;
  549. }
  550. return -1;
  551. }
  552. Array& operator=(const Array& arr)
  553. {
  554. clear();
  555. int anz = arr.getEntryCount();
  556. for (int i = 0; i < anz; i++)
  557. add(arr.get(i));
  558. return *this;
  559. }
  560. Stream<TYP> stream()
  561. {
  562. return Stream<TYP>(new IteratorSupplier<TYP>(new ArrayIterator<TYP>(
  563. entries, 0, &onSizeChanged, &onLastChanged)));
  564. }
  565. };
  566. template<class TYP>
  567. //! A linked list of pointers to objects that use reference counting
  568. class RCArray : public virtual ReferenceCounter
  569. {
  570. private:
  571. ArrayEntry<TYP*>* entries;
  572. ArrayEntry<TYP*>* last;
  573. int count;
  574. std::function<void(TYP*& removed)> onRemove;
  575. std::function<void(int change)> onSizeChanged;
  576. std::function<void(ArrayEntry<TYP*>* last)> onLastChanged;
  577. public:
  578. //! Creates a new linked list
  579. RCArray() noexcept
  580. : ReferenceCounter()
  581. {
  582. entries = new ArrayEntry<TYP*>();
  583. entries->var = 0;
  584. entries->set = 0;
  585. entries->next = 0;
  586. last = entries;
  587. count = 0;
  588. onRemove = [](TYP*& removed) { removed->release(); };
  589. onSizeChanged = [this](int change) { this->count += change; };
  590. onLastChanged
  591. = [this](ArrayEntry<TYP*>* last) { this->last = last; };
  592. }
  593. //! Copies a linked list
  594. RCArray(const RCArray& arr)
  595. : RCArray()
  596. {
  597. int anz = arr.getEntryCount();
  598. for (int i = 0; i < anz; i++)
  599. add(arr.get(i));
  600. }
  601. //! Clears and deletes the linked list
  602. ~RCArray()
  603. {
  604. clear();
  605. delete entries;
  606. }
  607. //! Appends an element to the end of the list
  608. //! \param t The new element
  609. void add(TYP* t)
  610. {
  611. count++;
  612. if (!last->set)
  613. {
  614. last->var = t;
  615. last->set = 1;
  616. return;
  617. }
  618. last->next = new ArrayEntry<TYP*>();
  619. last = last->next;
  620. last->var = t;
  621. last->set = 1;
  622. }
  623. //! Inserts an element at a specific position in the list
  624. //! \param t The new element
  625. //! \param i The position where the element is inserted (afterwards the
  626. //! index of the new element)
  627. void add(TYP* t, int i)
  628. {
  629. if (i < 0 || i > count)
  630. throwOutOfRange(__FILE__, __LINE__, i, count);
  631. if (i == count)
  632. {
  633. add(t);
  634. return;
  635. }
  636. ArrayEntry<TYP*>* e = entries;
  637. for (int a = 0; a < i; ++a)
  638. e = e->next;
  639. ArrayEntry<TYP*>* ne = new ArrayEntry<TYP*>();
  640. ne->var = e->var;
  641. ne->set = e->set;
  642. ne->next = e->next;
  643. e->next = ne;
  644. e->var = t;
  645. e->set = 1;
  646. if (last->next) last = last->next;
  647. count++;
  648. }
  649. //! Sets the value of the i-th entry
  650. //! \param t The new value
  651. //! \param i The index of the entry to be set
  652. void set(TYP* t, int i)
  653. {
  654. if (i < 0 || i >= count)
  655. throwOutOfRange(__FILE__, __LINE__, i, count);
  656. ArrayEntry<TYP*>* e = entries;
  657. for (int a = 0; a < i; ++a)
  658. e = e->next;
  659. if (e->set && e->var) e->var->release();
  660. e->var = t;
  661. e->set = 1;
  662. }
  663. //! Changes the position of the i-th element in the list
  664. //! \param i The index of the element to be moved
  665. //! \param p The target position of the element (afterwards the new
  666. //! index of the element)
  667. void setPosition(int i, int p)
  668. {
  669. if (i == p) return;
  670. if (i < 0 || p < 0 || i >= count || p >= count)
  671. throwOutOfRange(__FILE__, __LINE__, i, count);
  672. TYP* t = get(i);
  673. remove(i);
  674. add(t, p);
  675. }
  676. //! Deletes a specific element
  677. //! \param i The index of the element to be deleted
  678. void remove(int i)
  679. {
  680. if (i < 0 || i >= count)
  681. throwOutOfRange(__FILE__, __LINE__, i, count);
  682. ArrayEntry<TYP*>* e = entries;
  683. for (int a = 0; a < i; ++a)
  684. e = e->next;
  685. if (e->next)
  686. {
  687. if (e->set && e->var) e->var->release();
  688. e->var = e->next->var;
  689. e->set = e->next->set;
  690. }
  691. else
  692. {
  693. if (e->set && e->var) e->var->release();
  694. e->set = 0;
  695. }
  696. ArrayEntry<TYP*>* del = e->next;
  697. if (e->next)
  698. e->next = e->next->next;
  699. else
  700. e->next = 0;
  701. if (del)
  702. {
  703. del->set = 0;
  704. del->next = 0;
  705. if (last == del) last = e;
  706. delete del;
  707. }
  708. count--;
  709. }
  710. //! Swaps two elements in the list
  711. //! \param vi The index of the first element
  712. //! \param ni The index of the second element
  713. void swap(int vi, int ni)
  714. {
  715. if (vi < 0 || ni < 0) return;
  716. TYP* tmp = get(ni);
  717. set(get(vi), ni);
  718. set(tmp, vi);
  719. }
  720. //! Deletes all elements of the list
  721. void clear()
  722. {
  723. for (ArrayEntry<TYP*>* e = entries->next; e;)
  724. {
  725. if (e && e->var && e->set) e->var->release();
  726. auto tmp = e->next;
  727. delete e;
  728. e = tmp;
  729. }
  730. if (entries->var && entries->set) entries->var->release();
  731. entries->set = 0;
  732. entries->next = 0;
  733. last = entries;
  734. count = 0;
  735. }
  736. //! Returns an iterator.
  737. //! Use ++ to iterate through the list
  738. ArrayIterator<TYP*> begin() const
  739. {
  740. return ArrayIterator<TYP*>(
  741. entries, &onRemove, &onSizeChanged, &onLastChanged);
  742. }
  743. ArrayIterator<TYP*> end() const
  744. {
  745. return ArrayIterator<TYP*>(
  746. 0, &onRemove, &onSizeChanged, &onLastChanged);
  747. }
  748. //! Returns how many elements are in the list
  749. int getEntryCount() const
  750. {
  751. return count;
  752. }
  753. int getLastIndex() const
  754. {
  755. return count - 1;
  756. }
  757. //! Returns the value of the i-th element with increased reference
  758. //! counter \param i The index of the sought element, (0) if the
  759. //! index does not exist
  760. TYP* get(int i) const
  761. {
  762. if (i < 0 || i >= count)
  763. throwOutOfRange(__FILE__, __LINE__, i, count);
  764. ArrayEntry<TYP*>* e = entries;
  765. for (int a = 0; a < i && e; ++a)
  766. e = e->next;
  767. if (e && e->set && e->var)
  768. return dynamic_cast<TYP*>(e->var->getThis());
  769. return (TYP*)0;
  770. }
  771. //! Returns the value of the i-th element without increased reference
  772. //! counter \param i The index of the sought element, (0) if the
  773. //! index does not exist
  774. TYP* z(int i) const //! returns the index-th T
  775. {
  776. if (i < 0 || i >= count)
  777. throwOutOfRange(__FILE__, __LINE__, i, count);
  778. ArrayEntry<TYP*>* e = entries;
  779. for (int a = 0; a < i && e; ++a)
  780. e = e->next;
  781. if (e && e->set && e->var) return (TYP*)e->var;
  782. return (TYP*)0;
  783. }
  784. //! Checks whether an element exists in the list
  785. //! \param i The index of the sought element
  786. //! \return (true) if the index exists. (false) otherwise
  787. bool has(int i) const
  788. {
  789. return i >= 0 && i < count;
  790. }
  791. //! returns the index of the first element that matches zT or -1 if not
  792. //! found
  793. int indexOf(TYP* zT) const
  794. {
  795. int i = 0;
  796. for (TYP* t : *this)
  797. {
  798. if (t == zT) return i;
  799. ++i;
  800. }
  801. return -1;
  802. }
  803. bool anyMatch(std::function<bool(const TYP* zElement)> predicate) const
  804. {
  805. for (TYP* t : *this)
  806. {
  807. if (predicate(t)) return 1;
  808. }
  809. return 0;
  810. }
  811. bool allMatch(std::function<bool(const TYP* zElement)> predicate) const
  812. {
  813. for (TYP* t : *this)
  814. {
  815. if (!predicate(t)) return 0;
  816. }
  817. return 1;
  818. }
  819. int findIndex(std::function<bool(const TYP* zElement)> predicate) const
  820. {
  821. int index = 0;
  822. for (TYP* t : *this)
  823. {
  824. if (predicate(t)) return index;
  825. index++;
  826. }
  827. return -1;
  828. }
  829. RCArray& operator=(const RCArray& arr)
  830. {
  831. clear();
  832. int anz = arr.getEntryCount();
  833. for (int i = 0; i < anz; i++)
  834. add(arr.get(i));
  835. return *this;
  836. }
  837. Stream<TYP*> stream()
  838. {
  839. return Stream<TYP*>(
  840. new IteratorSupplier<TYP*>(new ArrayIterator<TYP*>(
  841. entries, &onRemove, &onSizeChanged, &onLastChanged)));
  842. }
  843. };
  844. } // namespace Framework