blob: 4c46052a174c00670bd332e455cbf8b629c040e6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
/******************************************************************************
* Top contributors (to current version):
* Tim King, Mathias Preiner
*
* This file is part of the cvc5 project.
*
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
* in the top-level source directory and their institutional affiliations.
* All rights reserved. See the file COPYING in the top-level source
* directory for licensing information.
* ****************************************************************************
* Utility functions for dealing with maps and related classes in a mostly
* uniform fashion.
*
* These are stylistically encouraged (but not required) in new code.
* Supports:
* - std::map
* - std::unordered_map
* - cvc5::context::CDHashmap
* - cvc5::context::CDInsertHashmap
* The ContainsKey function is also compatible with std::[unordered_]set.
*
* Currently implemented classes of functions:
* - ContainsKey
* Returns true if a map contains a key. (Also Supports std::set and
* std::unordered_set.)
* - FindOr*
* Finds an data element mapped to by the map. Variants include FindOrNull
* and FindOrDie.
*
* Potential future classes of functions:
* - InsertOrUpdate
* - InsertIfNotPresent
*/
#include "cvc4_private.h"
#ifndef CVC5__BASE__MAP_UTIL_H
#define CVC5__BASE__MAP_UTIL_H
#include "base/check.h"
namespace cvc5 {
// Returns true if the `map` contains the `key`.
//
// Supports sets as well.
template <class M, class Key>
bool ContainsKey(const M& map, const Key& key)
{
return map.find(key) != map.end();
}
template <typename M>
using MapKeyTypeT = typename M::value_type::first_type;
template <typename M>
using MapMappedTypeT = typename M::value_type::second_type;
// Returns a pointer to the const value mapped by `key` if it exists, or nullptr
// otherwise.
template <class M>
const MapMappedTypeT<M>* FindOrNull(const M& map, const MapKeyTypeT<M>& key)
{
auto it = map.find(key);
if (it == map.end())
{
return nullptr;
}
return &((*it).second);
}
// Returns a pointer to the non-const value mapped by `key` if it exists, or
// nullptr otherwise.
template <class M>
MapMappedTypeT<M>* FindOrNull(M& map, const MapKeyTypeT<M>& key)
{
auto it = map.find(key);
if (it == map.end())
{
return nullptr;
}
return &((*it).second);
}
// Returns a const reference to the value mapped by `key` if it exists. Dies
// if the element is not there.
template <class M>
const MapMappedTypeT<M>& FindOrDie(const M& map, const MapKeyTypeT<M>& key)
{
auto it = map.find(key);
AlwaysAssert(it != map.end()) << "The map does not contain the key.";
return (*it).second;
}
} // namespace cvc5
#endif /* CVC5__BASE__MAP_UTIL_H */
|