arrayObject.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Torque Script Array Object
  4. //
  5. // by Daniel Neilsen - 14/10/03
  6. // [email protected]
  7. //
  8. //-----------------------------------------------------------------------------
  9. //
  10. // Installation:
  11. // 1) BACKUP EVERYTHING
  12. // 2) Copy this file into the /engine/console dir and add to your project
  13. // 3) Compile
  14. //
  15. //
  16. // Usage:
  17. //
  18. // In script you should create an array object like so
  19. // %myarray = new array();
  20. //
  21. // With this object you can then do a variety of functions. Some examples are:
  22. // add() - (string key, string value)Adds a new element to the end of an array
  23. // append() - (Array target)Appends the target array to the array object
  24. // count() - Get the number of elements in the array
  25. // countKey() - (string key)Get the number of times a particular key is found in the array
  26. // countValue() - (string value)Get the number of times a particular value is found in the array
  27. // crop() - (Array target)Removes elements with matching keys from array
  28. // duplicate() - (Array target)Alters array into an exact duplicate of the target array
  29. // echo() - Echos the array in the console
  30. // empty() - Emptys all elements from an array
  31. // erase() - (int index)Removes an element at a specific position from the array
  32. // getCurrent() - Gets the current pointer index
  33. // getIndexFromKey() - (string key)Search array from current position for the first matching key
  34. // getIndexFromValue() - (string value)Search array from current position for the first matching value
  35. // getKey() - (int index)Get the key of the array element at the submitted index
  36. // getValue() - (int index)Get the value of the array element at the submitted index
  37. // setKey() - (string key, int index)Set the key at the given index
  38. // setValue() - (string key, int index)Set the value at the given index
  39. // insert() - (string key, string value, int index)Adds a new element to a specified position in the array
  40. // moveFirst() - Moves array pointer to start of array
  41. // moveLast() - Moves array pointer to end of array
  42. // moveNext() - Moves array pointer to next position (returns -1 if cannot move)
  43. // movePrev() - Moves array pointer to prev position (returns -1 if cannot move)
  44. // pop_back() - Removes the last element from the array
  45. // pop_front() - Removes the first element from the array
  46. // push_back() - (string key, string value)Adds a new element to the end of an array
  47. // push_front() - (string key, string value)Adds a new element to the front of an array
  48. // sort() - (bool desc)Sorts the array by value (default ascending sort)
  49. // sorta() - Alpha sorts the array by value in ascending order
  50. // sortd() - Alpha sorts the array by value in descending order
  51. // sortkd() - Alpha sorts the array by key in descending order
  52. // sortka() - (bool desc)Sorts the array by key (default ascending sort)
  53. // sortn() - (bool desc)Sorts the array numerically by value (default ascending sort)
  54. // sortna() - Numerical sorts the array by value in ascending order
  55. // sortnd() - Numerical sorts the array by value in descending order
  56. // sortnka() - Numerical sorts the array by key in ascending order
  57. // sortnkd() - Numerical sorts the array by key in descending order
  58. // uniqueKey() - Removes any elements that have duplicated keys (leaving the first instance)
  59. // uniqueValue() - Removes any elements that have duplicated values (leaving the first instance)
  60. //
  61. //
  62. //-----------------------------------------------------------------------------
  63. #include "platform/platform.h"
  64. #include "sim/simBase.h"
  65. #include "console/consoleTypes.h"
  66. #include "math/mMathFn.h"
  67. #include "string/stringTable.h"
  68. static bool sIncreasing;
  69. class Array : public SimObject
  70. {
  71. typedef SimObject Parent;
  72. private:
  73. public:
  74. struct Element
  75. {
  76. StringTableEntry key;
  77. StringTableEntry value;
  78. };
  79. U32 mCurrentIndex;
  80. Vector<Element> mArray;
  81. Array();
  82. bool onAdd();
  83. void onRemove();
  84. DECLARE_CONOBJECT(Array);
  85. S32 getIndexFromValue(StringTableEntry value);
  86. S32 getIndexFromKey(StringTableEntry key);
  87. StringTableEntry getKeyFromIndex(U32 index);
  88. StringTableEntry getValueFromIndex(U32 index);
  89. U32 count();
  90. U32 countValue(StringTableEntry value);
  91. U32 countKey(StringTableEntry key);
  92. void push_back(StringTableEntry key, StringTableEntry value);
  93. void push_front(StringTableEntry key, StringTableEntry value);
  94. void insert(StringTableEntry key, StringTableEntry value, U32 index);
  95. void pop_back();
  96. void pop_front();
  97. void erase(U32 index);
  98. void empty();
  99. void uniqueValue();
  100. void uniqueKey();
  101. void duplicate(Array* obj);
  102. void crop(Array* obj);
  103. void append(Array* obj);
  104. void setKey(StringTableEntry key, U32 index);
  105. void setValue(StringTableEntry value, U32 index);
  106. void sort(bool valtest, bool desc, bool numeric);
  107. U32 moveFirst();
  108. U32 moveLast();
  109. U32 moveNext();
  110. U32 movePrev();
  111. U32 getCurrent();
  112. void echo();
  113. void moveIndex(U32 prev, U32 index);
  114. S32 getIndexFromKeyValue( StringTableEntry key, StringTableEntry value);
  115. };
  116. IMPLEMENT_CONOBJECT(Array);
  117. static S32 QSORT_CALLBACK valueCompare( const void* a, const void* b )
  118. {
  119. Array::Element *ea = (Array::Element *) (a);
  120. Array::Element *eb = (Array::Element *) (b);
  121. S32 result = dStricmp(ea->value, eb->value);
  122. return ( sIncreasing ? result : -result );
  123. }
  124. static S32 QSORT_CALLBACK valueNumCompare( const void* a, const void* b )
  125. {
  126. Array::Element *ea = (Array::Element *) (a);
  127. Array::Element *eb = (Array::Element *) (b);
  128. F32 aCol = dAtof(ea->value);
  129. F32 bCol = dAtof(eb->value);
  130. F32 result = aCol - bCol;
  131. S32 res = result < 0 ? -1 : (result > 0 ? 1 : 0);
  132. return ( sIncreasing ? res : -res );
  133. }
  134. static S32 QSORT_CALLBACK keyCompare( const void* a, const void* b )
  135. {
  136. Array::Element *ea = (Array::Element *) (a);
  137. Array::Element *eb = (Array::Element *) (b);
  138. S32 result = dStricmp(ea->key, eb->key);
  139. return ( sIncreasing ? result : -result );
  140. }
  141. static S32 QSORT_CALLBACK keyNumCompare( const void* a, const void* b )
  142. {
  143. Array::Element *ea = (Array::Element *) (a);
  144. Array::Element *eb = (Array::Element *) (b);
  145. const char* aCol = ea->key;
  146. const char* bCol = eb->key;
  147. F32 result = dAtof(aCol) - dAtof(bCol);
  148. S32 res = result < 0 ? -1 : (result > 0 ? 1 : 0);
  149. return ( sIncreasing ? res : -res );
  150. }
  151. //-----------------------------------------------------------------------
  152. // Object Init
  153. Array::Array()
  154. {
  155. mCurrentIndex = 0;
  156. }
  157. bool Array::onAdd()
  158. {
  159. if(!Parent::onAdd())
  160. return false;
  161. return true;
  162. }
  163. void Array::onRemove()
  164. {
  165. //mArray.empty();
  166. empty(); // Fix per forums (.empty() returns a boolean check if the vector
  167. // is empty or not, but doesn't actually remove it)
  168. Parent::onRemove();
  169. }
  170. //---------------------------------------------------------------------
  171. // Data query functions
  172. S32 Array::getIndexFromValue( StringTableEntry value)
  173. {
  174. for(U32 i=mCurrentIndex; i<mArray.size(); i++)
  175. {
  176. if(mArray[i].value == value)
  177. {
  178. return i;
  179. }
  180. }
  181. for(U32 i=0; i<mCurrentIndex; i++)
  182. {
  183. if(mArray[i].value == value)
  184. {
  185. return i;
  186. }
  187. }
  188. return -1;
  189. }
  190. S32 Array::getIndexFromKey( StringTableEntry key)
  191. {
  192. for(U32 i=mCurrentIndex; i<mArray.size(); i++)
  193. {
  194. if(mArray[i].key == key)
  195. {
  196. return i;
  197. }
  198. }
  199. for(U32 i=0; i<mCurrentIndex; i++)
  200. {
  201. if(mArray[i].key == key)
  202. {
  203. return i;
  204. }
  205. }
  206. return -1;
  207. }
  208. S32 Array::getIndexFromKeyValue( StringTableEntry key, StringTableEntry value)
  209. {
  210. for(U32 i=mCurrentIndex; i<mArray.size(); i++)
  211. {
  212. if(mArray[i].key == key && mArray[i].value == value)
  213. {
  214. return i;
  215. }
  216. }
  217. for(U32 i=0; i<mCurrentIndex; i++)
  218. {
  219. if(mArray[i].key == key && mArray[i].value == value)
  220. {
  221. return i;
  222. }
  223. }
  224. return -1;
  225. }
  226. // Returns the key for a given index.
  227. // Will return a null value for an invalid index
  228. StringTableEntry Array::getKeyFromIndex(U32 index)
  229. {
  230. if(index >= mArray.size() || index < 0)
  231. return NULL;
  232. return mArray[index].key;
  233. }
  234. // Returns the value for a given index.
  235. // Will return a null value for an invalid index
  236. StringTableEntry Array::getValueFromIndex(U32 index)
  237. {
  238. if(index >= mArray.size() || index < 0)
  239. return NULL;
  240. return mArray[index].value;
  241. }
  242. // Counts the number of elements in the array
  243. U32 Array::count()
  244. {
  245. return mArray.size();
  246. }
  247. // Counts the number of instances of a particular value in the array
  248. U32 Array::countValue(StringTableEntry value)
  249. {
  250. U32 count = 0;
  251. for(U32 i=0; i<mArray.size(); i++)
  252. {
  253. if(mArray[i].value == value)
  254. count++;
  255. }
  256. return count;
  257. }
  258. // Counts the number of instances of a particular key in the array
  259. U32 Array::countKey(StringTableEntry key)
  260. {
  261. U32 count = 0;
  262. for(U32 i=0; i<mArray.size(); i++)
  263. {
  264. if(mArray[i].key == key)
  265. count++;
  266. }
  267. return count;
  268. }
  269. //---------------------------------------------------------------------
  270. // Basic Data Adding / Removing Functions
  271. // Adds a new array item to the end of the array
  272. void Array::push_back(StringTableEntry key, StringTableEntry value)
  273. {
  274. Element temp;
  275. temp.value = value;
  276. temp.key = key;
  277. mArray.push_back(temp);
  278. }
  279. // Adds a new array item to the front of the array
  280. void Array::push_front(StringTableEntry key, StringTableEntry value)
  281. {
  282. Element temp;
  283. temp.value = value;
  284. temp.key = key;
  285. mArray.push_front(temp);
  286. }
  287. // Adds a new array item to a particular index of the array
  288. void Array::insert(StringTableEntry key, StringTableEntry value, U32 index)
  289. {
  290. index = mClampF(index, 0, mArray.size()-1);
  291. S32 size = mArray.size() - 1;
  292. for(S32 i=size; i>=index; i--)
  293. {
  294. moveIndex(i, i+1);
  295. }
  296. Element temp;
  297. temp.value = value;
  298. temp.key = key;
  299. mArray[index] = temp;
  300. }
  301. // Removes an array item from the end of the array
  302. void Array::pop_back()
  303. {
  304. if(mArray.size() <= 0)
  305. return;
  306. mArray.pop_back();
  307. if(mCurrentIndex >= mArray.size())
  308. mCurrentIndex = mArray.size()-1;
  309. }
  310. // Removes an array item from the end of the array
  311. void Array::pop_front()
  312. {
  313. if(mArray.size() <= 0)
  314. return;
  315. mArray.pop_front();
  316. if(mCurrentIndex >= mArray.size())
  317. mCurrentIndex = mArray.size()-1;
  318. }
  319. // Removes an array item from a particular index of the array
  320. void Array::erase(U32 index)
  321. {
  322. if(index < 0 || index >= mArray.size())
  323. return;
  324. mArray.erase(index);
  325. }
  326. // Clears an array
  327. void Array::empty()
  328. {
  329. U32 size = mArray.size();
  330. for(U32 i=0; i<size; i++)
  331. mArray.pop_front();
  332. mCurrentIndex = 0;
  333. }
  334. // Moves a key and value from one index location to another.
  335. void Array::moveIndex(U32 prev, U32 index)
  336. {
  337. if(index >= mArray.size())
  338. push_back(mArray[prev].key, mArray[prev].value);
  339. else
  340. mArray[index] = mArray[prev];
  341. mArray[prev].value = NULL;
  342. mArray[prev].key = NULL;
  343. }
  344. //---------------------------------------------------------------------
  345. // Complex Data Alteration Functions
  346. // Removes any duplicate values from the array
  347. // (keeps the first instance only)
  348. void Array::uniqueValue()
  349. {
  350. for(U32 i=0; i<mArray.size(); i++)
  351. {
  352. for(U32 j=i+1; j<mArray.size(); j++)
  353. {
  354. if(mArray[i].value == mArray[j].value)
  355. {
  356. erase(j);
  357. j--;
  358. }
  359. }
  360. }
  361. }
  362. // Removes any duplicate keys from the array
  363. // (keeps the first instance only)
  364. void Array::uniqueKey()
  365. {
  366. for(U32 i=0; i<mArray.size(); i++)
  367. {
  368. for(U32 j=i+1; j<mArray.size(); j++)
  369. {
  370. if(mArray[i].key == mArray[j].key)
  371. {
  372. erase(j);
  373. j--;
  374. }
  375. }
  376. }
  377. }
  378. // Makes this array an exact duplicate of another array
  379. void Array::duplicate(Array* obj)
  380. {
  381. empty();
  382. for(U32 i=0; i<obj->count(); i++)
  383. {
  384. StringTableEntry tempval = obj->getValueFromIndex(i);
  385. StringTableEntry tempkey = obj->getKeyFromIndex(i);
  386. push_back(tempkey, tempval);
  387. }
  388. mCurrentIndex = obj->getCurrent();
  389. }
  390. // Crops the keys that exists in the target array from our current array
  391. void Array::crop(Array* obj)
  392. {
  393. for(U32 i=0; i<obj->count(); i++)
  394. {
  395. StringTableEntry tempkey = obj->getKeyFromIndex(i);
  396. for(U32 j=0; j<mArray.size(); j++)
  397. {
  398. if(mArray[j].key == tempkey)
  399. {
  400. mArray.erase(j);
  401. j--;
  402. }
  403. }
  404. }
  405. }
  406. // Appends the target array to our current array
  407. void Array::append(Array* obj)
  408. {
  409. for(U32 i=0; i<obj->count(); i++)
  410. {
  411. StringTableEntry tempval = obj->getValueFromIndex(i);
  412. StringTableEntry tempkey = obj->getKeyFromIndex(i);
  413. push_back(tempkey, tempval);
  414. }
  415. }
  416. // Sets the key at the given index
  417. void Array::setKey(StringTableEntry key, U32 index)
  418. {
  419. if(index >= mArray.size())
  420. return;
  421. mArray[index].key = key;
  422. }
  423. // Sets the key at the given index
  424. void Array::setValue(StringTableEntry value, U32 index)
  425. {
  426. if(index >= mArray.size())
  427. return;
  428. mArray[index].value = value;
  429. }
  430. //---------------------------------------------------------------------
  431. // Sorting
  432. // Sorts the array
  433. // First variable determines whether sorting by value or key
  434. // Second variable determines if sorting ascending or descending
  435. // Third variable determines if alpha or numeric search
  436. void Array::sort(bool valsort, bool desc, bool numeric)
  437. {
  438. sIncreasing = desc ? false : true;
  439. if(numeric)
  440. {
  441. if(valsort)
  442. dQsort((void *)&(mArray[0]), mArray.size(), sizeof(Element), valueNumCompare);
  443. else
  444. dQsort((void *)&(mArray[0]), mArray.size(), sizeof(Element), keyNumCompare);
  445. }
  446. else
  447. {
  448. if(valsort)
  449. dQsort((void *)&(mArray[0]), mArray.size(), sizeof(Element), valueCompare);
  450. else
  451. dQsort((void *)&(mArray[0]), mArray.size(), sizeof(Element), keyCompare);
  452. }
  453. }
  454. //---------------------------------------------------------------------
  455. // Pointer Manipulation Functions
  456. // Moves pointer to arrays first position
  457. U32 Array::moveFirst()
  458. {
  459. mCurrentIndex = 0;
  460. return mCurrentIndex;
  461. }
  462. // Moves pointer to arrays last position
  463. U32 Array::moveLast()
  464. {
  465. mCurrentIndex = mArray.size()-1;
  466. return mCurrentIndex;
  467. }
  468. // Moves pointer to arrays next position
  469. // If last position it returns -1 and no move occurs;
  470. U32 Array::moveNext()
  471. {
  472. if(mCurrentIndex >= mArray.size()-1)
  473. return -1;
  474. mCurrentIndex++;
  475. return mCurrentIndex;
  476. }
  477. // Moves pointer to arrays prev position
  478. // If first position it returns -1 and no move occurs;
  479. U32 Array::movePrev()
  480. {
  481. if(mCurrentIndex <= 0)
  482. return -1;
  483. mCurrentIndex--;
  484. return mCurrentIndex;
  485. }
  486. // Returns current pointer index
  487. U32 Array::getCurrent()
  488. {
  489. return mCurrentIndex;
  490. }
  491. //---------------------------------------------------------------------
  492. // Data Listing Functions
  493. // Echos the array to console
  494. void Array::echo()
  495. {
  496. Con::printf("Array Listing:");
  497. Con::printf("Index Key Value");
  498. for(U32 i=0; i < mArray.size(); i++)
  499. {
  500. StringTableEntry key = mArray[i].key;
  501. StringTableEntry val = mArray[i].value;
  502. Con::printf("%d [%s] => %s",(U32)i, key, val);
  503. }
  504. }
  505. //---------------------------------------------------------------------
  506. // Console Functions
  507. ConsoleMethod( Array, getIndexFromValue, S32, 3, 3, "(string value)"
  508. "Search array from current position for the first matching value")
  509. {
  510. StringTableEntry value = StringTable->insert(argv[2]);
  511. return (S32)object->getIndexFromValue(value);
  512. }
  513. ConsoleMethod( Array, getIndexFromKey, S32, 3, 3, "(string key)"
  514. "Search array from current position for the first matching key")
  515. {
  516. StringTableEntry value = StringTable->insert(argv[2]);
  517. return (S32)object->getIndexFromKey(value);
  518. }
  519. ConsoleMethod( Array, getValue, const char*, 3, 3, "(int index)"
  520. "Get the value of the array element at the submitted index")
  521. {
  522. StringTableEntry ret = object->getValueFromIndex(dAtoi(argv[2]));
  523. return ret; // oxe 20081103 - no need to use the return buffer mechanism because StringTableEntries are write-once read-many.
  524. }
  525. ConsoleMethod( Array, getKey, const char*, 3, 3, "(int index)"
  526. "Get the key of the array element at the submitted index")
  527. {
  528. StringTableEntry ret = object->getKeyFromIndex(dAtoi(argv[2]));
  529. return ret; // oxe 20081103 - no need to use the return buffer mechanism because StringTableEntries are write-once read-many.
  530. }
  531. ConsoleMethod( Array, setKey, void, 4, 4, "(string key, int index)"
  532. "Set the key at the given index")
  533. {
  534. StringTableEntry key = StringTable->insert(argv[2]);
  535. U32 index = dAtoi(argv[3]);
  536. object->setKey(key, index);
  537. }
  538. ConsoleMethod( Array, setValue, void, 4, 4, "(string key, int index)"
  539. "Set the value at the given index")
  540. {
  541. StringTableEntry value = StringTable->insert(argv[2]);
  542. U32 index = dAtoi(argv[3]);
  543. object->setValue(value, index);
  544. }
  545. ConsoleMethod( Array, count, S32, 2, 2, "Get the number of elements in the array")
  546. {
  547. return (S32)object->count();
  548. }
  549. ConsoleMethod( Array, countValue, S32, 3, 3, "(string value)"
  550. "Get the number of times a particular value is found in the array")
  551. {
  552. StringTableEntry value = StringTable->insert(argv[2]);
  553. return (S32)object->countValue(value);
  554. }
  555. ConsoleMethod( Array, countKey, S32, 3, 3, "(string key)"
  556. "Get the number of times a particular key is found in the array")
  557. {
  558. StringTableEntry value = StringTable->insert(argv[2]);
  559. return (S32)object->countKey(value);
  560. }
  561. ConsoleMethod( Array, add, void, 4, 4, "(string key, string value)"
  562. "Adds a new element to the end of an array")
  563. {
  564. StringTableEntry key = StringTable->insert(argv[2]);
  565. StringTableEntry value = StringTable->insert(argv[3]);
  566. object->push_back(key, value);
  567. }
  568. ConsoleMethod( Array, push_back, void, 4, 4, "(string key, string value)"
  569. "Adds a new element to the end of an array")
  570. {
  571. StringTableEntry key = StringTable->insert(argv[2]);
  572. StringTableEntry value = StringTable->insert(argv[3]);
  573. object->push_back(key, value);
  574. }
  575. ConsoleMethod( Array, push_front, void, 4, 4, "(string key, string value)"
  576. "Adds a new element to the front of an array")
  577. {
  578. StringTableEntry key = StringTable->insert(argv[2]);
  579. StringTableEntry value = StringTable->insert(argv[3]);
  580. object->push_front(key, value);
  581. }
  582. ConsoleMethod( Array, insert, void, 5, 5, "(string key, string value, int index)"
  583. "Adds a new element to a specified position in the array")
  584. {
  585. StringTableEntry key = StringTable->insert(argv[2]);
  586. StringTableEntry value = StringTable->insert(argv[3]);
  587. object->insert(key, value, dAtoi(argv[4]));
  588. }
  589. ConsoleMethod( Array, pop_back, void, 2, 2, "Removes the last element from the array")
  590. {
  591. object->pop_back();
  592. }
  593. ConsoleMethod( Array, pop_front, void, 2, 2, "Removes the first element from the array")
  594. {
  595. object->pop_front();
  596. }
  597. ConsoleMethod( Array, erase, void, 3, 3, "(int index)"
  598. "Removes an element at a specific position from the array")
  599. {
  600. object->erase(dAtoi(argv[2]));
  601. }
  602. ConsoleMethod( Array, empty, void, 2, 2, "Emptys all elements from an array")
  603. {
  604. object->empty();
  605. }
  606. ConsoleMethod( Array, uniqueValue, void, 2, 2, "Removes any elements that have duplicated values (leaving the first instance)")
  607. {
  608. object->uniqueValue();
  609. }
  610. ConsoleMethod( Array, uniqueKey, void, 2, 2, "Removes any elements that have duplicated keys (leaving the first instance)")
  611. {
  612. object->uniqueKey();
  613. }
  614. ConsoleMethod( Array, duplicate, bool, 3, 3, "(Array target)"
  615. "Alters array into an exact duplicate of the target array")
  616. {
  617. Array *target;
  618. if (Sim::findObject(argv[2],target))
  619. {
  620. object->duplicate(target);
  621. return true;
  622. }
  623. return false;
  624. }
  625. ConsoleMethod( Array, crop, bool, 3, 3, "(Array target)"
  626. "Removes elements with matching keys from array")
  627. {
  628. Array *target;
  629. if (Sim::findObject(argv[2],target))
  630. {
  631. object->crop(target);
  632. return true;
  633. }
  634. return false;
  635. }
  636. ConsoleMethod( Array, append, bool, 3, 3, "(Array target)"
  637. "Appends the target array to the array object")
  638. {
  639. Array *target;
  640. if (Sim::findObject(argv[2],target))
  641. {
  642. object->append(target);
  643. return true;
  644. }
  645. return false;
  646. }
  647. ConsoleMethod( Array, sort, void, 2, 3, "(bool desc)"
  648. "Alpha sorts the array by value (default ascending sort)")
  649. {
  650. bool descending = argc == 3 ? dAtob(argv[2]) : false;
  651. object->sort(true,descending,false);
  652. }
  653. ConsoleMethod( Array, sorta, void, 2, 2, "Alpha sorts the array by value in ascending order")
  654. {
  655. object->sort(true,false,false);
  656. }
  657. ConsoleMethod( Array, sortd, void, 2, 2, "Alpha sorts the array by value in descending order")
  658. {
  659. object->sort(true,true,false);
  660. }
  661. ConsoleMethod( Array, sortk, void, 2, 3, "(bool desc)"
  662. "Alpha sorts the array by key (default ascending sort)")
  663. {
  664. bool descending = argc == 3 ? dAtob(argv[2]) : false;
  665. object->sort(false,descending,false);
  666. }
  667. ConsoleMethod( Array, sortka, void, 2, 2, "Alpha sorts the array by key in ascending order")
  668. {
  669. object->sort(false,false,false);
  670. }
  671. ConsoleMethod( Array, sortkd, void, 2, 2, "Alpha sorts the array by key in descending order")
  672. {
  673. object->sort(false,true,false);
  674. }
  675. ConsoleMethod( Array, sortn, void, 2, 3, "(bool desc)"
  676. "Numerically sorts the array by value (default ascending sort)")
  677. {
  678. bool descending = argc == 3 ? dAtob(argv[2]) : false;
  679. object->sort(true,descending,true);
  680. }
  681. ConsoleMethod( Array, sortna, void, 2, 2, "Numerically sorts the array by value in ascending order")
  682. {
  683. object->sort(true,false,true);
  684. }
  685. ConsoleMethod( Array, sortnd, void, 2, 2, "Numerically sorts the array by value in descending order")
  686. {
  687. object->sort(true,true,true);
  688. }
  689. ConsoleMethod( Array, sortnk, void, 2, 3, "(bool desc)"
  690. "Numerically sorts the array by key (default ascending sort)")
  691. {
  692. bool descending = argc == 3 ? dAtob(argv[2]) : false;
  693. object->sort(false,descending,true);
  694. }
  695. ConsoleMethod( Array, sortnka, void, 2, 2, "Numerical sorts the array by key in ascending order")
  696. {
  697. object->sort(false,false,true);
  698. }
  699. ConsoleMethod( Array, sortnkd, void, 2, 2, "Numerical sorts the array by key in descending order")
  700. {
  701. object->sort(false,true,true);
  702. }
  703. ConsoleMethod( Array, moveFirst, S32, 2, 2, "Moves array pointer to start of array")
  704. {
  705. return object->moveFirst();
  706. }
  707. ConsoleMethod( Array, moveLast, S32, 2, 2, "Moves array pointer to end of array")
  708. {
  709. return object->moveLast();
  710. }
  711. ConsoleMethod( Array, moveNext, S32, 2, 2, "Moves array pointer to next position (returns -1 if cannot move)")
  712. {
  713. return object->moveNext();
  714. }
  715. ConsoleMethod( Array, movePrev, S32, 2, 2, "Moves array pointer to prev position (returns -1 if cannot move)")
  716. {
  717. return object->movePrev();
  718. }
  719. ConsoleMethod( Array, getCurrent, S32, 2, 2, "Gets the current pointer index")
  720. {
  721. return object->getCurrent();
  722. }
  723. ConsoleMethod( Array, echo, void, 2, 2, "Echos the array in the console")
  724. {
  725. object->echo();
  726. }