00001 // 00002 // Copyright (c) 2005-2008 Kenichi Watanabe. 00003 // Copyright (c) 2005-2008 Yasuhiro Watari. 00004 // Copyright (c) 2005-2008 Hironori Ichibayashi. 00005 // Copyright (c) 2008-2009 Kazuo Horio. 00006 // Copyright (c) 2009-2013 Naruki Kurata. 00007 // Copyright (c) 2005-2013 Ryota Shioya. 00008 // Copyright (c) 2005-2013 Masahiro Goshima. 00009 // 00010 // This software is provided 'as-is', without any express or implied 00011 // warranty. In no event will the authors be held liable for any damages 00012 // arising from the use of this software. 00013 // 00014 // Permission is granted to anyone to use this software for any purpose, 00015 // including commercial applications, and to alter it and redistribute it 00016 // freely, subject to the following restrictions: 00017 // 00018 // 1. The origin of this software must not be misrepresented; you must not 00019 // claim that you wrote the original software. If you use this software 00020 // in a product, an acknowledgment in the product documentation would be 00021 // appreciated but is not required. 00022 // 00023 // 2. Altered source versions must be plainly marked as such, and must not be 00024 // misrepresented as being the original software. 00025 // 00026 // 3. This notice may not be removed or altered from any source 00027 // distribution. 00028 // 00029 // 00030 00031 00032 #ifndef SHTTL_STATIC_OFF_HASHER_H 00033 #define SHTTL_STATIC_OFF_HASHER_H 00034 00035 #include <stdexcept> 00036 #include <iostream> 00037 00038 #include "bit.h" 00039 #include "hasher.h" 00040 00041 namespace shttl 00042 { 00043 00044 template<class T = u32, size_t OFFSET = 0> // Offset bits 00045 class static_off_hasher : public hasher<T> { 00046 public: 00047 00048 // Types 00049 typedef static_off_hasher<T, OFFSET> this_type; 00050 typedef hasher<T> base_type; 00051 00052 typedef typename base_type::size_type size_type; 00053 typedef T value_type; 00054 00055 static const std::numeric_limits<value_type> value_info; 00056 00057 // Accessors 00058 size_type idx_bit() const { return m_idx_bit; } 00059 value_type idx_mask() const { return m_idx_mask; } 00060 size_type off_bit() const { return OFFSET; } 00061 u64 off_mask() const { return shttl::mask(0, OFFSET); } 00062 00063 // size() returns maximum value of index() 00064 size_type size() const 00065 { 00066 return (size_type)idx_mask() + 1; 00067 } 00068 00069 // Constructor(s) 00070 explicit static_off_hasher(const size_type index_bit_size) : 00071 m_idx_bit ( index_bit_size ), 00072 m_idx_mask( (value_type)shttl::mask(0, index_bit_size) ) 00073 { 00074 if( (/*idx_bit() < 0 || */(size_type)value_info.digits < idx_bit()) || 00075 (/*off_bit() < 0 || */(size_type)value_info.digits < off_bit()) || 00076 ( (size_type)value_info.digits < idx_bit() + off_bit() ) 00077 ){ 00078 throw std::invalid_argument("static_off_hasher::static_off_hasher"); 00079 } 00080 } 00081 00082 00083 // Member Functions 00084 size_type index(const T& t) const 00085 { 00086 return static_cast<size_type>((t >> off_bit()) & idx_mask()); 00087 } 00088 00089 T tag(const T& t) const 00090 { 00091 return t >> (off_bit()+idx_bit()); 00092 } 00093 00094 value_type rebuild(const value_type& tag, size_type index) const 00095 { 00096 return (value_type)( 00097 ((tag << idx_bit()) | index) << off_bit() 00098 ); 00099 } 00100 00101 bool match(const T& lhs, const T& rhs) const 00102 { 00103 return (lhs & ~off_mask()) == (rhs & ~off_mask()); 00104 } 00105 00106 protected: 00107 00108 size_type m_idx_bit; 00109 value_type m_idx_mask; 00110 00111 }; 00112 00113 } // namespace shttl 00114 00115 #endif // SHTTL_STD_HASHER_H