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.

90 lines
2.6 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1995 - 2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: OrderedArray.inl
  6. * Content: COrderedArray methods
  7. *
  8. * History:
  9. * Date By Reason
  10. * ====== == ======
  11. * 12-12-2001 simonpow Created
  12. ***************************************************************************/
  13. template <class T>
  14. BOOL COrderedArray<T>::AddElements(const COrderedArray<T>& array)
  15. {
  16. //ensure array isn't passed to itself
  17. DNASSERT(this!=&array);
  18. if (!m_data.AllocAtLeast(m_dwTopFreeSlot+array.GetNumElements()))
  19. return FALSE;
  20. m_data.SetExistingElements(m_dwTopFreeSlot,
  21. const_cast<COrderedArray<T>&>(array).GetAllElements(), array.GetNumElements());
  22. m_dwTopFreeSlot+=array.GetNumElements();
  23. return TRUE;
  24. }
  25. template <class T>
  26. BOOL COrderedArray<T>::InsertElement(DWORD dwIndex, const T& elem)
  27. {
  28. DNASSERT(dwIndex<=m_dwTopFreeSlot);
  29. if (dwIndex==m_dwTopFreeSlot)
  30. return m_data.SetElement(m_dwTopFreeSlot++, elem);
  31. //move all the elements above index up one and insert the new element
  32. if (!m_data.MoveElements(dwIndex, m_dwTopFreeSlot-dwIndex, dwIndex+1, FALSE))
  33. return FALSE;
  34. m_data.SetExistingElement(dwIndex, elem);
  35. m_dwTopFreeSlot++;
  36. return true;
  37. }
  38. template <class T>
  39. BOOL COrderedArray<T>::InsertElements(DWORD dwIndex, const T * pElems, DWORD dwNumElem)
  40. {
  41. //ensure hole isn't created
  42. DNASSERT(dwIndex<=m_dwTopFreeSlot);
  43. //ensure pointer passed isn't into this arrays contents
  44. DNASSERT(!(pElems>=m_data.GetAllElements() && pElems<m_data.GetAllElements()+m_data.GetCurrentSize()));
  45. if (dwIndex!=m_dwTopFreeSlot)
  46. {
  47. if (!m_data.MoveElements(dwIndex, m_dwTopFreeSlot-dwIndex, dwIndex+dwNumElem, FALSE))
  48. return FALSE;
  49. m_data.SetExistingElements(dwIndex, pElems, dwNumElem);
  50. }
  51. else if (!m_data.SetElements(dwIndex, pElems, dwNumElem))
  52. return FALSE;
  53. m_dwTopFreeSlot+=dwNumElem;
  54. return TRUE;
  55. }
  56. template <class T>
  57. BOOL COrderedArray<T>::RemoveElementByValue(const T& elem)
  58. {
  59. for (DWORD dwLoop=0; dwLoop<m_dwTopFreeSlot; dwLoop++)
  60. {
  61. if (m_data.GetElementRef(dwLoop)==elem)
  62. {
  63. m_data.MoveElements(dwLoop+1, m_dwTopFreeSlot-dwLoop-1, dwLoop, FALSE);
  64. m_dwTopFreeSlot--;
  65. return TRUE;
  66. }
  67. }
  68. return FALSE;
  69. }
  70. template <class T>
  71. BOOL COrderedArray<T>::FindElement(const T& elem, DWORD * pdwIndex) const
  72. {
  73. for (DWORD dwLoop=0; dwLoop<m_dwTopFreeSlot; dwLoop++)
  74. {
  75. if (m_data.GetElementRef(dwLoop)==elem)
  76. {
  77. *pdwIndex=dwLoop;
  78. return TRUE;
  79. }
  80. }
  81. return FALSE;
  82. }