summaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorTim King <taking@cs.nyu.edu>2018-08-08 16:50:16 -0700
committerGitHub <noreply@github.com>2018-08-08 16:50:16 -0700
commit987df3df987768e2ce0c36d17469929f8e92fdec (patch)
tree6d85a85966084879e55b8ab04c330662a4cfe7f0 /src/base
parentece17ee2c38fa5769ae3ab7fa3607c0e88c0021f (diff)
Proposal for adding map utility functions to CVC4. (#2232)
* Proposal for adding map utility functions to CVC4.
Diffstat (limited to 'src/base')
-rw-r--r--src/base/Makefile.am1
-rw-r--r--src/base/map_util.h97
2 files changed, 98 insertions, 0 deletions
diff --git a/src/base/Makefile.am b/src/base/Makefile.am
index 3619b226e..3706f57a3 100644
--- a/src/base/Makefile.am
+++ b/src/base/Makefile.am
@@ -26,6 +26,7 @@ libbase_la_SOURCES = \
exception.h \
listener.cpp \
listener.h \
+ map_util.h \
modal_exception.h \
output.cpp \
output.h
diff --git a/src/base/map_util.h b/src/base/map_util.h
new file mode 100644
index 000000000..2e17c9290
--- /dev/null
+++ b/src/base/map_util.h
@@ -0,0 +1,97 @@
+/********************* */
+/*! \file map_util.h
+ ** \verbatim
+ ** Top contributors (to current version):
+ ** Tim King
+ ** This file is part of the CVC4 project.
+ ** Copyright (c) 2009-2018 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.\endverbatim
+ **
+ ** \brief Utility functions for dealing with maps in a mostly uniform fashion.
+ **
+ ** Utility functions for dealing with maps and related classed in a mostly
+ ** uniform fashion. These are stylistically encouraged (but not required) in
+ ** new code. Supports:
+ ** - std::map
+ ** - std::unordered_map
+ ** - CVC4::context::CDHashmap
+ ** - CVC4::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 __CVC4__BASE__MAP_UTIL_H
+#define __CVC4__BASE__MAP_UTIL_H
+
+#include "base/cvc4_check.h"
+
+namespace CVC4 {
+
+// 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);
+ CVC4_CHECK(it != map.end()) << "The map does not contain the key.";
+ return (*it).second;
+}
+
+} // namespace CVC4
+
+#endif /* __CVC4__BASE__MAP_UTIL_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback