GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/util/utility.h Lines: 15 15 100.0 %
Date: 2021-05-22 Branches: 18 52 34.6 %

Line Exec Source
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 <string>
25
26
namespace cvc5 {
27
28
/**
29
 * Using std::find_if(), finds the first iterator in [first,last)
30
 * range that satisfies predicate.  If none, return last; otherwise,
31
 * search for a second one.  If there IS a second one, return last,
32
 * otherwise return the first (and unique) iterator satisfying pred().
33
 */
34
template <class InputIterator, class Predicate>
35
487435
inline InputIterator find_if_unique(InputIterator first, InputIterator last, Predicate pred) {
36
487435
  InputIterator match = std::find_if(first, last, pred);
37
487435
  if(match == last) {
38
2
    return last;
39
  }
40
41
487433
  InputIterator match2 = match;
42
487433
  match2 = std::find_if(++match2, last, pred);
43
487433
  return (match2 == last) ? match : last;
44
}
45
46
template <typename T>
47
15
void container_to_stream(std::ostream& out,
48
                         const T& container,
49
                         const char* prefix = "[",
50
                         const char* postfix = "]",
51
                         const char* sep = ", ")
52
{
53
15
  out << prefix;
54
15
  bool is_first = true;
55
29
  for (const auto& item : container)
56
  {
57
14
    out << (!is_first ? sep : "") << item;
58
14
    is_first = false;
59
  }
60
15
  out << postfix;
61
15
}
62
63
/**
64
 * Opens a new temporary file with a given filename pattern and returns an
65
 * fstream to it. The directory that the file is created in is either TMPDIR or
66
 * /tmp/ if TMPDIR is not set.
67
 *
68
 * @param pattern The filename pattern. This string is modified to contain the
69
 * name of the temporary file.
70
 *
71
 * @return A unique pointer to the filestream for the temporary file.
72
 *
73
 * Note: We use `std::unique_ptr<std::fstream>` instead of `std::fstream`
74
 * because GCC < 5 does not support the move constructor of `std::fstream`. See
75
 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316 for details.
76
 */
77
std::unique_ptr<std::fstream> openTmpFile(std::string* pattern);
78
79
}  // namespace cvc5
80
81
#endif /* CVC5__UTILITY_H */