Updated copyrights
[libdai.git] / include / dai / diffs.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #ifndef __defined_libdai_diffs_h
24 #define __defined_libdai_diffs_h
25
26
27 #include <vector>
28
29
30 namespace dai {
31
32
33 class Diffs : public std::vector<double> {
34 private:
35 size_t _maxsize;
36 double _def;
37 std::vector<double>::iterator _pos;
38 std::vector<double>::iterator _maxpos;
39 public:
40 Diffs(long maxsize, double def) : std::vector<double>(), _maxsize(maxsize), _def(def) {
41 this->reserve(_maxsize);
42 _pos = begin();
43 _maxpos = begin();
44 };
45 double maxDiff() {
46 if( size() < _maxsize )
47 return _def;
48 else
49 return( *_maxpos );
50 }
51 void push(double x) {
52 if( size() < _maxsize ) {
53 push_back(x);
54 _pos = end();
55 if( size() > 1 ) {
56 if( *_maxpos < back() ) {
57 _maxpos = end();
58 _maxpos--;
59 }
60 } else {
61 _maxpos = begin();
62 }
63 }
64 else {
65 if( _pos == end() )
66 _pos = begin();
67 if( _maxpos == _pos ) {
68 *_pos++ = x;
69 _maxpos = max_element(begin(),end());
70 } else {
71 if( x > *_maxpos )
72 _maxpos = _pos;
73 *_pos++ = x;
74 }
75 }
76 }
77 size_t maxSize() { return _maxsize; }
78 };
79
80
81 } // end of namespace dai
82
83
84 #endif