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. e->set = 0;
  407. ArrayEntry<TYP>* del = e->next;
  408. if (e->next)
  409. e->next = e->next->next;
  410. else
  411. e->next = 0;
  412. if (del)
  413. {
  414. del->set = 0;
  415. del->next = 0;
  416. if (last == del) last = e;
  417. delete del;
  418. }
  419. count--;
  420. }
  421. //! Deletes a specific element by value
  422. //! \param value The value of the element to be deleted
  423. void removeValue(TYP value)
  424. {
  425. ArrayEntry<TYP>* e = entries;
  426. while (e->var != value)
  427. {
  428. if (!e->next) return;
  429. e = e->next;
  430. }
  431. if (e->next)
  432. {
  433. e->var = e->next->var;
  434. e->set = e->next->set;
  435. }
  436. else
  437. e->set = 0;
  438. ArrayEntry<TYP>* del = e->next;
  439. if (e->next)
  440. e->next = e->next->next;
  441. else
  442. e->next = 0;
  443. if (del)
  444. {
  445. del->set = 0;
  446. del->next = 0;
  447. if (last == del) last = e;
  448. delete del;
  449. }
  450. count--;
  451. }
  452. //! Swaps two elements in the list
  453. //! \param vi The index of the first element
  454. //! \param ni The index of the second element
  455. void swap(int vi, int ni)
  456. {
  457. TYP tmp = get(ni);
  458. set(get(vi), ni);
  459. set(tmp, vi);
  460. }
  461. //! Deletes all elements of the list
  462. void clear()
  463. {
  464. ArrayEntry<TYP>* e2 = 0;
  465. for (ArrayEntry<TYP>* e = entries->next; e; e = e->next)
  466. {
  467. delete e2;
  468. e2 = e;
  469. }
  470. delete e2;
  471. entries->set = 0;
  472. entries->next = 0;
  473. last = entries;
  474. count = 0;
  475. }
  476. //! Returns an iterator.
  477. //! Use ++ to iterate through the list
  478. ArrayIterator<TYP> begin() const
  479. {
  480. return ArrayIterator<TYP>(
  481. entries, 0, &onSizeChanged, &onLastChanged);
  482. }
  483. ArrayIterator<TYP> end() const
  484. {
  485. return ArrayIterator<TYP>(0, 0, &onSizeChanged, &onLastChanged);
  486. }
  487. //! Returns how many elements are in the list
  488. int getEntryCount() const
  489. {
  490. return count;
  491. }
  492. //! Returns the value of the i-th element
  493. //! \param i The index of the sought element
  494. //! throws:
  495. //! \param std:out_of_range if i < 0 or i >= getEntryCount()
  496. TYP get(int i) const
  497. {
  498. if (i < 0 || i >= count)
  499. throwOutOfRange(__FILE__, __LINE__, i, count);
  500. ArrayEntry<TYP>* e = entries;
  501. for (int a = 0; a < i && e; ++a)
  502. e = e->next;
  503. return e->var;
  504. }
  505. //! Checks whether an element exists in the list
  506. //! \param i The index of the sought element
  507. //! \return (true) if the index exists. (false) otherwise
  508. bool has(int i) const
  509. {
  510. return i >= 0 && i < count;
  511. }
  512. //! Returns the index of a value
  513. //! \param t The value to search for
  514. int getValueIndex(TYP t) const
  515. {
  516. int ret = 0;
  517. for (ArrayEntry<TYP>* e = entries; e; e = e->next)
  518. {
  519. if (e->set && e->var == t) return ret;
  520. ++ret;
  521. }
  522. return -1;
  523. }
  524. bool anyMatch(std::function<bool(const TYP element)> predicate) const
  525. {
  526. for (TYP t : *this)
  527. {
  528. if (predicate(t)) return 1;
  529. }
  530. return 0;
  531. }
  532. bool allMatch(std::function<bool(const TYP element)> predicate) const
  533. {
  534. for (TYP t : *this)
  535. {
  536. if (!predicate(t)) return 0;
  537. }
  538. return 1;
  539. }
  540. int findIndex(std::function<bool(const TYP element)> predicate) const
  541. {
  542. int index = 0;
  543. for (TYP t : *this)
  544. {
  545. if (predicate(t)) return index;
  546. index++;
  547. }
  548. return -1;
  549. }
  550. Array& operator=(const Array& arr)
  551. {
  552. clear();
  553. int anz = arr.getEntryCount();
  554. for (int i = 0; i < anz; i++)
  555. add(arr.get(i));
  556. return *this;
  557. }
  558. Stream<TYP> stream()
  559. {
  560. return Stream<TYP>(new IteratorSupplier<TYP>(new ArrayIterator<TYP>(
  561. entries, 0, &onSizeChanged, &onLastChanged)));
  562. }
  563. };
  564. template<class TYP>
  565. //! A linked list of pointers to objects that use reference counting
  566. class RCArray : public virtual ReferenceCounter
  567. {
  568. private:
  569. ArrayEntry<TYP*>* entries;
  570. ArrayEntry<TYP*>* last;
  571. int count;
  572. std::function<void(TYP*& removed)> onRemove;
  573. std::function<void(int change)> onSizeChanged;
  574. std::function<void(ArrayEntry<TYP*>* last)> onLastChanged;
  575. public:
  576. //! Creates a new linked list
  577. RCArray() noexcept
  578. : ReferenceCounter()
  579. {
  580. entries = new ArrayEntry<TYP*>();
  581. entries->var = 0;
  582. entries->set = 0;
  583. entries->next = 0;
  584. last = entries;
  585. count = 0;
  586. onRemove = [](TYP*& removed) { removed->release(); };
  587. onSizeChanged = [this](int change) { this->count += change; };
  588. onLastChanged
  589. = [this](ArrayEntry<TYP*>* last) { this->last = last; };
  590. }
  591. //! Copies a linked list
  592. RCArray(const RCArray& arr)
  593. : RCArray()
  594. {
  595. int anz = arr.getEntryCount();
  596. for (int i = 0; i < anz; i++)
  597. add(arr.get(i));
  598. }
  599. //! Clears and deletes the linked list
  600. ~RCArray()
  601. {
  602. clear();
  603. delete entries;
  604. }
  605. //! Appends an element to the end of the list
  606. //! \param t The new element
  607. void add(TYP* t)
  608. {
  609. count++;
  610. if (!last->set)
  611. {
  612. last->var = t;
  613. last->set = 1;
  614. return;
  615. }
  616. last->next = new ArrayEntry<TYP*>();
  617. last = last->next;
  618. last->var = t;
  619. last->set = 1;
  620. }
  621. //! Inserts an element at a specific position in the list
  622. //! \param t The new element
  623. //! \param i The position where the element is inserted (afterwards the
  624. //! index of the new element)
  625. void add(TYP* t, int i)
  626. {
  627. if (i < 0 || i > count)
  628. throwOutOfRange(__FILE__, __LINE__, i, count);
  629. if (i == count)
  630. {
  631. add(t);
  632. return;
  633. }
  634. ArrayEntry<TYP*>* e = entries;
  635. for (int a = 0; a < i; ++a)
  636. e = e->next;
  637. ArrayEntry<TYP*>* ne = new ArrayEntry<TYP*>();
  638. ne->var = e->var;
  639. ne->set = e->set;
  640. ne->next = e->next;
  641. e->next = ne;
  642. e->var = t;
  643. e->set = 1;
  644. if (last->next) last = last->next;
  645. count++;
  646. }
  647. //! Sets the value of the i-th entry
  648. //! \param t The new value
  649. //! \param i The index of the entry to be set
  650. void set(TYP* t, int i)
  651. {
  652. if (i < 0 || i >= count)
  653. throwOutOfRange(__FILE__, __LINE__, i, count);
  654. ArrayEntry<TYP*>* e = entries;
  655. for (int a = 0; a < i; ++a)
  656. e = e->next;
  657. if (e->set && e->var) e->var->release();
  658. e->var = t;
  659. e->set = 1;
  660. }
  661. //! Changes the position of the i-th element in the list
  662. //! \param i The index of the element to be moved
  663. //! \param p The target position of the element (afterwards the new
  664. //! index of the element)
  665. void setPosition(int i, int p)
  666. {
  667. if (i == p) return;
  668. if (i < 0 || p < 0 || i >= count || p >= count)
  669. throwOutOfRange(__FILE__, __LINE__, i, count);
  670. TYP* t = get(i);
  671. remove(i);
  672. add(t, p);
  673. }
  674. //! Deletes a specific element
  675. //! \param i The index of the element to be deleted
  676. void remove(int i)
  677. {
  678. if (i < 0 || i >= count)
  679. throwOutOfRange(__FILE__, __LINE__, i, count);
  680. ArrayEntry<TYP*>* e = entries;
  681. for (int a = 0; a < i; ++a)
  682. e = e->next;
  683. if (e->next)
  684. {
  685. if (e->set && e->var) e->var->release();
  686. e->var = e->next->var;
  687. e->set = e->next->set;
  688. }
  689. else
  690. {
  691. if (e->set && e->var) e->var->release();
  692. e->set = 0;
  693. }
  694. ArrayEntry<TYP*>* del = e->next;
  695. if (e->next)
  696. e->next = e->next->next;
  697. else
  698. e->next = 0;
  699. if (del)
  700. {
  701. del->set = 0;
  702. del->next = 0;
  703. if (last == del) last = e;
  704. delete del;
  705. }
  706. count--;
  707. }
  708. //! Swaps two elements in the list
  709. //! \param vi The index of the first element
  710. //! \param ni The index of the second element
  711. void swap(int vi, int ni)
  712. {
  713. if (vi < 0 || ni < 0) return;
  714. TYP* tmp = get(ni);
  715. set(get(vi), ni);
  716. set(tmp, vi);
  717. }
  718. //! Deletes all elements of the list
  719. void clear()
  720. {
  721. for (ArrayEntry<TYP*>* e = entries->next; e;)
  722. {
  723. if (e && e->var && e->set) e->var->release();
  724. auto tmp = e->next;
  725. delete e;
  726. e = tmp;
  727. }
  728. if (entries->var && entries->set) entries->var->release();
  729. entries->set = 0;
  730. entries->next = 0;
  731. last = entries;
  732. count = 0;
  733. }
  734. //! Returns an iterator.
  735. //! Use ++ to iterate through the list
  736. ArrayIterator<TYP*> begin() const
  737. {
  738. return ArrayIterator<TYP*>(
  739. entries, &onRemove, &onSizeChanged, &onLastChanged);
  740. }
  741. ArrayIterator<TYP*> end() const
  742. {
  743. return ArrayIterator<TYP*>(
  744. 0, &onRemove, &onSizeChanged, &onLastChanged);
  745. }
  746. //! Returns how many elements are in the list
  747. int getEntryCount() const
  748. {
  749. return count;
  750. }
  751. int getLastIndex() const
  752. {
  753. return count - 1;
  754. }
  755. //! Returns the value of the i-th element with increased reference
  756. //! counter \param i The index of the sought element, (0) if the
  757. //! index does not exist
  758. TYP* get(int i) const
  759. {
  760. if (i < 0 || i >= count)
  761. throwOutOfRange(__FILE__, __LINE__, i, count);
  762. ArrayEntry<TYP*>* e = entries;
  763. for (int a = 0; a < i && e; ++a)
  764. e = e->next;
  765. if (e && e->set && e->var)
  766. return dynamic_cast<TYP*>(e->var->getThis());
  767. return (TYP*)0;
  768. }
  769. //! Returns the value of the i-th element without increased reference
  770. //! counter \param i The index of the sought element, (0) if the
  771. //! index does not exist
  772. TYP* z(int i) const //! returns the index-th T
  773. {
  774. if (i < 0 || i >= count)
  775. throwOutOfRange(__FILE__, __LINE__, i, count);
  776. ArrayEntry<TYP*>* e = entries;
  777. for (int a = 0; a < i && e; ++a)
  778. e = e->next;
  779. if (e && e->set && e->var) return (TYP*)e->var;
  780. return (TYP*)0;
  781. }
  782. //! Checks whether an element exists in the list
  783. //! \param i The index of the sought element
  784. //! \return (true) if the index exists. (false) otherwise
  785. bool has(int i) const
  786. {
  787. return i >= 0 && i < count;
  788. }
  789. //! returns the index of the first element that matches zT or -1 if not
  790. //! found
  791. int indexOf(TYP* zT) const
  792. {
  793. int i = 0;
  794. for (TYP* t : *this)
  795. {
  796. if (t == zT) return i;
  797. ++i;
  798. }
  799. return -1;
  800. }
  801. bool anyMatch(std::function<bool(const TYP* zElement)> predicate) const
  802. {
  803. for (TYP* t : *this)
  804. {
  805. if (predicate(t)) return 1;
  806. }
  807. return 0;
  808. }
  809. bool allMatch(std::function<bool(const TYP* zElement)> predicate) const
  810. {
  811. for (TYP* t : *this)
  812. {
  813. if (!predicate(t)) return 0;
  814. }
  815. return 1;
  816. }
  817. int findIndex(std::function<bool(const TYP* zElement)> predicate) const
  818. {
  819. int index = 0;
  820. for (TYP* t : *this)
  821. {
  822. if (predicate(t)) return index;
  823. index++;
  824. }
  825. return -1;
  826. }
  827. RCArray& operator=(const RCArray& arr)
  828. {
  829. clear();
  830. int anz = arr.getEntryCount();
  831. for (int i = 0; i < anz; i++)
  832. add(arr.get(i));
  833. return *this;
  834. }
  835. Stream<TYP*> stream()
  836. {
  837. return Stream<TYP*>(
  838. new IteratorSupplier<TYP*>(new ArrayIterator<TYP*>(
  839. entries, &onRemove, &onSizeChanged, &onLastChanged)));
  840. }
  841. };
  842. } // namespace Framework