-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmax_value.h
More file actions
48 lines (40 loc) · 1.31 KB
/
max_value.h
File metadata and controls
48 lines (40 loc) · 1.31 KB
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
//
// max_value.h
// SubmatrixQueries
//
// Created by Raphael Bost on 16/01/13.
// Copyright (c) 2013 Raphael Bost. All rights reserved.
//
#ifndef __SubmatrixQueries__max_value__
#define __SubmatrixQueries__max_value__
#include <algorithm>
#include <cassert>
/* class MaxValue
*
* This class is a wrapper class for maximum values.
* The problem is that for a template implementation, you just cannot predefine the minimum value for a specific type:
* for example FLT_MAX is different of DBL_MAX or INT_MAX ...
* So, to easily implement maxima, we just create this wrapper that encaspulates a value and a flag that is set since a value has been put in the wrapper.
*/
template <typename T>
class MaxValue {
T _value;
bool _hasBeenSet;
public:
MaxValue() : _hasBeenSet(false) {};
MaxValue(T v) : _value(v), _hasBeenSet(true) {};
// Replaces the wrapper's value by the input value if it is bigger
T updateMax(T v)
{
if (_hasBeenSet) {
_value = std::max(_value,v);
}else{
_value = v;
_hasBeenSet = true;
}
return _value;
}
inline bool hasBeenSet() const { return _hasBeenSet;}
inline T value() const { assert(_hasBeenSet); return _value; }
};
#endif /* defined(__SubmatrixQueries__max_value__) */