LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
peek_map.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
25 //
26 // peek_map .hpp .cpp - Utility to peek into a map without the side effect
27 // of adding the default value when a key is not found
29 
30 #ifndef LBANN_PEEK_MAP_HPP_INCLUDED
31 #define LBANN_PEEK_MAP_HPP_INCLUDED
32 
33 #include <map>
34 
35 namespace lbann {
36 
45 template <class KEY_T, // map::key_type
46  class VAL_T, // map::mapped_type
47  class CMP_T = std::less<KEY_T> // map::key_compare
48  >
49 VAL_T peek_map(const std::map<KEY_T, VAL_T, CMP_T>& map_to_peek,
50  KEY_T idx,
51  bool& found)
52 {
53  using map_to_peek_t = std::map<KEY_T, VAL_T, CMP_T>;
54  typename map_to_peek_t::const_iterator it = map_to_peek.find(idx);
55  if (it == map_to_peek.cend()) {
56  found = false;
57  return VAL_T();
58  }
59  found = true;
60  return it->second;
61 }
62 
67 template <class KEY_T, // map::key_type
68  class VAL_T, // map::mapped_type
69  class CMP_T = std::less<KEY_T> // map::key_compare
70  >
71 VAL_T peek_map(const std::map<KEY_T, VAL_T, CMP_T>& map_to_peek, KEY_T idx)
72 {
73  using map_to_peek_t = std::map<KEY_T, VAL_T, CMP_T>;
74  typename map_to_peek_t::const_iterator it = map_to_peek.find(idx);
75  if (it == map_to_peek.cend()) {
76  return VAL_T();
77  }
78  return it->second;
79 }
80 
89 template <class KEY_T, // unordered_map::key_type
90  class VAL_T, // unordered_map::mapped_type
91  class HASH_T = std::hash<KEY_T>, // unordered_map::hasher
92  class KEYeq_T = std::equal_to<KEY_T> // unordered_map::key_equal
93  >
94 VAL_T peek_map(
95  const std::unordered_map<KEY_T, VAL_T, HASH_T, KEYeq_T>& map_to_peek,
96  KEY_T idx,
97  bool& found)
98 {
99  using map_to_peek_t = std::unordered_map<KEY_T, VAL_T, HASH_T, KEYeq_T>;
100  typename map_to_peek_t::const_iterator it = map_to_peek.find(idx);
101  if (it == map_to_peek.cend()) {
102  found = false;
103  return VAL_T();
104  }
105  found = true;
106  return it->second;
107 }
108 
113 template <class KEY_T, // unordered_map::key_type
114  class VAL_T, // unordered_map::mapped_type
115  class HASH_T = std::hash<KEY_T>, // unordered_map::hasher
116  class KEYeq_T = std::equal_to<KEY_T> // unordered_map::key_equal
117  >
118 VAL_T peek_map(
119  const std::unordered_map<KEY_T, VAL_T, HASH_T, KEYeq_T>& map_to_peek,
120  KEY_T idx)
121 {
122  using map_to_peek_t = std::unordered_map<KEY_T, VAL_T, HASH_T, KEYeq_T>;
123  typename map_to_peek_t::const_iterator it = map_to_peek.find(idx);
124  if (it == map_to_peek.cend()) {
125  return VAL_T();
126  }
127  return it->second;
128 }
129 
130 } // namespace lbann
131 
132 #endif // LBANN_PEEK_MAP_HPP_INCLUDED
VAL_T peek_map(const std::map< KEY_T, VAL_T, CMP_T > &map_to_peek, KEY_T idx, bool &found)
Definition: peek_map.hpp:49