Merge branch '1.4.x'
[cvc5.git] / src / util / utility.h
1 /********************* */
2 /*! \file utility.h
3 ** \verbatim
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
11 **
12 ** \brief Some standard STL-related utility functions for CVC4
13 **
14 ** Some standard STL-related utility functions for CVC4.
15 **/
16
17 #include "cvc4_private.h"
18
19 #ifndef __CVC4__UTILITY_H
20 #define __CVC4__UTILITY_H
21
22 #include <utility>
23 #include <functional>
24
25 namespace CVC4 {
26
27
28 /**
29 * Like std::equal_to<>, but tests equality between the first element
30 * of a pair and an element.
31 */
32 template <class T, class U>
33 struct first_equal_to : std::binary_function<std::pair<T, U>, T, bool> {
34 bool operator()(const std::pair<T, U>& pr, const T& x) const {
35 return pr.first == x;
36 }
37 };/* struct first_equal_to<T> */
38
39
40 /**
41 * Like std::equal_to<>, but tests equality between the second element
42 * of a pair and an element.
43 */
44 template <class T, class U>
45 struct second_equal_to : std::binary_function<std::pair<T, U>, U, bool> {
46 bool operator()(const std::pair<T, U>& pr, const U& x) const {
47 return pr.second == x;
48 }
49 };/* struct first_equal_to<T> */
50
51
52 /**
53 * Using std::find_if(), finds the first iterator in [first,last)
54 * range that satisfies predicate. If none, return last; otherwise,
55 * search for a second one. If there IS a second one, return last,
56 * otherwise return the first (and unique) iterator satisfying pred().
57 */
58 template <class InputIterator, class Predicate>
59 inline InputIterator find_if_unique(InputIterator first, InputIterator last, Predicate pred) {
60 InputIterator match = std::find_if(first, last, pred);
61 if(match == last) {
62 return last;
63 }
64
65 InputIterator match2 = match;
66 match2 = std::find_if(++match2, last, pred);
67 return (match2 == last) ? match : last;
68 }
69
70 }/* CVC4 namespace */
71
72 #endif /* __CVC4__UTILITY_H */