vector_util.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 Equinor ASA, Norway.
3
4 The file 'vector_util.h' is part of ERT - Ensemble based Reservoir Tool.
5
6 ERT is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 ERT is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
16 for more details.
17*/
18
19#include <stdio.h>
20
21#include <vector>
22#include <algorithm>
23
24template <class T>
25int vector_util_index(const std::vector<T>& vec, T value) {
26
27 int index;
28 auto iter = find(vec.begin(), vec.end(), value);
29 if (iter == vec.end())
30 index = -1;
31 else
32 index = iter - vec.begin();
33 return index;
34}
35
36
37template <class T>
38void vector_util_fprintf(const std::vector<T>& vec , FILE * stream , const char * name , const char * fmt) {
39 size_t i;
40 if (name != NULL)
41 fprintf(stream , "%s = [" , name);
42 else
43 fprintf(stream , "[");
44
45 for (i = 0; i < vec.size(); i++) {
46 fprintf(stream , fmt , vec[i]);
47 if (i < (vec.size() - 1))
48 fprintf(stream , ", ");
49 }
50
51 fprintf(stream , "]\n");
52}
const char *const name
Definition: cJSON.h:258
int index
Definition: cJSON.h:168
int cJSON_bool fmt
Definition: cJSON.h:158
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955
int vector_util_index(const std::vector< T > &vec, T value)
Definition: vector_util.hpp:25
void vector_util_fprintf(const std::vector< T > &vec, FILE *stream, const char *name, const char *fmt)
Definition: vector_util.hpp:38