blob: d15d22d47beceed2ce383aa823f6c9ef26e72024 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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
31#ifndef CERES_PUBLIC_ORDERED_GROUPS_H_
32#define CERES_PUBLIC_ORDERED_GROUPS_H_
33
34#include <map>
35#include <set>
36#include <unordered_map>
37#include <vector>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038
Austin Schuh3de38b02024-06-25 18:25:10 -070039#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080040#include "glog/logging.h"
41
42namespace ceres {
43
44// A class for storing and manipulating an ordered collection of
45// groups/sets with the following semantics:
46//
47// Group ids are non-negative integer values. Elements are any type
48// that can serve as a key in a map or an element of a set.
49//
50// An element can only belong to one group at a time. A group may
51// contain an arbitrary number of elements.
52//
53// Groups are ordered by their group id.
54template <typename T>
55class OrderedGroups {
56 public:
57 // Add an element to a group. If a group with this id does not
58 // exist, one is created. This method can be called any number of
59 // times for the same element. Group ids should be non-negative
60 // numbers.
61 //
62 // Return value indicates if adding the element was a success.
63 bool AddElementToGroup(const T element, const int group) {
64 if (group < 0) {
65 return false;
66 }
67
68 auto it = element_to_group_.find(element);
69 if (it != element_to_group_.end()) {
70 if (it->second == group) {
71 // Element is already in the right group, nothing to do.
72 return true;
73 }
74
75 group_to_elements_[it->second].erase(element);
76 if (group_to_elements_[it->second].size() == 0) {
77 group_to_elements_.erase(it->second);
78 }
79 }
80
81 element_to_group_[element] = group;
82 group_to_elements_[group].insert(element);
83 return true;
84 }
85
86 void Clear() {
87 group_to_elements_.clear();
88 element_to_group_.clear();
89 }
90
91 // Remove the element, no matter what group it is in. Return value
92 // indicates if the element was actually removed.
93 bool Remove(const T element) {
94 const int current_group = GroupId(element);
95 if (current_group < 0) {
96 return false;
97 }
98
99 group_to_elements_[current_group].erase(element);
100
101 if (group_to_elements_[current_group].size() == 0) {
102 // If the group is empty, then get rid of it.
103 group_to_elements_.erase(current_group);
104 }
105
106 element_to_group_.erase(element);
107 return true;
108 }
109
110 // Bulk remove elements. The return value indicates the number of
111 // elements successfully removed.
112 int Remove(const std::vector<T>& elements) {
113 if (NumElements() == 0 || elements.size() == 0) {
114 return 0;
115 }
116
117 int num_removed = 0;
118 for (int i = 0; i < elements.size(); ++i) {
119 num_removed += Remove(elements[i]);
120 }
121 return num_removed;
122 }
123
124 // Reverse the order of the groups in place.
125 void Reverse() {
126 if (NumGroups() == 0) {
127 return;
128 }
129
130 auto it = group_to_elements_.rbegin();
131 std::map<int, std::set<T>> new_group_to_elements;
132 new_group_to_elements[it->first] = it->second;
133
134 int new_group_id = it->first + 1;
135 for (++it; it != group_to_elements_.rend(); ++it) {
136 for (const auto& element : it->second) {
137 element_to_group_[element] = new_group_id;
138 }
139 new_group_to_elements[new_group_id] = it->second;
140 new_group_id++;
141 }
142
143 group_to_elements_.swap(new_group_to_elements);
144 }
145
146 // Return the group id for the element. If the element is not a
147 // member of any group, return -1.
148 int GroupId(const T element) const {
149 auto it = element_to_group_.find(element);
150 if (it == element_to_group_.end()) {
151 return -1;
152 }
153 return it->second;
154 }
155
156 bool IsMember(const T element) const {
157 auto it = element_to_group_.find(element);
158 return (it != element_to_group_.end());
159 }
160
161 // This function always succeeds, i.e., implicitly there exists a
162 // group for every integer.
163 int GroupSize(const int group) const {
164 auto it = group_to_elements_.find(group);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800165 return (it == group_to_elements_.end()) ? 0 : it->second.size();
Austin Schuh70cc9552019-01-21 19:46:48 -0800166 }
167
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800168 int NumElements() const { return element_to_group_.size(); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800169
170 // Number of groups with one or more elements.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800171 int NumGroups() const { return group_to_elements_.size(); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800172
173 // The first group with one or more elements. Calling this when
174 // there are no groups with non-zero elements will result in a
175 // crash.
176 int MinNonZeroGroup() const {
177 CHECK_NE(NumGroups(), 0);
178 return group_to_elements_.begin()->first;
179 }
180
181 const std::map<int, std::set<T>>& group_to_elements() const {
182 return group_to_elements_;
183 }
184
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800185 const std::map<T, int>& element_to_group() const { return element_to_group_; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800186
187 private:
188 std::map<int, std::set<T>> group_to_elements_;
189 std::unordered_map<T, int> element_to_group_;
190};
191
192// Typedef for the most commonly used version of OrderedGroups.
Austin Schuh3de38b02024-06-25 18:25:10 -0700193using ParameterBlockOrdering = OrderedGroups<double*>;
Austin Schuh70cc9552019-01-21 19:46:48 -0800194
195} // namespace ceres
196
197#endif // CERES_PUBLIC_ORDERED_GROUP_H_