Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.8 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. SORTARR.H
  5. Abstract:
  6. History:
  7. --*/
  8. #ifndef __WMI__SMART_SORTED_ARRAY__H_
  9. #define __WMI__SMART_SORTED_ARRAY__H_
  10. #pragma warning( disable : 4786 )
  11. #include <vector>
  12. #include <arrtempl.h>
  13. #include <wstlallc.h>
  14. template<class TKey, class TEl, class TManager, class TComparer>
  15. class CSmartSortedArray
  16. {
  17. protected:
  18. std::vector<TEl, wbem_allocator<TEl> > m_v;
  19. TManager m_Manager;
  20. TComparer m_Comparer;
  21. public:
  22. typedef typename std::vector<TEl, wbem_allocator<TEl> >::iterator TIterator;
  23. typedef typename std::vector<TEl, wbem_allocator<TEl> >::const_iterator TConstIterator;
  24. CSmartSortedArray(){}
  25. virtual ~CSmartSortedArray();
  26. TIterator Begin() {return m_v.begin();}
  27. TConstIterator Begin() const {return m_v.begin();}
  28. TIterator End() {return m_v.end();}
  29. TConstIterator End() const {return m_v.end();}
  30. int Add(TEl& NewEl, RELEASE_ME TEl* pOld = NULL);
  31. bool Remove(const TKey& K, RELEASE_ME TEl* pOld = NULL);
  32. TIterator Remove(TIterator it, RELEASE_ME TEl* pOld = NULL);
  33. TIterator Insert(TIterator it, TEl& NewEl);
  34. void Append(TEl& NewEl);
  35. bool Find(const TKey& K, RELEASE_ME TEl* pEl);
  36. bool Find(const TKey& K, TIterator* pit);
  37. void Empty() {m_v.clear();}
  38. int GetSize() {return m_v.size();}
  39. TEl* UnbindPtr();
  40. void Clear();
  41. };
  42. template<class TKey, class TEl, class TComparer>
  43. class TExtractor : public std::unary_function<TEl, TKey>
  44. {
  45. TComparer m_Comparer;
  46. public:
  47. const TKey& operator()(const TEl& El) const
  48. {
  49. return m_Comparer.Extract(El);
  50. }
  51. };
  52. template<class TKey, class TEl, class TManager, class TComparer>
  53. class CSmartSortedTree
  54. {
  55. protected:
  56. TManager m_Manager;
  57. TComparer m_Comparer;
  58. class TPredicate : public std::binary_function<TKey, TKey, bool>
  59. {
  60. TComparer m_Comparer;
  61. public:
  62. bool operator()(const TKey& k1, const TKey& k2) const
  63. {
  64. return (m_Comparer.Compare(k1, k2) < 0);
  65. }
  66. };
  67. typedef
  68. std::_Tree< TKey, TEl, TExtractor<TKey, TEl, TComparer>, TPredicate, wbem_allocator<TEl> >
  69. TTree;
  70. TTree m_t;
  71. public:
  72. typedef typename TTree::iterator TIterator;
  73. typedef typename TTree::const_iterator TConstIterator;
  74. CSmartSortedTree() : m_t(TPredicate(), false) {}
  75. virtual ~CSmartSortedTree();
  76. TIterator Begin() {return m_t.begin();}
  77. TConstIterator Begin() const {return m_t.begin();}
  78. TIterator End() {return m_t.end();}
  79. TConstIterator End() const {return m_t.end();}
  80. int Add(TEl& NewEl, RELEASE_ME TEl* pOld = NULL);
  81. bool Remove(const TKey& K, RELEASE_ME TEl* pOld = NULL);
  82. TIterator Remove(TIterator it, RELEASE_ME TEl* pOld = NULL);
  83. TIterator Insert(TIterator it, TEl& NewEl);
  84. void Append(TEl& NewEl);
  85. bool Find(const TKey& K, RELEASE_ME TEl* pEl);
  86. bool Find(const TKey& K, TIterator* pit);
  87. void Empty() {m_t.clear();}
  88. int GetSize() {return (int)m_t.size();}
  89. TEl* UnbindPtr();
  90. void Clear();
  91. };
  92. #include "sortarr.inl"
  93. template<class TKey, class TEl, class TComparer>
  94. class CUniquePointerSortedArray :
  95. public CSmartSortedArray<TKey, TEl*, CUniqueManager<TEl>, TComparer>
  96. {
  97. };
  98. template<class TKey, class TEl, class TComparer>
  99. class CRefedPointerSortedArray :
  100. public CSmartSortedArray<TKey, TEl*, CReferenceManager<TEl>, TComparer>
  101. {
  102. };
  103. template<class TKey, class TEl, class TComparer>
  104. class CUniquePointerSortedTree :
  105. public CSmartSortedTree<TKey, TEl*, CUniqueManager<TEl>, TComparer>
  106. {
  107. };
  108. template<class TKey, class TEl, class TComparer>
  109. class CRefedPointerSortedTree :
  110. public CSmartSortedTree<TKey, TEl*, CReferenceManager<TEl>, TComparer>
  111. {
  112. };
  113. #endif