2
0

inflection_map.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #pragma once
  31. #include "core/templates/hash_map.h"
  32. #include "core/templates/local_vector.h"
  33. /// An unordered map that splits elements between a fast-access vector of LinearCount consecutively
  34. /// indexed elements, and a slower-access map holding sparse indexes larger than LinearCount.
  35. ///
  36. /// \tparam KeyType is used to lookup values, and must be a type that is convertible to an unsigned integer.
  37. /// \tparam ValueType must have an empty constructor (default or otherwise).
  38. /// \tparam LinearCount
  39. /// \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.
  40. template <typename KeyType, typename ValueType, size_t LinearCount, typename IndexType = uint16_t>
  41. class InflectionMap {
  42. public:
  43. using value_type = ValueType;
  44. class Iterator {
  45. InflectionMap *map;
  46. IndexType index;
  47. public:
  48. using iterator_category = std::forward_iterator_tag;
  49. using value_type = ValueType;
  50. using pointer = value_type *;
  51. using reference = value_type &;
  52. Iterator() :
  53. map(nullptr), index(0) {}
  54. Iterator(InflectionMap &p_m, const IndexType p_i) :
  55. map(&p_m), index(p_i) {}
  56. Iterator &operator=(const Iterator &p_it) {
  57. map = p_it.map;
  58. index = p_it.index;
  59. return *this;
  60. }
  61. ValueType *operator->() { return &map->_values[index]; }
  62. ValueType &operator*() { return map->_values[index]; }
  63. operator ValueType *() { return &map->_values[index]; }
  64. bool operator==(const Iterator &p_it) const { return map == p_it.map && index == p_it.index; }
  65. bool operator!=(const Iterator &p_it) const { return map != p_it.map || index != p_it.index; }
  66. Iterator &operator++() {
  67. index++;
  68. return *this;
  69. }
  70. Iterator operator++(int) {
  71. Iterator t = *this;
  72. index++;
  73. return t;
  74. }
  75. bool is_valid() const { return index < map->_values.size(); }
  76. };
  77. const ValueType &operator[](const KeyType p_idx) const { return get_value(p_idx); }
  78. ValueType &operator[](const KeyType p_idx) { return get_value(p_idx); }
  79. Iterator begin() { return Iterator(*this, 0); }
  80. Iterator end() { return Iterator(*this, _values.size()); }
  81. bool is_empty() { return _values.is_empty(); }
  82. size_t size() { return _values.size(); }
  83. void reserve(size_t p_new_cap) { _values.reserve(p_new_cap); }
  84. protected:
  85. static constexpr IndexType INVALID = std::numeric_limits<IndexType>::max();
  86. typedef struct IndexValue {
  87. IndexType value = INVALID;
  88. } IndexValue;
  89. // Returns a reference to the value at the index.
  90. // If the index has not been initialized, add an empty element at
  91. // the end of the values array, and set the index to its position.
  92. ValueType &get_value(KeyType p_idx) {
  93. IndexValue *val_idx = p_idx < LinearCount ? &_linear_indexes[p_idx] : _inflection_indexes.getptr(p_idx);
  94. if (val_idx == nullptr || val_idx->value == INVALID) {
  95. _values.push_back({});
  96. if (val_idx == nullptr) {
  97. val_idx = &_inflection_indexes.insert(p_idx, {})->value;
  98. }
  99. val_idx->value = _values.size() - 1;
  100. }
  101. return _values[val_idx->value];
  102. }
  103. TightLocalVector<ValueType> _values;
  104. HashMap<KeyType, IndexValue> _inflection_indexes;
  105. IndexValue _linear_indexes[LinearCount];
  106. };