LabeledSymbol.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 
19 
20 #include <iostream>
21 #include <limits>
22 
23 namespace gtsam {
24 
25 using namespace std;
26 
27 /* ************************************************************************* */
29 : c_(0), label_(0), j_(0) {}
30 
31 /* ************************************************************************* */
33 : c_(key.c_), label_(key.label_), j_(key.j_) {}
34 
35 /* ************************************************************************* */
36 LabeledSymbol::LabeledSymbol(unsigned char c, unsigned char label, std::uint64_t j)
37 : c_(c), label_(label), j_(j) {}
38 
39 /* ************************************************************************* */
41  const size_t keyBits = sizeof(gtsam::Key) * 8;
42  const size_t chrBits = sizeof(unsigned char) * 8;
43  const size_t lblBits = sizeof(unsigned char) * 8;
44  const size_t indexBits = keyBits - chrBits - lblBits;
45  const gtsam::Key chrMask = gtsam::Key(std::numeric_limits<unsigned char>::max()) << (indexBits + lblBits);
47  const gtsam::Key indexMask = ~(chrMask | lblMask);
48  c_ = (unsigned char)((key & chrMask) >> (indexBits + lblBits));
49  label_ = (unsigned char)((key & lblMask) >> indexBits);
50  j_ = key & indexMask;
51 }
52 
53 /* ************************************************************************* */
54 LabeledSymbol::operator gtsam::Key() const {
55  const size_t keyBits = sizeof(gtsam::Key) * 8;
56  const size_t chrBits = sizeof(unsigned char) * 8;
57  const size_t lblBits = sizeof(unsigned char) * 8;
58  const size_t indexBits = keyBits - chrBits - lblBits;
59  const gtsam::Key chrMask = gtsam::Key(std::numeric_limits<unsigned char>::max()) << (indexBits + lblBits);
61  const gtsam::Key indexMask = ~(chrMask | lblMask);
62  if(j_ > indexMask)
63  throw std::invalid_argument("Symbol index is too large");
64  gtsam::Key key = (gtsam::Key(c_) << (indexBits + lblBits)) | (gtsam::Key(label_) << indexBits) | j_;
65  return key;
66 }
67 
68 /* ************************************************************************* */
69 void LabeledSymbol::print(const std::string& s) const {
70  std::cout << s << ": " << (std::string) (*this) << std::endl;
71 }
72 
73 /* ************************************************************************* */
74 LabeledSymbol::operator std::string() const {
75  char buffer[100];
76  snprintf(buffer, 100, "%c%c%llu", c_, label_,
77  static_cast<unsigned long long>(j_));
78  return std::string(buffer);
79 }
80 
81 /* ************************************************************************* */
82 bool LabeledSymbol::operator<(const LabeledSymbol& comp) const {
83  return c_ < comp.c_
84  || (comp.c_ == c_ && label_ < comp.label_)
85  || (comp.c_ == c_ && comp.label_ == label_ && j_ < comp.j_);
86 }
87 
88 /* ************************************************************************* */
89 bool LabeledSymbol::operator==(const LabeledSymbol& comp) const {
90  return comp.c_ == c_ && comp.label_ == label_ && comp.j_ == j_;
91 }
92 
93 /* ************************************************************************* */
94 bool LabeledSymbol::operator!=(const LabeledSymbol& comp) const {
95  return comp.c_ != c_ || comp.label_ != label_ || comp.j_ != j_;
96 }
97 
98 /* ************************************************************************* */
100  return comp == (gtsam::Key)(*this);
101 }
102 
103 /* ************************************************************************* */
105  return comp != (gtsam::Key)(*this);
106 }
107 
108 /* ************************************************************************* */
110 
111 std::function<bool(gtsam::Key)> LabeledSymbol::TypeTest(unsigned char c) {
112  // Use lambda function to check equality
113  auto equals = [](unsigned char s, unsigned char c) { return s == c; };
114  return std::bind(
115  equals,
116  std::bind(&LabeledSymbol::chr, std::bind(make, std::placeholders::_1)),
117  c);
118 }
119 
120 std::function<bool(gtsam::Key)> LabeledSymbol::LabelTest(unsigned char label) {
121  // Use lambda function to check equality
122  auto equals = [](unsigned char s, unsigned char c) { return s == c; };
123  return std::bind(
124  equals,
125  std::bind(&LabeledSymbol::label, std::bind(make, std::placeholders::_1)),
126  label);
127 }
128 
129 std::function<bool(gtsam::Key)> LabeledSymbol::TypeLabelTest(unsigned char c, unsigned char label) {
130  // Use lambda functions for && and ==
131  auto logical_and = [](bool is_type, bool is_label) { return is_type == is_label; };
132  auto equals = [](unsigned char s, unsigned char c) { return s == c; };
133  return std::bind(logical_and,
134  std::bind(equals,
135  std::bind(&LabeledSymbol::chr,
136  std::bind(make, std::placeholders::_1)),
137  c),
138  std::bind(equals,
139  std::bind(&LabeledSymbol::label,
140  std::bind(make, std::placeholders::_1)),
141  label));
142 }
143 
144 /* ************************************************************************* */
145 GTSAM_EXPORT std::ostream &operator<<(std::ostream &os, const LabeledSymbol &symbol) {
146  os << StreamedKey(symbol);
147  return os;
148 }
149 
150 /* ************************************************************************* */
151 
152 } // \namespace gtsam
153 
unsigned char chr() const
Definition: LabeledSymbol.h:70
#define max(a, b)
Definition: datatypes.h:20
unsigned char label_
Definition: LabeledSymbol.h:37
static LabeledSymbol make(gtsam::Key key)
static const Key chrMask
Definition: Symbol.cpp:32
static const Key indexMask
Definition: Symbol.cpp:33
std::uint64_t j_
Definition: LabeledSymbol.h:38
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Definition: BFloat16.h:88
static std::function< bool(gtsam::Key)> LabelTest(unsigned char label)
bool operator==(const LabeledSymbol &comp) const
static const size_t indexBits
Definition: Symbol.cpp:31
gtsam::Key key() const
Definition: LabeledSymbol.h:64
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
To use the key_formatter on Keys, they must be wrapped in a StreamedKey.
Definition: Key.h:58
unsigned char c_
Definition: LabeledSymbol.h:37
static std::function< bool(gtsam::Key)> TypeTest(unsigned char c)
bool operator!=(const LabeledSymbol &comp) const
unsigned char label() const
Definition: LabeledSymbol.h:67
RealScalar s
Key symbol(unsigned char c, std::uint64_t j)
static const size_t chrBits
Definition: Symbol.cpp:30
static std::function< bool(gtsam::Key)> TypeLabelTest(unsigned char c, unsigned char label)
bool operator<(const LabeledSymbol &comp) const
traits
Definition: chartTesting.h:28
static const size_t keyBits
Definition: Symbol.cpp:29
ofstream os("timeSchurFactors.csv")
friend GTSAM_EXPORT std::ostream & operator<<(std::ostream &, const LabeledSymbol &)
Output stream operator that can be used with key_formatter (see Key.h).
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::ptrdiff_t j
void print(const std::string &s="") const


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:30