forked from Begun/libslave
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdecimal_supp.h
83 lines (70 loc) · 2.9 KB
/
decimal_supp.h
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
#pragma once
#include "decimal.h"
#include <stdint.h>
#include <iterator>
#include <utility>
// convenient utilities for decimal
namespace slave::decimal
{
using digit_and_count_t = std::pair<digit_t, uint8_t>;
// @brief range of decimal number digits
template <typename Traits>
class Digits
{
public:
// @brief iterate over decimal digits from high to low
// @value is pair digits value and number of digits
class Iterator : public std::iterator<std::input_iterator_tag, digit_and_count_t>
{
public:
Iterator(const Decimal& d, size_t pos) : m_value(d), m_pos(pos) {}
Iterator& operator++() { ++m_pos; return *this; }
bool operator==(Iterator& other) const { return m_pos == other.m_pos; }
bool operator!=(Iterator& other) const { return m_pos != other.m_pos; }
value_type operator*() const { return Traits::read(m_value, m_pos); }
private:
const Decimal& m_value;
size_t m_pos;
};
// @brief iterate over decimal digits, from low bit to high
class ReverseIterator : public std::iterator<std::input_iterator_tag, digit_and_count_t>
{
public:
ReverseIterator(const Decimal& d, size_t pos) : m_value(d), m_pos(pos) {}
ReverseIterator& operator++() { --m_pos; return *this; }
bool operator==(ReverseIterator& other) const { return m_pos == other.m_pos; }
bool operator!=(ReverseIterator& other) const { return m_pos != other.m_pos; }
value_type operator*() const { return Traits::read(m_value, m_pos - 1); }
private:
const Decimal& m_value;
size_t m_pos;
};
using iterator = Iterator;
using reverse_iterator = ReverseIterator;
Digits(const Decimal& v) : m_value(v) {}
// @brief direct iterators
iterator begin() const { return Iterator(m_value, 0); }
iterator end() const { return Iterator(m_value, Traits::size(m_value)); }
// @brief reverse iterators
reverse_iterator rbegin() const { return ReverseIterator(m_value, Traits::size(m_value)); }
reverse_iterator rend() const { return ReverseIterator(m_value, 0); }
private:
const Decimal& m_value;
};
// @brief trait for integer digits range
struct IntegerDigitsTrait
{
static digit_and_count_t read(const Decimal& d, size_t pos);
static size_t size(const Decimal& d);
};
// @brief trait for fractional digits range
struct FractionalDigitsTrait
{
static digit_and_count_t read(const Decimal& d, size_t pos);
static size_t size(const Decimal& d);
};
// @brief integer digits of decimal value
using IntegerDigits = Digits<IntegerDigitsTrait>;
// @brief fractional digits of decimal value
using FractionalDigits = Digits<FractionalDigitsTrait>;
}