Skip to content

Commit e879e60

Browse files
MurmurHash : Added string constructor and fromString function.
1 parent 996cbc1 commit e879e60

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

Changes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
10.4.2.1 (relative to 10.4.2.0)
22
========
33

4+
Improvements
5+
------------
6+
7+
- Added string constructor and `fromString` function to `IECore.MurmurHash`.
8+
49
Build
510
-----
611

include/IECore/MurmurHash.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class IECORE_API MurmurHash
6161
inline MurmurHash();
6262
inline MurmurHash( const MurmurHash &other );
6363

64+
// Construct directly from string representation
65+
inline explicit MurmurHash( const std::string &repr );
66+
6467
// Construct directly from known internal values
6568
inline MurmurHash( uint64_t h1, uint64_t h2 );
6669

@@ -84,6 +87,7 @@ class IECORE_API MurmurHash
8487
inline bool operator < ( const MurmurHash &other ) const;
8588

8689
std::string toString() const;
90+
void fromString( const std::string &repr );
8791

8892
// Access internal storage for special cases
8993
inline uint64_t h1() const;

include/IECore/MurmurHash.inl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ 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+
8288
inline MurmurHash::MurmurHash( uint64_t h1, uint64_t h2 )
8389
: m_h1( h1 ), m_h2( h2 )
8490
{

src/IECore/MurmurHash.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ std::string MurmurHash::toString() const
4646
return s.str();
4747
}
4848

49+
void MurmurHash::fromString( const std::string &repr )
50+
{
51+
std::stringstream s;
52+
s.str( repr.substr( 0, 16 ) );
53+
s >> std::hex >> m_h1;
54+
s.clear();
55+
s.str( repr.substr( 16, 16 ) );
56+
s >> std::hex >> m_h2;
57+
}
58+
4959
std::ostream &IECore::operator << ( std::ostream &o, const MurmurHash &hash )
5060
{
5161
o << hash.toString();

src/IECorePython/MurmurHashBinding.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ void IECorePython::bindMurmurHash()
128128
class_<MurmurHash>( "MurmurHash" )
129129
.def( init<>() )
130130
.def( init<const MurmurHash &>() )
131+
.def( init<const std::string &>() )
131132
.def( init<uint64_t, uint64_t>() )
132133
.def( "append", (MurmurHash &(MurmurHash::*)( const float & ))&MurmurHash::append, return_self<>() )
133134
.def( "append", (MurmurHash &(MurmurHash::*)( const double & ))&MurmurHash::append, return_self<>() )
@@ -198,6 +199,7 @@ void IECorePython::bindMurmurHash()
198199
.def( "__str__", &MurmurHash::toString )
199200
.def( "__hash__", &hash )
200201
.def( "toString", &MurmurHash::toString )
202+
.def( "fromString", &MurmurHash::fromString )
201203
.def( "h1", &MurmurHash::h1 )
202204
.def( "h2", &MurmurHash::h2 )
203205
;

test/IECore/MurmurHashTest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ def testConstructor( self ) :
4545
self.assertEqual( str( h ), "0" * 32 )
4646
self.assertEqual( h, IECore.MurmurHash() )
4747

48+
def testStringConstructor( self ) :
49+
50+
h = IECore.MurmurHash()
51+
h.append( 1 )
52+
h.append( "hello" )
53+
54+
s = h.toString()
55+
hs = IECore.MurmurHash( s )
56+
self.assertEqual( h, hs )
57+
4858
def testCopyConstructor( self ) :
4959

5060
h = IECore.MurmurHash()
@@ -54,6 +64,17 @@ def testCopyConstructor( self ) :
5464
self.assertEqual( h, IECore.MurmurHash( h ) )
5565
self.assertNotEqual( h, IECore.MurmurHash() )
5666

67+
def testFromString( self ) :
68+
69+
h = IECore.MurmurHash()
70+
h.append( 1 )
71+
h.append( "hello" )
72+
73+
s = h.toString()
74+
hs = IECore.MurmurHash()
75+
hs.fromString( s )
76+
self.assertEqual( h, hs )
77+
5778
def testAppend( self ) :
5879

5980
h = IECore.MurmurHash()

0 commit comments

Comments
 (0)