1 #include <config.h>
2 #include <sarray/SArray.h>
3 #include <util/nainf.h>
4 
5 #include <stdexcept>
6 #include <algorithm>
7 
8 using std::vector;
9 using std::logic_error;
10 using std::length_error;
11 using std::copy;
12 using std::string;
13 
14 namespace jags {
15 
SArray(vector<unsigned int> const & dim)16 SArray::SArray(vector<unsigned int> const &dim)
17     : _range(dim), _value(_range.length(), JAGS_NA), _discrete(false),
18       _s_dimnames(dim.size())
19 {
20 }
21 
SArray(SArray const & orig)22 SArray::SArray(SArray const &orig)
23     : _range(orig._range), _value(orig._value), _discrete(orig._discrete),
24       _s_dimnames(orig._s_dimnames), _dimnames(orig._dimnames)
25 {
26 }
27 
range() const28 SimpleRange const &SArray::range() const
29 {
30     return _range;
31 }
32 
setValue(vector<double> const & x)33 void SArray::setValue(vector<double> const &x)
34 {
35     if (x.size() != _value.size()) {
36 	throw length_error("Length mismatch error in SArray::setValue");
37     }
38     else {
39         copy(x.begin(), x.end(), _value.begin());
40 	_discrete = false;
41     }
42 }
43 
setValue(vector<int> const & x)44 void SArray::setValue(vector<int> const &x)
45 {
46     if (x.size() != _value.size()) {
47 	throw length_error("Length mismatch error in SArray::setValue");
48     }
49     else {
50         copy(x.begin(), x.end(), _value.begin());
51 	_discrete = true;
52     }
53 }
54 
setValue(double value,unsigned int i)55 void SArray::setValue(double value, unsigned int i)
56 {
57     if (i >= _range.length()) {
58 	throw logic_error("Attempt to set value of invalid element of SArray");
59     }
60     else {
61 	_value[i] = value;
62     }
63 }
64 
value() const65 vector<double> const &SArray::value() const
66 {
67     return _value;
68 }
69 
isDiscreteValued() const70 bool SArray::isDiscreteValued() const
71 {
72     return _discrete;
73 }
74 
dimNames() const75 vector<string> const &SArray::dimNames() const
76 {
77     return _dimnames;
78 }
79 
setDimNames(vector<string> const & names)80 void SArray::setDimNames(vector<string> const &names)
81 {
82     if (names.empty() || names.size() == _range.ndim(false)) {
83 	_dimnames = names;
84     }
85     else {
86 	throw length_error("Invalid length in SArray::setDimNames");
87     }
88 }
89 
getSDimNames(unsigned int i) const90 vector<string> const &SArray::getSDimNames(unsigned int i) const
91 {
92     if (i >= _range.ndim(false))
93 	throw logic_error("Dimension out of range in setSDimNames");
94 
95     return _s_dimnames[i];
96 }
97 
setSDimNames(vector<string> const & names,unsigned int i)98 void SArray::setSDimNames(vector<string> const &names, unsigned int i)
99 {
100     if (i >= _range.ndim(false))
101 	throw logic_error("Dimension out of range in setSDimNames");
102 
103     if (names.empty() || names.size() == _range.dim(false)[i]) {
104 	_s_dimnames[i] = names;
105     }
106     else {
107 	throw length_error("Invalid length in SArray::setSDimNames");
108     }
109 }
110 
111 } //namespace jags
112