LiteralTable.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (c) 2008 Roberto Raggi <[email protected]>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #ifndef CPLUSPLUS_LITERALTABLE_H
  21. #define CPLUSPLUS_LITERALTABLE_H
  22. #include "CPlusPlusForwardDeclarations.h"
  23. #include <cstring>
  24. namespace CPlusPlus {
  25. template <typename Literal>
  26. class LiteralTable
  27. {
  28. LiteralTable(const LiteralTable &other);
  29. void operator =(const LiteralTable &other);
  30. public:
  31. typedef Literal *const *iterator;
  32. public:
  33. LiteralTable()
  34. : _literals(0),
  35. _buckets(0),
  36. _allocatedLiterals(0),
  37. _literalCount(-1),
  38. _allocatedBuckets(0)
  39. { }
  40. ~LiteralTable()
  41. {
  42. reset();
  43. }
  44. void reset()
  45. {
  46. if (_literals) {
  47. Literal **lastLiteral = _literals + _literalCount + 1;
  48. for (Literal **it = _literals; it != lastLiteral; ++it)
  49. delete *it;
  50. std::free(_literals);
  51. }
  52. if (_buckets)
  53. std::free(_buckets);
  54. _literals = 0;
  55. _buckets = 0;
  56. _allocatedLiterals = 0;
  57. _literalCount = -1;
  58. _allocatedBuckets = 0;
  59. }
  60. bool empty() const
  61. { return _literalCount == -1; }
  62. unsigned size() const
  63. { return _literalCount + 1; }
  64. const Literal *at(unsigned index) const
  65. { return _literals[index]; }
  66. iterator begin() const
  67. { return _literals; }
  68. iterator end() const
  69. { return _literals + _literalCount + 1; }
  70. const Literal *findLiteral(const char *chars, unsigned size) const
  71. {
  72. if (_buckets) {
  73. unsigned h = Literal::hashCode(chars, size);
  74. Literal *literal = _buckets[h % _allocatedBuckets];
  75. for (; literal; literal = static_cast<Literal *>(literal->_next)) {
  76. if (literal->size() == size && ! std::strncmp(literal->chars(), chars, size))
  77. return literal;
  78. }
  79. }
  80. return 0;
  81. }
  82. const Literal *findOrInsertLiteral(const char *chars, unsigned size)
  83. {
  84. if (_buckets) {
  85. unsigned h = Literal::hashCode(chars, size);
  86. Literal *literal = _buckets[h % _allocatedBuckets];
  87. for (; literal; literal = static_cast<Literal *>(literal->_next)) {
  88. if (literal->size() == size && ! std::strncmp(literal->chars(), chars, size))
  89. return literal;
  90. }
  91. }
  92. Literal *literal = new Literal(chars, size);
  93. if (++_literalCount == _allocatedLiterals) {
  94. if (! _allocatedLiterals)
  95. _allocatedLiterals = 4;
  96. else
  97. _allocatedLiterals <<= 1;
  98. _literals = (Literal **) std::realloc(_literals, sizeof(Literal *) * _allocatedLiterals);
  99. }
  100. _literals[_literalCount] = literal;
  101. if (! _buckets || _literalCount * 5 >= _allocatedBuckets * 3)
  102. rehash();
  103. else {
  104. unsigned h = literal->hashCode() % _allocatedBuckets;
  105. literal->_next = _buckets[h];
  106. _buckets[h] = literal;
  107. }
  108. return literal;
  109. }
  110. protected:
  111. void rehash()
  112. {
  113. if (_buckets)
  114. std::free(_buckets);
  115. if (! _allocatedBuckets)
  116. _allocatedBuckets = 4;
  117. else
  118. _allocatedBuckets <<= 1;
  119. _buckets = (Literal **) std::calloc(_allocatedBuckets, sizeof(Literal *));
  120. Literal **lastLiteral = _literals + (_literalCount + 1);
  121. for (Literal **it = _literals; it != lastLiteral; ++it) {
  122. Literal *literal = *it;
  123. unsigned h = literal->hashCode() % _allocatedBuckets;
  124. literal->_next = _buckets[h];
  125. _buckets[h] = literal;
  126. }
  127. }
  128. protected:
  129. Literal **_literals;
  130. Literal **_buckets;
  131. int _allocatedLiterals;
  132. int _literalCount;
  133. int _allocatedBuckets;
  134. };
  135. } // namespace CPlusPlus
  136. #endif // CPLUSPLUS_LITERALTABLE_H