Small doc changes
[libdai.git] / include / dai / diffs.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
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.
10
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.
15
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
19 */
20
21
22 #ifndef __defined_libdai_diffs_h
23 #define __defined_libdai_diffs_h
24
25
26 #include <vector>
27
28
29 namespace dai {
30
31
32 class Diffs : public std::vector<double> {
33 private:
34 size_t _maxsize;
35 double _def;
36 std::vector<double>::iterator _pos;
37 std::vector<double>::iterator _maxpos;
38 public:
39 Diffs(long maxsize, double def) : std::vector<double>(), _maxsize(maxsize), _def(def) {
40 this->reserve(_maxsize);
41 _pos = begin();
42 _maxpos = begin();
43 };
44 double maxDiff() {
45 if( size() < _maxsize )
46 return _def;
47 else
48 return( *_maxpos );
49 }
50 void push(double x) {
51 if( size() < _maxsize ) {
52 push_back(x);
53 _pos = end();
54 if( size() > 1 ) {
55 if( *_maxpos < back() ) {
56 _maxpos = end();
57 _maxpos--;
58 }
59 } else {
60 _maxpos = begin();
61 }
62 }
63 else {
64 if( _pos == end() )
65 _pos = begin();
66 if( _maxpos == _pos ) {
67 *_pos++ = x;
68 _maxpos = max_element(begin(),end());
69 } else {
70 if( x > *_maxpos )
71 _maxpos = _pos;
72 *_pos++ = x;
73 }
74 }
75 }
76 size_t maxSize() { return _maxsize; }
77 };
78
79
80 } // end of namespace dai
81
82
83 #endif