GeometryDatabase.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 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 Core {
  33. namespace GeometryDatabase {
  34. class Database {
  35. public:
  36. Database() {
  37. constexpr size_t reserve_size = 512;
  38. geometry_list.reserve(reserve_size);
  39. free_list.reserve(reserve_size);
  40. }
  41. ~Database() {
  42. #ifdef RMLUI_TESTS_ENABLED
  43. RMLUI_ASSERT(geometry_list.size() == free_list.size());
  44. std::sort(free_list.begin(), free_list.end());
  45. for (size_t i = 0; i < free_list.size(); i++)
  46. {
  47. RMLUI_ASSERT(i == free_list[i]);
  48. }
  49. #endif
  50. }
  51. GeometryDatabaseHandle insert(Geometry* value)
  52. {
  53. GeometryDatabaseHandle handle;
  54. if (free_list.empty())
  55. {
  56. handle = GeometryDatabaseHandle(geometry_list.size());
  57. geometry_list.push_back(value);
  58. }
  59. else
  60. {
  61. handle = free_list.back();
  62. free_list.pop_back();
  63. geometry_list[handle] = value;
  64. }
  65. return handle;
  66. }
  67. void erase(GeometryDatabaseHandle handle)
  68. {
  69. free_list.push_back(handle);
  70. }
  71. // Iterate over every item in the database, skipping free slots.
  72. template<typename Func>
  73. void for_each(Func func)
  74. {
  75. std::sort(free_list.begin(), free_list.end());
  76. size_t i_begin_next = 0;
  77. for (GeometryDatabaseHandle freelist_entry : free_list)
  78. {
  79. const size_t i_end = size_t(freelist_entry);
  80. const size_t i_begin = i_begin_next;
  81. i_begin_next = i_end + 1;
  82. for (size_t i = i_begin; i < i_end; i++)
  83. func(geometry_list[i]);
  84. }
  85. for (size_t i = i_begin_next; i < geometry_list.size(); i++)
  86. func(geometry_list[i]);
  87. }
  88. private:
  89. // List of all active geometry, in addition to free slots.
  90. // Free slots (as defined by the 'free_list') may contain dangling pointers and must not be dereferenced.
  91. std::vector<Geometry*> geometry_list;
  92. // Declares free slots in the 'geometry_list' as indices.
  93. std::vector<GeometryDatabaseHandle> free_list;
  94. };
  95. static Database geometry_database;
  96. GeometryDatabaseHandle Insert(Geometry* geometry)
  97. {
  98. return geometry_database.insert(geometry);
  99. }
  100. void Erase(GeometryDatabaseHandle handle)
  101. {
  102. geometry_database.erase(handle);
  103. }
  104. void ReleaseAll()
  105. {
  106. geometry_database.for_each([](Geometry* geometry) {
  107. geometry->Release();
  108. });
  109. }
  110. #ifdef RMLUI_TESTS_ENABLED
  111. static class TestGeometryDatabase {
  112. private:
  113. std::vector<Geometry> geometry_list;
  114. bool list_database_equivalent()
  115. {
  116. int i = 0;
  117. bool result = true;
  118. GetDatabase().for_each([this, &i, &result](Geometry* geometry) {
  119. result &= (geometry == &geometry_list[i++]);
  120. });
  121. return result;
  122. }
  123. public:
  124. TestGeometryDatabase() : geometry_list(10)
  125. {
  126. bool result = true;
  127. int i = 0;
  128. for (auto& geometry : geometry_list)
  129. geometry.GetIndices().push_back(i++);
  130. result &= list_database_equivalent();
  131. geometry_list.reserve(2000);
  132. result &= list_database_equivalent();
  133. geometry_list.erase(geometry_list.begin() + 5);
  134. result &= list_database_equivalent();
  135. std::swap(geometry_list.front(), geometry_list.back());
  136. geometry_list.pop_back();
  137. result &= list_database_equivalent();
  138. std::swap(geometry_list.front(), geometry_list.back());
  139. result &= list_database_equivalent();
  140. geometry_list.emplace_back();
  141. result &= list_database_equivalent();
  142. geometry_list.clear();
  143. result &= list_database_equivalent();
  144. RMLUI_ASSERT(result);
  145. }
  146. } test_geometry_database;
  147. #endif
  148. }
  149. }
  150. }