1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andres Noetzli, Morgan Deters, Aina Niemetz |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* Some standard STL-related utility functions for cvc5. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "cvc5_private.h" |
17 |
|
|
18 |
|
#ifndef CVC5__UTILITY_H |
19 |
|
#define CVC5__UTILITY_H |
20 |
|
|
21 |
|
#include <algorithm> |
22 |
|
#include <fstream> |
23 |
|
#include <memory> |
24 |
|
#include <optional> |
25 |
|
#include <string> |
26 |
|
|
27 |
|
namespace cvc5 { |
28 |
|
|
29 |
|
/** |
30 |
|
* Using std::find_if(), finds the first iterator in [first,last) |
31 |
|
* range that satisfies predicate. If none, return last; otherwise, |
32 |
|
* search for a second one. If there IS a second one, return last, |
33 |
|
* otherwise return the first (and unique) iterator satisfying pred(). |
34 |
|
*/ |
35 |
|
template <class InputIterator, class Predicate> |
36 |
464150 |
inline InputIterator find_if_unique(InputIterator first, InputIterator last, Predicate pred) { |
37 |
464150 |
InputIterator match = std::find_if(first, last, pred); |
38 |
464150 |
if(match == last) { |
39 |
1 |
return last; |
40 |
|
} |
41 |
|
|
42 |
464149 |
InputIterator match2 = match; |
43 |
464149 |
match2 = std::find_if(++match2, last, pred); |
44 |
464149 |
return (match2 == last) ? match : last; |
45 |
|
} |
46 |
|
|
47 |
|
template <typename T> |
48 |
9 |
void container_to_stream(std::ostream& out, |
49 |
|
const T& container, |
50 |
|
const char* prefix = "[", |
51 |
|
const char* postfix = "]", |
52 |
|
const char* sep = ", ") |
53 |
|
{ |
54 |
9 |
out << prefix; |
55 |
9 |
bool is_first = true; |
56 |
15 |
for (const auto& item : container) |
57 |
|
{ |
58 |
6 |
out << (!is_first ? sep : "") << item; |
59 |
6 |
is_first = false; |
60 |
|
} |
61 |
9 |
out << postfix; |
62 |
9 |
} |
63 |
|
|
64 |
|
/** |
65 |
|
* Generates a string representation of std::optional and inserts it into a |
66 |
|
* stream. |
67 |
|
* |
68 |
|
* @param out The stream |
69 |
|
* @param m The value |
70 |
|
* @return The stream |
71 |
|
*/ |
72 |
|
template <class T> |
73 |
|
std::ostream& operator<<(std::ostream& out, const std::optional<T>& m) |
74 |
|
{ |
75 |
|
out << "{"; |
76 |
|
if (m) |
77 |
|
{ |
78 |
|
out << "Just "; |
79 |
|
out << *m; |
80 |
|
} |
81 |
|
else |
82 |
|
{ |
83 |
|
out << "Nothing"; |
84 |
|
} |
85 |
|
out << "}"; |
86 |
|
return out; |
87 |
|
} |
88 |
|
|
89 |
|
/** |
90 |
|
* Opens a new temporary file with a given filename pattern and returns an |
91 |
|
* fstream to it. The directory that the file is created in is either TMPDIR or |
92 |
|
* /tmp/ if TMPDIR is not set. |
93 |
|
* |
94 |
|
* @param pattern The filename pattern. This string is modified to contain the |
95 |
|
* name of the temporary file. |
96 |
|
* |
97 |
|
* @return A unique pointer to the filestream for the temporary file. |
98 |
|
* |
99 |
|
* Note: We use `std::unique_ptr<std::fstream>` instead of `std::fstream` |
100 |
|
* because GCC < 5 does not support the move constructor of `std::fstream`. See |
101 |
|
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316 for details. |
102 |
|
*/ |
103 |
|
std::unique_ptr<std::fstream> openTmpFile(std::string* pattern); |
104 |
|
|
105 |
|
} // namespace cvc5 |
106 |
|
|
107 |
|
#endif /* CVC5__UTILITY_H */ |