PageRenderTime 15ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llindexedqueue.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 155 lines | 92 code | 19 blank | 44 comment | 14 complexity | f796b503e1c2091f8ec5f16e9d5c5d2c MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llindexedqueue.h
  3. * @brief An indexed FIFO queue, where only one element with each key
  4. * can be in the queue.
  5. *
  6. * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #ifndef LL_LLINDEXEDQUEUE_H
  28. #define LL_LLINDEXEDQUEUE_H
  29. // An indexed FIFO queue, where only one element with each key can be in the queue.
  30. // This is ONLY used in the interest list, you'll probably want to review this code
  31. // carefully if you want to use it elsewhere - Doug
  32. template <typename Type>
  33. class LLIndexedQueue
  34. {
  35. protected:
  36. typedef std::deque<Type> type_deque;
  37. type_deque mQueue;
  38. std::set<Type> mKeySet;
  39. public:
  40. LLIndexedQueue() {}
  41. // move_if_there is an O(n) operation
  42. bool push_back(const Type &value, bool move_if_there = false)
  43. {
  44. if (mKeySet.find(value) != mKeySet.end())
  45. {
  46. // Already on the queue
  47. if (move_if_there)
  48. {
  49. // Remove the existing entry.
  50. typename type_deque::iterator it;
  51. for (it = mQueue.begin(); it != mQueue.end(); ++it)
  52. {
  53. if (*it == value)
  54. {
  55. break;
  56. }
  57. }
  58. // This HAS to succeed, otherwise there's a serious bug in the keyset implementation
  59. // (although this isn't thread safe, at all)
  60. mQueue.erase(it);
  61. }
  62. else
  63. {
  64. // We're not moving it, leave it alone
  65. return false;
  66. }
  67. }
  68. else
  69. {
  70. // Doesn't exist, add it to the key set
  71. mKeySet.insert(value);
  72. }
  73. mQueue.push_back(value);
  74. // We succeeded in adding the new element.
  75. return true;
  76. }
  77. bool push_front(const Type &value, bool move_if_there = false)
  78. {
  79. if (mKeySet.find(value) != mKeySet.end())
  80. {
  81. // Already on the queue
  82. if (move_if_there)
  83. {
  84. // Remove the existing entry.
  85. typename type_deque::iterator it;
  86. for (it = mQueue.begin(); it != mQueue.end(); ++it)
  87. {
  88. if (*it == value)
  89. {
  90. break;
  91. }
  92. }
  93. // This HAS to succeed, otherwise there's a serious bug in the keyset implementation
  94. // (although this isn't thread safe, at all)
  95. mQueue.erase(it);
  96. }
  97. else
  98. {
  99. // We're not moving it, leave it alone
  100. return false;
  101. }
  102. }
  103. else
  104. {
  105. // Doesn't exist, add it to the key set
  106. mKeySet.insert(value);
  107. }
  108. mQueue.push_front(value);
  109. return true;
  110. }
  111. void pop()
  112. {
  113. Type value = mQueue.front();
  114. mKeySet.erase(value);
  115. mQueue.pop_front();
  116. }
  117. Type &front()
  118. {
  119. return mQueue.front();
  120. }
  121. S32 size() const
  122. {
  123. return mQueue.size();
  124. }
  125. bool empty() const
  126. {
  127. return mQueue.empty();
  128. }
  129. void clear()
  130. {
  131. // Clear out all elements on the queue
  132. mQueue.clear();
  133. mKeySet.clear();
  134. }
  135. };
  136. #endif // LL_LLINDEXEDQUEUE_H