arrayObject.cpp 27 KB

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