gtsam
Loading...
Searching...
No Matches
VariableIndex-inl.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
17
18#pragma once
19
21#include <gtsam/base/timing.h>
22
23namespace gtsam {
24
25/* ************************************************************************* */
26template<class FG>
27void VariableIndex::augment(const FG& factors, const FactorIndices* newFactorIndices) {
28 gttic(VariableIndex_augment);
29
30 // Augment index for each factor
31 for (size_t i = 0; i < factors.size(); ++i) {
32 if (factors[i]) {
33 const size_t globalI =
34 newFactorIndices ? (*newFactorIndices)[i] : nFactors_;
35 for(const Key key: *factors[i]) {
36 index_[key].push_back(globalI);
37 ++nEntries_;
38 }
39 }
40
41 // Increment factor count even if factors are null, to keep indices consistent
42 if (newFactorIndices) {
43 if ((*newFactorIndices)[i] >= nFactors_)
44 nFactors_ = (*newFactorIndices)[i] + 1;
45 } else {
46 ++nFactors_;
47 }
48 }
49}
50
51/* ************************************************************************* */
52template<typename ITERATOR, class FG>
53void VariableIndex::remove(ITERATOR firstFactor, ITERATOR lastFactor,
54 const FG& factors) {
55 gttic(VariableIndex_remove);
56
57 // NOTE: We intentionally do not decrement nFactors_ because the factor
58 // indices need to remain consistent. Removing factors from a factor graph
59 // does not shift the indices of other factors. Also, we keep nFactors_
60 // one greater than the highest-numbered factor referenced in a VariableIndex.
61 ITERATOR factorIndex = firstFactor;
62 size_t i = 0;
63 for (; factorIndex != lastFactor; ++factorIndex, ++i) {
64 if (i >= factors.size())
65 throw std::invalid_argument(
66 "Internal error, requested inconsistent number of factor indices and factors in VariableIndex::remove");
67 if (factors[i]) {
68 for(Key j: *factors[i]) {
69 FactorIndices& factorEntries = internalAt(j);
70 auto entry = std::find(factorEntries.begin(),
71 factorEntries.end(), *factorIndex);
72 if (entry == factorEntries.end())
73 throw std::invalid_argument(
74 "Internal error, indices and factors passed into VariableIndex::remove are not consistent with the existing variable index");
75 factorEntries.erase(entry);
76 --nEntries_;
77 }
78 }
79 }
80}
81
82/* ************************************************************************* */
83template<typename ITERATOR>
84void VariableIndex::removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey) {
85 for (ITERATOR key = firstKey; key != lastKey; ++key) {
86 KeyMap::iterator entry = index_.find(*key);
87 if (!entry->second.empty())
88 throw std::invalid_argument(
89 "Asking to remove variables from the variable index that are not unused");
90 index_.erase(entry);
91 }
92}
93
94}
Timing utilities.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition Factor.h:37
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
const FactorIndices & internalAt(Key variable) const
Internal version of 'at' that asserts existence.
Definition VariableIndex.h:173
void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG &factors)
Remove entries corresponding to the specified factors.
Definition VariableIndex-inl.h:53
void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey)
Remove unused empty variables (in debug mode verifies they are empty).
Definition VariableIndex-inl.h:84
void augment(const FG &factors, const FactorIndices *newFactorIndices=nullptr)
Augment the variable index with new factors.
Definition VariableIndex-inl.h:27