PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llpriqueuemap.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 145 lines | 95 code | 16 blank | 34 comment | 13 complexity | edc0912c31fc0653647ca4d8ce3ff2ab MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llpriqueuemap.h
  3. * @brief Priority queue implementation
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLPRIQUEUEMAP_H
  27. #define LL_LLPRIQUEUEMAP_H
  28. #include <map>
  29. //
  30. // Priority queue, implemented under the hood as a
  31. // map. Needs to be done this way because none of the
  32. // standard STL containers provide a representation
  33. // where it's easy to reprioritize.
  34. //
  35. template <class DATA>
  36. class LLPQMKey
  37. {
  38. public:
  39. LLPQMKey(const F32 priority, DATA data) : mPriority(priority), mData(data)
  40. {
  41. }
  42. bool operator<(const LLPQMKey &b) const
  43. {
  44. if (mPriority > b.mPriority)
  45. {
  46. return TRUE;
  47. }
  48. if (mPriority < b.mPriority)
  49. {
  50. return FALSE;
  51. }
  52. if (mData > b.mData)
  53. {
  54. return TRUE;
  55. }
  56. return FALSE;
  57. }
  58. F32 mPriority;
  59. DATA mData;
  60. };
  61. template <class DATA_TYPE>
  62. class LLPriQueueMap
  63. {
  64. public:
  65. typedef typename std::map<LLPQMKey<DATA_TYPE>, DATA_TYPE>::iterator pqm_iter;
  66. typedef std::pair<LLPQMKey<DATA_TYPE>, DATA_TYPE> pqm_pair;
  67. typedef void (*set_pri_fn)(DATA_TYPE &data, const F32 priority);
  68. typedef F32 (*get_pri_fn)(DATA_TYPE &data);
  69. LLPriQueueMap(set_pri_fn set_pri, get_pri_fn get_pri) : mSetPriority(set_pri), mGetPriority(get_pri)
  70. {
  71. }
  72. void push(const F32 priority, DATA_TYPE data)
  73. {
  74. #ifdef _DEBUG
  75. pqm_iter iter = mMap.find(LLPQMKey<DATA_TYPE>(priority, data));
  76. if (iter != mMap.end())
  77. {
  78. llerrs << "Pushing already existing data onto queue!" << llendl;
  79. }
  80. #endif
  81. mMap.insert(pqm_pair(LLPQMKey<DATA_TYPE>(priority, data), data));
  82. }
  83. BOOL pop(DATA_TYPE *datap)
  84. {
  85. pqm_iter iter;
  86. iter = mMap.begin();
  87. if (iter == mMap.end())
  88. {
  89. return FALSE;
  90. }
  91. *datap = (*(iter)).second;
  92. mMap.erase(iter);
  93. return TRUE;
  94. }
  95. void reprioritize(const F32 new_priority, DATA_TYPE data)
  96. {
  97. pqm_iter iter;
  98. F32 cur_priority = mGetPriority(data);
  99. LLPQMKey<DATA_TYPE> cur_key(cur_priority, data);
  100. iter = mMap.find(cur_key);
  101. if (iter == mMap.end())
  102. {
  103. llwarns << "Data not on priority queue!" << llendl;
  104. // OK, try iterating through all of the data and seeing if we just screwed up the priority
  105. // somehow.
  106. for (iter = mMap.begin(); iter != mMap.end(); iter++)
  107. {
  108. if ((*(iter)).second == data)
  109. {
  110. llerrs << "Data on priority queue but priority not matched!" << llendl;
  111. }
  112. }
  113. return;
  114. }
  115. mMap.erase(iter);
  116. mSetPriority(data, new_priority);
  117. push(new_priority, data);
  118. }
  119. S32 getLength() const
  120. {
  121. return (S32)mMap.size();
  122. }
  123. // Hack: public for use by the transfer manager, ugh.
  124. std::map<LLPQMKey<DATA_TYPE>, DATA_TYPE> mMap;
  125. protected:
  126. void (*mSetPriority)(DATA_TYPE &data, const F32 priority);
  127. F32 (*mGetPriority)(DATA_TYPE &data);
  128. };
  129. #endif // LL_LLPRIQUEUEMAP_H