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.

368 lines
9.1 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. SORTARR.INL
  5. Abstract:
  6. History:
  7. --*/
  8. // included into sortarr.h
  9. template<class TKey, class TEl, class TManager, class TComparer>
  10. CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  11. ~CSmartSortedArray()
  12. {
  13. Clear();
  14. }
  15. template<class TKey, class TEl, class TManager, class TComparer>
  16. void CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  17. Clear()
  18. {
  19. // Release all elements
  20. // ====================
  21. for(int i = 0; i < m_v.size(); i++)
  22. m_Manager.ReleaseElement(m_v[i]);
  23. m_v.clear();
  24. }
  25. template<class TKey, class TEl, class TManager, class TComparer>
  26. int CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  27. Add(TEl& NewEl, TEl* pOld)
  28. {
  29. // Binary search
  30. // =============
  31. int nLower = 0;
  32. int nUpper = m_v.size() - 1;
  33. while(nUpper >= nLower)
  34. {
  35. int nMiddle = (nUpper + nLower) / 2;
  36. int nCompare = m_Comparer.Compare(NewEl, m_v[nMiddle]);
  37. if(nCompare < 0)
  38. nUpper = nMiddle-1;
  39. else if(nCompare > 0)
  40. nLower = nMiddle+1;
  41. else
  42. {
  43. // Already there!
  44. // ==============
  45. if(pOld)
  46. *pOld = m_v[nMiddle];
  47. else
  48. m_Manager.ReleaseElement(m_v[nMiddle]);
  49. m_v[nMiddle] = NewEl;
  50. m_Manager.AddRefElement(NewEl);
  51. return nMiddle;
  52. }
  53. }
  54. // At this point, nUpper == nLower - 1 and our element should be between
  55. // =====================================================================
  56. m_Manager.AddRefElement(NewEl);
  57. m_v.insert(m_v.begin()+nLower, NewEl);
  58. return nLower;
  59. }
  60. template<class TKey, class TEl, class TManager, class TComparer>
  61. bool CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  62. Remove(const TKey& K, TEl* pOld)
  63. {
  64. // Binary search
  65. // =============
  66. int nLower = 0;
  67. int nUpper = m_v.size() - 1;
  68. while(nUpper >= nLower)
  69. {
  70. int nMiddle = (nUpper + nLower) / 2;
  71. int nCompare = m_Comparer.Compare(K, m_v[nMiddle]);
  72. if(nCompare < 0)
  73. nUpper = nMiddle-1;
  74. else if(nCompare > 0)
  75. nLower = nMiddle+1;
  76. else // found it
  77. {
  78. if(pOld)
  79. *pOld = m_v[nMiddle];
  80. else
  81. m_Manager.ReleaseElement(m_v[nMiddle]);
  82. m_v.erase(m_v.begin() + nMiddle);
  83. return true;
  84. }
  85. }
  86. // never found it
  87. // ==============
  88. return false;
  89. }
  90. template<class TKey, class TEl, class TManager, class TComparer>
  91. typename CSmartSortedArray<TKey, TEl, TManager, TComparer>::TIterator
  92. CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  93. Insert(TIterator it, TEl& NewEl)
  94. {
  95. m_Manager.AddRefElement(NewEl);
  96. return m_v.insert(it, NewEl);
  97. }
  98. template<class TKey, class TEl, class TManager, class TComparer>
  99. void CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  100. Append(TEl& NewEl)
  101. {
  102. m_Manager.AddRefElement(NewEl);
  103. m_v.insert(m_v.end(), NewEl);
  104. }
  105. template<class TKey, class TEl, class TManager, class TComparer>
  106. typename CSmartSortedArray<TKey, TEl, TManager, TComparer>::TIterator
  107. CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  108. Remove(TIterator it, RELEASE_ME TEl* pOld)
  109. {
  110. if(pOld)
  111. *pOld = *it;
  112. else
  113. m_Manager.ReleaseElement(*it);
  114. return m_v.erase(it);
  115. }
  116. template<class TKey, class TEl, class TManager, class TComparer>
  117. bool CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  118. Find(const TKey& K, TEl* pEl)
  119. {
  120. TIterator it;
  121. bool bFound = Find(K, &it);
  122. if(bFound && pEl)
  123. {
  124. *pEl = *it;
  125. m_Manager.AddRefElement(*pEl);
  126. }
  127. return bFound;
  128. }
  129. template<class TKey, class TEl, class TManager, class TComparer>
  130. bool CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  131. Find(const TKey& K, TIterator* pit)
  132. {
  133. // Binary search
  134. // =============
  135. int nLower = 0;
  136. int nUpper = m_v.size() - 1;
  137. while(nUpper >= nLower)
  138. {
  139. int nMiddle = (nUpper + nLower) / 2;
  140. int nCompare = m_Comparer.Compare(K, m_v[nMiddle]);
  141. if(nCompare < 0)
  142. nUpper = nMiddle-1;
  143. else if(nCompare > 0)
  144. nLower = nMiddle+1;
  145. else // found it
  146. {
  147. *pit = m_v.begin() + nMiddle;
  148. return true;
  149. }
  150. }
  151. *pit = m_v.begin() + nLower;
  152. return false;
  153. }
  154. template<class TKey, class TEl, class TManager, class TComparer>
  155. TEl* CSmartSortedArray<TKey, TEl, TManager, TComparer>::
  156. UnbindPtr()
  157. {
  158. // Allocate a new buffer
  159. // =====================
  160. TEl* aBuffer = new TEl[GetSize()];
  161. if(aBuffer == NULL)
  162. return NULL;
  163. // Copy the elements over --- no addreffing required
  164. // =================================================
  165. for(int i = 0; i < GetSize(); i++)
  166. aBuffer[i] = m_v[i];
  167. m_v.clear();
  168. return aBuffer;
  169. }
  170. //*****************************************************************************
  171. //*****************************************************************************
  172. template<class TKey, class TEl, class TManager, class TComparer>
  173. CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  174. ~CSmartSortedTree()
  175. {
  176. Clear();
  177. }
  178. template<class TKey, class TEl, class TManager, class TComparer>
  179. void CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  180. Clear()
  181. {
  182. // Release all elements
  183. // ====================
  184. for(TIterator it = Begin(); it != End(); it++)
  185. m_Manager.ReleaseElement(*it);
  186. m_t.clear();
  187. }
  188. template<class TKey, class TEl, class TManager, class TComparer>
  189. int CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  190. Add(TEl& NewEl, TEl* pOld)
  191. {
  192. TIterator it = m_t.lower_bound(m_Comparer.Extract(NewEl));
  193. // Check if there
  194. // ==============
  195. if(it != m_t.end() && m_Comparer.Compare(NewEl, *it) == 0)
  196. {
  197. // Already there!
  198. // ==============
  199. if(pOld)
  200. *pOld = *it;
  201. else
  202. m_Manager.ReleaseElement(*it);
  203. *it = NewEl;
  204. m_Manager.AddRefElement(NewEl);
  205. }
  206. else
  207. {
  208. // Not there
  209. // =========
  210. m_Manager.AddRefElement(NewEl);
  211. m_t.insert(it, NewEl);
  212. }
  213. return 1;
  214. }
  215. template<class TKey, class TEl, class TManager, class TComparer>
  216. bool CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  217. Remove(const TKey& K, TEl* pOld)
  218. {
  219. TIterator it = m_t.find(K);
  220. if(it != m_t.end())
  221. {
  222. // Found it
  223. // ========
  224. if(pOld)
  225. *pOld = *it;
  226. else
  227. m_Manager.ReleaseElement(*it);
  228. m_t.erase(it);
  229. return true;
  230. }
  231. else
  232. {
  233. // never found it
  234. // ==============
  235. return false;
  236. }
  237. }
  238. template<class TKey, class TEl, class TManager, class TComparer>
  239. typename CSmartSortedTree<TKey, TEl, TManager, TComparer>::TIterator
  240. CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  241. Insert(TIterator it, TEl& NewEl)
  242. {
  243. m_Manager.AddRefElement(NewEl);
  244. // This iterator points to the position *after* insertion point. STL likes
  245. // it before
  246. // =======================================================================
  247. TIterator it1 = it;
  248. it1--;
  249. return m_t.insert(it1, NewEl);
  250. }
  251. template<class TKey, class TEl, class TManager, class TComparer>
  252. void CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  253. Append(TEl& NewEl)
  254. {
  255. m_Manager.AddRefElement(NewEl);
  256. m_t.insert(m_t.end(), NewEl);
  257. }
  258. template<class TKey, class TEl, class TManager, class TComparer>
  259. typename CSmartSortedTree<TKey, TEl, TManager, TComparer>::TIterator
  260. CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  261. Remove(TIterator it, RELEASE_ME TEl* pOld)
  262. {
  263. if(pOld)
  264. *pOld = *it;
  265. else
  266. m_Manager.ReleaseElement(*it);
  267. return m_t.erase(it);
  268. }
  269. template<class TKey, class TEl, class TManager, class TComparer>
  270. bool CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  271. Find(const TKey& K, TEl* pEl)
  272. {
  273. TIterator it;
  274. bool bFound = Find(K, &it);
  275. if(bFound && pEl)
  276. {
  277. *pEl = *it;
  278. m_Manager.AddRefElement(*pEl);
  279. }
  280. return bFound;
  281. }
  282. template<class TKey, class TEl, class TManager, class TComparer>
  283. bool CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  284. Find(const TKey& K, TIterator* pit)
  285. {
  286. *pit = m_t.lower_bound(K);
  287. if(*pit != m_t.end() && m_Comparer.Compare(K, **pit) == 0)
  288. return true;
  289. else
  290. return false;
  291. }
  292. template<class TKey, class TEl, class TManager, class TComparer>
  293. TEl* CSmartSortedTree<TKey, TEl, TManager, TComparer>::
  294. UnbindPtr()
  295. {
  296. // Allocate a new buffer
  297. // =====================
  298. TEl* aBuffer = new TEl[GetSize()];
  299. if(aBuffer == NULL)
  300. return NULL;
  301. // Copy the elements over --- no addreffing required
  302. // =================================================
  303. int i = 0;
  304. for(TIterator it = Begin(); it != End(); it++)
  305. aBuffer[i++] = *it;
  306. m_t.clear();
  307. return aBuffer;
  308. }