Source code of Windows XP (NT5)
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.

136 lines
3.3 KiB

  1. /*
  2. * @doc INTERNAL
  3. *
  4. * @module DYNARRAY.H -- 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-2000 Microsoft Corporation. All rights reserved.
  14. */
  15. #include "_w32sys.h"
  16. #ifndef __DYNARRAY_H__
  17. #define __DYNARRAY_H__
  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._lparamData = 0;
  69. _sDummy._uHeight = 0;
  70. return _sDummy;
  71. }
  72. //return value at requested index
  73. return _rg[nSlot][nIdx];
  74. }
  75. //==========================================================================
  76. // The function will be called if a l-value is requested therefore
  77. // the index does not necessarily have to be valid
  78. //==========================================================================
  79. template <class T>
  80. T& CDynamicArray<T>::operator[](int i)
  81. {
  82. Assert(i >= 0);
  83. // Get the slot number and index
  84. int nSlot = i / DYNARRAY_ARRAY_SIZE;
  85. int nIdx = i % DYNARRAY_ARRAY_SIZE;
  86. // Check if the slot exists
  87. Assert(nSlot < DYNARRAY_ARRAY_SLOT);
  88. if (nSlot >= DYNARRAY_ARRAY_SLOT)
  89. return _sDummy;
  90. if (_rg[nSlot] == NULL)
  91. {
  92. //Need to allocate memory for this
  93. T* prg = new T[DYNARRAY_ARRAY_SIZE];
  94. if (!prg)
  95. {
  96. _sDummy._fSelected = 0;
  97. _sDummy._lparamData = 0;
  98. _sDummy._uHeight = 0;
  99. return _sDummy;
  100. }
  101. memset(prg, 0, sizeof(T) * DYNARRAY_ARRAY_SIZE);
  102. _rg[nSlot] = prg;
  103. if (nSlot >= _nMax / DYNARRAY_ARRAY_SIZE)
  104. _nMax = (nSlot + 1) * DYNARRAY_ARRAY_SIZE;
  105. }
  106. //return value at requested index
  107. return _rg[nSlot][nIdx];
  108. }
  109. #endif //__DYNARRAY_H__