Array.h 25 KB

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