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.

113 lines
2.1 KiB

  1. //
  2. // Systrack - System resource tracking
  3. // Copyright (c) Microsoft Corporation, 1997
  4. //
  5. //
  6. // header: table.hxx
  7. // author: silviuc
  8. // created: Wed Nov 11 13:02:47 1998
  9. //
  10. #ifndef _TABLE_HXX_INCLUDED_
  11. #define _TABLE_HXX_INCLUDED_
  12. #include "assert.hxx"
  13. template<class T, unsigned Sz>
  14. class Table
  15. {
  16. private:
  17. unsigned _Size;
  18. T _Data [Sz];
  19. private:
  20. unsigned Hash (unsigned Key) {
  21. unsigned char *KeyChar;
  22. KeyChar = (unsigned char *)(& Key);
  23. return (KeyChar[0] ^ KeyChar[1] ^ KeyChar[2] ^ KeyChar[3]) % _Size;
  24. }
  25. public:
  26. Table () {
  27. _Size = Sz;
  28. }
  29. T* Find (const unsigned Key) {
  30. unsigned Index = Hash (Key);
  31. unsigned FirstIndex = Index;
  32. do {
  33. if (_Data[Index].Key == Key)
  34. return &(_Data[Index]);
  35. Index = (Index + 1) % _Size;
  36. } while (Index != FirstIndex);
  37. return 0;
  38. }
  39. T* Add (const unsigned Key) {
  40. unsigned Index = Hash (Key);
  41. unsigned FirstIndex = Index;
  42. do {
  43. if (_Data[Index].Key == Key)
  44. return &(_Data[Index]);
  45. Index = (Index + 1) % _Size;
  46. } while (Index != FirstIndex);
  47. do {
  48. if (_Data[Index].Key == 0)
  49. {
  50. _Data[Index].Key = Key;
  51. return &(_Data[Index]);
  52. }
  53. Index = (Index + 1) % _Size;
  54. } while (Index != FirstIndex);
  55. return 0;
  56. }
  57. T* Delete (const unsigned Key) {
  58. unsigned Index = Hash (Key);
  59. unsigned FirstIndex = Index;
  60. do {
  61. if (_Data[Index].Key == Key)
  62. {
  63. _Data[Index].Key = 0;
  64. return &(_Data[Index]);
  65. }
  66. Index = (Index + 1) % _Size;
  67. } while (Index != FirstIndex);
  68. return 0;
  69. }
  70. void Delete (T& Entry) {
  71. Entry.Key = 0;
  72. }
  73. };
  74. // ...
  75. #endif // #ifndef _TABLE_HXX_INCLUDED_
  76. //
  77. // end of header: table.hxx
  78. //