series.h 10.1 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2015 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5 6 7 8 9 10 11 12 13 14
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

gejun's avatar
gejun committed
15 16 17 18 19 20
// Author: Ge,Jun (gejun@baidu.com)
// Date: Tue Jul 28 18:15:57 CST 2015

#ifndef  BVAR_DETAIL_SERIES_H
#define  BVAR_DETAIL_SERIES_H

21
#include <math.h>                       // round
gejun's avatar
gejun committed
22
#include <ostream>
23 24
#include "butil/scoped_lock.h"           // BAIDU_SCOPED_LOCK
#include "butil/type_traits.h"
gejun's avatar
gejun committed
25 26
#include "bvar/vector.h"
#include "bvar/detail/call_op_returning_void.h"
27
#include "butil/string_splitter.h"
gejun's avatar
gejun committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

namespace bvar {
namespace detail {

template <typename T, typename Op, typename Enabler = void>
struct DivideOnAddition {
    static void inplace_divide(T& /*obj*/, const Op&, int /*number*/) {
        // do nothing
    }
};

template <typename T, typename Op>
struct ProbablyAddtition {
    ProbablyAddtition(const Op& op) {
        T res(32);
        call_op_returning_void(op, res, T(64));
44
        _ok = (res == T(96));  // works for integral/floating point.
gejun's avatar
gejun committed
45 46 47 48 49 50 51
    }
    operator bool() const { return _ok; }
private:
    bool _ok;
};

template <typename T, typename Op>
52 53
struct DivideOnAddition<T, Op, typename butil::enable_if<
                                   butil::is_integral<T>::value>::type> {
gejun's avatar
gejun committed
54 55 56 57 58 59 60 61 62
    static void inplace_divide(T& obj, const Op& op, int number) {
        static ProbablyAddtition<T, Op> probably_add(op);
        if (probably_add) {
            obj = (T)round(obj / (double)number);
        }
    }
};

template <typename T, typename Op>
63 64
struct DivideOnAddition<T, Op, typename butil::enable_if<
                                   butil::is_floating_point<T>::value>::type> {
gejun's avatar
gejun committed
65 66 67 68 69 70 71 72 73
    static void inplace_divide(T& obj, const Op& op, int number) {
        static ProbablyAddtition<T, Op> probably_add(op);
        if (probably_add) {
            obj /= number;
        }
    }
};

template <typename T, size_t N, typename Op>
74 75
struct DivideOnAddition<Vector<T,N>, Op, typename butil::enable_if<
                                             butil::is_integral<T>::value>::type> {
gejun's avatar
gejun committed
76 77 78 79 80 81 82 83 84 85 86
    static void inplace_divide(Vector<T, N>& obj, const Op& op, int number) {
        static ProbablyAddtition<Vector<T, N>, Op> probably_add(op);
        if (probably_add) {
            for (size_t i = 0; i < N; ++i) {
                obj[i] = (T)round(obj[i] / (double)number);
            }
        }
    }
};

template <typename T, size_t N, typename Op>
87 88
struct DivideOnAddition<Vector<T,N>, Op, typename butil::enable_if<
                                   butil::is_floating_point<T>::value>::type> {
gejun's avatar
gejun committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    static void inplace_divide(Vector<T,N>& obj, const Op& op, int number) {
        static ProbablyAddtition<Vector<T,N>, Op> probably_add(op);
        if (probably_add) {
            obj /= number;
        }
    }
};

template <typename T, typename Op>
class SeriesBase {
public:
    explicit SeriesBase(const Op& op)
        : _op(op)
        , _nsecond(0)
        , _nminute(0)
        , _nhour(0)
        , _nday(0) {
        pthread_mutex_init(&_mutex, NULL);
    }
    ~SeriesBase() {
        pthread_mutex_destroy(&_mutex);
    }

    void append(const T& value) {
        BAIDU_SCOPED_LOCK(_mutex);
        return append_second(value, _op);
    }

private:
    void append_second(const T& value, const Op& op);
    void append_minute(const T& value, const Op& op);
    void append_hour(const T& value, const Op& op);
    void append_day(const T& value);

    struct Data {
    public:
        Data() {
            // is_pod does not work for gcc 3.4
127 128
            if (butil::is_integral<T>::value ||
                butil::is_floating_point<T>::value) {
gejun's avatar
gejun committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
                memset(_array, 0, sizeof(_array));
            }
        }
        
        T& second(int index) { return _array[index]; }
        const T& second(int index) const { return _array[index]; }

        T& minute(int index) { return _array[60 + index]; }
        const T& minute(int index) const { return _array[60 + index]; }

        T& hour(int index) { return _array[120 + index]; }
        const T& hour(int index) const { return _array[120 + index]; }

