MyMapPtrToPtr.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // MyMapPtrToPtr.cpp: implementation of the CMyMapPtrToPtr class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "datamanager.h"
  6. #include "MyMapPtrToPtr.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CMyMapPtrToPtr::CMyMapPtrToPtr()
  16. {
  17. }
  18. CMyMapPtrToPtr::~CMyMapPtrToPtr()
  19. {
  20. }
  21. void CMyMapPtrToPtr::SetAt(LPVOID key, LPVOID newValue )
  22. {
  23. UINT uID = (UINT)key;
  24. CDataNodeBase* pNodeNew = (CDataNodeBase*)newValue;
  25. CDataNodeBase* pNode = NULL;
  26. if(this->Lookup(key, (void*&)pNode))
  27. {
  28. POSITION pos = m_listSubs.Find(pNode);
  29. ASSERT(pos);
  30. if(pos)
  31. m_listSubs.RemoveAt(pos);
  32. }
  33. //维护按ID递增管理的列表:
  34. if(m_listSubs.GetCount())
  35. {
  36. pNode = (CDataNodeBase*)m_listSubs.GetHead();
  37. if(pNodeNew->GetID() < pNode->GetID())
  38. m_listSubs.AddHead(pNodeNew);
  39. else
  40. {
  41. pNode = (CDataNodeBase*)m_listSubs.GetTail();
  42. if(pNodeNew->GetID() > pNode->GetID())
  43. m_listSubs.AddTail(pNodeNew);
  44. else
  45. {
  46. POSITION pos = m_listSubs.GetHeadPosition();
  47. POSITION posLast = pos;
  48. while(pos)
  49. {
  50. posLast = pos;
  51. pNode = (CDataNodeBase*)m_listSubs.GetNext(pos);
  52. if(pNode->GetID() > pNodeNew->GetID())
  53. break;
  54. }
  55. m_listSubs.InsertBefore(posLast, pNodeNew);
  56. }
  57. }
  58. }
  59. else
  60. {
  61. m_listSubs.AddTail(pNodeNew);
  62. }
  63. CMapPtrToPtr::SetAt(key, newValue);
  64. }
  65. BOOL CMyMapPtrToPtr::RemoveKey(LPVOID key)
  66. {
  67. UINT uID = (UINT)key;
  68. CDataNodeBase* pNode = NULL;
  69. if(this->Lookup(key, (void*&)pNode))
  70. {
  71. POSITION pos = m_listSubs.Find(pNode);
  72. ASSERT(pos);
  73. if(pos)
  74. m_listSubs.RemoveAt(pos);
  75. }
  76. return CMapPtrToPtr::RemoveKey(key);
  77. }
  78. void CMyMapPtrToPtr::RemoveAll( )
  79. {
  80. m_listSubs.RemoveAll();
  81. CMapPtrToPtr::RemoveAll();
  82. }
  83. //ID递增方式管理的子项集合
  84. CPtrList& CMyMapPtrToPtr::GetPtrList()
  85. {
  86. return m_listSubs;
  87. }