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.

134 lines
3.4 KiB

  1. /*
  2. * @doc INTERNAL
  3. *
  4. * @module DYNARRAY.HXX -- CDynamicArray class which is used to complement
  5. * the CLstBxWndHost object.
  6. *
  7. * Original Author:
  8. * Jerry Kim
  9. *
  10. * History: <nl>
  11. * 12/15/97 - v-jerrki Created
  12. *
  13. * Copyright (c) 1997-1998 Microsoft Corporation. All rights reserved.
  14. */
  15. #include "_w32sys.h"
  16. #ifndef __DYNARRAY_HXX__
  17. #define __DYNARRAY_HXX__
  18. #define DYNARRAY_ARRAY_SIZE 128
  19. #define DYNARRAY_ARRAY_SLOT 256
  20. template<class T>
  21. class CDynamicArray
  22. {
  23. protected:
  24. T* _rg[DYNARRAY_ARRAY_SLOT];
  25. int _nMax; //indicate maximum valid index
  26. public:
  27. static T _sDummy; // dummy struct if invalid index is requested
  28. public:
  29. CDynamicArray() : _nMax(0) { memset(_rg, 0, sizeof(T*) * DYNARRAY_ARRAY_SLOT); }
  30. ~CDynamicArray() { Clear(); }
  31. //==========================================================================
  32. // Reinitializes the class to its construction state
  33. //==========================================================================
  34. void Clear()
  35. {
  36. // Start removing all items from the list
  37. // Start removing items from the list
  38. for (int i = ((_nMax - 1) / DYNARRAY_ARRAY_SIZE); i >= 0; i--)
  39. {
  40. if (_rg[i])
  41. delete _rg[i];
  42. }
  43. _nMax = 0;
  44. memset(_rg, 0, sizeof(T*) * DYNARRAY_ARRAY_SLOT);
  45. }
  46. const T& Get(int i);
  47. T& operator[](int i);
  48. };
  49. //==========================================================================
  50. // The function returns the requested index. if the requested index is
  51. // invalid then return dummy variable
  52. //==========================================================================
  53. template <class T>
  54. const T& CDynamicArray<T>::Get(int i)
  55. {
  56. // If item is negative or equal to zero, for efficiency reasons,
  57. // then just return the item in the head of array
  58. Assert(i >= 0);
  59. Assert(i < DYNARRAY_ARRAY_SLOT * DYNARRAY_ARRAY_SIZE)
  60. // Get the number of links we have to travel
  61. int nSlot = i / DYNARRAY_ARRAY_SIZE;
  62. int nIdx = i % DYNARRAY_ARRAY_SIZE;
  63. // That link doesn't exist so just pass dummy struct
  64. Assert(nSlot < DYNARRAY_ARRAY_SLOT);
  65. if (i >= _nMax || nSlot >= DYNARRAY_ARRAY_SLOT || _rg[nSlot] == NULL)
  66. {
  67. _sDummy._fSelected = 0;
  68. _sDummy._dwData = 0;
  69. return _sDummy;
  70. }
  71. //return value at requested index
  72. return _rg[nSlot][nIdx];
  73. }
  74. //==========================================================================
  75. // The function will be called if a l-value is requested therefore
  76. // the index does not necessarily have to be valid
  77. //==========================================================================
  78. template <class T>
  79. T& CDynamicArray<T>::operator[](int i)
  80. {
  81. Assert(i >= 0);
  82. // Get the slot number and index
  83. int nSlot = i / DYNARRAY_ARRAY_SIZE;
  84. int nIdx = i % DYNARRAY_ARRAY_SIZE;
  85. // Check if the slot exists
  86. Assert(nSlot < DYNARRAY_ARRAY_SLOT);
  87. if (nSlot >= DYNARRAY_ARRAY_SLOT)
  88. return _sDummy;
  89. if (_rg[nSlot] == NULL)
  90. {
  91. //Need to allocate memory for this
  92. T* prg = new T[DYNARRAY_ARRAY_SIZE];
  93. if (!prg)
  94. {
  95. _sDummy._fSelected = 0;
  96. _sDummy._dwData = 0;
  97. return _sDummy;
  98. }
  99. memset(prg, 0, sizeof(T) * DYNARRAY_ARRAY_SIZE);
  100. _rg[nSlot] = prg;
  101. if (nSlot >= _nMax / DYNARRAY_ARRAY_SIZE)
  102. _nMax = (nSlot + 1) * DYNARRAY_ARRAY_SIZE;
  103. }
  104. //return value at requested index
  105. return _rg[nSlot][nIdx];
  106. }
  107. #endif //__DYNARRAY_HXX__