aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/btree/minmaxaggregated.h
blob: 802f9822d715c5bdb8c1f4d5f5eccc1338bbd607 (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <limits>
#include <cstdint>

namespace vespalib::btree {

class MinMaxAggregated
{
    int32_t _min;
    int32_t _max;

public:
    MinMaxAggregated()
        : _min(std::numeric_limits<int32_t>::max()),
          _max(std::numeric_limits<int32_t>::min())
    { }

    MinMaxAggregated(int32_t min, int32_t max)
        : _min(min),
          _max(max)
    { }

    int32_t getMin() const { return _min; }
    int32_t getMax() const { return _max; }

    bool operator==(const MinMaxAggregated &rhs) const {
        return ((_min == rhs._min) && (_max == rhs._max));
    }

    bool operator!=(const MinMaxAggregated &rhs) const {
        return ((_min != rhs._min) || (_max != rhs._max));
    }

    void
    add(int32_t val)
    {
        if (_min > val)
            _min = val;
        if (_max < val)
            _max = val;
    }

    void
    add(const MinMaxAggregated &ca)
    {
        if (_min > ca._min)
            _min = ca._min;
        if (_max < ca._max)
            _max = ca._max;
    }

    void
    add(const MinMaxAggregated &oldca,
        const MinMaxAggregated &ca)
    {
        (void) oldca;
        add(ca);
    }

    /* Returns true if recalculation is needed */
    bool
    remove(int32_t val)
    {
        return (_min == val || _max == val);
    }

    /* Returns true if recalculation is needed */
    bool
    remove(const MinMaxAggregated &oldca,
           const MinMaxAggregated &ca)
    {
        return (_min == oldca._min && _min != ca._min) ||
            (_max == oldca._max && _max != ca._max);
    }

    /* Returns true if recalculation is needed */
    bool
    update(int32_t oldVal, int32_t val)
    {
        if ((_min == oldVal && _min < val) ||
            (_max == oldVal && _max > val)) {
            return true;
        }
        add(val);
        return false;
    }

    /* Returns true if recalculation is needed */
    bool
    update(const MinMaxAggregated &oldca,
           const MinMaxAggregated &ca)
    {
        if ((_min == oldca._min && _min < ca._min) ||
            (_max == oldca._max && _max > ca._max)) {
            return true;
        }
        add(ca);
        return false;
    }
};

}