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