Skip to content

Commit

Permalink
Implement eql? and hash for UUID in Ruby so UUID can be used as a…
Browse files Browse the repository at this point in the history
… key in a Ruby Hash.
  • Loading branch information
macumber committed Jun 14, 2020
1 parent c710464 commit ebd7b9f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ test_gems
test_fails.txt
# Ignore build folders (at root level only)
/build*/
/debug*/
/release*/

developer/msvc/Visualizers/all_concat.natvis
.vscode/
Expand Down
62 changes: 62 additions & 0 deletions ruby/test/UUID_Test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,68 @@ def test_uuid_big_set

File.delete("UUIDSet.txt")
end

def test_uuid_hash
model = OpenStudio::Model::Model.new
space1 = OpenStudio::Model::Space.new(model)
space1.setName("Space 1")
space2 = OpenStudio::Model::Space.new(model)
space2.setName("Space 2")

space1_handle1 = space1.handle
space1_handle2 = space1.handle
space2_handle1 = space2.handle
space2_handle2 = space2.handle

assert(space1_handle1 == space1_handle1)
assert(space1_handle1 == space1_handle2)
assert(space1_handle1 != space2_handle1)
assert(space1_handle1 != space2_handle2)

assert(space1_handle1.to_s == space1_handle1.to_s)
assert(space1_handle1.to_s == space1_handle2.to_s)
assert(space1_handle1.to_s != space2_handle1.to_s)
assert(space1_handle1.to_s != space2_handle2.to_s)

assert(space1_handle1.hash == space1_handle1.hash)
assert(space1_handle1.hash == space1_handle2.hash)
assert(space1_handle1.hash != space2_handle1.hash)
assert(space1_handle1.hash != space2_handle2.hash)

assert(space1_handle1.eql?(space1_handle1))
assert(space1_handle1.eql?(space1_handle2))
assert(!space1_handle1.eql?(space2_handle1))
assert(!space1_handle1.eql?(space2_handle2))

assert(space1_handle1.to_s != space1_handle1)
assert(space1_handle1.to_s != space1_handle2)
assert(space1_handle1.to_s != space2_handle1)
assert(space1_handle1.to_s != space2_handle2)

handle_to_space_map = {}
handle_to_space_map[space1_handle1] = space1
handle_to_space_map[space2_handle1] = space2
assert(handle_to_space_map[space1_handle1] == space1)
assert(handle_to_space_map[space1_handle2] == space1)
assert(handle_to_space_map[space2_handle1] == space2)
assert(handle_to_space_map[space2_handle2] == space2)
assert(handle_to_space_map[space1_handle1.to_s] != space1)
assert(handle_to_space_map[space1_handle2.to_s] != space1)
assert(handle_to_space_map[space2_handle1.to_s] != space2)
assert(handle_to_space_map[space2_handle2.to_s] != space2)

handle_str_to_space_map = {}
handle_str_to_space_map[space1_handle1.to_s] = space1
handle_str_to_space_map[space2_handle1.to_s] = space2
assert(handle_str_to_space_map[space1_handle1.to_s] == space1)
assert(handle_str_to_space_map[space1_handle2.to_s] == space1)
assert(handle_str_to_space_map[space2_handle1.to_s] == space2)
assert(handle_str_to_space_map[space2_handle2.to_s] == space2)
assert(handle_str_to_space_map[space1_handle1] != space1)
assert(handle_str_to_space_map[space1_handle2] != space1)
assert(handle_str_to_space_map[space2_handle1] != space2)
assert(handle_str_to_space_map[space2_handle2] != space2)
end

end

Expand Down
4 changes: 4 additions & 0 deletions src/utilities/core/UUID.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ namespace openstudio {
return is_nil();
}

bool isEqual(const UUID& other) const {
return (*this == other);
}

using boost::uuids::uuid::iterator;
using boost::uuids::uuid::const_iterator;
using boost::uuids::uuid::begin;
Expand Down
12 changes: 12 additions & 0 deletions src/utilities/core/UUID.i
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@

%{
#include <utilities/core/UUID.hpp>
#include <functional>
%}

namespace openstudio{

#ifdef SWIGRUBY
%rename("nil?") isNull();

%alias UUID::isEqual "eql?";
#endif

class UUID {
public:
bool isNull() const;
bool isEqual(const UUID& other) const;
~UUID();

protected:
Expand All @@ -40,6 +44,14 @@ namespace openstudio{
return openstudio::toString(*self);
}

int __hash__() const{
return std::hash<std::string>{}(openstudio::toString(*self));
}

bool __eq__ (const UUID & other) const{
return *self == other;
}

bool operator!= ( const UUID & other ) const {
return *self != other;
}
Expand Down

0 comments on commit ebd7b9f

Please sign in to comment.