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.

103 lines
1.8 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. #if (_ATL_VER < 0x0300)
  3. /////////////////////////////////////////////////////////////////////////////
  4. // Collection helpers - CSimpleArray & CSimpleMap
  5. #ifndef __SIMPLEARRAY__
  6. #define __SIMPLEARRAY__
  7. #ifndef ATLASSERT
  8. #define ATLASSERT(expr) _ASSERTE(expr)
  9. #endif
  10. template <class T>
  11. class CSimpleArray
  12. {
  13. public:
  14. T* m_aT;
  15. int m_nSize;
  16. int m_nAllocSize;
  17. // Construction/destruction
  18. CSimpleArray() : m_aT(NULL), m_nSize(0), m_nAllocSize(0)
  19. { }
  20. ~CSimpleArray()
  21. {
  22. RemoveAll();
  23. }
  24. // Operations
  25. int GetSize() const
  26. {
  27. return m_nSize;
  28. }
  29. BOOL Add(T& t)
  30. {
  31. if(m_nSize == m_nAllocSize)
  32. {
  33. T* aT;
  34. int nNewAllocSize = (m_nAllocSize == 0) ? 1 : (m_nSize * 2);
  35. aT = (T*)realloc(m_aT, nNewAllocSize * sizeof(T));
  36. if(aT == NULL)
  37. return FALSE;
  38. m_nAllocSize = nNewAllocSize;
  39. m_aT = aT;
  40. }
  41. m_nSize++;
  42. SetAtIndex(m_nSize - 1, t);
  43. return TRUE;
  44. }
  45. BOOL Remove(T& t)
  46. {
  47. int nIndex = Find(t);
  48. if(nIndex == -1)
  49. return FALSE;
  50. return RemoveAt(nIndex);
  51. }
  52. BOOL RemoveAt(int nIndex)
  53. {
  54. if(nIndex != (m_nSize - 1))
  55. memmove((void*)&m_aT[nIndex], (void*)&m_aT[nIndex + 1], (m_nSize - (nIndex + 1)) * sizeof(T));
  56. m_nSize--;
  57. return TRUE;
  58. }
  59. void RemoveAll()
  60. {
  61. if(m_aT != NULL)
  62. {
  63. free(m_aT);
  64. m_aT = NULL;
  65. }
  66. m_nSize = 0;
  67. m_nAllocSize = 0;
  68. }
  69. T& operator[] (int nIndex) const
  70. {
  71. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  72. return m_aT[nIndex];
  73. }
  74. T* GetData() const
  75. {
  76. return m_aT;
  77. }
  78. // Implementation
  79. void SetAtIndex(int nIndex, T& t)
  80. {
  81. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  82. m_aT[nIndex] = t;
  83. }
  84. int Find(T& t) const
  85. {
  86. for(int i = 0; i < m_nSize; i++)
  87. {
  88. if(m_aT[i] == t)
  89. return i;
  90. }
  91. return -1; // not found
  92. }
  93. };
  94. #endif __SIMPLEARRAY__
  95. #endif