PVRTMap.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*!****************************************************************************
  2. @file PVRTMap.h
  3. @copyright Copyright (c) Imagination Technologies Limited.
  4. @brief A simple and easy-to-use implementation of a map.
  5. ******************************************************************************/
  6. #ifndef __PVRTMAP_H__
  7. #define __PVRTMAP_H__
  8. #include "PVRTArray.h"
  9. /*!***************************************************************************
  10. @class CPVRTMap
  11. @brief Expanding map template class.
  12. @details A simple and easy-to-use implementation of a map.
  13. *****************************************************************************/
  14. template <typename KeyType, typename DataType>
  15. class CPVRTMap
  16. {
  17. public:
  18. /*!***********************************************************************
  19. @brief Constructor for a CPVRTMap.
  20. @return A new CPVRTMap.
  21. *************************************************************************/
  22. CPVRTMap() : m_Keys(), m_Data(), m_uiSize(0)
  23. {}
  24. /*!***********************************************************************
  25. @brief Destructor for a CPVRTMap.
  26. *************************************************************************/
  27. ~CPVRTMap()
  28. {
  29. //Clear the map, that's enough - the CPVRTArray members will tidy everything else up.
  30. Clear();
  31. }
  32. EPVRTError Reserve(const PVRTuint32 uiSize)
  33. {
  34. //Sets the capacity of each member array to the requested size. The array used will only expand.
  35. //Returns the most serious error from either method.
  36. return PVRT_MAX(m_Keys.SetCapacity(uiSize),m_Data.SetCapacity(uiSize));
  37. }
  38. /*!***********************************************************************
  39. @brief Returns the number of meaningful members in the map.
  40. @return Number of meaningful members in the map.
  41. *************************************************************************/
  42. PVRTuint32 GetSize() const
  43. {
  44. //Return the size.
  45. return m_uiSize;
  46. }
  47. /*!***********************************************************************
  48. @brief Gets the position of a particular key/data within the map.
  49. If the return value is exactly equal to the value of
  50. GetSize() then the item has not been found.
  51. @param[in] key Key type
  52. @return The index value for a mapped item.
  53. *************************************************************************/
  54. PVRTuint32 GetIndexOf(const KeyType key) const
  55. {
  56. //Loop through all the valid keys.
  57. for (PVRTuint32 i=0; i<m_uiSize; ++i)
  58. {
  59. //Check if a key matches.
  60. if (m_Keys[i]==key)
  61. {
  62. //If a matched key is found, return the position.
  63. return i;
  64. }
  65. }
  66. //If not found, return the number of meaningful members.
  67. return m_uiSize;
  68. }
  69. /*!***********************************************************************
  70. @brief Returns a pointer to the Data at a particular index.
  71. If the index supplied is not valid, NULL is returned
  72. instead. Deletion of data at this pointer will lead
  73. to undefined behaviour.
  74. @param[in] uiIndex Index number
  75. @return Data type at the specified position.
  76. *************************************************************************/
  77. const DataType* GetDataAtIndex(const PVRTuint32 uiIndex) const
  78. {
  79. if (uiIndex>=m_uiSize)
  80. return NULL;
  81. return &(m_Data[uiIndex]);
  82. }
  83. /*!***********************************************************************
  84. @brief If a mapping already exists for 'key' then it will return
  85. the associated data. If no mapping currently exists, a new
  86. element is created in place.
  87. @param[in] key Key type
  88. @return Data that is mapped to 'key'.
  89. *************************************************************************/
  90. DataType& operator[] (const KeyType key)
  91. {
  92. //Get the index of the key.
  93. PVRTuint32 uiIndex = GetIndexOf(key);
  94. //Check the index is valid
  95. if (uiIndex != m_uiSize)
  96. {
  97. //Return mapped data if the index is valid.
  98. return m_Data[uiIndex];
  99. }
  100. else
  101. {
  102. //Append the key to the Keys array.
  103. m_Keys.Append(key);
  104. //Create a new DataType.
  105. DataType sNewData;
  106. //Append the new pointer to the Data array.
  107. m_Data.Append(sNewData);
  108. //Increment the size of meaningful data.
  109. ++m_uiSize;
  110. //Return the contents of pNewData.
  111. return m_Data[m_Keys.GetSize()-1];
  112. }
  113. }
  114. /*!***********************************************************************
  115. @brief Removes an element from the map if it exists.
  116. @param[in] key Key type
  117. @return Returns PVR_FAIL if item doesn't exist.
  118. Otherwise returns PVR_SUCCESS.
  119. *************************************************************************/
  120. EPVRTError Remove(const KeyType key)
  121. {
  122. //Finds the index of the key.
  123. PVRTuint32 uiIndex=GetIndexOf(key);
  124. //If the key is invalid, fail.
  125. if (uiIndex==m_uiSize)
  126. {
  127. //Return failure.
  128. return PVR_FAIL;
  129. }
  130. //Decrement the size of the map to ignore the last element in each array.
  131. m_uiSize--;
  132. //Copy the last key over the deleted key. There are now two copies of one element,
  133. //but the one at the end of the array is ignored.
  134. m_Keys[uiIndex] = m_Keys[m_uiSize]; m_Keys.RemoveLast();
  135. //Copy the last data over the deleted data in the same way as the keys.
  136. m_Data[uiIndex] = m_Data[m_uiSize]; m_Data.RemoveLast();
  137. //Return success.
  138. return PVR_SUCCESS;
  139. }
  140. /*!***********************************************************************
  141. @brief Clears the Map of all data values.
  142. *************************************************************************/
  143. void Clear()
  144. {
  145. //Set the size to 0.
  146. m_uiSize=0;
  147. m_Keys.Clear();
  148. m_Data.Clear();
  149. }
  150. /*!***********************************************************************
  151. @brief Checks whether or not data exists for the specified key.
  152. @param[in] key Key type
  153. @return Whether data exists for the specified key or not.
  154. *************************************************************************/
  155. bool Exists(const KeyType key) const
  156. {
  157. //Checks for a valid index for key, if not, returns false.
  158. return (GetIndexOf(key) != m_uiSize);
  159. }
  160. private:
  161. CPVRTArray<KeyType> m_Keys; /*!< Array of all the keys. Indices match m_Data. */
  162. CPVRTArray<DataType> m_Data; /*!< Array of pointers to all the allocated data. */
  163. PVRTuint32 m_uiSize; /*!< The number of meaningful members in the map. */
  164. };
  165. #endif // __PVRTMAP_H__
  166. /*****************************************************************************
  167. End of file (PVRTMap.h)
  168. *****************************************************************************/