1
0

CCArray.cpp 16 KB

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