From f8e12d5f693b9ca0fab5d57d3cc257903ec754f0 Mon Sep 17 00:00:00 2001 From: Laurent Georget Date: Thu, 26 Mar 2020 18:57:26 +0100 Subject: [PATCH] Add comparison operators for CassUuid --- include/cassandra.h | 26 ++++++++++++++++++++++++++ src/uuids.cpp | 12 ++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/cassandra.h b/include/cassandra.h index 500419150..4cffeaa57 100644 --- a/include/cassandra.h +++ b/include/cassandra.h @@ -11448,4 +11448,30 @@ cass_alloc_set_functions(CassMallocFunction malloc_func, } /* extern "C" */ #endif +#ifdef __cplusplus +/** + * Compare two UUIDs for ordering + * + * This operator is useful to use CassUuid variables as std::map keys, for + * instance. + * + * @param uuid1 A UUID + * @param uuid2 Another UUID + * @return true if, and only if, uuid1 is numerically less or equal than uuid2 + */ +bool operator<(const CassUuid& uuid1, const CassUuid& uuid2); + +/** + * Compare two UUIDs for equality + * + * This operator is useful to use CassUuid variables as std::map keys, for + * instance. + * + * @param uuid1 A UUID + * @param uuid2 Another UUID + * @return true if, and only if, uuid1 is numerically less or equal than uuid2 + */ +bool operator==(const CassUuid& uuid1, const CassUuid& uuid2); +#endif + #endif /* __CASS_H_INCLUDED__ */ diff --git a/src/uuids.cpp b/src/uuids.cpp index 1bd0d3bd3..32b90d48c 100644 --- a/src/uuids.cpp +++ b/src/uuids.cpp @@ -44,6 +44,18 @@ static uint64_t set_version(uint64_t timestamp, uint8_t version) { return (timestamp & 0x0FFFFFFFFFFFFFFFLL) | (static_cast(version) << 60); } +bool operator<(const CassUuid& uuid1, const CassUuid& uuid2) { + if (uuid1.time_and_version == uuid2.time_and_version) + return uuid1.clock_seq_and_node < uuid2.clock_seq_and_node; + else + return uuid1.time_and_version < uuid2.time_and_version; +} + +bool operator==(const CassUuid& uuid1, const CassUuid& uuid2) { + return uuid1.time_and_version == uuid2.time_and_version + && uuid1.clock_seq_and_node == uuid2.clock_seq_and_node; +} + extern "C" { CassUuidGen* cass_uuid_gen_new() { return CassUuidGen::to(new UuidGen()); }