src/Lib/shttl/counter.h

説明を見る。
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 //
00033 // Simple counter
00034 //
00035 
00036 #ifndef SHTTL_COUNTER_H
00037 #define SHTTL_COUNTER_H
00038 
00039 #include <stdexcept>
00040 
00041 #include "bit.h"
00042 
00043 namespace shttl
00044 {
00045 
00046     //
00047     // On shttl::counter, value_body_type is a type of a value itself.
00048     // On shttl::counter_array, value_body_type is a reference and 
00049     // m_value referes a element of a large array in shttl::counter_array.
00050     //
00051     template < typename value_type = u8, typename value_body_type = value_type >
00052     class counter_base
00053     {
00054 
00055     public:
00056         explicit counter_base(
00057             value_body_type value,
00058             value_type initv = value_type(),
00059             value_type min  = 0,
00060             value_type max  = 3,
00061             value_type add = 1,
00062             value_type sub = 1,
00063             value_type threshold = 0
00064         ) :
00065             m_value( value ),
00066             m_initv( initv ),
00067             m_min  ( min   ),
00068             m_max  ( max   ),
00069             m_add  ( add   ),
00070             m_sub  ( sub   ),
00071             m_threshold( threshold )
00072         {
00073         /*  static_assert( 
00074                 sizeof(int) >= sizeof(value_type), 
00075                 "The size of value_type is too large." 
00076             );*/
00077         }
00078 
00079         value_type initv() const
00080         {
00081             return m_initv;
00082         }
00083 
00084         value_type min() const
00085         {
00086             return m_min;
00087         }
00088 
00089         value_type max() const
00090         {
00091             return m_max;
00092         }
00093 
00094         value_type threshold() const
00095         {
00096             return m_threshold;
00097         }
00098 
00099         // Conversion to value_type
00100         operator value_type() const 
00101         {
00102             return m_value; 
00103         }
00104 
00105         void set(       
00106             value_type initv = value_type(),
00107             value_type min  = 0,
00108             value_type max  = 3,
00109             value_type add = 1,
00110             value_type sub = 1,
00111             value_type threshold = 0
00112         )
00113         {
00114             m_initv = initv;
00115             m_min = min;
00116             m_max = max;
00117             m_add = add;
00118             m_sub = sub;
00119             m_threshold = threshold;
00120         }
00121 
00122         void reset()
00123         {
00124             m_value = m_initv;
00125         }
00126 
00127         value_type inc()
00128         {
00129             int value = m_value;
00130             value += m_add;
00131             if( value > m_max ){
00132                 value = m_max;
00133             }
00134             m_value = (value_type)value;
00135             return m_value;
00136         }
00137 
00138         value_type dec()
00139         {
00140             int value = m_value;
00141             value -= m_sub;
00142             if( value < m_min ){
00143                 value = m_min;
00144             }
00145             m_value = (value_type)value;
00146             return m_value;
00147         }
00148 
00149         // Returns whether a value is above a threshold.
00150         bool above_threshold() const
00151         {
00152             return m_value >= m_threshold;
00153         }
00154 
00155     protected:
00156         value_body_type m_value;
00157         value_type m_initv;
00158         value_type m_min;
00159         value_type m_max;
00160         value_type m_add;
00161         value_type m_sub;
00162         value_type m_threshold;
00163 
00164     };
00165 
00166     template < typename value_type = u8 >
00167     class counter : public counter_base< value_type, value_type >
00168     {
00169 
00170     public:
00171         counter(
00172             value_type initv = value_type(),
00173             value_type min = 0,
00174             value_type max = 3,
00175             value_type add = 1,
00176             value_type sub = 1,
00177             value_type threshold = 0
00178         ) : 
00179             base_type( initv, initv, min, max, add, sub, threshold )
00180         {
00181         }
00182 
00183     protected:
00184         typedef counter_base< value_type, value_type > base_type;
00185 
00186     };
00187 
00188 
00189 } // namespace shttl
00190 
00191 #endif // SHTTL_COUNTER_H

Onikiri2に対してTue Jun 18 14:34:21 2013に生成されました。  doxygen 1.4.7