inflection_map.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**************************************************************************/
  2. /* inflection_map.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef INFLECTION_MAP_H
  31. #define INFLECTION_MAP_H
  32. #include "core/templates/hash_map.h"
  33. #include "core/templates/local_vector.h"
  34. /// An unordered map that splits elements between a fast-access vector of LinearCount consecutively
  35. /// indexed elements, and a slower-access map holding sparse indexes larger than LinearCount.
  36. ///
  37. /// \tparam KeyType is used to lookup values, and must be a type that is convertible to an unsigned integer.
  38. /// \tparam ValueType must have an empty constructor (default or otherwise).
  39. /// \tparam LinearCount
  40. /// \tparam IndexType must be a type that is convertible to an unsigned integer (eg. uint8_t...uint64_t), and which is large enough to represent the number of values in this map.
  41. template <typename KeyType, typename ValueType, size_t LinearCount, typename IndexType = uint16_t>
  42. class InflectionMap {
  43. public:
  44. using value_type = ValueType;
  45. class Iterator {
  46. InflectionMap *map;
  47. IndexType index;
  48. public:
  49. using iterator_category = std::forward_iterator_tag;
  50. using value_type = ValueType;
  51. using pointer = value_type *;
  52. using reference = value_type &;
  53. Iterator() :
  54. map(nullptr), index(0) {}
  55. Iterator(InflectionMap &p_m, const IndexType p_i) :
  56. map(&p_m), index(p_i) {}
  57. Iterator &operator=(const Iterator &p_it) {
  58. map = p_it.map;
  59. index = p_it.index;
  60. return *this;
  61. }
  62. ValueType *operator->() { return &map->_values[index]; }
  63. ValueType &operator*() { return map->_values[index]; }
  64. operator ValueType *() { return &map->_values[index]; }
  65. bool operator==(const Iterator &p_it) const { return map == p_it.map && index == p_it.index; }
  66. bool operator!=(const Iterator &p_it) const { return map != p_it.map || index != p_it.index; }
  67. Iterator &operator++() {
  68. index++;
  69. return *this;
  70. }
  71. Iterator operator++(int) {
  72. Iterator t = *this;
  73. index++;
  74. return t;
  75. }
  76. bool is_valid() const { return index < map->_values.size(); }
  77. };
  78. const ValueType &operator[](const KeyType p_idx) const { return get_value(p_idx); }
  79. ValueType &operator[](const KeyType p_idx) { return get_value(p_idx); }
  80. Iterator begin() { return Iterator(*this, 0); }
  81. Iterator end() { return Iterator(*this, _values.size()); }
  82. bool is_empty() { return _values.is_empty(); }
  83. size_t size() { return _values.size(); }
  84. void reserve(size_t p_new_cap) { _values.reserve(p_new_cap); }
  85. protected:
  86. static constexpr IndexType INVALID = std::numeric_limits<IndexType>::max();
  87. typedef struct IndexValue {
  88. IndexType value = INVALID;
  89. } IndexValue;
  90. // Returns a reference to the value at the index.
  91. // If the index has not been initialized, add an empty element at
  92. // the end of the values array, and set the index to its position.
  93. ValueType &get_value(KeyType p_idx) {
  94. IndexValue *val_idx = p_idx < LinearCount ? &_linear_indexes[p_idx] : _inflection_indexes.getptr(p_idx);
  95. if (val_idx == nullptr || val_idx->value == INVALID) {
  96. _values.push_back({});
  97. if (val_idx == nullptr) {
  98. val_idx = &_inflection_indexes.insert(p_idx, {})->value;
  99. }
  100. val_idx->value = _values.size() - 1;
  101. }
  102. return _values[val_idx->value];
  103. }
  104. TightLocalVector<ValueType> _values;
  105. HashMap<KeyType, IndexValue> _inflection_indexes;
  106. IndexValue _linear_indexes[LinearCount];
  107. };
  108. #endif // INFLECTION_MAP_H