        T& day(int index) { return _array[144 + index]; }
        const T& day(int index) const { return _array[144 + index]; }
    private:
        T _array[60 + 60 + 24 + 30];
    };

protected:
    Op _op;
    mutable pthread_mutex_t _mutex;
    char _nsecond;
    char _nminute;
    char _nhour;
    char _nday;
    Data _data;
};

template <typename T, typename Op>
void SeriesBase<T, Op>::append_second(const T& value, const Op& op) {
    _data.second(_nsecond) = value;
    ++_nsecond;
    if (_nsecond >= 60) {
        _nsecond = 0;
        T tmp = _data.second(0);
        for (int i = 1; i < 60; ++i) {
            call_op_returning_void(op, tmp, _data.second(i));
        }
        DivideOnAddition<T, Op>::inplace_divide(tmp, op, 60);
        append_minute(tmp, op);
    }
}

template <typename T, typename Op>
void SeriesBase<T, Op>::append_minute(const T& value, const Op& op) {
    _data.minute(_nminute) = value;
    ++_nminute;
    if (_nminute >= 60) {
        _nminute = 0;
        T tmp = _data.minute(0);
        for (int i = 1; i < 60; ++i) {
            call_op_returning_void(op, tmp, _data.minute(i));
        }
        DivideOnAddition<T, Op>::inplace_divide(tmp, op, 60);
        append_hour(tmp, op);
    }
}

template <typename T, typename Op>
void SeriesBase<T, Op>::append_hour(const T& value, const Op& op) {
    _data.hour(_nhour) = value;
    ++_nhour;
    if (_nhour >= 24) {
        _nhour = 0;
        T tmp = _data.hour(0);
        for (int i = 1; i < 24; ++i) {
            call_op_returning_void(op, tmp, _data.hour(i));
        }
        DivideOnAddition<T, Op>::inplace_divide(tmp, op, 24);
        append_day(tmp);
    }
}

template <typename T, typename Op>
void SeriesBase<T, Op>::append_day(const T& value) {
    _data.day(_nday) = value;
    ++_nday;
    if (_nday >= 30) {
        _nday = 0;
    }
}

template <typename T, typename Op>
class Series : public SeriesBase<T, Op> {
    typedef SeriesBase<T, Op> Base;
public:
    explicit Series(const Op& op) : Base(op) {}
    void describe(std::ostream& os, const std::string* vector_names) const;
};

template <typename T, size_t N, typename Op>
class Series<Vector<T,N>, Op> : public SeriesBase<Vector<T,N>, Op> {
    typedef SeriesBase<Vector<T,N>, Op> Base;
public:
    explicit Series(const Op& op) : Base(op) {}
    void describe(std::ostream& os, const std::string* vector_names) const;
};

template <typename T, typename Op>
void Series<T, Op>::describe(std::ostream& os,
                             const std::string* vector_names) const {
    CHECK(vector_names == NULL);
    pthread_mutex_lock(&this->_mutex);
    const int second_begin = this->_nsecond;
    const int minute_begin = this->_nminute;
    const int hour_begin = this->_nhour;
    const int day_begin = this->_nday;
    // NOTE: we don't save _data which may be inconsistent sometimes, but
    // this output is generally for "peeking the trend" and does not need
    // to exactly accurate.
    pthread_mutex_unlock(&this->_mutex);
    int c = 0;
    os << "{\"label\":\"trend\",\"data\":[";
    for (int i = 0; i < 30; ++i, ++c) {
        if (c) {
            os << ',';
        }
        os << '[' << c << ',' << this->_data.day((i + day_begin) % 30) << ']';
    }
    for (int i = 0; i < 24; ++i, ++c) {
        if (c) {
            os << ',';
        }
        os << '[' << c << ',' << this->_data.hour((i + hour_begin) % 24) << ']';
    }
    for (int i = 0; i < 60; ++i, ++c) {
        if (c) {
            os << ',';
        }
        os << '[' << c << ',' << this->_data.minute((i + minute_begin) % 60) << ']';
    }
    for (int i = 0; i < 60; ++i, ++c) {
        if (c) {
            os << ',';
        }
        os << '[' << c << ',' << this->_data.second((i + second_begin) % 60) << ']';
    }
    os << "]}";
}

template <typename T, size_t N, typename Op>
void Series<Vector<T,N>, Op>::describe(std::ostream& os,
                                       const std::string* vector_names) const {
    pthread_mutex_lock(&this->_mutex);
    const int second_begin = this->_nsecond;
    const int minute_begin = this->_nminute;
    const int hour_begin = this->_nhour;
    const int day_begin = this->_nday;
    // NOTE: we don't save _data which may be inconsistent sometimes, but
    // this output is generally for "peeking the trend" and does not need
    // to exactly accurate.
    pthread_mutex_unlock(&this->_mutex);

283
    butil::StringSplitter sp(vector_names ? vector_names->c_str() : "", ',');
gejun's avatar
gejun committed
284 285 286 287 288 289 290 291
    os << '[';
    for (size_t j = 0; j < N; ++j) {
        if (j) {
            os << ',';
        }
        int c = 0;
        os << "{\"label\":\"";
        if (sp) {
292
            os << butil::StringPiece(sp.field(), sp.length());
gejun's avatar
gejun committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
            ++sp;
        } else {
            os << "Vector[" << j << ']';
        }
        os << "\",\"data\":[";
        for (int i = 0; i < 30; ++i, ++c) {
            if (c) {
                os << ',';
            }
            os << '[' << c << ',' << this->_data.day((i + day_begin) % 30)[j] << ']';
        }
        for (int i = 0; i < 24; ++i, ++c) {
            if (c) {
                os << ',';
            }
            os << '[' << c << ',' << this->_data.hour((i + hour_begin) % 24)[j] << ']';
        }
        for (int i = 0; i < 60; ++i, ++c) {
            if (c) {
                os << ',';
            }
            os << '[' << c << ',' << this->_data.minute((i + minute_begin) % 60)[j] << ']';
        }
        for (int i = 0; i < 60; ++i, ++c) {
            if (c) {
                os << ',';
            }
            os << '[' << c << ',' << this->_data.second((i + second_begin) % 60)[j] << ']';
        }
        os << "]}";
    }
    os << ']';
}

}  // namespace detail
}  // namespace bvar

#endif  // BVAR_DETAIL_SERIES_H