| 1 | /* | ||
| 2 | * Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation; either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #pragma once | ||
| 20 | |||
| 21 | /* | ||
| 22 | #include "FastAlloc.h" | ||
| 23 | */ | ||
| 24 | #include "Encoder.h" | ||
| 25 | |||
| 26 | namespace dcpp { | ||
| 27 | |||
| 28 | template<class Hasher> | ||
| 29 | struct HashValue /* : FastAlloc<HashValue<Hasher> > */ { | ||
| 30 | static const size_t BITS = Hasher::BITS; | ||
| 31 | static const size_t BYTES = Hasher::BYTES; | ||
| 32 | |||
| 33 | 10 | HashValue() { } | |
| 34 | 82 | explicit HashValue(const uint8_t* aData) { memcpy(data, aData, BYTES); } | |
| 35 | explicit HashValue(const std::string& base32) { Encoder::fromBase32(base32.c_str(), data, BYTES); } | ||
| 36 | 170 | HashValue(const HashValue& rhs) { memcpy(data, rhs.data, BYTES); } | |
| 37 | 46 | HashValue& operator=(const HashValue& rhs) { memcpy(data, rhs.data, BYTES); return *this; } | |
| 38 | bool operator!=(const HashValue& rhs) const { return !(*this == rhs); } | ||
| 39 | bool operator==(const HashValue& rhs) const { return memcmp(data, rhs.data, BYTES) == 0; } | ||
| 40 | bool operator<(const HashValue& rhs) const { return memcmp(data, rhs.data, BYTES) < 0; } | ||
| 41 | |||
| 42 | std::string toBase32() const { return Encoder::toBase32(data, BYTES); } | ||
| 43 | std::string& toBase32(std::string& tmp) const { return Encoder::toBase32(data, BYTES, tmp); } | ||
| 44 | |||
| 45 | uint8_t data[BYTES]; | ||
| 46 | }; | ||
| 47 | |||
| 48 | } // namespace dcpp | ||
| 49 | |||
| 50 | namespace std { | ||
| 51 | template<typename T> | ||
| 52 | struct hash<dcpp::HashValue<T> > { | ||
| 53 | size_t operator()(const dcpp::HashValue<T>& rhs) const { | ||
| 54 | // RVO should handle this as efficiently as reinterpret_cast | ||
| 55 | size_t hvHash; | ||
| 56 | memcpy(&hvHash, rhs.data, sizeof(size_t)); | ||
| 57 | return hvHash; | ||
| 58 | } | ||
| 59 | }; | ||
| 60 | |||
| 61 | } |