song: use boost::hash

This commit is contained in:
Andrzej Rybczak
2015-04-18 22:46:42 +02:00
parent 7f9f055082
commit 9e85f38865
2 changed files with 12 additions and 11 deletions

View File

@@ -21,6 +21,7 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <boost/format.hpp> #include <boost/format.hpp>
#include <boost/functional/hash.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@@ -32,12 +33,11 @@
namespace { namespace {
size_t calc_hash(const char* s, unsigned seed = 0) size_t calc_hash(const char *s, size_t seed = 0)
{ {
size_t hash = seed; for (; *s != '\0'; ++s)
while (*s) boost::hash_combine(seed, *s);
hash = hash * 101 + *s++; return seed;
return hash;
} }
} }

View File

@@ -46,7 +46,8 @@ struct Song
Song(const Song &rhs) : m_song(rhs.m_song), m_hash(rhs.m_hash) { } Song(const Song &rhs) : m_song(rhs.m_song), m_hash(rhs.m_hash) { }
Song(Song &&rhs) : m_song(std::move(rhs.m_song)), m_hash(rhs.m_hash) { } Song(Song &&rhs) : m_song(std::move(rhs.m_song)), m_hash(rhs.m_hash) { }
Song &operator=(Song rhs) { Song &operator=(Song rhs)
{
m_song = std::move(rhs.m_song); m_song = std::move(rhs.m_song);
m_hash = rhs.m_hash; m_hash = rhs.m_hash;
return *this; return *this;
@@ -85,15 +86,15 @@ struct Song
virtual bool empty() const; virtual bool empty() const;
bool operator==(const Song &rhs) const { bool operator==(const Song &rhs) const
{
if (m_hash != rhs.m_hash) if (m_hash != rhs.m_hash)
return false; return false;
return strcmp(c_uri(), rhs.c_uri()) == 0; return strcmp(c_uri(), rhs.c_uri()) == 0;
} }
bool operator!=(const Song &rhs) const { bool operator!=(const Song &rhs) const
if (m_hash != rhs.m_hash) {
return true; return !(operator==(rhs));
return strcmp(c_uri(), rhs.c_uri()) != 0;
} }
const char *c_uri() const { return m_song ? mpd_song_get_uri(m_song.get()) : ""; } const char *c_uri() const { return m_song ? mpd_song_get_uri(m_song.get()) : ""; }