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_STD_HASHER_H 00033 #define SHTTL_STD_HASHER_H 00034 00035 #include "bit.h" 00036 #include "hasher.h" 00037 #include <limits> 00038 #include <stdexcept> 00039 00040 namespace shttl 00041 { 00042 template<class T = size_t> 00043 class std_hasher : public hasher<T> { 00044 public: 00045 // -- 00046 00047 typedef shttl::hasher<T> base_type; 00048 typedef std_hasher<T> this_type; 00049 00050 typedef typename base_type::size_type size_type; 00051 typedef T value_type; 00052 00053 static const std::numeric_limits<value_type> value_info; 00054 00055 00056 // Accessors 00057 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 m_off_bit; } 00061 value_type off_mask() const { return m_off_mask; } 00062 00063 // size() returns maximum value of index() 00064 size_type size() const { return (size_type)(idx_mask() + 1); } 00065 00066 00067 // Constructor(s) 00068 explicit std_hasher(const size_type idx_bit_arg, const size_type off_bit_arg) : 00069 m_idx_bit ( idx_bit_arg ), 00070 m_idx_mask( (value_type)mask(0, idx_bit_arg) ), 00071 m_off_bit ( off_bit_arg), 00072 m_off_mask( (value_type)mask(0, off_bit_arg ) 00073 ){ 00074 if(( (size_type)value_info.digits < idx_bit() ) || 00075 ( (size_type)value_info.digits < off_bit() ) || 00076 ( (size_type)value_info.digits < idx_bit() + off_bit() ) 00077 ){ 00078 throw std::invalid_argument("std_hasher::std_hasher"); 00079 } 00080 } 00081 00082 ~std_hasher() 00083 { 00084 } 00085 00086 // Member Functions 00087 size_type index(const value_type& t) const 00088 { 00089 return (size_type)((t >> off_bit()) & idx_mask()); 00090 } 00091 00092 value_type tag(const value_type& t) const 00093 { 00094 return t >> (off_bit()+idx_bit()); 00095 } 00096 00097 value_type rebuild(const value_type& tag, size_type index) const 00098 { 00099 return static_cast<value_type>( ((tag << idx_bit()) | index) << off_bit() ); 00100 } 00101 00102 bool match(const value_type& lhs, const value_type& rhs) const 00103 { 00104 return (lhs & ~off_mask()) == (rhs & ~off_mask()); 00105 } 00106 00107 protected: 00108 size_type m_idx_bit; 00109 value_type m_idx_mask; 00110 size_type m_off_bit; 00111 value_type m_off_mask; 00112 00113 }; 00114 00115 } // namespace shttl 00116 00117 #endif // SHTTL_STD_HASHER_H