-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariablesHolder.cpp
107 lines (87 loc) · 2.38 KB
/
VariablesHolder.cpp
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
104
105
106
/*
* VariablesHolder.cpp
*
* Created on: 16/10/2015
* Author: ssat335
*/
#include "VariablesHolder.h"
#include "Utils.h"
#include <stdio.h>
#include <algorithm>
using namespace std;
VariablesHolder::VariablesHolder()
{
}
VariablesHolder::VariablesHolder(const VariablesHolder& other)
{
m_Vars.assign(other.m_Vars.begin(),other.m_Vars.end());
}
VariablesHolder::~VariablesHolder()
{
}
VariablesHolder& VariablesHolder::operator=(const VariablesHolder& other)
{
if(&other!=this)
m_Vars.assign(other.m_Vars.begin(),other.m_Vars.end());
return *this;
}
double VariablesHolder::operator()(const std::wstring& name)
{
// get iterator to the pair in m_Vars for which the first member equals name (end if no such pair)
ALLELE::iterator it=find_if(m_Vars.begin(),m_Vars.end(),
bind1st(pair_equal_to<std::wstring,double>(),name));
return (it==m_Vars.end()?double(0.0):it->second);
}
double VariablesHolder::operator()(const std::wstring& name,double val)
{
// find if matching allele already exists in VarHold
ALLELE::iterator it=find_if(m_Vars.begin(),m_Vars.end(),
bind1st(pair_equal_to<std::wstring,double>(),name));
if(it!=m_Vars.end())
it->second=val; // update the existing allele
else
m_Vars.push_back(std::make_pair<std::wstring,double>(std::wstring(name),double(val))); // add the allele
return val;
}
std::wstring VariablesHolder::name(int index)
{
return (index>=0 && index<m_Vars.size())?m_Vars[index].first:std::wstring();
}
bool VariablesHolder::exists(const std::wstring& name)
{
ALLELE::iterator it=find_if(m_Vars.begin(),m_Vars.end(),
bind1st(pair_equal_to<std::wstring,double>(),name));
return (it!=m_Vars.end());
}
size_t VariablesHolder::size()
{
// is the size of m_Vars vector i.e. number of alleles in stored in the m_Vars vector
return m_Vars.size();
}
void VariablesHolder::collate(std::vector<double>& v)
{
for(ALLELE::iterator it=m_Vars.begin();it!=m_Vars.end();++it)
{
v.push_back(it->second);
}
}
void VariablesHolder::print(FILE *pfout)
{
for(ALLELE::iterator it=m_Vars.begin();it!=m_Vars.end();++it)
{
fprintf(pfout,"%s=%.5e ",convert(it->first).c_str(),it->second);
}
fprintf(pfout,"\n");
}
bool VariablesHolder::fillup(std::vector<double>& v)
{
// check if the sizes are equal
if(v.size()!=m_Vars.size())
return false;
// assign allele values
for(int i=0;i<v.size();i++)
{
m_Vars[i].second=v[i];
}
return true;
}