map_util.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // 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: keir@google.com (Keir Mierle)
  30. //
  31. // Originally by Anton Carver
  32. #ifndef CERES_INTERNAL_MAP_UTIL_H_
  33. #define CERES_INTERNAL_MAP_UTIL_H_
  34. #include <utility>
  35. #include "ceres/internal/export.h"
  36. #include "glog/logging.h"
  37. namespace ceres {
  38. // Perform a lookup in a map or hash_map, assuming that the key exists.
  39. // Crash if it does not.
  40. //
  41. // This is intended as a replacement for operator[] as an rvalue (for reading)
  42. // when the key is guaranteed to exist.
  43. //
  44. // operator[] is discouraged for several reasons:
  45. // * It has a side-effect of inserting missing keys
  46. // * It is not thread-safe (even when it is not inserting, it can still
  47. // choose to resize the underlying storage)
  48. // * It invalidates iterators (when it chooses to resize)
  49. // * It default constructs a value object even if it doesn't need to
  50. //
  51. // This version assumes the key is printable, and includes it in the fatal log
  52. // message.
  53. template <class Collection>
  54. const typename Collection::value_type::second_type& FindOrDie(
  55. const Collection& collection,
  56. const typename Collection::value_type::first_type& key) {
  57. typename Collection::const_iterator it = collection.find(key);
  58. CHECK(it != collection.end()) << "Map key not found: " << key;
  59. return it->second;
  60. }
  61. // Perform a lookup in a map or hash_map.
  62. // If the key is present in the map then the value associated with that
  63. // key is returned, otherwise the value passed as a default is returned.
  64. template <class Collection>
  65. const typename Collection::value_type::second_type FindWithDefault(
  66. const Collection& collection,
  67. const typename Collection::value_type::first_type& key,
  68. const typename Collection::value_type::second_type& value) {
  69. typename Collection::const_iterator it = collection.find(key);
  70. if (it == collection.end()) {
  71. return value;
  72. }
  73. return it->second;
  74. }
  75. // Insert a new key and value into a map or hash_map.
  76. // If the key is not present in the map the key and value are
  77. // inserted, otherwise nothing happens. True indicates that an insert
  78. // took place, false indicates the key was already present.
  79. template <class Collection>
  80. bool InsertIfNotPresent(
  81. Collection* const collection,
  82. const typename Collection::value_type::first_type& key,
  83. const typename Collection::value_type::second_type& value) {
  84. std::pair<typename Collection::iterator, bool> ret =
  85. collection->insert(typename Collection::value_type(key, value));
  86. return ret.second;
  87. }
  88. // Perform a lookup in a map or hash_map.
  89. // Same as above but the returned pointer is not const and can be used to change
  90. // the stored value.
  91. template <class Collection>
  92. typename Collection::value_type::second_type* FindOrNull(
  93. Collection& collection, // NOLINT
  94. const typename Collection::value_type::first_type& key) {
  95. typename Collection::iterator it = collection.find(key);
  96. if (it == collection.end()) {
  97. return 0;
  98. }
  99. return &it->second;
  100. }
  101. // Test to see if a set, map, hash_set or hash_map contains a particular key.
  102. // Returns true if the key is in the collection.
  103. template <class Collection, class Key>
  104. bool ContainsKey(const Collection& collection, const Key& key) {
  105. typename Collection::const_iterator it = collection.find(key);
  106. return it != collection.end();
  107. }
  108. // Inserts a new key/value into a map or hash_map.
  109. // Dies if the key is already present.
  110. template <class Collection>
  111. void InsertOrDie(Collection* const collection,
  112. const typename Collection::value_type::first_type& key,
  113. const typename Collection::value_type::second_type& data) {
  114. using value_type = typename Collection::value_type;
  115. CHECK(collection->insert(value_type(key, data)).second)
  116. << "duplicate key: " << key;
  117. }
  118. } // namespace ceres
  119. #endif // CERES_INTERNAL_MAP_UTIL_H_