Branch data Line data Source code
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-2025 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::internal { 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 : 203959 : inline InputIterator find_if_unique(InputIterator first, InputIterator last, Predicate pred) { 37 : 203959 : InputIterator match = std::find_if(first, last, pred); 38 [ + + ]: 203959 : if(match == last) { 39 : 14 : return last; 40 : : } 41 : : 42 : 203945 : InputIterator match2 = match; 43 : 203945 : match2 = std::find_if(++match2, last, pred); 44 [ + + ]: 203945 : return (match2 == last) ? match : last; 45 : : } 46 : : 47 : : template <typename T> 48 : 736 : 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 : 736 : out << prefix; 55 : 736 : bool is_first = true; 56 [ + + ]: 1709 : for (const auto& item : container) 57 : : { 58 [ + + ]: 973 : out << (!is_first ? sep : "") << item; 59 : 973 : is_first = false; 60 : : } 61 : 736 : out << postfix; 62 : 736 : } 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 : 0 : std::ostream& operator<<(std::ostream& out, const std::optional<T>& m) 74 : : { 75 : 0 : out << "{"; 76 [ - - ]: 0 : if (m) 77 : : { 78 : 0 : out << "Just "; 79 : 0 : out << *m; 80 : : } 81 : : else 82 : : { 83 : 0 : out << "Nothing"; 84 : : } 85 : 0 : out << "}"; 86 : 0 : 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::internal 106 : : 107 : : #endif /* CVC5__UTILITY_H */