arrayObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. //
  23. // adopted from original code by Daniel Neilsen - 14/10/03
  24. // [email protected]
  25. #include "platform/platform.h"
  26. #include "sim/simBase.h"
  27. #include "console/consoleTypes.h"
  28. #include "console/arrayObject.h"
  29. #include "math/mMathFn.h"
  30. #include "string/stringTable.h"
  31. // Script bindings.
  32. #include "arrayObject_ScriptBinding.h"
  33. IMPLEMENT_CONOBJECT(Array);
  34. static S32 QSORT_CALLBACK valueCompare( const void* a, const void* b )
  35. {
  36. Array::Element *ea = (Array::Element *) (a);
  37. Array::Element *eb = (Array::Element *) (b);
  38. S32 result = dStricmp(ea->value, eb->value);
  39. return ( sIncreasing ? result : -result );
  40. }
  41. static S32 QSORT_CALLBACK valueNumCompare( const void* a, const void* b )
  42. {
  43. Array::Element *ea = (Array::Element *) (a);
  44. Array::Element *eb = (Array::Element *) (b);
  45. F32 aCol = dAtof(ea->value);
  46. F32 bCol = dAtof(eb->value);
  47. F32 result = aCol - bCol;
  48. S32 res = result < 0 ? -1 : (result > 0 ? 1 : 0);
  49. return ( sIncreasing ? res : -res );
  50. }
  51. static S32 QSORT_CALLBACK keyCompare( const void* a, const void* b )
  52. {
  53. Array::Element *ea = (Array::Element *) (a);
  54. Array::Element *eb = (Array::Element *) (b);
  55. S32 result = dStricmp(ea->key, eb->key);
  56. return ( sIncreasing ? result : -result );
  57. }
  58. static S32 QSORT_CALLBACK keyNumCompare( const void* a, const void* b )
  59. {
  60. Array::Element *ea = (Array::Element *) (a);
  61. Array::Element *eb = (Array::Element *) (b);
  62. const char* aCol = ea->key;
  63. const char* bCol = eb->key;
  64. F32 result = dAtof(aCol) - dAtof(bCol);
  65. S32 res = result < 0 ? -1 : (result > 0 ? 1 : 0);
  66. return ( sIncreasing ? res : -res );
  67. }
  68. Array::Array()
  69. {
  70. mCurrentIndex = 0;
  71. }
  72. bool Array::onAdd()
  73. {
  74. if(!Parent::onAdd())
  75. return false;
  76. return true;
  77. }
  78. void Array::onRemove()
  79. {
  80. //mArray.empty();
  81. empty(); // Fix per forums (.empty() returns a boolean check if the vector
  82. // is empty or not, but doesn't actually remove it)
  83. Parent::onRemove();
  84. }
  85. S32 Array::getIndexFromValue( StringTableEntry value)
  86. {
  87. for(S32 i=mCurrentIndex; i<mArray.size(); i++)
  88. {
  89. if(mArray[i].value == value)
  90. {
  91. return i;
  92. }
  93. }
  94. for(S32 i=0; i<mCurrentIndex; i++)
  95. {
  96. if(mArray[i].value == value)
  97. {
  98. return i;
  99. }
  100. }
  101. return -1;
  102. }
  103. S32 Array::getIndexFromKey( StringTableEntry key)
  104. {
  105. for(S32 i=mCurrentIndex; i<mArray.size(); i++)
  106. {
  107. if(mArray[i].key == key)
  108. {
  109. return i;
  110. }
  111. }
  112. for(S32 i=0; i<mCurrentIndex; i++)
  113. {
  114. if(mArray[i].key == key)
  115. {
  116. return i;
  117. }
  118. }
  119. return -1;
  120. }
  121. S32 Array::getIndexFromKeyValue( StringTableEntry key, StringTableEntry value)
  122. {
  123. for(S32 i=mCurrentIndex; i<mArray.size(); i++)
  124. {
  125. if(mArray[i].key == key && mArray[i].value == value)
  126. {
  127. return i;
  128. }
  129. }
  130. for(S32 i=0; i<mCurrentIndex; i++)
  131. {
  132. if(mArray[i].key == key && mArray[i].value == value)
  133. {
  134. return i;
  135. }
  136. }
  137. return -1;
  138. }
  139. // Returns the key for a given index.
  140. // Will return a null value for an invalid index
  141. StringTableEntry Array::getKeyFromIndex(S32 index)
  142. {
  143. if(index >= mArray.size() || index < 0)
  144. return NULL;
  145. return mArray[index].key;
  146. }
  147. // Returns the value for a given index.
  148. // Will return a null value for an invalid index
  149. StringTableEntry Array::getValueFromIndex(S32 index)
  150. {
  151. if(index >= mArray.size() || index < 0)
  152. return NULL;
  153. return mArray[index].value;
  154. }
  155. // Counts the number of elements in the array
  156. S32 Array::count()
  157. {
  158. return mArray.size();
  159. }
  160. // Counts the number of instances of a particular value in the array
  161. S32 Array::countValue(StringTableEntry value)
  162. {
  163. S32 count = 0;
  164. for(S32 i=0; i<mArray.size(); i++)
  165. {
  166. if(mArray[i].value == value)
  167. count++;
  168. }
  169. return count;
  170. }
  171. // Counts the number of instances of a particular key in the array
  172. S32 Array::countKey(StringTableEntry key)
  173. {
  174. S32 count = 0;
  175. for(S32 i=0; i<mArray.size(); i++)
  176. {
  177. if(mArray[i].key == key)
  178. count++;
  179. }
  180. return count;
  181. }
  182. //---------------------------------------------------------------------
  183. // Basic Data Adding / Removing Functions
  184. // Adds a new array item to the end of the array
  185. void Array::push_back(StringTableEntry key, StringTableEntry value)
  186. {
  187. Element temp;
  188. temp.value = value;
  189. temp.key = key;
  190. mArray.push_back(temp);
  191. }
  192. // Adds a new array item to the front of the array
  193. void Array::push_front(StringTableEntry key, StringTableEntry value)
  194. {
  195. Element temp;
  196. temp.value = value;
  197. temp.key = key;
  198. mArray.push_front(temp);
  199. }
  200. // Adds a new array item to a particular index of the array
  201. void Array::insert(StringTableEntry key, StringTableEntry value, S32 index)
  202. {
  203. index = mClampF(index, 0, mArray.size()-1);
  204. S32 size = mArray.size() - 1;
  205. for(S32 i=size; i>=index; i--)
  206. {
  207. moveIndex(i, i+1);
  208. }
  209. Element temp;
  210. temp.value = value;
  211. temp.key = key;
  212. mArray[index] = temp;
  213. }
  214. // Removes an array item from the end of the array
  215. void Array::pop_back()
  216. {
  217. if(mArray.size() <= 0)
  218. return;
  219. mArray.pop_back();
  220. if(mCurrentIndex >= mArray.size())
  221. mCurrentIndex = mArray.size()-1;
  222. }
  223. // Removes an array item from the end of the array
  224. void Array::pop_front()
  225. {
  226. if(mArray.size() <= 0)
  227. return;
  228. mArray.pop_front();
  229. if(mCurrentIndex >= mArray.size())
  230. mCurrentIndex = mArray.size()-1;
  231. }
  232. // Removes an array item from a particular index of the array
  233. void Array::erase(S32 index)
  234. {
  235. if(index < 0 || index >= mArray.size())
  236. return;
  237. mArray.erase(index);
  238. }
  239. // Clears an array
  240. void Array::empty()
  241. {
  242. S32 size = mArray.size();
  243. for(S32 i=0; i<size; i++)
  244. mArray.pop_front();
  245. mCurrentIndex = 0;
  246. }
  247. // Moves a key and value from one index location to another.
  248. void Array::moveIndex(S32 prev, S32 index)
  249. {
  250. if(index >= mArray.size())
  251. push_back(mArray[prev].key, mArray[prev].value);
  252. else
  253. mArray[index] = mArray[prev];
  254. mArray[prev].value = NULL;
  255. mArray[prev].key = NULL;
  256. }
  257. //---------------------------------------------------------------------
  258. // Complex Data Alteration Functions
  259. // Removes any duplicate values from the array
  260. // (keeps the first instance only)
  261. void Array::uniqueValue()
  262. {
  263. for(S32 i=0; i<mArray.size(); i++)
  264. {
  265. for(S32 j=i+1; j<mArray.size(); j++)
  266. {
  267. if(mArray[i].value == mArray[j].value)
  268. {
  269. erase(j);
  270. j--;
  271. }
  272. }
  273. }
  274. }
  275. // Removes any duplicate keys from the array
  276. // (keeps the first instance only)
  277. void Array::uniqueKey()
  278. {
  279. for(S32 i=0; i<mArray.size(); i++)
  280. {
  281. for(S32 j=i+1; j<mArray.size(); j++)
  282. {
  283. if(mArray[i].key == mArray[j].key)
  284. {
  285. erase(j);
  286. j--;
  287. }
  288. }
  289. }
  290. }
  291. // Makes this array an exact duplicate of another array
  292. void Array::duplicate(Array* obj)
  293. {
  294. empty();
  295. for(S32 i=0; i<obj->count(); i++)
  296. {
  297. StringTableEntry tempval = obj->getValueFromIndex(i);
  298. StringTableEntry tempkey = obj->getKeyFromIndex(i);
  299. push_back(tempkey, tempval);
  300. }
  301. mCurrentIndex = obj->getCurrent();
  302. }
  303. // Crops the keys that exists in the target array from our current array
  304. void Array::crop(Array* obj)
  305. {
  306. for(S32 i=0; i<obj->count(); i++)
  307. {
  308. StringTableEntry tempkey = obj->getKeyFromIndex(i);
  309. for(S32 j=0; j<mArray.size(); j++)
  310. {
  311. if(mArray[j].key == tempkey)
  312. {
  313. mArray.erase(j);
  314. j--;
  315. }
  316. }
  317. }
  318. }
  319. // Appends the target array to our current array
  320. void Array::append(Array* obj)
  321. {
  322. for(S32 i=0; i<obj->count(); i++)
  323. {
  324. StringTableEntry tempval = obj->getValueFromIndex(i);
  325. StringTableEntry tempkey = obj->getKeyFromIndex(i);
  326. push_back(tempkey, tempval);
  327. }
  328. }
  329. // Sets the key at the given index
  330. void Array::setKey(StringTableEntry key, S32 index)
  331. {
  332. if(index >= mArray.size())
  333. return;
  334. mArray[index].key = key;
  335. }
  336. // Sets the key at the given index
  337. void Array::setValue(StringTableEntry value, S32 index)
  338. {
  339. if(index >= mArray.size())
  340. return;
  341. mArray[index].value = value;
  342. }
  343. //---------------------------------------------------------------------
  344. // Sorting
  345. // Sorts the array
  346. // First variable determines whether sorting by value or key
  347. // Second variable determines if sorting ascending or descending
  348. // Third variable determines if alpha or numeric search
  349. void Array::sort(bool valsort, bool desc, bool numeric)
  350. {
  351. sIncreasing = desc ? false : true;
  352. if(numeric)
  353. {
  354. if(valsort)
  355. dQsort((void *)&(mArray[0]), mArray.size(), sizeof(Element), valueNumCompare);
  356. else
  357. dQsort((void *)&(mArray[0]), mArray.size(), sizeof(Element), keyNumCompare);
  358. }
  359. else
  360. {
  361. if(valsort)
  362. dQsort((void *)&(mArray[0]), mArray.size(), sizeof(Element), valueCompare);
  363. else
  364. dQsort((void *)&(mArray[0]), mArray.size(), sizeof(Element), keyCompare);
  365. }
  366. }
  367. S32 Array::moveFirst()
  368. {
  369. mCurrentIndex = 0;
  370. return mCurrentIndex;
  371. }
  372. S32 Array::moveLast()
  373. {
  374. mCurrentIndex = mArray.size()-1;
  375. return mCurrentIndex;
  376. }
  377. S32 Array::moveNext()
  378. {
  379. if(mCurrentIndex >= mArray.size()-1)
  380. return -1;
  381. mCurrentIndex++;
  382. return mCurrentIndex;
  383. }
  384. S32 Array::movePrev()
  385. {
  386. if(mCurrentIndex <= 0)
  387. return -1;
  388. mCurrentIndex--;
  389. return mCurrentIndex;
  390. }
  391. S32 Array::getCurrent()
  392. {
  393. return mCurrentIndex;
  394. }
  395. void Array::setCurrent( S32 idx )
  396. {
  397. if ( idx < 0 || idx >= mArray.size() )
  398. {
  399. Con::errorf( "Array::setCurrent( %d ) is out of the array bounds!", idx );
  400. return;
  401. }
  402. mCurrentIndex = idx;
  403. }
  404. void Array::echo()
  405. {
  406. Con::printf("Array Listing:");
  407. Con::printf("Index Key Value");
  408. for(U32 i=0; i < mArray.size(); i++)
  409. {
  410. StringTableEntry key = mArray[i].key;
  411. StringTableEntry val = mArray[i].value;
  412. Con::printf("%d [%s] => %s",(U32)i, key, val);
  413. }
  414. }