GeometryDatabase.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "GeometryDatabase.h"
  29. #include "../../Include/RmlUi/Core/Geometry.h"
  30. #include <algorithm>
  31. namespace Rml {
  32. namespace GeometryDatabase {
  33. class Database {
  34. public:
  35. Database()
  36. {
  37. constexpr size_t reserve_size = 512;
  38. geometry_list.reserve(reserve_size);
  39. free_list.reserve(reserve_size);
  40. }
  41. ~Database()
  42. {
  43. #ifdef RMLUI_TESTS_ENABLED
  44. RMLUI_ASSERT(geometry_list.size() == free_list.size());
  45. std::sort(free_list.begin(), free_list.end());
  46. for (size_t i = 0; i < free_list.size(); i++)
  47. {
  48. RMLUI_ASSERT(i == free_list[i]);
  49. }
  50. #endif
  51. }
  52. GeometryDatabaseHandle insert(Geometry* value)
  53. {
  54. GeometryDatabaseHandle handle;
  55. if (free_list.empty())
  56. {
  57. handle = GeometryDatabaseHandle(geometry_list.size());
  58. geometry_list.push_back(value);
  59. }
  60. else
  61. {
  62. handle = free_list.back();
  63. free_list.pop_back();
  64. geometry_list[handle] = value;
  65. }
  66. return handle;
  67. }
  68. int size() const { return (int)geometry_list.size() - (int)free_list.size(); }
  69. void clear()
  70. {
  71. geometry_list.clear();
  72. free_list.clear();
  73. }
  74. void erase(GeometryDatabaseHandle handle) { free_list.push_back(handle); }
  75. // Iterate over every item in the database, skipping free slots.
  76. template <typename Func>
  77. void for_each(Func&& func)
  78. {
  79. std::sort(free_list.begin(), free_list.end());
  80. size_t i_begin_next = 0;
  81. for (GeometryDatabaseHandle freelist_entry : free_list)
  82. {
  83. const size_t i_end = size_t(freelist_entry);
  84. const size_t i_begin = i_begin_next;
  85. i_begin_next = i_end + 1;
  86. for (size_t i = i_begin; i < i_end; i++)
  87. func(geometry_list[i]);
  88. }
  89. for (size_t i = i_begin_next; i < geometry_list.size(); i++)
  90. func(geometry_list[i]);
  91. }
  92. private:
  93. // List of all active geometry, in addition to free slots.
  94. // Free slots (as defined by the 'free_list') may contain dangling pointers and must not be dereferenced.
  95. Vector<Geometry*> geometry_list;
  96. // Declares free slots in the 'geometry_list' as indices.
  97. Vector<GeometryDatabaseHandle> free_list;
  98. };
  99. static Database geometry_database;
  100. GeometryDatabaseHandle Insert(Geometry* geometry)
  101. {
  102. return geometry_database.insert(geometry);
  103. }
  104. void Erase(GeometryDatabaseHandle handle)
  105. {
  106. geometry_database.erase(handle);
  107. }
  108. void ReleaseAll()
  109. {
  110. geometry_database.for_each([](Geometry* geometry) { geometry->Release(); });
  111. }
  112. #ifdef RMLUI_TESTS_ENABLED
  113. bool PrepareForTests()
  114. {
  115. if (geometry_database.size() > 0)
  116. return false;
  117. // Even with size()==0 we can have items in the geometry list which should all be duplicated by the free list.
  118. // We want to clear them for the tests.
  119. geometry_database.clear();
  120. return true;
  121. }
  122. bool ListMatchesDatabase(const Vector<Geometry>& geometry_list)
  123. {
  124. Vector<const Geometry*> geometry_list_ptrs;
  125. std::for_each(geometry_list.begin(), geometry_list.end(), [&](const Geometry& geometry) { geometry_list_ptrs.push_back(&geometry); });
  126. Vector<const Geometry*> database_ptrs;
  127. geometry_database.for_each([&](const Geometry* geometry) { database_ptrs.push_back(geometry); });
  128. const bool result = std::is_permutation(geometry_list_ptrs.begin(), geometry_list_ptrs.end(), database_ptrs.begin(), database_ptrs.end());
  129. return result;
  130. }
  131. #endif // RMLUI_TESTS_ENABLED
  132. } // namespace GeometryDatabase
  133. } // namespace Rml