CCArray.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /****************************************************************************
  2. Copyright (c) 2010 ForzeField Studios S.L. http://forzefield.com
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "deprecated/CCArray.h"
  24. #include "deprecated/CCString.h"
  25. #include "base/ccUTF8.h"
  26. #include "platform/CCFileUtils.h"
  27. NS_CC_BEGIN
  28. #if CC_USE_ARRAY_VECTOR
  29. // ----------------------------------------------------------------------------------
  30. // std::vector implementation
  31. // ----------------------------------------------------------------------------------
  32. __Array::__Array()
  33. : data(nullptr)
  34. {
  35. init();
  36. }
  37. __Array* __Array::create()
  38. {
  39. __Array* array = new (std::nothrow) __Array();
  40. if (array && array->initWithCapacity(7))
  41. {
  42. array->autorelease();
  43. }
  44. else
  45. {
  46. CC_SAFE_DELETE(array);
  47. }
  48. return array;
  49. }
  50. __Array* __Array::createWithObject(Ref* object)
  51. {
  52. __Array* array = new (std::nothrow) __Array();
  53. if (array && array->initWithObject(object))
  54. {
  55. array->autorelease();
  56. }
  57. else
  58. {
  59. CC_SAFE_DELETE(array);
  60. }
  61. return array;
  62. }
  63. __Array* __Array::create(Ref* object, ...)
  64. {
  65. va_list args;
  66. va_start(args,object);
  67. __Array* array = create();
  68. if (array && object)
  69. {
  70. array->addObject(object);
  71. Ref* i = va_arg(args, Ref*);
  72. while (i)
  73. {
  74. array->addObject(i);
  75. i = va_arg(args, Ref*);
  76. }
  77. }
  78. else
  79. {
  80. CC_SAFE_DELETE(array);
  81. }
  82. va_end(args);
  83. return array;
  84. }
  85. __Array* __Array::createWithArray(__Array* otherArray)
  86. {
  87. return otherArray->clone();
  88. }
  89. __Array* __Array::createWithCapacity(int capacity)
  90. {
  91. CCASSERT(capacity>=0, "Invalid capacity");
  92. __Array* array = new (std::nothrow) __Array();
  93. if (array && array->initWithCapacity(capacity))
  94. {
  95. array->autorelease();
  96. }
  97. else
  98. {
  99. CC_SAFE_DELETE(array);
  100. }
  101. return array;
  102. }
  103. __Array* __Array::createWithContentsOfFile(const std::string& fileName)
  104. {
  105. __Array* ret = __Array::createWithContentsOfFileThreadSafe(fileName);
  106. if (ret != nullptr)
  107. {
  108. ret->autorelease();
  109. }
  110. return ret;
  111. }
  112. __Array* __Array::createWithContentsOfFileThreadSafe(const std::string& fileName)
  113. {
  114. return FileUtils::getInstance()->createArrayWithContentsOfFile(fileName);
  115. }
  116. bool __Array::init()
  117. {
  118. return initWithCapacity(7);
  119. }
  120. bool __Array::initWithObject(Ref* object)
  121. {
  122. bool ret = initWithCapacity(7);
  123. if (ret)
  124. {
  125. addObject(object);
  126. }
  127. return ret;
  128. }
  129. /** Initializes an array with some objects */
  130. bool __Array::initWithObjects(Ref* object, ...)
  131. {
  132. bool ret = false;
  133. do
  134. {
  135. CC_BREAK_IF(object == nullptr);
  136. va_list args;
  137. va_start(args, object);
  138. if (object)
  139. {
  140. this->addObject(object);
  141. Ref* i = va_arg(args, Ref*);
  142. while (i)
  143. {
  144. this->addObject(i);
  145. i = va_arg(args, Ref*);
  146. }
  147. ret = true;
  148. }
  149. va_end(args);
  150. } while (false);
  151. return ret;
  152. }
  153. bool __Array::initWithCapacity(int capacity)
  154. {
  155. CCASSERT(capacity>=0, "Invalid capacity");
  156. data.reserve(capacity);
  157. return true;
  158. }
  159. bool __Array::initWithArray(__Array* otherArray)
  160. {
  161. data = otherArray->data;
  162. return true;
  163. }
  164. ssize_t __Array::getIndexOfObject(Ref* object) const
  165. {
  166. auto it = data.begin();
  167. for (ssize_t i = 0; it != data.end(); ++it, ++i)
  168. {
  169. if (it->get() == object)
  170. {
  171. return i;
  172. }
  173. }
  174. return -1;
  175. }
  176. Ref* __Array::getRandomObject()
  177. {
  178. if (data.size()==0)
  179. {
  180. return nullptr;
  181. }
  182. float r = CCRANDOM_0_1();
  183. if (r == 1) // to prevent from accessing data-arr[data->num], out of range.
  184. {
  185. r = 0;
  186. }
  187. r *= data.size();
  188. return data[r].get();
  189. }
  190. bool __Array::containsObject(Ref* object) const
  191. {
  192. ssize_t i = this->getIndexOfObject(object);
  193. return (i >= 0);
  194. }
  195. bool __Array::isEqualToArray(__Array* otherArray)
  196. {
  197. for (ssize_t i = 0; i < this->count(); ++i)
  198. {
  199. if (!this->getObjectAtIndex(i)->isEqual(otherArray->getObjectAtIndex(i)))
  200. {
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. void __Array::addObject(Ref* object)
  207. {
  208. data.push_back(RCPtr<Object>(object));
  209. }
  210. void __Array::addObjectsFromArray(__Array* otherArray)
  211. {
  212. data.insert(data.end(), otherArray->data.begin(), otherArray->data.end());
  213. }
  214. void __Array::insertObject(Ref* object, int index)
  215. {
  216. data.insert(std::begin(data) + index, RCPtr<Object>(object));
  217. }
  218. void __Array::setObject(Ref* object, int index)
  219. {
  220. data[index] = RCPtr<Object>(object);
  221. }
  222. void __Array::removeLastObject(bool releaseObj)
  223. {
  224. CCASSERT(data.size(), "no objects added");
  225. data.pop_back();
  226. }
  227. void __Array::removeObject(Ref* object, bool releaseObj /* ignored */)
  228. {
  229. data.erase(std::remove(data.begin(), data.end(), object));
  230. }
  231. void __Array::removeObjectAtIndex(ssize_t index, bool releaseObj /* ignored */)
  232. {
  233. auto obj = data[index];
  234. data.erase(data.begin() + index);
  235. }
  236. void __Array::removeObjectsInArray(__Array* otherArray)
  237. {
  238. CCASSERT(false, "not implemented");
  239. }
  240. void __Array::removeAllObjects()
  241. {
  242. data.erase(std::begin(data), std::end(data));
  243. }
  244. void __Array::fastRemoveObjectAtIndex(int index)
  245. {
  246. removeObjectAtIndex(index);
  247. }
  248. void __Array::fastRemoveObject(Ref* object)
  249. {
  250. removeObject(object);
  251. }
  252. void __Array::exchangeObject(Ref* object1, Ref* object2)
  253. {
  254. ssize_t idx1 = getIndexOfObject(object1);
  255. ssize_t idx2 = getIndexOfObject(object2);
  256. CCASSERT(idx1 >= 0 && idx2 >= 2, "invalid object index");
  257. std::swap(data[idx1], data[idx2]);
  258. }
  259. void __Array::exchangeObjectAtIndex(ssize_t index1, ssize_t index2)
  260. {
  261. std::swap(data[index1], data[index2]);
  262. }
  263. void __Array::replaceObjectAtIndex(int index, Ref* object, bool releaseObject /* ignored */)
  264. {
  265. data[index] = object;
  266. }
  267. void __Array::reverseObjects()
  268. {
  269. std::reverse(std::begin(data), std::end(data));
  270. }
  271. void __Array::reduceMemoryFootprint()
  272. {
  273. // N/A
  274. }
  275. __Array::~Array()
  276. {
  277. CCLOGINFO("deallocing Array: %p - len: %d", this, count() );
  278. }
  279. __Array* __Array::clone() const
  280. {
  281. __Array* ret = new (std::nothrow) __Array();
  282. ret->autorelease();
  283. ret->initWithCapacity(this->data.size() > 0 ? this->data.size() : 1);
  284. Ref* obj = nullptr;
  285. Ref* tmpObj = nullptr;
  286. Clonable* clonable = nullptr;
  287. const __Array* self( this );
  288. CCARRAY_FOREACH(self, obj)
  289. {
  290. clonable = dynamic_cast<Clonable*>(obj);
  291. if (clonable)
  292. {
  293. tmpObj = dynamic_cast<Ref*>(clonable->clone());
  294. if (tmpObj)
  295. {
  296. ret->addObject(tmpObj);
  297. }
  298. }
  299. else
  300. {
  301. CCLOGWARN("%s isn't clonable.", typeid(*obj).name());
  302. }
  303. }
  304. return ret;
  305. }
  306. void __Array::acceptVisitor(DataVisitor &visitor)
  307. {
  308. visitor.visit(this);
  309. }
  310. // ----------------------------------------------------------------------------------
  311. // ccArray implementation
  312. // ----------------------------------------------------------------------------------
  313. #else
  314. __Array::__Array()
  315. : data(nullptr)
  316. {
  317. // init();
  318. }
  319. __Array* __Array::create()
  320. {
  321. __Array* array = new (std::nothrow) __Array();
  322. if (array && array->initWithCapacity(7))
  323. {
  324. array->autorelease();
  325. }
  326. else
  327. {
  328. CC_SAFE_DELETE(array);
  329. }
  330. return array;
  331. }
  332. __Array* __Array::createWithObject(Ref* object)
  333. {
  334. __Array* array = new (std::nothrow) __Array();
  335. if (array && array->initWithObject(object))
  336. {
  337. array->autorelease();
  338. }
  339. else
  340. {
  341. CC_SAFE_DELETE(array);
  342. }
  343. return array;
  344. }
  345. __Array* __Array::create(Ref* object, ...)
  346. {
  347. va_list args;
  348. va_start(args,object);
  349. __Array* array = create();
  350. if (array && object)
  351. {
  352. array->addObject(object);
  353. Ref *i = va_arg(args, Ref*);
  354. while (i)
  355. {
  356. array->addObject(i);
  357. i = va_arg(args, Ref*);
  358. }
  359. }
  360. else
  361. {
  362. CC_SAFE_DELETE(array);
  363. }
  364. va_end(args);
  365. return array;
  366. }
  367. __Array* __Array::createWithArray(__Array* otherArray)
  368. {
  369. return otherArray->clone();
  370. }
  371. __Array* __Array::createWithCapacity(ssize_t capacity)
  372. {
  373. CCASSERT(capacity>=0, "Invalid capacity");
  374. __Array* array = new (std::nothrow) __Array();
  375. if (array && array->initWithCapacity(capacity))
  376. {
  377. array->autorelease();
  378. }
  379. else
  380. {
  381. CC_SAFE_DELETE(array);
  382. }
  383. return array;
  384. }
  385. __Array* __Array::createWithContentsOfFile(const std::string& fileName)
  386. {
  387. __Array* ret = __Array::createWithContentsOfFileThreadSafe(fileName);
  388. if (ret != nullptr)
  389. {
  390. ret->autorelease();
  391. }
  392. return ret;
  393. }
  394. __Array* __Array::createWithContentsOfFileThreadSafe(const std::string& fileName)
  395. {
  396. ValueVector arr = FileUtils::getInstance()->getValueVectorFromFile(fileName);
  397. __Array* ret = __Array::createWithCapacity(static_cast<int>(arr.size()));
  398. for(const auto &value : arr) {
  399. ret->addObject(__String::create(value.asString()));
  400. }
  401. return ret;
  402. }
  403. bool __Array::init()
  404. {
  405. CCASSERT(!data, "Array cannot be re-initialized");
  406. return initWithCapacity(7);
  407. }
  408. bool __Array::initWithObject(Ref* object)
  409. {
  410. CCASSERT(!data, "Array cannot be re-initialized");
  411. bool ret = initWithCapacity(7);
  412. if (ret)
  413. {
  414. addObject(object);
  415. }
  416. return ret;
  417. }
  418. /** Initializes an array with some objects */
  419. bool __Array::initWithObjects(Ref* object, ...)
  420. {
  421. CCASSERT(!data, "Array cannot be re-initialized");
  422. bool ret = false;
  423. do
  424. {
  425. CC_BREAK_IF(object == nullptr);
  426. CC_BREAK_IF(!initWithCapacity(1));
  427. va_list args;
  428. va_start(args, object);
  429. if (object)
  430. {
  431. this->addObject(object);
  432. Ref* i = va_arg(args, Ref*);
  433. while (i)
  434. {
  435. this->addObject(i);
  436. i = va_arg(args, Ref*);
  437. }
  438. ret = true;
  439. }
  440. va_end(args);
  441. } while (false);
  442. return ret;
  443. }
  444. bool __Array::initWithCapacity(ssize_t capacity)
  445. {
  446. CCASSERT(capacity>=0 && !data, "Array cannot be re-initialized");
  447. data = ccArrayNew(capacity);
  448. return true;
  449. }
  450. bool __Array::initWithArray(__Array* otherArray)
  451. {
  452. CCASSERT(!data, "Array cannot be re-initialized");
  453. bool ret = false;
  454. do
  455. {
  456. CC_BREAK_IF(! initWithCapacity(otherArray->data->num));
  457. addObjectsFromArray(otherArray);
  458. ret = true;
  459. } while (0);
  460. return ret;
  461. }
  462. ssize_t __Array::getIndexOfObject(Ref* object) const
  463. {
  464. return ccArrayGetIndexOfObject(data, object);
  465. }
  466. Ref* __Array::getRandomObject()
  467. {
  468. if (data->num == 0)
  469. {
  470. return nullptr;
  471. }
  472. float r = CCRANDOM_0_1();
  473. if (r == 1) // to prevent from accessing data-arr[data->num], out of range.
  474. {
  475. r = 0;
  476. }
  477. return data->arr[static_cast<int>(data->num * r)];
  478. }
  479. bool __Array::containsObject(Ref* object) const
  480. {
  481. return ccArrayContainsObject(data, object);
  482. }
  483. bool __Array::isEqualToArray(__Array* otherArray)
  484. {
  485. for (int i = 0; i < this->count(); ++i)
  486. {
  487. //FIXME:james
  488. if (this->getObjectAtIndex(i) != otherArray->getObjectAtIndex(i))
  489. {
  490. return false;
  491. }
  492. }
  493. return true;
  494. }
  495. void __Array::addObject(Ref* object)
  496. {
  497. CCASSERT(data, "Array not initialized");
  498. ccArrayAppendObjectWithResize(data, object);
  499. }
  500. void __Array::addObjectsFromArray(__Array* otherArray)
  501. {
  502. CCASSERT(data, "Array not initialized");
  503. ccArrayAppendArrayWithResize(data, otherArray->data);
  504. }
  505. void __Array::insertObject(Ref* object, ssize_t index)
  506. {
  507. CCASSERT(data, "Array not initialized");
  508. ccArrayInsertObjectAtIndex(data, object, index);
  509. }
  510. void __Array::setObject(Ref* object, ssize_t index)
  511. {
  512. CCASSERT(index >= 0 && index < count(), "Invalid index");
  513. if (object != data->arr[index])
  514. {
  515. data->arr[index]->release();
  516. data->arr[index] = object;
  517. object->retain();
  518. }
  519. }
  520. void __Array::removeLastObject(bool releaseObj)
  521. {
  522. CCASSERT(data->num, "no objects added");
  523. ccArrayRemoveObjectAtIndex(data, data->num - 1, releaseObj);
  524. }
  525. void __Array::removeObject(Ref* object, bool releaseObj/* = true*/)
  526. {
  527. ccArrayRemoveObject(data, object, releaseObj);
  528. }
  529. void __Array::removeObjectAtIndex(ssize_t index, bool releaseObj)
  530. {
  531. ccArrayRemoveObjectAtIndex(data, index, releaseObj);
  532. }
  533. void __Array::removeObjectsInArray(__Array* otherArray)
  534. {
  535. ccArrayRemoveArray(data, otherArray->data);
  536. }
  537. void __Array::removeAllObjects()
  538. {
  539. ccArrayRemoveAllObjects(data);
  540. }
  541. void __Array::fastRemoveObjectAtIndex(ssize_t index)
  542. {
  543. ccArrayFastRemoveObjectAtIndex(data, index);
  544. }
  545. void __Array::fastRemoveObject(Ref* object)
  546. {
  547. ccArrayFastRemoveObject(data, object);
  548. }
  549. void __Array::exchangeObject(Ref* object1, Ref* object2)
  550. {
  551. auto index1 = ccArrayGetIndexOfObject(data, object1);
  552. if (index1 == CC_INVALID_INDEX)
  553. {
  554. return;
  555. }
  556. auto index2 = ccArrayGetIndexOfObject(data, object2);
  557. if (index2 == CC_INVALID_INDEX)
  558. {
  559. return;
  560. }
  561. ccArraySwapObjectsAtIndexes(data, index1, index2);
  562. }
  563. void __Array::exchangeObjectAtIndex(ssize_t index1, ssize_t index2)
  564. {
  565. ccArraySwapObjectsAtIndexes(data, index1, index2);
  566. }
  567. void __Array::replaceObjectAtIndex(ssize_t index, Ref* object, bool releaseObject/* = true*/)
  568. {
  569. ccArrayInsertObjectAtIndex(data, object, index);
  570. ccArrayRemoveObjectAtIndex(data, index + 1, releaseObject);
  571. }
  572. void __Array::reverseObjects()
  573. {
  574. if (data->num > 1)
  575. {
  576. // floorf(), since in the case of an even number, the number of swaps stays the same
  577. auto count = static_cast<ssize_t>(floorf(data->num/2.f));
  578. ssize_t maxIndex = data->num - 1;
  579. for (ssize_t i = 0; i < count ; ++i)
  580. {
  581. ccArraySwapObjectsAtIndexes(data, i, maxIndex);
  582. --maxIndex;
  583. }
  584. }
  585. }
  586. void __Array::reduceMemoryFootprint()
  587. {
  588. ccArrayShrink(data);
  589. }
  590. __Array::~__Array()
  591. {
  592. CCLOGINFO("deallocing Array: %p - len: %d", this, count() );
  593. ccArrayFree(data);
  594. }
  595. __Array* __Array::clone() const
  596. {
  597. __Array* ret = new (std::nothrow) __Array();
  598. ret->autorelease();
  599. ret->initWithCapacity(this->data->num > 0 ? this->data->num : 1);
  600. if (data->num <= 0) {
  601. return ret;
  602. }
  603. Ref* obj = nullptr;
  604. Ref* tmpObj = nullptr;
  605. Clonable* clonable = nullptr;
  606. CC_ASSERT(data->num > 0);
  607. for (Ref** arr = data->arr, **end = data->arr + data->num - 1;
  608. arr <= end && ((obj = *arr) != nullptr); arr++)
  609. {
  610. clonable = dynamic_cast<Clonable*>(obj);
  611. if (clonable)
  612. {
  613. tmpObj = dynamic_cast<Ref*>(clonable->clone());
  614. if (tmpObj)
  615. {
  616. ret->addObject(tmpObj);
  617. }
  618. }
  619. else
  620. {
  621. CCLOGWARN("%s isn't clonable.", typeid(*obj).name());
  622. }
  623. }
  624. return ret;
  625. }
  626. void __Array::acceptVisitor(DataVisitor &visitor)
  627. {
  628. visitor.visit(this);
  629. }
  630. #endif // uses ccArray
  631. NS_CC_END