arrayObject.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef _ARRAYOBJECT_H_
  23. #define _ARRAYOBJECT_H_
  24. #ifndef _SIMBASE_H_
  25. #include "sim/simBase.h"
  26. #endif
  27. // adopted from original code by Daniel Neilsen - 14/10/03
  28. // [email protected]
  29. static bool sIncreasing;
  30. /// A data structure holding indexed sequences of key/value pairs for script use.
  31. class Array : public SimObject
  32. {
  33. typedef SimObject Parent;
  34. private:
  35. public:
  36. struct Element
  37. {
  38. StringTableEntry key;
  39. StringTableEntry value;
  40. };
  41. S32 mCurrentIndex;
  42. Vector<Element> mArray;
  43. Array();
  44. void onRemove();
  45. // add() - (string key, string value)Adds a new element to the end of an array
  46. bool onAdd();
  47. // append() - (Array target)Appends the target array to the array object
  48. void append(Array* obj);
  49. // count() - Get the number of elements in the array
  50. S32 count();
  51. // countKey() - (string key)Get the number of times a particular key is found in the array
  52. S32 countKey(StringTableEntry key);
  53. // countValue() - (string value)Get the number of times a particular value is found in the array
  54. S32 countValue(StringTableEntry value);
  55. // crop() - (Array target)Removes elements with matching keys from array
  56. void crop(Array* obj);
  57. // duplicate() - (Array target)Alters array into an exact duplicate of the target array
  58. void duplicate(Array* obj);
  59. // echo() - Echos the array in the console
  60. void echo();
  61. // empty() - Emptys all elements from an array
  62. void empty();
  63. // erase() - (int index)Removes an element at a specific position from the array
  64. void erase(S32 index);
  65. // getCurrent() - Gets the current pointer index
  66. S32 getCurrent();
  67. // setCurrent() - Sets the current pointer index
  68. void setCurrent( S32 idx );
  69. // getIndexFromKey() - (string key)Search array from current position for the first matching key
  70. S32 getIndexFromKey(StringTableEntry key);
  71. // getIndexFromValue() - (string value)Search array from current position for the first matching value
  72. S32 getIndexFromValue(StringTableEntry value);
  73. // getIndexFromKeyValue() - Search array and return the index from the key and value
  74. S32 getIndexFromKeyValue( StringTableEntry key, StringTableEntry value);
  75. // getKeyFromIndex() - (int index)Get the key of the array element at the submitted index
  76. StringTableEntry getKeyFromIndex(S32 index);
  77. // getValueFromIndex() - (int index)Get the value of the array element at the submitted index
  78. StringTableEntry getValueFromIndex(S32 index);
  79. // setKey() - (string key, int index)Set the key at the given index
  80. void setKey(StringTableEntry key, S32 index);
  81. // setValue() - (string key, int index)Set the value at the given index
  82. void setValue(StringTableEntry value, S32 index);
  83. // insert() - (string key, string value, int index)Adds a new element to a specified position in the array
  84. void insert(StringTableEntry key, StringTableEntry value, S32 index);
  85. // moveFirst() - Moves array pointer to start of array
  86. S32 moveFirst();
  87. // moveLast() - Moves array pointer to end of array
  88. S32 moveLast();
  89. // moveNext() - Moves array pointer to next position (returns -1 if cannot move)
  90. S32 moveNext();
  91. // movePrev() - Moves array pointer to prev position (returns -1 if cannot move)
  92. S32 movePrev();
  93. // moveIndex() - Moves a key and value from one index location to another.
  94. void moveIndex(S32 prev, S32 index);
  95. // pop_back() - Removes the last element from the array
  96. void pop_back();
  97. // pop_front() - Removes the first element from the array
  98. void pop_front();
  99. // push_back() - (string key, string value)Adds a new element to the end of an array
  100. void push_back(StringTableEntry key, StringTableEntry value);
  101. // push_front() - (string key, string value)Adds a new element to the front of an array
  102. void push_front(StringTableEntry key, StringTableEntry value);
  103. // sort() - (bool desc)Sorts the array by value (default ascending sort)
  104. void sort(bool valtest, bool desc, bool numeric);
  105. // uniqueKey() - Removes any elements that have duplicated keys (leaving the first instance)
  106. void uniqueKey();
  107. // uniqueValue() - Removes any elements that have duplicated values (leaving the first instance)
  108. void uniqueValue();
  109. //-----------------------------------------------------------------------------
  110. DECLARE_CONOBJECT(Array);
  111. };
  112. #endif // _ARRAYOBJECT_H_