Skip to content

Commit 6010c8d

Browse files
author
sancar
committed
address review comments
1 parent 01f4fef commit 6010c8d

File tree

5 files changed

+124
-104
lines changed

5 files changed

+124
-104
lines changed

hazelcast/include/hazelcast/client/decimal.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ struct HAZELCAST_API decimal
4949
bool HAZELCAST_API
5050
operator==(const decimal& lhs, const decimal& rhs);
5151

52+
bool HAZELCAST_API
53+
operator<(const decimal& lhs, const decimal& rhs);
54+
} // namespace client
55+
} // namespace hazelcast
56+
namespace std {
57+
template<>
58+
struct HAZELCAST_API hash<hazelcast::client::decimal>
59+
{
60+
std::size_t operator()(const hazelcast::client::decimal& f) const;
61+
};
62+
} // namespace std
63+
namespace hazelcast {
64+
namespace client {
5265
namespace pimpl {
5366

5467
/**
@@ -79,7 +92,8 @@ from_bytes(std::vector<int8_t> v);
7992
* if i is a negative number, we take the two's complement on resulting vector,
8093
* to get negative representation of the number.
8194
* We also add one extra byte to the end of the vector to preserve the sign if
82-
* necessary.
95+
* sign of the integer is not same as the most significant byte's sign.
96+
* Otherwise we don't add it to have minimum size vector to represent the value.
8397
* @param i the number to convert to bytes
8498
* @return the vector of int8_t representing the number
8599
*/

hazelcast/src/hazelcast/client/client_impl.cpp

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include "hazelcast/logger.h"
4646
#include "hazelcast/client/member_selectors.h"
4747
#include "hazelcast/client/client_properties.h"
48-
48+
#include "hazelcast/client/decimal.h"
4949
#ifndef HAZELCAST_VERSION
5050
#define HAZELCAST_VERSION "NOT_FOUND"
5151
#endif
@@ -591,6 +591,79 @@ operator<<(std::ostream& stream, const address& address)
591591
return stream << address.to_string();
592592
}
593593

594+
bool
595+
operator==(const decimal& lhs, const decimal& rhs)
596+
{
597+
return lhs.unscaled == rhs.unscaled && lhs.scale == rhs.scale;
598+
}
599+
600+
bool
601+
operator<(const decimal& lhs, const decimal& rhs)
602+
{
603+
if (lhs.scale != rhs.scale) {
604+
return lhs.scale < rhs.scale;
605+
}
606+
return lhs.unscaled < rhs.unscaled;
607+
}
608+
609+
namespace pimpl {
610+
611+
void
612+
twos_complement(std::vector<int8_t>& a)
613+
{
614+
// twos complement is calculated via flipping the bits and adding 1
615+
// flip the bits
616+
for (auto& item : a) {
617+
item = ~item;
618+
}
619+
// add 1
620+
int8_t carry = 1;
621+
for (int i = a.size() - 1; i >= 0; i--) {
622+
a[i] = a[i] + carry;
623+
if (a[i] == 0) {
624+
carry = 1;
625+
} else {
626+
break;
627+
}
628+
}
629+
}
630+
631+
boost::multiprecision::cpp_int
632+
from_bytes(std::vector<int8_t> v)
633+
{
634+
boost::multiprecision::cpp_int i;
635+
bool is_negative = v[0] < 0;
636+
if (is_negative) {
637+
twos_complement(v);
638+
}
639+
import_bits(i, v.begin(), v.end(), 8);
640+
if (is_negative) {
641+
return -i;
642+
}
643+
return i;
644+
}
645+
646+
std::vector<int8_t>
647+
to_bytes(const boost::multiprecision::cpp_int& i)
648+
{
649+
std::vector<int8_t> v;
650+
export_bits(i, std::back_inserter(v), 8);
651+
if (i < 0) {
652+
twos_complement(v);
653+
if (v[0] > 0) {
654+
// add -1 as the most significant to have a negative sign bit
655+
v.insert(v.begin(), -1);
656+
}
657+
} else {
658+
// add 0 as the most significant byte to have a positive sign bit
659+
if (v[0] < 0) {
660+
v.insert(v.begin(), 0);
661+
}
662+
}
663+
return v;
664+
}
665+
} // namespace pimpl
666+
594667
namespace serialization {
595668
int32_t
596669
hz_serializer<address>::get_factory_id()
@@ -1152,4 +1225,14 @@ hash<hazelcast::client::address>::operator()(
11521225
boost::hash_combine(seed, address.type_);
11531226
return seed;
11541227
}
1228+
1229+
std::size_t
1230+
hash<hazelcast::client::decimal>::operator()(
1231+
const hazelcast::client::decimal& dec) const
1232+
{
1233+
std::size_t seed = 0;
1234+
boost::hash_combine(seed, dec.unscaled);
1235+
boost::hash_combine(seed, dec.scale);
1236+
return seed;
1237+
};
11551238
} // namespace std

hazelcast/src/hazelcast/client/decimal.cpp

Lines changed: 0 additions & 81 deletions
This file was deleted.

hazelcast/test/src/HazelcastTests2.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class DecimalTest : public ::testing::Test
8181
{};
8282

8383
void
84-
assertEqual(const std::string& expected_string,
85-
const std::vector<int8_t>& expected_vector)
84+
assert_equal(const std::string& expected_string,
85+
const std::vector<int8_t>& expected_vector)
8686
{
8787
using boost::multiprecision::cpp_int;
8888
cpp_int expected_int(expected_string);
@@ -95,7 +95,7 @@ assertEqual(const std::string& expected_string,
9595

9696
TEST_F(DecimalTest, positive_test)
9797
{
98-
assertEqual(
98+
assert_equal(
9999
"236095134049630962491764987683473058401811134068823290126231516129",
100100
{
101101
2, 61, -22, 92, -44, -54, -45, -9, -17, -4, -66, -5, -12, 19,
@@ -105,21 +105,21 @@ TEST_F(DecimalTest, positive_test)
105105

106106
TEST_F(DecimalTest, negative_test)
107107
{
108-
assertEqual("-158058224523514071900098807210097354699988293366",
109-
{
110-
-28, 80, 108, -112, -19, -44, 84, -98, 96, 106,
111-
53, -88, 77, -45, 89, 119, 109, -109, -87, 10,
112-
});
108+
assert_equal("-158058224523514071900098807210097354699988293366",
109+
{
110+
-28, 80, 108, -112, -19, -44, 84, -98, 96, 106,
111+
53, -88, 77, -45, 89, 119, 109, -109, -87, 10,
112+
});
113113
}
114114

115115
TEST_F(DecimalTest, preserve_positive_sign_test)
116116
{
117-
assertEqual("53220513728803604", { 0, -67, 19, -58, 119, -111, -77, 20 });
117+
assert_equal("53220513728803604", { 0, -67, 19, -58, 119, -111, -77, 20 });
118118
}
119119

120120
TEST_F(DecimalTest, preserve_negative_sign_test)
121121
{
122-
assertEqual(
122+
assert_equal(
123123
"-78097809300018214368298043748751294327036591272091714272720014418",
124124
{
125125
-1, 66, 39, -99, 44, 53, -23, 60, 125, 105, 65, -21, 104, -36,
@@ -129,21 +129,21 @@ TEST_F(DecimalTest, preserve_negative_sign_test)
129129

130130
TEST_F(DecimalTest, carry_bit_test)
131131
{
132-
assertEqual("-4172290065390264938962604145655817690459633380799476516330728"
133-
"71499276353298132342018230923743606150479511296",
134-
{
135-
-46, -123, 61, 41, -1, 115, -54, 91, -48,
136-
79, 55, 25, 41, -90, 14, 109, -115, 68,
137-
-122, 46, 70, 90, 47, -103, -21, -39, 126,
138-
-45, 37, 58, 60, -76, -44, 91, 97, 52,
139-
31, -38, 23, -111, 18, -112, -109, -127, 0,
140-
});
132+
assert_equal(
133+
"-4172290065390264938962604145655817690459633380799476516330728"
134+
"71499276353298132342018230923743606150479511296",
135+
{
136+
-46, -123, 61, 41, -1, 115, -54, 91, -48, 79, 55, 25,
137+
41, -90, 14, 109, -115, 68, -122, 46, 70, 90, 47, -103,
138+
-21, -39, 126, -45, 37, 58, 60, -76, -44, 91, 97, 52,
139+
31, -38, 23, -111, 18, -112, -109, -127, 0,
140+
});
141141
}
142142

143143
TEST_F(DecimalTest, cascading_carry_bit_test)
144144
{
145-
assertEqual("-1234506895138773532672",
146-
{ -67, 19, -58, 119, -111, -77, 0, 0, 0 });
145+
assert_equal("-1234506895138773532672",
146+
{ -67, 19, -58, 119, -111, -77, 0, 0, 0 });
147147
}
148148

149149
class AddressHelperTest : public ClientTest

hazelcast/test/src/compact_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#endif
2626

2727
#include <hazelcast/client/serialization/serialization.h>
28+
#include <hazelcast/client/decimal.h>
2829

2930
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
3031
#pragma warning(push)
@@ -494,6 +495,9 @@ check_schema_field(const schema& schema,
494495

495496
TEST_F(CompactSerializationTest, test_field_order_fixed_size)
496497
{
498+
std::unordered_map<hazelcast::client::decimal, hazelcast::client::decimal> m(4);
499+
m.insert(std::make_pair(hazelcast::client::decimal{1, 2},
500+
hazelcast::client::decimal{3, 4}));
497501
schema_writer schema_writer("typeName");
498502
auto writer = serialization::pimpl::create_compact_writer(&schema_writer);
499503
serialization::hz_serializer<employee_dto>::write(employee_dto{}, writer);

0 commit comments

Comments
 (0)