Skip to content

Commit

Permalink
Add comparison operators for CassUuid
Browse files Browse the repository at this point in the history
  • Loading branch information
lgeorget committed Mar 26, 2020
1 parent 2b3fb19 commit f8e12d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/cassandra.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
12 changes: 12 additions & 0 deletions src/uuids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ static uint64_t set_version(uint64_t timestamp, uint8_t version) {
return (timestamp & 0x0FFFFFFFFFFFFFFFLL) | (static_cast<uint64_t>(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()); }
Expand Down

0 comments on commit f8e12d5

Please sign in to comment.