1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
6 libDAI 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 2 of the License, or
9 (at your option) any later version.
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef __defined_libdai_index_h
23 #define __defined_libdai_index_h
27 #include <dai/varset.h>
35 * Index i ({s_j_1,s_j_2,...,s_j_m}, {s_1,...,s_N}); // j_k in {1,...,N}
36 * for( ; i>=0; ++i ) {
37 * // loops over all states of (s_1,...,s_N)
38 * // i is linear index of corresponding state of (s_j_1, ..., s_j_m)
47 std::vector
<int> _count
,_max
,_sum
;
49 Index () { _index
=-1; };
50 Index (const VarSet
& P
, const VarSet
& ns
)
53 VarSet::const_iterator j
=ns
.begin();
54 for(VarSet::const_iterator i
=P
.begin();i
!=P
.end();++i
)
56 for(;j
!=ns
.end()&&j
->label()<=i
->label();++j
)
59 _max
.push_back(j
->states());
60 _sum
.push_back((i
->label()==j
->label())?sum
:0);
67 _max
.push_back(j
->states());
72 Index (const Index
& ind
) : _index(ind
._index
), _count(ind
._count
), _max(ind
._max
), _sum(ind
._sum
) {};
73 Index
& operator=(const Index
& ind
) {
84 for(unsigned i
=0;i
!=_count
.size();++i
) _count
[i
]=0;
88 operator long () const { return(_index
); };
94 for(i
=0;(i
<_count
.size())
95 &&(_index
+=_sum
[i
],++_count
[i
]==_max
[i
]);++i
)
97 _index
-=_sum
[i
]*_max
[i
];
100 if(i
==_count
.size()) _index
=-1;
109 std::vector
<size_t> _dims
; // dimensions
110 std::vector
<size_t> _pdims
; // products of dimensions
113 multind(const std::vector
<size_t> di
) {
116 for( std::vector
<size_t>::const_iterator i
=di
.begin(); i
!=di
.end(); i
++ ) {
117 _pdims
.push_back(prod
);
120 _pdims
.push_back(prod
);
122 multind(const VarSet
& ns
) {
123 _dims
.reserve( ns
.size() );
124 _pdims
.reserve( ns
.size() + 1 );
126 for( VarSet::const_iterator n
= ns
.begin(); n
!= ns
.end(); n
++ ) {
127 _pdims
.push_back( prod
);
129 _dims
.push_back( n
->states() );
131 _pdims
.push_back( prod
);
133 std::vector
<size_t> vi(size_t li
) const { // linear index to vector index
134 std::vector
<size_t> v(_dims
.size(),0);
135 assert(li
< _pdims
.back());
136 for( long j
= v
.size()-1; j
>= 0; j
-- ) {
137 size_t q
= li
/ _pdims
[j
];
139 li
= li
- q
* _pdims
[j
];
143 size_t li(const std::vector
<size_t> vi
) const { // linear index
145 assert(vi
.size() == _dims
.size());
146 for( size_t j
= 0; j
< vi
.size(); j
++ )
147 s
+= vi
[j
] * _pdims
[j
];
150 size_t max() const { return( _pdims
.back() ); };
152 // FIXME add an iterator, which increases a vector index just using addition
156 } // end of namespace dai