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_NLU_H 00033 #define SHTTL_NLU_H 00034 00035 #include "shttl_types.h" 00036 #include "bit.h" 00037 #include "replacement.h" 00038 00039 namespace shttl 00040 { 00041 template< typename key_type > 00042 class nlu : replacer< key_type > 00043 { 00044 public: 00045 typedef typename replacer< key_type >::size_type size_type; 00046 00047 00048 nlu() : 00049 m_way_num(0), 00050 m_index_num(0) 00051 { 00052 } 00053 00054 void construct( const size_type set_num, const size_type way_num ) 00055 { 00056 if( std::numeric_limits<bitmap_type>().digits < (int)way_num ){ 00057 throw std::invalid_argument( 00058 "shttl::nlu_array() \n Specified way number is too large." 00059 ); 00060 } 00061 00062 m_way_num = way_num; 00063 m_index_num = set_num; 00064 m_bitmap.resize( m_index_num, 0 ); 00065 } 00066 00067 size_type size() 00068 { 00069 return m_index_num; 00070 } 00071 00072 void touch( 00073 const size_type index, 00074 const size_type way, 00075 const key_type key 00076 ){ 00077 get_set( index ).touch( way ); 00078 } 00079 00080 size_type target( 00081 const size_type index 00082 ){ 00083 return get_set( index ).target(); 00084 } 00085 00086 00087 protected: 00088 00089 size_type m_way_num; 00090 size_type m_index_num; 00091 00092 typedef u32 bitmap_type; 00093 typedef std::vector<bitmap_type> bitmap_array_type; 00094 bitmap_array_type m_bitmap; 00095 00096 class set 00097 { 00098 public: 00099 00100 set( bitmap_array_type::iterator bitmap, size_t way_num ) : 00101 m_bitmap( bitmap ), 00102 m_way_num( way_num ) 00103 { 00104 } 00105 00106 size_t target() 00107 { 00108 for(size_type i = 0; i < m_way_num; i++){ 00109 if(!(*m_bitmap & (1 << i))) 00110 return i; 00111 } 00112 00113 SHTTL_ASSERT(0); 00114 return 0; 00115 } 00116 00117 void touch(const size_t way) 00118 { 00119 #ifdef SHTTL_DEBUG 00120 if ( (size_type)m_way_num <= way ){ 00121 throw std::out_of_range("nlu::touch"); 00122 } 00123 #endif 00124 00125 *m_bitmap |= 1 << way; 00126 if( *m_bitmap == mask(0, m_way_num) ) 00127 *m_bitmap = 0; 00128 } 00129 00130 protected: 00131 bitmap_array_type::iterator m_bitmap; 00132 size_t m_way_num; 00133 00134 }; 00135 00136 set get_set( const size_type index ) 00137 { 00138 return set( m_bitmap.begin() + index, m_way_num ); 00139 } 00140 00141 }; 00142 } // namespace shttl 00143 00144 #endif // SHTTL_NLU_H 00145