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.

93 lines
2.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1994, Microsoft Corporation
  4. //
  5. // File: tbag.hxx
  6. //
  7. // Contents: Bag of items template.
  8. //
  9. // Notes: For performance reasons, you don't want to put more than
  10. // 20 or so items in a bag.
  11. //
  12. // Templates: TBag
  13. //
  14. // History: 6-Oct-94 Dlee Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #pragma once
  18. template <class T> class TBag
  19. {
  20. public:
  21. TBag() : _cItems(0), _aItems()
  22. {
  23. }
  24. ~TBag(void) { }
  25. T First(void)
  26. {
  27. _iEnumItem = 1;
  28. return (0 == _cItems) ? 0 : _aItems[0];
  29. }
  30. T Next(void)
  31. { return (_iEnumItem >= _cItems) ? 0 : _aItems[_iEnumItem++]; }
  32. void Add(T tItem)
  33. {
  34. _aItems[_cItems] = tItem;
  35. _cItems++;
  36. }
  37. T & operator[](ULONG iItem)
  38. {
  39. Win4Assert(iItem < _cItems);
  40. return _aItems[iItem];
  41. }
  42. T & operator[](ULONG iItem) const
  43. {
  44. Win4Assert(iItem < _cItems);
  45. return _aItems[iItem];
  46. }
  47. ULONG Count() const
  48. { return _cItems; }
  49. BOOL Exists(T tItem) const
  50. {
  51. for (ULONG item = 0; item < _cItems; item++)
  52. if (_aItems[item] == tItem)
  53. return TRUE;
  54. return FALSE;
  55. }
  56. void Remove(T tItem)
  57. {
  58. for (ULONG item = 0; item < _cItems; item++)
  59. {
  60. if (_aItems[item] == tItem)
  61. {
  62. _cItems--;
  63. // overwrite deleted item with last item
  64. if (item < _cItems)
  65. _aItems[item] = _aItems[_cItems];
  66. // mark previous last item as absent
  67. _aItems[_cItems] = 0;
  68. break; // from the for loop
  69. }
  70. }
  71. }
  72. private:
  73. ULONG _cItems;
  74. CDynArrayInPlace<T> _aItems;
  75. ULONG _iEnumItem;
  76. };