ordered_groups.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: sameeragarwal@google.com (Sameer Agarwal)
  30. #ifndef CERES_PUBLIC_ORDERED_GROUPS_H_
  31. #define CERES_PUBLIC_ORDERED_GROUPS_H_
  32. #include <map>
  33. #include <set>
  34. #include <unordered_map>
  35. #include <vector>
  36. #include "ceres/internal/export.h"
  37. #include "glog/logging.h"
  38. namespace ceres {
  39. // A class for storing and manipulating an ordered collection of
  40. // groups/sets with the following semantics:
  41. //
  42. // Group ids are non-negative integer values. Elements are any type
  43. // that can serve as a key in a map or an element of a set.
  44. //
  45. // An element can only belong to one group at a time. A group may
  46. // contain an arbitrary number of elements.
  47. //
  48. // Groups are ordered by their group id.
  49. template <typename T>
  50. class OrderedGroups {
  51. public:
  52. // Add an element to a group. If a group with this id does not
  53. // exist, one is created. This method can be called any number of
  54. // times for the same element. Group ids should be non-negative
  55. // numbers.
  56. //
  57. // Return value indicates if adding the element was a success.
  58. bool AddElementToGroup(const T element, const int group) {
  59. if (group < 0) {
  60. return false;
  61. }
  62. auto it = element_to_group_.find(element);
  63. if (it != element_to_group_.end()) {
  64. if (it->second == group) {
  65. // Element is already in the right group, nothing to do.
  66. return true;
  67. }
  68. group_to_elements_[it->second].erase(element);
  69. if (group_to_elements_[it->second].size() == 0) {
  70. group_to_elements_.erase(it->second);
  71. }
  72. }
  73. element_to_group_[element] = group;
  74. group_to_elements_[group].insert(element);
  75. return true;
  76. }
  77. void Clear() {
  78. group_to_elements_.clear();
  79. element_to_group_.clear();
  80. }
  81. // Remove the element, no matter what group it is in. Return value
  82. // indicates if the element was actually removed.
  83. bool Remove(const T element) {
  84. const int current_group = GroupId(element);
  85. if (current_group < 0) {
  86. return false;
  87. }
  88. group_to_elements_[current_group].erase(element);
  89. if (group_to_elements_[current_group].size() == 0) {
  90. // If the group is empty, then get rid of it.
  91. group_to_elements_.erase(current_group);
  92. }
  93. element_to_group_.erase(element);
  94. return true;
  95. }
  96. // Bulk remove elements. The return value indicates the number of
  97. // elements successfully removed.
  98. int Remove(const std::vector<T>& elements) {
  99. if (NumElements() == 0 || elements.size() == 0) {
  100. return 0;
  101. }
  102. int num_removed = 0;
  103. for (int i = 0; i < elements.size(); ++i) {
  104. num_removed += Remove(elements[i]);
  105. }
  106. return num_removed;
  107. }
  108. // Reverse the order of the groups in place.
  109. void Reverse() {
  110. if (NumGroups() == 0) {
  111. return;
  112. }
  113. auto it = group_to_elements_.rbegin();
  114. std::map<int, std::set<T>> new_group_to_elements;
  115. new_group_to_elements[it->first] = it->second;
  116. int new_group_id = it->first + 1;
  117. for (++it; it != group_to_elements_.rend(); ++it) {
  118. for (const auto& element : it->second) {
  119. element_to_group_[element] = new_group_id;
  120. }
  121. new_group_to_elements[new_group_id] = it->second;
  122. new_group_id++;
  123. }
  124. group_to_elements_.swap(new_group_to_elements);
  125. }
  126. // Return the group id for the element. If the element is not a
  127. // member of any group, return -1.
  128. int GroupId(const T element) const {
  129. auto it = element_to_group_.find(element);
  130. if (it == element_to_group_.end()) {
  131. return -1;
  132. }
  133. return it->second;
  134. }
  135. bool IsMember(const T element) const {
  136. auto it = element_to_group_.find(element);
  137. return (it != element_to_group_.end());
  138. }
  139. // This function always succeeds, i.e., implicitly there exists a
  140. // group for every integer.
  141. int GroupSize(const int group) const {
  142. auto it = group_to_elements_.find(group);
  143. return (it == group_to_elements_.end()) ? 0 : it->second.size();
  144. }
  145. int NumElements() const { return element_to_group_.size(); }
  146. // Number of groups with one or more elements.
  147. int NumGroups() const { return group_to_elements_.size(); }
  148. // The first group with one or more elements. Calling this when
  149. // there are no groups with non-zero elements will result in a
  150. // crash.
  151. int MinNonZeroGroup() const {
  152. CHECK_NE(NumGroups(), 0);
  153. return group_to_elements_.begin()->first;
  154. }
  155. const std::map<int, std::set<T>>& group_to_elements() const {
  156. return group_to_elements_;
  157. }
  158. const std::map<T, int>& element_to_group() const { return element_to_group_; }
  159. private:
  160. std::map<int, std::set<T>> group_to_elements_;
  161. std::unordered_map<T, int> element_to_group_;
  162. };
  163. // Typedef for the most commonly used version of OrderedGroups.
  164. using ParameterBlockOrdering = OrderedGroups<double*>;
  165. } // namespace ceres
  166. #endif // CERES_PUBLIC_ORDERED_GROUP_H_