arrayObject.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/arrayObject.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "math/mMathFn.h"
  27. IMPLEMENT_CONOBJECT(ArrayObject);
  28. ConsoleDocClass( ArrayObject,
  29. "@brief Data structure for storing indexed sequences of key/value pairs.\n\n"
  30. "This is a powerful array class providing PHP style arrays in TorqueScript.\n\n"
  31. "The following features are supported:<ul>\n"
  32. "<li>array pointers: this allows you to move forwards or backwards through "
  33. "the array as if it was a list, including jumping to the start or end.</li>\n"
  34. "<li>sorting: the array can be sorted in either alphabetic or numeric mode, "
  35. "on the key or the value, and in ascending or descending order</li>\n"
  36. "<li>add/remove elements: elements can be pushed/popped from the start or "
  37. "end of the array, or can be inserted/erased from anywhere in the middle</li>\n"
  38. "<li>removal of duplicates: remove duplicate keys or duplicate values</li>\n"
  39. "<li>searching: search the array and return the index of a particular key or "
  40. "value</li>\n"
  41. "<li>counting: count the number of instaces of a particular value or key in "
  42. "the array, as well as the total number of elements</li>\n"
  43. "<li>advanced features: array append, array crop and array duplicate</li>\n"
  44. "</ul>\n\n"
  45. "Array element keys and values can be strings or numbers\n\n"
  46. "@ingroup Scripting"
  47. );
  48. bool ArrayObject::smDecreasing = false;
  49. bool ArrayObject::smCaseSensitive = false;
  50. const char* ArrayObject::smCompareFunction;
  51. S32 QSORT_CALLBACK ArrayObject::_valueCompare( const void* a, const void* b )
  52. {
  53. ArrayObject::Element *ea = (ArrayObject::Element *) (a);
  54. ArrayObject::Element *eb = (ArrayObject::Element *) (b);
  55. S32 result = smCaseSensitive ? dStrnatcmp(ea->value, eb->value) : dStrnatcasecmp(ea->value, eb->value);
  56. return ( smDecreasing ? -result : result );
  57. }
  58. S32 QSORT_CALLBACK ArrayObject::_valueNumCompare( const void* a, const void* b )
  59. {
  60. ArrayObject::Element *ea = (ArrayObject::Element *) (a);
  61. ArrayObject::Element *eb = (ArrayObject::Element *) (b);
  62. F32 aCol = dAtof(ea->value);
  63. F32 bCol = dAtof(eb->value);
  64. F32 result = aCol - bCol;
  65. S32 res = result < 0 ? -1 : (result > 0 ? 1 : 0);
  66. return ( smDecreasing ? -res : res );
  67. }
  68. S32 QSORT_CALLBACK ArrayObject::_keyCompare( const void* a, const void* b )
  69. {
  70. ArrayObject::Element *ea = (ArrayObject::Element *) (a);
  71. ArrayObject::Element *eb = (ArrayObject::Element *) (b);
  72. S32 result = smCaseSensitive ? dStrnatcmp(ea->key, eb->key) : dStrnatcasecmp(ea->key, eb->key);
  73. return ( smDecreasing ? -result : result );
  74. }
  75. S32 QSORT_CALLBACK ArrayObject::_keyNumCompare( const void* a, const void* b )
  76. {
  77. ArrayObject::Element *ea = (ArrayObject::Element *) (a);
  78. ArrayObject::Element *eb = (ArrayObject::Element *) (b);
  79. const char* aCol = ea->key;
  80. const char* bCol = eb->key;
  81. F32 result = dAtof(aCol) - dAtof(bCol);
  82. S32 res = result < 0 ? -1 : (result > 0 ? 1 : 0);
  83. return ( smDecreasing ? -res : res );
  84. }
  85. S32 QSORT_CALLBACK ArrayObject::_keyFunctionCompare( const void* a, const void* b )
  86. {
  87. ArrayObject::Element* ea = ( ArrayObject::Element* )( a );
  88. ArrayObject::Element* eb = ( ArrayObject::Element* )( b );
  89. S32 result = dAtoi(Con::executef((const char*)smCompareFunction, ea->key, eb->key));
  90. S32 res = result < 0 ? -1 : ( result > 0 ? 1 : 0 );
  91. return ( smDecreasing ? -res : res );
  92. }
  93. S32 QSORT_CALLBACK ArrayObject::_valueFunctionCompare( const void* a, const void* b )
  94. {
  95. ArrayObject::Element* ea = ( ArrayObject::Element* )( a );
  96. ArrayObject::Element* eb = ( ArrayObject::Element* )( b );
  97. S32 result = dAtoi( Con::executef( (const char*)smCompareFunction, ea->value, eb->value ) );
  98. S32 res = result < 0 ? -1 : ( result > 0 ? 1 : 0 );
  99. return ( smDecreasing ? -res : res );
  100. }
  101. //-----------------------------------------------------------------------------
  102. ArrayObject::ArrayObject()
  103. : mCurrentIndex( NULL ),
  104. mCaseSensitive( false )
  105. {
  106. }
  107. //-----------------------------------------------------------------------------
  108. void ArrayObject::initPersistFields()
  109. {
  110. addField( "caseSensitive", TypeBool, Offset( mCaseSensitive, ArrayObject ),
  111. "Makes the keys and values case-sensitive.\n"
  112. "By default, comparison of key and value strings will be case-insensitive." );
  113. addProtectedField( "key", TypeCaseString, NULL, &_addKeyFromField, &emptyStringProtectedGetFn,
  114. "Helper field which allows you to add new key['keyname'] = value pairs." );
  115. Parent::initPersistFields();
  116. }
  117. //-----------------------------------------------------------------------------
  118. bool ArrayObject::_addKeyFromField( void *object, const char *index, const char *data )
  119. {
  120. static_cast<ArrayObject*>( object )->push_back( index, data );
  121. return false;
  122. }
  123. //-----------------------------------------------------------------------------
  124. S32 ArrayObject::getIndexFromValue( const String &value ) const
  125. {
  126. S32 foundIndex = -1;
  127. for ( S32 i = mCurrentIndex; i < mArray.size(); i++ )
  128. {
  129. if ( isEqual( mArray[i].value, value ) )
  130. {
  131. foundIndex = i;
  132. break;
  133. }
  134. }
  135. if( foundIndex < 0 )
  136. {
  137. for ( S32 i = 0; i < mCurrentIndex; i++ )
  138. {
  139. if ( isEqual( mArray[i].value, value ) )
  140. {
  141. foundIndex = i;
  142. break;
  143. }
  144. }
  145. }
  146. return foundIndex;
  147. }
  148. //-----------------------------------------------------------------------------
  149. S32 ArrayObject::getIndexFromKey( const String &key ) const
  150. {
  151. S32 foundIndex = -1;
  152. for ( S32 i = mCurrentIndex; i < mArray.size(); i++ )
  153. {
  154. if ( isEqual( mArray[i].key, key ) )
  155. {
  156. foundIndex = i;
  157. break;
  158. }
  159. }
  160. if( foundIndex < 0 )
  161. {
  162. for ( S32 i = 0; i < mCurrentIndex; i++ )
  163. {
  164. if ( isEqual( mArray[i].key, key ) )
  165. {
  166. foundIndex = i;
  167. break;
  168. }
  169. }
  170. }
  171. return foundIndex;
  172. }
  173. //-----------------------------------------------------------------------------
  174. S32 ArrayObject::getIndexFromKeyValue( const String &key, const String &value ) const
  175. {
  176. S32 foundIndex = -1;
  177. for ( S32 i = mCurrentIndex; i < mArray.size(); i++ )
  178. {
  179. if ( isEqual( mArray[i].key, key ) && isEqual( mArray[i].value, value ) )
  180. {
  181. foundIndex = i;
  182. break;
  183. }
  184. }
  185. if ( foundIndex < 0 )
  186. {
  187. for ( S32 i = 0; i < mCurrentIndex; i++ )
  188. {
  189. if ( isEqual( mArray[i].key, key ) && isEqual( mArray[i].value, value ) )
  190. {
  191. foundIndex = i;
  192. break;
  193. }
  194. }
  195. }
  196. return foundIndex;
  197. }
  198. //-----------------------------------------------------------------------------
  199. const String& ArrayObject::getKeyFromIndex( S32 index ) const
  200. {
  201. if ( index >= mArray.size() || index < 0 )
  202. return String::EmptyString;
  203. return mArray[index].key;
  204. }
  205. //-----------------------------------------------------------------------------
  206. const String& ArrayObject::getValueFromIndex( S32 index ) const
  207. {
  208. if( index >= mArray.size() || index < 0 )
  209. return String::EmptyString;
  210. return mArray[index].value;
  211. }
  212. //-----------------------------------------------------------------------------
  213. S32 ArrayObject::countValue( const String &value ) const
  214. {
  215. S32 count = 0;
  216. for ( S32 i = 0; i < mArray.size(); i++ )
  217. {
  218. if ( isEqual( mArray[i].value, value ) )
  219. count++;
  220. }
  221. return count;
  222. }
  223. //-----------------------------------------------------------------------------
  224. S32 ArrayObject::countKey( const String &key) const
  225. {
  226. S32 count = 0;
  227. for ( S32 i = 0; i < mArray.size(); i++ )
  228. {
  229. if ( isEqual( mArray[i].key, key ) )
  230. count++;
  231. }
  232. return count;
  233. }
  234. //-----------------------------------------------------------------------------
  235. void ArrayObject::push_back( const String &key, const String &value )
  236. {
  237. mArray.push_back( Element( key, value ) );
  238. }
  239. //-----------------------------------------------------------------------------
  240. void ArrayObject::push_front( const String &key, const String &value )
  241. {
  242. mArray.push_front( Element( key, value ) );
  243. }
  244. //-----------------------------------------------------------------------------
  245. void ArrayObject::insert( const String &key, const String &value, S32 index )
  246. {
  247. index = mClamp( index, 0, mArray.size() );
  248. mArray.insert( index, Element( key, value ) );
  249. }
  250. //-----------------------------------------------------------------------------
  251. void ArrayObject::pop_back()
  252. {
  253. if(mArray.size() <= 0)
  254. return;
  255. mArray.pop_back();
  256. if( mCurrentIndex >= mArray.size() )
  257. mCurrentIndex = mArray.size() - 1;
  258. }
  259. //-----------------------------------------------------------------------------
  260. void ArrayObject::pop_front()
  261. {
  262. if( mArray.size() <= 0 )
  263. return;
  264. mArray.pop_front();
  265. if( mCurrentIndex >= mArray.size() )
  266. mCurrentIndex = mArray.size() - 1;
  267. }
  268. //-----------------------------------------------------------------------------
  269. void ArrayObject::erase( S32 index )
  270. {
  271. if(index < 0 || index >= mArray.size())
  272. return;
  273. mArray.erase( index );
  274. }
  275. //-----------------------------------------------------------------------------
  276. void ArrayObject::empty()
  277. {
  278. mArray.clear();
  279. mCurrentIndex = 0;
  280. }
  281. //-----------------------------------------------------------------------------
  282. void ArrayObject::moveIndex(S32 prev, S32 index)
  283. {
  284. if(index >= mArray.size())
  285. push_back(mArray[prev].key, mArray[prev].value);
  286. else
  287. mArray[index] = mArray[prev];
  288. mArray[prev].value = String::EmptyString;
  289. mArray[prev].key = String::EmptyString;
  290. }
  291. //-----------------------------------------------------------------------------
  292. void ArrayObject::uniqueValue()
  293. {
  294. for(S32 i=0; i<mArray.size(); i++)
  295. {
  296. for(S32 j=i+1; j<mArray.size(); j++)
  297. {
  298. if ( isEqual( mArray[i].value, mArray[j].value ) )
  299. {
  300. erase(j);
  301. j--;
  302. }
  303. }
  304. }
  305. }
  306. //-----------------------------------------------------------------------------
  307. void ArrayObject::uniqueKey()
  308. {
  309. for(S32 i=0; i<mArray.size(); i++)
  310. {
  311. for(S32 j=i+1; j<mArray.size(); j++)
  312. {
  313. if( isEqual( mArray[i].key, mArray[j].key ) )
  314. {
  315. erase(j);
  316. j--;
  317. }
  318. }
  319. }
  320. }
  321. //-----------------------------------------------------------------------------
  322. void ArrayObject::duplicate(ArrayObject* obj)
  323. {
  324. empty();
  325. for(S32 i=0; i<obj->count(); i++)
  326. {
  327. const String& tempval = obj->getValueFromIndex(i);
  328. const String& tempkey = obj->getKeyFromIndex(i);
  329. push_back(tempkey, tempval);
  330. }
  331. mCurrentIndex = obj->getCurrent();
  332. }
  333. //-----------------------------------------------------------------------------
  334. void ArrayObject::crop( ArrayObject *obj )
  335. {
  336. for( S32 i = 0; i < obj->count(); i++ )
  337. {
  338. const String &tempkey = obj->getKeyFromIndex( i );
  339. for( S32 j = 0; j < mArray.size(); j++ )
  340. {
  341. if( isEqual( mArray[j].key, tempkey ) )
  342. {
  343. mArray.erase( j );
  344. j--;
  345. }
  346. }
  347. }
  348. }
  349. //-----------------------------------------------------------------------------
  350. void ArrayObject::append(ArrayObject* obj)
  351. {
  352. for(S32 i=0; i<obj->count(); i++)
  353. {
  354. const String& tempval = obj->getValueFromIndex(i);
  355. const String& tempkey = obj->getKeyFromIndex(i);
  356. push_back(tempkey, tempval);
  357. }
  358. }
  359. //-----------------------------------------------------------------------------
  360. void ArrayObject::setKey( const String &key, S32 index )
  361. {
  362. if ( index >= mArray.size() )
  363. return;
  364. mArray[index].key = key;
  365. }
  366. //-----------------------------------------------------------------------------
  367. void ArrayObject::setValue( const String &value, S32 index )
  368. {
  369. if ( index >= mArray.size() )
  370. return;
  371. mArray[index].value = value;
  372. }
  373. //-----------------------------------------------------------------------------
  374. void ArrayObject::sort( bool valsort, bool asc, bool numeric )
  375. {
  376. if ( mArray.size() <= 1 )
  377. return;
  378. smDecreasing = asc ? false : true;
  379. smCaseSensitive = isCaseSensitive();
  380. if ( numeric )
  381. {
  382. if ( valsort )
  383. dQsort( (void *)&(mArray[0]), mArray.size(), sizeof(Element), _valueNumCompare) ;
  384. else
  385. dQsort( (void *)&(mArray[0]), mArray.size(), sizeof(Element), _keyNumCompare );
  386. }
  387. else
  388. {
  389. if( valsort )
  390. dQsort( (void *)&(mArray[0]), mArray.size(), sizeof(Element), _valueCompare );
  391. else
  392. dQsort( (void *)&(mArray[0]), mArray.size(), sizeof(Element), _keyCompare );
  393. }
  394. }
  395. //-----------------------------------------------------------------------------
  396. void ArrayObject::sort( bool valsort, bool asc, const char* callbackFunctionName )
  397. {
  398. if( mArray.size() <= 1 )
  399. return;
  400. smDecreasing = asc ? false : true;
  401. smCompareFunction = callbackFunctionName;
  402. if( valsort )
  403. dQsort( ( void* ) &( mArray[ 0 ] ), mArray.size(), sizeof( Element ), _valueFunctionCompare ) ;
  404. else
  405. dQsort( ( void* ) &( mArray[ 0 ] ), mArray.size(), sizeof( Element ), _keyFunctionCompare );
  406. smCompareFunction = NULL;
  407. }
  408. //-----------------------------------------------------------------------------
  409. S32 ArrayObject::moveFirst()
  410. {
  411. mCurrentIndex = 0;
  412. return mCurrentIndex;
  413. }
  414. //-----------------------------------------------------------------------------
  415. S32 ArrayObject::moveLast()
  416. {
  417. if ( mArray.empty() )
  418. mCurrentIndex = 0;
  419. else
  420. mCurrentIndex = mArray.size() - 1;
  421. return mCurrentIndex;
  422. }
  423. //-----------------------------------------------------------------------------
  424. S32 ArrayObject::moveNext()
  425. {
  426. if ( mCurrentIndex >= mArray.size() - 1 )
  427. return -1;
  428. mCurrentIndex++;
  429. return mCurrentIndex;
  430. }
  431. //-----------------------------------------------------------------------------
  432. S32 ArrayObject::movePrev()
  433. {
  434. if ( mCurrentIndex <= 0 )
  435. return -1;
  436. mCurrentIndex--;
  437. return mCurrentIndex;
  438. }
  439. //-----------------------------------------------------------------------------
  440. void ArrayObject::setCurrent( S32 idx )
  441. {
  442. if ( idx < 0 || idx >= mArray.size() )
  443. {
  444. Con::errorf( "ArrayObject::setCurrent( %d ) is out of the array bounds!", idx );
  445. return;
  446. }
  447. mCurrentIndex = idx;
  448. }
  449. //-----------------------------------------------------------------------------
  450. void ArrayObject::echo()
  451. {
  452. Con::printf( "ArrayObject Listing:" );
  453. Con::printf( "Index Key Value" );
  454. for ( U32 i = 0; i < mArray.size(); i++ )
  455. {
  456. const String& key = mArray[i].key;
  457. const String& val = mArray[i].value;
  458. Con::printf( "%d [%s] => %s", i, key.c_str(), val.c_str() );
  459. }
  460. }
  461. //=============================================================================
  462. // Console Methods.
  463. //=============================================================================
  464. DefineEngineMethod( ArrayObject, getIndexFromValue, S32, ( const char* value ),,
  465. "Search the array from the current position for the element "
  466. "@param value Array value to search for\n"
  467. "@return Index of the first element found, or -1 if none\n" )
  468. {
  469. return object->getIndexFromValue( value );
  470. }
  471. DefineEngineMethod( ArrayObject, getIndexFromKey, S32, ( const char* key ),,
  472. "Search the array from the current position for the key "
  473. "@param value Array key to search for\n"
  474. "@return Index of the first element found, or -1 if none\n" )
  475. {
  476. return object->getIndexFromKey( key );
  477. }
  478. DefineEngineMethod( ArrayObject, getValue, const char*, ( S32 index ),,
  479. "Get the value of the array element at the submitted index.\n"
  480. "@param index 0-based index of the array element to get\n"
  481. "@return The value of the array element at the specified index, "
  482. "or \"\" if the index is out of range\n" )
  483. {
  484. return object->getValueFromIndex( index ).c_str();
  485. }
  486. DefineEngineMethod( ArrayObject, getKey, const char*, ( S32 index ),,
  487. "Get the key of the array element at the submitted index.\n"
  488. "@param index 0-based index of the array element to get\n"
  489. "@return The key associated with the array element at the "
  490. "specified index, or \"\" if the index is out of range\n" )
  491. {
  492. return object->getKeyFromIndex( index ).c_str();
  493. }
  494. DefineEngineMethod( ArrayObject, setKey, void, ( const char* key, S32 index ),,
  495. "Set the key at the given index.\n"
  496. "@param key New key value\n"
  497. "@param index 0-based index of the array element to update\n" )
  498. {
  499. object->setKey( key, index );
  500. }
  501. DefineEngineMethod( ArrayObject, setValue, void, ( const char* value, S32 index ),,
  502. "Set the value at the given index.\n"
  503. "@param value New array element value\n"
  504. "@param index 0-based index of the array element to update\n" )
  505. {
  506. object->setValue( value, index );
  507. }
  508. DefineEngineMethod( ArrayObject, count, S32, (),,
  509. "Get the number of elements in the array." )
  510. {
  511. return (S32)object->count();
  512. }
  513. DefineEngineMethod( ArrayObject, countValue, S32, ( const char* value ),,
  514. "Get the number of times a particular value is found in the array.\n"
  515. "@param value Array element value to count\n" )
  516. {
  517. return (S32)object->countValue( value );
  518. }
  519. DefineEngineMethod( ArrayObject, countKey, S32, ( const char* key ),,
  520. "Get the number of times a particular key is found in the array.\n"
  521. "@param key Key value to count\n" )
  522. {
  523. return (S32)object->countKey( key );
  524. }
  525. DefineEngineMethod( ArrayObject, add, void, ( const char* key, const char* value ), ( "" ),
  526. "Adds a new element to the end of an array (same as push_back()).\n"
  527. "@param key Key for the new element\n"
  528. "@param value Value for the new element\n" )
  529. {
  530. object->push_back( key, value );
  531. }
  532. DefineEngineMethod( ArrayObject, push_back, void, ( const char* key, const char* value ), ( "" ),
  533. "Adds a new element to the end of an array.\n"
  534. "@param key Key for the new element\n"
  535. "@param value Value for the new element\n" )
  536. {
  537. object->push_back( key, value );
  538. }
  539. DefineEngineMethod( ArrayObject, push_front, void, ( const char* key, const char* value ), ( "" ),
  540. "Adds a new element to the front of an array" )
  541. {
  542. object->push_front( key, value );
  543. }
  544. DefineEngineMethod( ArrayObject, insert, void, ( const char* key, const char* value, S32 index ),,
  545. "Adds a new element to a specified position in the array.\n"
  546. "- @a index = 0 will insert an element at the start of the array (same as push_front())\n"
  547. "- @a index = %array.count() will insert an element at the end of the array (same as push_back())\n\n"
  548. "@param key Key for the new element\n"
  549. "@param value Value for the new element\n"
  550. "@param index 0-based index at which to insert the new element" )
  551. {
  552. object->insert( key, value, index );
  553. }
  554. DefineEngineMethod( ArrayObject, pop_back, void, (),,
  555. "Removes the last element from the array" )
  556. {
  557. object->pop_back();
  558. }
  559. DefineEngineMethod( ArrayObject, pop_front, void, (),,
  560. "Removes the first element from the array" )
  561. {
  562. object->pop_front();
  563. }
  564. DefineEngineMethod( ArrayObject, erase, void, ( S32 index ),,
  565. "Removes an element at a specific position from the array.\n"
  566. "@param index 0-based index of the element to remove\n" )
  567. {
  568. object->erase( index );
  569. }
  570. DefineEngineMethod( ArrayObject, empty, void, (),,
  571. "Emptys all elements from an array" )
  572. {
  573. object->empty();
  574. }
  575. DefineEngineMethod( ArrayObject, uniqueValue, void, (),,
  576. "Removes any elements that have duplicated values (leaving the first instance)" )
  577. {
  578. object->uniqueValue();
  579. }
  580. DefineEngineMethod( ArrayObject, uniqueKey, void, (),,
  581. "Removes any elements that have duplicated keys (leaving the first instance)" )
  582. {
  583. object->uniqueKey();
  584. }
  585. DefineEngineMethod( ArrayObject, duplicate, bool, ( ArrayObject* target ),,
  586. "Alters array into an exact duplicate of the target array.\n"
  587. "@param target ArrayObject to duplicate\n" )
  588. {
  589. if ( target )
  590. {
  591. object->duplicate( target );
  592. return true;
  593. }
  594. return false;
  595. }
  596. DefineEngineMethod( ArrayObject, crop, bool, ( ArrayObject* target ),,
  597. "Removes elements with matching keys from array.\n"
  598. "@param target ArrayObject containing keys to remove from this array\n" )
  599. {
  600. if ( target )
  601. {
  602. object->crop( target );
  603. return true;
  604. }
  605. return false;
  606. }
  607. DefineEngineMethod( ArrayObject, append, bool, ( ArrayObject* target ),,
  608. "Appends the target array to the array object.\n"
  609. "@param target ArrayObject to append to the end of this array\n" )
  610. {
  611. if ( target )
  612. {
  613. object->append( target );
  614. return true;
  615. }
  616. return false;
  617. }
  618. DefineEngineMethod( ArrayObject, sort, void, ( bool ascending ), ( false ),
  619. "Alpha sorts the array by value\n\n"
  620. "@param ascending [optional] True for ascending sort, false for descending sort\n" )
  621. {
  622. object->sort( true, ascending, false );
  623. }
  624. DefineEngineMethod( ArrayObject, sorta, void, (),,
  625. "Alpha sorts the array by value in ascending order" )
  626. {
  627. object->sort( true, true, false );
  628. }
  629. DefineEngineMethod( ArrayObject, sortd, void, (),,
  630. "Alpha sorts the array by value in descending order" )
  631. {
  632. object->sort( true, false, false );
  633. }
  634. DefineEngineMethod( ArrayObject, sortk, void, ( bool ascending ), ( false ),
  635. "Alpha sorts the array by key\n\n"
  636. "@param ascending [optional] True for ascending sort, false for descending sort\n" )
  637. {
  638. object->sort( false, ascending, false );
  639. }
  640. DefineEngineMethod( ArrayObject, sortka, void, (),,
  641. "Alpha sorts the array by key in ascending order" )
  642. {
  643. object->sort( false, true, false );
  644. }
  645. DefineEngineMethod( ArrayObject, sortkd, void, (),,
  646. "Alpha sorts the array by key in descending order" )
  647. {
  648. object->sort( false, false, false );
  649. }
  650. DefineEngineMethod( ArrayObject, sortn, void, ( bool ascending ), ( false ),
  651. "Numerically sorts the array by value\n\n"
  652. "@param ascending [optional] True for ascending sort, false for descending sort\n" )
  653. {
  654. object->sort( true, ascending, true );
  655. }
  656. DefineEngineMethod( ArrayObject, sortna, void, (),,
  657. "Numerically sorts the array by value in ascending order" )
  658. {
  659. object->sort( true, true, true );
  660. }
  661. DefineEngineMethod( ArrayObject, sortnd, void, (),,
  662. "Numerically sorts the array by value in descending order" )
  663. {
  664. object->sort( true, false, true );
  665. }
  666. DefineEngineMethod( ArrayObject, sortnk, void, ( bool ascending ), ( false ),
  667. "Numerically sorts the array by key\n\n"
  668. "@param ascending [optional] True for ascending sort, false for descending sort\n" )
  669. {
  670. object->sort( false, ascending, true );
  671. }
  672. DefineEngineMethod( ArrayObject, sortnka, void, (),,
  673. "Numerical sorts the array by key in ascending order" )
  674. {
  675. object->sort( false, true, true );
  676. }
  677. DefineEngineMethod( ArrayObject, sortnkd, void, (),,
  678. "Numerical sorts the array by key in descending order" )
  679. {
  680. object->sort( false, false, true );
  681. }
  682. DefineEngineMethod( ArrayObject, sortf, void, ( const char* functionName ),,
  683. "Sorts the array by value in ascending order using the given callback function.\n"
  684. "@param functionName Name of a function that takes two arguments A and B and returns -1 if A is less, 1 if B is less, and 0 if both are equal.\n\n"
  685. "@tsexample\n"
  686. "function mySortCallback(%a, %b)\n"
  687. "{\n"
  688. " return strcmp( %a.name, %b.name );\n"
  689. "}\n\n"
  690. "%array.sortf( \"mySortCallback\" );\n"
  691. "@endtsexample\n" )
  692. {
  693. object->sort( true, true, functionName );
  694. }
  695. DefineEngineMethod( ArrayObject, sortfk, void, ( const char* functionName ),,
  696. "Sorts the array by key in ascending order using the given callback function.\n"
  697. "@param functionName Name of a function that takes two arguments A and B and returns -1 if A is less, 1 if B is less, and 0 if both are equal."
  698. "@see sortf\n" )
  699. {
  700. object->sort( false, true, functionName );
  701. }
  702. DefineEngineMethod( ArrayObject, sortfd, void, ( const char* functionName ),,
  703. "Sorts the array by value in descending order using the given callback function.\n"
  704. "@param functionName Name of a function that takes two arguments A and B and returns -1 if A is less, 1 if B is less, and 0 if both are equal."
  705. "@see sortf\n" )
  706. {
  707. object->sort( true, false, functionName );
  708. }
  709. DefineEngineMethod( ArrayObject, sortfkd, void, ( const char* functionName ),,
  710. "Sorts the array by key in descending order using the given callback function.\n"
  711. "@param functionName Name of a function that takes two arguments A and B and returns -1 if A is less, 1 if B is less, and 0 if both are equal."
  712. "@see sortf\n" )
  713. {
  714. object->sort( false, false, functionName );
  715. }
  716. DefineEngineMethod( ArrayObject, moveFirst, S32, (),,
  717. "Moves array pointer to start of array\n\n"
  718. "@return Returns the new array pointer" )
  719. {
  720. return object->moveFirst();
  721. }
  722. DefineEngineMethod( ArrayObject, moveLast, S32, (),,
  723. "Moves array pointer to end of array\n\n"
  724. "@return Returns the new array pointer" )
  725. {
  726. return object->moveLast();
  727. }
  728. DefineEngineMethod( ArrayObject, moveNext, S32, (),,
  729. "Moves array pointer to next position\n\n"
  730. "@return Returns the new array pointer, or -1 if already at the end" )
  731. {
  732. return object->moveNext();
  733. }
  734. DefineEngineMethod( ArrayObject, movePrev, S32, (),,
  735. "Moves array pointer to prev position\n\n"
  736. "@return Returns the new array pointer, or -1 if already at the start" )
  737. {
  738. return object->movePrev();
  739. }
  740. DefineEngineMethod( ArrayObject, getCurrent, S32, (),,
  741. "Gets the current pointer index" )
  742. {
  743. return object->getCurrent();
  744. }
  745. DefineEngineMethod( ArrayObject, setCurrent, void, ( S32 index ),,
  746. "Sets the current pointer index.\n"
  747. "@param index New 0-based pointer index\n" )
  748. {
  749. object->setCurrent( index );
  750. }
  751. DefineEngineMethod( ArrayObject, echo, void, (),,
  752. "Echos the array contents to the console" )
  753. {
  754. object->echo();
  755. }