llstl.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @file llstl.h
  3. * @brief helper object & functions for use with the stl.
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewergpl$
  6. *
  7. * Copyright (c) 2003-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLSTL_H
  33. #define LL_LLSTL_H
  34. #include <algorithm>
  35. #include <list>
  36. #include <map>
  37. #include <set>
  38. #include <vector>
  39. #include "stdtypes.h"
  40. #include "llpreprocessor.h"
  41. // DeletePointer is a simple helper for deleting all pointers in a container.
  42. // The general form is:
  43. //
  44. // std::for_each(cont.begin(), cont.end(), DeletePointer());
  45. // somemap.clear();
  46. //
  47. // Do not forget to clear() !
  48. struct DeletePointer
  49. {
  50. template<typename T> void operator()(T* ptr) const
  51. {
  52. delete ptr;
  53. }
  54. };
  55. // DeletePairedPointer is a simple helper for deleting all pointers in a map.
  56. // The general form is:
  57. //
  58. // std::for_each(somemap.begin(), somemap.end(), DeletePairedPointer());
  59. // somemap.clear(); // Do not leave dangling pointers around
  60. // WARNING: this does NOT work with Tessil fast hash maps and sets, that
  61. // return a const pair as iter->second !
  62. struct DeletePairedPointer
  63. {
  64. template<typename T> void operator()(T& ptr) const
  65. {
  66. delete ptr.second;
  67. ptr.second = NULL;
  68. }
  69. };
  70. template<typename T, typename ALLOC>
  71. void delete_and_clear(std::list<T*, ALLOC>& list)
  72. {
  73. std::for_each(list.begin(), list.end(), DeletePointer());
  74. list.clear();
  75. }
  76. template<typename T, typename ALLOC>
  77. void delete_and_clear(std::vector<T*, ALLOC>& vector)
  78. {
  79. std::for_each(vector.begin(), vector.end(), DeletePointer());
  80. vector.clear();
  81. }
  82. template<typename T, typename COMPARE, typename ALLOC>
  83. void delete_and_clear(std::set<T*, COMPARE, ALLOC>& set)
  84. {
  85. std::for_each(set.begin(), set.end(), DeletePointer());
  86. set.clear();
  87. }
  88. template<typename K, typename V, typename COMPARE, typename ALLOC>
  89. void delete_and_clear(std::map<K, V*, COMPARE, ALLOC>& map)
  90. {
  91. std::for_each(map.begin(), map.end(), DeletePairedPointer());
  92. map.clear();
  93. }
  94. template<typename T>
  95. void delete_and_clear(T*& ptr)
  96. {
  97. if (ptr)
  98. {
  99. delete ptr;
  100. ptr = NULL;
  101. }
  102. }
  103. // Similar to get_ptr_in_map, but for any type with a valid T(0) constructor.
  104. // WARNING: Make sure default_value (generally 0) is not a valid map entry !
  105. template <typename T>
  106. LL_INLINE typename T::mapped_type get_if_there(const T& inmap,
  107. typename T::key_type const& key,
  108. typename T::mapped_type default_value)
  109. {
  110. // Typedef here avoids warnings because of new C++ naming rules.
  111. typedef typename T::const_iterator map_it_t;
  112. map_it_t iter = inmap.find(key);
  113. if (iter == inmap.end())
  114. {
  115. return default_value;
  116. }
  117. return iter->second;
  118. };
  119. // Simple function to help with finding pointers in maps.
  120. // For example:
  121. // typedef map_t;
  122. // std::map<int, const char*> foo;
  123. // foo[18] = "there";
  124. // foo[2] = "hello";
  125. // const char* bar = get_ptr_in_map(foo, 2); // bar -> "hello"
  126. // const char* baz = get_ptr_in_map(foo, 3); // baz == NULL
  127. template <typename T>
  128. LL_INLINE typename T::mapped_type get_ptr_in_map(const T& inmap,
  129. typename T::key_type const& key)
  130. {
  131. // Typedef here avoids warnings because of new C++ naming rules.
  132. typedef typename T::const_iterator map_it_t;
  133. map_it_t iter = inmap.find(key);
  134. if (iter == inmap.end())
  135. {
  136. return NULL;
  137. }
  138. return iter->second;
  139. };
  140. // Example:
  141. // for (std::vector<T>::iterator iter = mList.begin(); iter != mList.end(); )
  142. // {
  143. // if ((*iter)->isMarkedForRemoval())
  144. // iter = vector_replace_with_last(mList, iter);
  145. // else
  146. // ++iter;
  147. // }
  148. template <typename T>
  149. LL_INLINE typename std::vector<T>::iterator vector_replace_with_last(std::vector<T>& invec,
  150. typename std::vector<T>::iterator iter)
  151. {
  152. typename std::vector<T>::iterator last = invec.end();
  153. if (iter == last)
  154. {
  155. return iter;
  156. }
  157. if (iter == --last)
  158. {
  159. invec.pop_back();
  160. return invec.end();
  161. }
  162. *iter = *last;
  163. invec.pop_back();
  164. return iter;
  165. };
  166. // Example: vector_replace_with_last(mList, x);
  167. template <typename T>
  168. LL_INLINE bool vector_replace_with_last(std::vector<T>& invec, const T& val)
  169. {
  170. typename std::vector<T>::iterator last = invec.end();
  171. typename std::vector<T>::iterator it = std::find(invec.begin(), last, val);
  172. if (it == last)
  173. {
  174. return false;
  175. }
  176. if (it != --last)
  177. {
  178. *it = *last;
  179. }
  180. invec.pop_back();
  181. return true;
  182. }
  183. #endif // LL_LLSTL_H