arrayObject.cpp 27 KB

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