Open3D (C++ API)
Helper.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <functional>
30 #include <string>
31 #include <tuple>
32 #include <vector>
33 
34 namespace open3d {
35 namespace utility {
36 
44 
45 namespace hash_tuple {
46 
47 template <typename TT>
48 struct hash {
49  size_t operator()(TT const& tt) const { return std::hash<TT>()(tt); }
50 };
51 
52 namespace {
53 
54 template <class T>
55 inline void hash_combine(std::size_t& seed, T const& v) {
56  seed ^= hash_tuple::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
57 }
58 
59 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
60 struct HashValueImpl {
61  static void apply(size_t& seed, Tuple const& tuple) {
62  HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
63  hash_combine(seed, std::get<Index>(tuple));
64  }
65 };
66 
67 template <class Tuple>
68 struct HashValueImpl<Tuple, 0> {
69  static void apply(size_t& seed, Tuple const& tuple) {
70  hash_combine(seed, std::get<0>(tuple));
71  }
72 };
73 
74 } // unnamed namespace
75 
76 template <typename... TT>
77 struct hash<std::tuple<TT...>> {
78  size_t operator()(std::tuple<TT...> const& tt) const {
79  size_t seed = 0;
80  HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
81  return seed;
82  }
83 };
84 
85 } // namespace hash_tuple
86 
87 namespace hash_eigen {
88 
89 template <typename T>
90 struct hash : std::unary_function<T, size_t> {
91  std::size_t operator()(T const& matrix) const {
92  size_t seed = 0;
93  for (int i = 0; i < (int)matrix.size(); i++) {
94  auto elem = *(matrix.data() + i);
95  seed ^= std::hash<typename T::Scalar>()(elem) + 0x9e3779b9 +
96  (seed << 6) + (seed >> 2);
97  }
98  return seed;
99  }
100 };
101 
102 } // namespace hash_eigen
103 
106 void SplitString(std::vector<std::string>& tokens,
107  const std::string& str,
108  const std::string& delimiters = " ",
109  bool trim_empty_str = true);
110 
113 std::string StripString(const std::string& s,
114  const std::string& white_space = " \t\n");
115 
119 size_t WordLength(const std::string& doc,
120  size_t start_pos,
121  const std::string& valid_chars = "_");
122 
123 } // namespace utility
124 } // namespace open3d
Definition: Helper.h:90
Definition: Helper.h:48
size_t operator()(std::tuple< TT... > const &tt) const
Definition: Helper.h:78
size_t WordLength(const std::string &doc, size_t start_pos, const std::string &valid_chars)
Definition: Helper.cpp:60
Definition: PinholeCameraIntrinsic.cpp:34
std::size_t operator()(T const &matrix) const
Definition: Helper.h:91
std::string StripString(const std::string &s, const std::string &white_space)
Definition: Helper.cpp:50
void SplitString(std::vector< std::string > &tokens, const std::string &str, const std::string &delimiters, bool trim_empty_str)
Definition: Helper.cpp:35
size_t operator()(TT const &tt) const
Definition: Helper.h:49