-
Notifications
You must be signed in to change notification settings - Fork 0
/
Properties.hpp
103 lines (87 loc) · 2.38 KB
/
Properties.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) <vincent.richefeu@3sr-grenoble.fr>
//
// This file is part of TOOFUS (TOols OFten USued)
//
// It can not be copied and/or distributed without the express
// permission of the authors.
// It is coded for academic purposes.
//
// Note
// Without a license, the code is copyrighted by default.
// People can read the code, but they have no legal right to use it.
// To use the code, you must contact the author directly and ask permission.
#ifndef PROPERTIES_HPP
#define PROPERTIES_HPP
// =========================================================
// Store and manage parameters of groups and between groups
// =========================================================
#include <string>
#include <iostream>
#include <vector>
#include <map>
/// @brief Definition and storage of global properties (e.g. density) for each group
class Properties
{
public:
size_t ngroup;
std::vector<std::vector<double> > prop;
std::map <std::string, size_t> prop_id;
public:
Properties() {
set_ngroup(1);
}
/// Clear all properties
void clear() {
for (size_t t = 0 ; t < prop.size() ; ++t) {
prop[t].clear();
}
prop.clear();
prop_id.clear();
set_ngroup(1);
}
/// set the number of groups
void set_ngroup(size_t n) {
ngroup = n;
for (size_t t = 0 ; t < prop.size() ; ++t) {
prop[t].resize(ngroup, 0.0);
}
}
bool exists(const std::string & name) const {
std::map<std::string, size_t >::const_iterator ip = prop_id.find(name);
if (ip == prop_id.end()) {
return false;
}
return true;
}
size_t add(const std::string & name) {
std::map<std::string, size_t >::const_iterator ip = prop_id.find(name);
if (ip != prop_id.end()) return (size_t) ip->second;
prop_id[name] = prop.size();
std::vector<double> g;
g.resize(ngroup, 0.0);
prop.push_back(g);
return (prop.size() - 1);
}
size_t get_id (const std::string & name) const {
std::map<std::string, size_t >::const_iterator ip = prop_id.find(name);
if (ip != prop_id.end()) {
return (size_t) ip->second;
}
else {
return 0;
}
}
double get(size_t id, size_t g) const {
return prop[id][g];
}
void set(size_t id, size_t g, double val) {
if (g >= ngroup) set_ngroup(g + 1);
if (id < prop.size()) prop[id][g] = val;
}
size_t set(const std::string & name, size_t g, double val) {
size_t id = add(name);
set (id, g, val);
return id;
}
};
#endif /* end of include guard: PROPERTIES_HPP */