Key.cpp
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 
20 #include <gtsam/inference/Key.h>
22 
23 #include <iostream>
24 
25 using namespace std;
26 
27 namespace gtsam {
28 
32 
33 /* ************************************************************************* */
35  const Symbol asSymbol(key);
36  if (asSymbol.chr() > 0) {
37  return (string) asSymbol;
38  }
39  else {
40  return std::to_string(key);
41  }
42 }
43 
44 /* ************************************************************************* */
45 void PrintKey(Key key, const string& s, const KeyFormatter& keyFormatter) {
46  cout << s << keyFormatter(key);
47 }
48 
49 /* ************************************************************************* */
51  const LabeledSymbol asLabeledSymbol(key);
52  if (asLabeledSymbol.chr() > 0 && asLabeledSymbol.label() > 0)
53  return (string) asLabeledSymbol;
54 
55  const Symbol asSymbol(key);
56  if (asLabeledSymbol.chr() > 0) {
57  return (string) asSymbol;
58  }
59  else {
60  return std::to_string(key);
61  }
62 }
63 
64 /* ************************************************************************* */
65 template<class CONTAINER>
66 void Print(const CONTAINER& keys, const string& s,
67  const KeyFormatter& keyFormatter) {
68  cout << s << " ";
69  if (keys.empty())
70  cout << "(none)" << endl;
71  else {
72  for(const Key& key: keys)
73  cout << keyFormatter(key) << " ";
74  cout << endl;
75  }
76 }
77 
78 /* ************************************************************************* */
79 void PrintKeyList(const KeyList& keys, const string& s,
80  const KeyFormatter& keyFormatter) {
81  Print(keys, s, keyFormatter);
82 }
83 /* ************************************************************************* */
84 void PrintKeyVector(const KeyVector& keys, const string& s,
85  const KeyFormatter& keyFormatter) {
86  Print(keys, s, keyFormatter);
87 }
88 /* ************************************************************************* */
89 void PrintKeySet(const KeySet& keys, const string& s,
90  const KeyFormatter& keyFormatter) {
91  Print(keys, s, keyFormatter);
92 }
93 
94 /* ************************************************************************* */
95 // Access to custom stream property.
96 void *&key_formatter::property(ios_base &s) {
97  static int kUniqueIndex = ios_base::xalloc();
98  return s.pword(kUniqueIndex);
99 }
100 
101 /* ************************************************************************* */
102 // Store pointer to formatter in property.
103 void key_formatter::set_property(ios_base &s, const KeyFormatter &f) {
104  property(s) = (void *)(&f);
105 }
106 
107 /* ************************************************************************* */
108 // Get pointer to formatter from property.
109 KeyFormatter *key_formatter::get_property(ios_base &s) {
110  return (KeyFormatter *)(property(s));
111 }
112 
113 /* ************************************************************************* */
114 // Stream operator that will take a key_formatter and set the stream property.
115 ostream &operator<<(ostream &os, const key_formatter &m) {
116  key_formatter::set_property(os, m.formatter_);
117  return os;
118 }
119 
120 /* ************************************************************************* */
121 // Stream operator that takes a StreamedKey and properly formats it
122 ostream &operator<<(ostream &os, const StreamedKey &streamedKey) {
123  const KeyFormatter *formatter = key_formatter::get_property(os);
124  if (formatter == nullptr) {
126  }
127  os << (*formatter)(streamedKey.key_);
128  return (os);
129 }
130 
131 /* ************************************************************************* */
132 
133 } // \namespace gtsam
gtsam::LabeledSymbol::chr
unsigned char chr() const
Retrieve key character.
Definition: LabeledSymbol.h:84
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::LabeledSymbol
Definition: LabeledSymbol.h:37
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::StreamedKey::key_
const Key & key_
Definition: Key.h:64
gtsam::PrintKeyVector
void PrintKeyVector(const KeyVector &keys, const string &s, const KeyFormatter &keyFormatter)
Utility function to print sets of keys with optional prefix.
Definition: Key.cpp:84
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::FastSet< Key >
os
ofstream os("timeSchurFactors.csv")
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:91
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::Symbol::chr
unsigned char chr() const
Definition: inference/Symbol.h:75
Key.h
gtsam::_multirobotKeyFormatter
string _multirobotKeyFormatter(Key key)
Definition: Key.cpp:50
gtsam::PrintKey
void PrintKey(Key key, const string &s, const KeyFormatter &keyFormatter)
Utility function to print one key with optional prefix.
Definition: Key.cpp:45
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
gtsam::operator<<
ostream & operator<<(ostream &os, const StreamedKey &streamedKey)
Definition: Key.cpp:122
LabeledSymbol.h
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
gtsam::FastList
Definition: FastList.h:43
gtsam::LabeledSymbol::label
unsigned char label() const
Retrieve label character.
Definition: LabeledSymbol.h:81
key
const gtsam::Symbol key('X', 0)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam::MultiRobotKeyFormatter
KeyFormatter MultiRobotKeyFormatter
Definition: Key.cpp:31
gtsam::StreamedKey
To use the key_formatter on Keys, they must be wrapped in a StreamedKey.
Definition: Key.h:63
gtsam
traits
Definition: ABC.h:17
std
Definition: BFloat16.h:88
gtsam::key_formatter
Definition: Key.h:77
gtsam::_defaultKeyFormatter
string _defaultKeyFormatter(Key key)
Definition: Key.cpp:34
gtsam::Print
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:66
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::PrintKeySet
void PrintKeySet(const KeySet &keys, const string &s, const KeyFormatter &keyFormatter)
Utility function to print sets of keys with optional prefix.
Definition: Key.cpp:89
gtsam::PrintKeyList
void PrintKeyList(const KeyList &keys, const string &s, const KeyFormatter &keyFormatter)
Utility function to print sets of keys with optional prefix.
Definition: Key.cpp:79
gtsam::Symbol
Definition: inference/Symbol.h:37


gtsam
Author(s):
autogenerated on Wed May 28 2025 03:01:37