Skip to content

Commit d55cbb9

Browse files
fixup! MurmurHash : Added string constructor and fromString function.
1 parent 99bad18 commit d55cbb9

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
lines changed

Changes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Build
99
Improvements
1010
------------
1111

12-
- Added string constructor and `fromString` function to `IECore.MurmurHash`.
12+
- Added string constructor and static `fromString` function to `IECore.MurmurHash`.
1313

1414
10.4.2.1 (relative to 10.4.2.0)
1515
========

include/IECore/MurmurHash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class IECORE_API MurmurHash
6262
inline MurmurHash( const MurmurHash &other );
6363

6464
// Construct directly from string representation
65-
inline explicit MurmurHash( const std::string &repr );
65+
explicit MurmurHash( const std::string &repr );
6666

6767
// Construct directly from known internal values
6868
inline MurmurHash( uint64_t h1, uint64_t h2 );
@@ -87,7 +87,7 @@ class IECORE_API MurmurHash
8787
inline bool operator < ( const MurmurHash &other ) const;
8888

8989
std::string toString() const;
90-
void fromString( const std::string &repr );
90+
static MurmurHash fromString( const std::string &repr );
9191

9292
// Access internal storage for special cases
9393
inline uint64_t h1() const;

include/IECore/MurmurHash.inl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ inline MurmurHash::MurmurHash( const MurmurHash &other )
7979
{
8080
}
8181

82-
inline MurmurHash::MurmurHash( const std::string &repr )
83-
: m_h1( 0 ), m_h2( 0 )
84-
{
85-
fromString( repr );
86-
}
87-
8882
inline MurmurHash::MurmurHash( uint64_t h1, uint64_t h2 )
8983
: m_h1( h1 ), m_h2( h2 )
9084
{

src/IECore/MurmurHash.cpp

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,61 @@
3333
//////////////////////////////////////////////////////////////////////////
3434

3535
#include "IECore/MurmurHash.h"
36+
#include "IECore/Exception.h"
37+
38+
#include <boost/format.hpp>
3639

3740
#include <iomanip>
3841
#include <sstream>
3942

4043
using namespace IECore;
4144

42-
std::string MurmurHash::toString() const
45+
namespace
46+
{
47+
48+
std::string internalToString( uint64_t const h1, uint64_t const h2 )
4349
{
4450
std::stringstream s;
45-
s << std::hex << std::setfill( '0' ) << std::setw( 16 ) << m_h1 << std::setw( 16 ) << m_h2;
51+
s << std::hex << std::setfill( '0' ) << std::setw( 16 ) << h1 << std::setw( 16 ) << h2;
4652
return s.str();
4753
}
4854

49-
void MurmurHash::fromString( const std::string &repr )
55+
void internalFromString( const std::string &repr, uint64_t &h1, uint64_t &h2 )
5056
{
57+
if( repr.length() != static_cast<std::string::size_type>( 32 ) )
58+
{
59+
throw Exception(
60+
boost::str(
61+
boost::format(
62+
"Invalid IECore::MurmurHash string representation \"%s\", must have 32 characters" )
63+
% repr
64+
) );
65+
}
66+
5167
std::stringstream s;
5268
s.str( repr.substr( 0, 16 ) );
53-
s >> std::hex >> m_h1;
69+
s >> std::hex >> h1;
5470
s.clear();
5571
s.str( repr.substr( 16, 16 ) );
56-
s >> std::hex >> m_h2;
72+
s >> std::hex >> h2;
73+
}
74+
75+
} // namespace
76+
77+
MurmurHash::MurmurHash( const std::string &repr )
78+
: m_h1( 0 ), m_h2( 0 )
79+
{
80+
internalFromString( repr, m_h1, m_h2 );
81+
}
82+
83+
std::string MurmurHash::toString() const
84+
{
85+
return internalToString( m_h1, m_h2 );
86+
}
87+
88+
MurmurHash MurmurHash::fromString( const std::string &repr )
89+
{
90+
return MurmurHash( repr );
5791
}
5892

5993
std::ostream &IECore::operator << ( std::ostream &o, const MurmurHash &hash )

src/IECorePython/MurmurHashBinding.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ void IECorePython::bindMurmurHash()
199199
.def( "__str__", &MurmurHash::toString )
200200
.def( "__hash__", &hash )
201201
.def( "toString", &MurmurHash::toString )
202-
.def( "fromString", &MurmurHash::fromString )
202+
.def( "fromString", (MurmurHash (*)( const std::string & ))&MurmurHash::fromString )
203+
.staticmethod( "fromString" )
203204
.def( "h1", &MurmurHash::h1 )
204205
.def( "h2", &MurmurHash::h2 )
205206
;

test/IECore/MurmurHashTest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import unittest
3636
import imath
37+
import six
3738

3839
import IECore
3940

@@ -71,10 +72,12 @@ def testFromString( self ) :
7172
h.append( "hello" )
7273

7374
s = h.toString()
74-
hs = IECore.MurmurHash()
75-
hs.fromString( s )
75+
hs = IECore.MurmurHash.fromString( s )
7676
self.assertEqual( h, hs )
7777

78+
with six.assertRaisesRegex( self, Exception, ".*must have 32 characters.*" ) :
79+
IECore.MurmurHash.fromString( "InvalidStringRepresentation" )
80+
7881
def testAppend( self ) :
7982

8083
h = IECore.MurmurHash()

0 commit comments

Comments
 (0)