/root/doris/be/src/olap/uint24.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <cstdint> |
21 | | #include <cstring> |
22 | | #include <iostream> |
23 | | #include <string> |
24 | | |
25 | | namespace doris { |
26 | | |
27 | | // 24bit int type, used to store date type in storage |
28 | | struct uint24_t { |
29 | | public: |
30 | | uint24_t() = default; |
31 | | |
32 | 7.99M | uint24_t(const uint32_t& value) { |
33 | 7.99M | data[0] = static_cast<uint8_t>(value); |
34 | 7.99M | data[1] = static_cast<uint8_t>(value >> 8); |
35 | 7.99M | data[2] = static_cast<uint8_t>(value >> 16); |
36 | 7.99M | } |
37 | | |
38 | 0 | uint24_t& operator=(const uint32_t value) { |
39 | 0 | data[0] = static_cast<uint8_t>(value); |
40 | 0 | data[1] = static_cast<uint8_t>(value >> 8); |
41 | 0 | data[2] = static_cast<uint8_t>(value >> 16); |
42 | 0 | return *this; |
43 | 0 | } |
44 | | |
45 | 0 | uint24_t& operator=(const unsigned __int128& value) { |
46 | 0 | data[0] = static_cast<uint8_t>(value); |
47 | 0 | data[1] = static_cast<uint8_t>(value >> 8); |
48 | 0 | data[2] = static_cast<uint8_t>(value >> 16); |
49 | 0 | return *this; |
50 | 0 | } |
51 | | |
52 | 3.00M | uint24_t& operator=(const uint64_t value) { |
53 | 3.00M | data[0] = static_cast<uint8_t>(value); |
54 | 3.00M | data[1] = static_cast<uint8_t>(value >> 8); |
55 | 3.00M | data[2] = static_cast<uint8_t>(value >> 16); |
56 | 3.00M | return *this; |
57 | 3.00M | } |
58 | | |
59 | 0 | uint24_t& operator+=(const uint24_t& value) { |
60 | 0 | *this = static_cast<int>(*this) + static_cast<int>(value); |
61 | 0 | return *this; |
62 | 0 | } |
63 | | |
64 | 0 | uint24_t& operator>>=(const int bits) { |
65 | 0 | *this = static_cast<unsigned int>(*this) >> bits; |
66 | 0 | return *this; |
67 | 0 | } |
68 | | |
69 | 0 | uint24_t& operator|=(const uint24_t& value) { |
70 | 0 | *this = static_cast<int>(*this) | static_cast<int>(value); |
71 | 0 | return *this; |
72 | 0 | } |
73 | | |
74 | 9.90M | operator uint32_t() const { |
75 | 9.90M | uint32_t value = static_cast<uint8_t>(data[0]); |
76 | 9.90M | value += (static_cast<uint32_t>(static_cast<uint8_t>(data[1]))) << 8; |
77 | 9.90M | value += (static_cast<uint32_t>(static_cast<uint8_t>(data[2]))) << 16; |
78 | 9.90M | return value; |
79 | 9.90M | } |
80 | | |
81 | 39.7k | uint24_t& operator=(const int value) { |
82 | 39.7k | data[0] = static_cast<uint8_t>(value); |
83 | 39.7k | data[1] = static_cast<uint8_t>(value >> 8); |
84 | 39.7k | data[2] = static_cast<uint8_t>(value >> 16); |
85 | 39.7k | return *this; |
86 | 39.7k | } |
87 | | |
88 | 0 | uint24_t& operator=(const int64_t value) { |
89 | 0 | data[0] = static_cast<uint8_t>(value); |
90 | 0 | data[1] = static_cast<uint8_t>(value >> 8); |
91 | 0 | data[2] = static_cast<uint8_t>(value >> 16); |
92 | 0 | return *this; |
93 | 0 | } |
94 | | |
95 | 2 | bool operator==(const uint24_t& value) const { return cmp(value) == 0; } |
96 | | |
97 | 0 | bool operator!=(const uint24_t& value) const { return cmp(value) != 0; } |
98 | | |
99 | 4.58M | bool operator<(const uint24_t& value) const { return cmp(value) < 0; } |
100 | | |
101 | 0 | bool operator<=(const uint24_t& value) const { return cmp(value) <= 0; } |
102 | | |
103 | 978 | bool operator>(const uint24_t& value) const { return cmp(value) > 0; } |
104 | | |
105 | 0 | bool operator>=(const uint24_t& value) const { return cmp(value) >= 0; } |
106 | | |
107 | 4.58M | int32_t cmp(const uint24_t& other) const { |
108 | 4.58M | if (data[2] > other.data[2]) { |
109 | 1.30k | return 1; |
110 | 4.58M | } else if (data[2] < other.data[2]) { |
111 | 2.58k | return -1; |
112 | 2.58k | } |
113 | | |
114 | 4.57M | if (data[1] > other.data[1]) { |
115 | 695k | return 1; |
116 | 3.88M | } else if (data[1] < other.data[1]) { |
117 | 1.80M | return -1; |
118 | 1.80M | } |
119 | | |
120 | 2.07M | if (data[0] > other.data[0]) { |
121 | 1.59M | return 1; |
122 | 1.59M | } else if (data[0] < other.data[0]) { |
123 | 474k | return -1; |
124 | 474k | } |
125 | | |
126 | 9.85k | return 0; |
127 | 2.07M | } |
128 | | |
129 | 1.32k | std::string to_string() const { |
130 | 1.32k | tm time_tm; |
131 | 1.32k | int value = *reinterpret_cast<const uint24_t*>(data); |
132 | 1.32k | memset(&time_tm, 0, sizeof(time_tm)); |
133 | 1.32k | time_tm.tm_mday = static_cast<int>(value & 31); |
134 | 1.32k | time_tm.tm_mon = static_cast<int>(value >> 5 & 15) - 1; |
135 | 1.32k | time_tm.tm_year = static_cast<int>(value >> 9) - 1900; |
136 | 1.32k | char buf[20] = {'\0'}; |
137 | 1.32k | strftime(buf, sizeof(buf), "%Y-%m-%d", &time_tm); |
138 | 1.32k | return std::string(buf); |
139 | 1.32k | } |
140 | | |
141 | 0 | const uint8_t* get_data() const { return data; } |
142 | | |
143 | | private: |
144 | | uint8_t data[3]; |
145 | | } __attribute__((packed)); |
146 | | |
147 | | static_assert(std::is_trivial<uint24_t>::value, "uint24_t should be a POD type"); |
148 | | |
149 | | inline std::ostream& operator<<(std::ostream& os, const uint24_t& val) { |
150 | | os << val.to_string(); |
151 | | return os; |
152 | | } |
153 | | |
154 | | } // namespace doris |