SequentialIntegerSet.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file SequentialIntegerSet.h
  13. @brief The SequentialIntegerSet class. */
  14. #pragma once
  15. #include "kNet/Alignment.h"
  16. #include "kNet/NetworkLogging.h"
  17. namespace kNet
  18. {
  19. class SequentialIntegerSet
  20. {
  21. public:
  22. explicit SequentialIntegerSet(int tableSize_)
  23. :tableSize(tableSize_), tableSizeMask(tableSize_-1)
  24. {
  25. assert(IS_POW2(tableSize));
  26. table = new unsigned long[tableSize];
  27. // A bit unconventionally, a value of 0xFFFFFFFF denotes an empty spot.
  28. memset(table, 0xFF, sizeof(unsigned long) * tableSize);
  29. size = 0;
  30. }
  31. ~SequentialIntegerSet()
  32. {
  33. delete[] table;
  34. }
  35. /// Cannot necessarily return the exact size, but only an upper bound.
  36. int Size() const { return size; }
  37. int Capacity() const { return tableSize; }
  38. /// Recomputes the size of this set, so that Size() returns the exact value.
  39. void CountSize()
  40. {
  41. size = 0;
  42. for(int i = 0; i < tableSize; ++i)
  43. if (IsValid(table[i]))
  44. ++size;
  45. }
  46. void Prune()
  47. {
  48. unsigned long *newTable = new unsigned long[tableSize];
  49. size = 0;
  50. for(int i = 0; i < tableSize; ++i)
  51. if (IsValid(table[i]))
  52. {
  53. Add(newTable, table[i]);
  54. ++size;
  55. }
  56. delete[] table;
  57. table = newTable;
  58. }
  59. /// Returns the index in the table where the given value should exist.
  60. int Hash(unsigned long value) const { return (int)(value & tableSizeMask); }
  61. /// Probes the given index linearly and returns the next index to look in.
  62. int LinearProbe(int idx) const { return (idx + 1) & tableSizeMask; }
  63. bool IsNull(unsigned long value) const { return value == 0xFFFFFFFF; }
  64. bool IsValid(unsigned long value) const { return !IsNull(value); }
  65. void Add(unsigned long value)
  66. {
  67. Add(table, value);
  68. }
  69. bool Exists(unsigned long value) const
  70. {
  71. return table[Hash(value)] == value;
  72. }
  73. private:
  74. unsigned long *table;
  75. int tableSize;
  76. int tableSizeMask;
  77. int size;
  78. void Add(unsigned long *dstTable, int value)
  79. {
  80. dstTable[Hash(value)] = value;
  81. }
  82. SequentialIntegerSet(const SequentialIntegerSet &); ///\todo Implement.
  83. void operator =(const SequentialIntegerSet &); ///\todo Implement.
  84. };
  85. } // ~kNet