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.

111 lines
2.2 KiB

  1. //==============================================================;
  2. //
  3. // This source code is only intended as a supplement to existing Microsoft documentation.
  4. //
  5. //
  6. //
  7. //
  8. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  9. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  11. // PURPOSE.
  12. //
  13. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  14. //
  15. //
  16. //
  17. //==============================================================;
  18. #if (_ATL_VER < 0x0300)
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Collection helpers - CSimpleArray & CSimpleMap
  21. template <class T>
  22. class CSimpleArray
  23. {
  24. public:
  25. T* m_aT;
  26. int m_nSize;
  27. int m_nAllocSize;
  28. // Construction/destruction
  29. CSimpleArray() : m_aT(NULL), m_nSize(0), m_nAllocSize(0)
  30. { }
  31. ~CSimpleArray()
  32. {
  33. RemoveAll();
  34. }
  35. // Operations
  36. int GetSize() const
  37. {
  38. return m_nSize;
  39. }
  40. BOOL Add(T& t)
  41. {
  42. if(m_nSize == m_nAllocSize)
  43. {
  44. T* aT;
  45. int nNewAllocSize = (m_nAllocSize == 0) ? 1 : (m_nSize * 2);
  46. aT = (T*)realloc(m_aT, nNewAllocSize * sizeof(T));
  47. if(aT == NULL)
  48. return FALSE;
  49. m_nAllocSize = nNewAllocSize;
  50. m_aT = aT;
  51. }
  52. m_nSize++;
  53. SetAtIndex(m_nSize - 1, t);
  54. return TRUE;
  55. }
  56. BOOL Remove(T& t)
  57. {
  58. int nIndex = Find(t);
  59. if(nIndex == -1)
  60. return FALSE;
  61. return RemoveAt(nIndex);
  62. }
  63. BOOL RemoveAt(int nIndex)
  64. {
  65. if(nIndex != (m_nSize - 1))
  66. memmove((void*)&m_aT[nIndex], (void*)&m_aT[nIndex + 1], (m_nSize - (nIndex + 1)) * sizeof(T));
  67. m_nSize--;
  68. return TRUE;
  69. }
  70. void RemoveAll()
  71. {
  72. if(m_aT != NULL)
  73. {
  74. free(m_aT);
  75. m_aT = NULL;
  76. }
  77. m_nSize = 0;
  78. m_nAllocSize = 0;
  79. }
  80. T& operator[] (int nIndex) const
  81. {
  82. _ASSERT(nIndex >= 0 && nIndex < m_nSize);
  83. return m_aT[nIndex];
  84. }
  85. T* GetData() const
  86. {
  87. return m_aT;
  88. }
  89. // Implementation
  90. void SetAtIndex(int nIndex, T& t)
  91. {
  92. _ASSERT(nIndex >= 0 && nIndex < m_nSize);
  93. m_aT[nIndex] = t;
  94. }
  95. int Find(T& t) const
  96. {
  97. for(int i = 0; i < m_nSize; i++)
  98. {
  99. if(m_aT[i] == t)
  100. return i;
  101. }
  102. return -1; // not found
  103. }
  104. };
  105. #endif