|
Equalizer
1.3.1-git
|
00001 00002 /* Copyright (c) 2010, Cedric Stalder <cedric.stalder@gmail.com> 00003 * 2010-2012, Stefan Eilemann <eile@eyescale.ch> 00004 * 00005 * This library is free software; you can redistribute it and/or modify it under 00006 * the terms of the GNU Lesser General Public License version 2.1 as published 00007 * by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, but WITHOUT 00010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00012 * details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this library; if not, write to the Free Software Foundation, Inc., 00016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00017 */ 00018 00019 #ifndef LUNCHBOX_UINT128_H 00020 #define LUNCHBOX_UINT128_H 00021 00022 #include <lunchbox/api.h> 00023 #include <sstream> 00024 00025 #ifdef _MSC_VER 00026 // Don't include <lunchbox/types.h> to be minimally intrusive for apps 00027 // using uint128_t 00028 # include <basetsd.h> 00029 typedef UINT64 uint64_t; 00030 #else 00031 # include <stdint.h> 00032 #endif 00033 00034 namespace lunchbox 00035 { 00037 class uint128_t 00038 { 00039 public: 00044 uint128_t( const uint64_t low_ = 0 ) 00045 : _high( 0 ), _low( low_ ) {} 00046 00051 explicit uint128_t( const uint64_t high_, const uint64_t low_ ) 00052 : _high( high_ ), _low( low_ ) {} 00053 00055 uint128_t& operator = ( const uint128_t& rhs ) 00056 { 00057 _high = rhs._high; 00058 _low = rhs._low; 00059 return *this; 00060 } 00061 00063 uint128_t& operator = ( const uint64_t rhs ) 00064 { 00065 _high = 0; 00066 _low = rhs; 00067 return *this; 00068 } 00069 00071 LUNCHBOX_API uint128_t& operator = ( const std::string& from ); 00072 00077 bool operator == ( const uint128_t& rhs ) const 00078 { return _high == rhs._high && _low == rhs._low; } 00079 00084 bool operator != ( const uint128_t& rhs ) const 00085 { return _high != rhs._high || _low != rhs._low; } 00086 00091 bool operator < ( const uint128_t& rhs ) const 00092 { 00093 if( _high < rhs._high ) 00094 return true; 00095 if( _high > rhs._high ) 00096 return false; 00097 return _low < rhs._low; 00098 } 00099 00104 bool operator > ( const uint128_t& rhs ) const 00105 { 00106 if( _high > rhs._high ) 00107 return true; 00108 if( _high < rhs._high ) 00109 return false; 00110 return _low > rhs._low; 00111 } 00112 00118 bool operator <= ( const uint128_t& rhs ) const 00119 { 00120 if( _high < rhs._high ) 00121 return true; 00122 if( _high > rhs._high ) 00123 return false; 00124 return _low <= rhs._low; 00125 } 00126 00132 bool operator >= ( const uint128_t& rhs ) const 00133 { 00134 if( _high > rhs._high ) 00135 return true; 00136 if( _high < rhs._high ) 00137 return false; 00138 return _low >= rhs._low; 00139 } 00140 00142 uint128_t& operator ++() 00143 { 00144 ++_low; 00145 if( !_low ) 00146 ++_high; 00147 00148 return *this; 00149 } 00150 00152 uint128_t& operator --() 00153 { 00154 if( !_low ) 00155 --_high; 00156 --_low; 00157 return *this; 00158 } 00159 00161 const uint64_t& low() const { return _low; } 00163 const uint64_t& high() const { return _high; } 00164 00166 uint64_t& low() { return _low; } 00168 uint64_t& high() { return _high; } 00169 00171 std::string getShortString() const 00172 { 00173 std::stringstream stream; 00174 stream << std::hex << _high << _low; 00175 const std::string str = stream.str(); 00176 return str.substr( 0, 3 ) + ".." + 00177 str.substr( str.length() - 3, std::string::npos ); 00178 } 00179 00181 template< class Archive > 00182 void serialize( Archive& ar, const unsigned int version ) 00183 { 00184 ar & low(); 00185 ar & high(); 00186 } 00187 00189 static LUNCHBOX_API const uint128_t ZERO; 00190 00191 private: 00192 uint64_t _high; 00193 uint64_t _low; 00194 }; 00195 00197 inline std::ostream& operator << ( std::ostream& os, const uint128_t& id ) 00198 { 00199 if( id.high() == 0 ) 00200 os << std::hex << id.low() << std::dec; 00201 else 00202 os << std::hex << id.high() << ':' << id.low() << std::dec; 00203 return os; 00204 } 00205 00207 inline uint128_t operator+ ( const uint128_t& a, const uint64_t& b ) 00208 { 00209 uint128_t result = a; 00210 result.low() += b; 00211 if( result.low() < a.low( )) 00212 ++result.high(); 00213 return result; 00214 }; 00215 00217 inline uint128_t operator- ( const uint128_t& a, const uint64_t& b ) 00218 { 00219 uint128_t result = a; 00220 result.low() -= b; 00221 if( result.low() > a.low( )) 00222 --result.high(); 00223 return result; 00224 }; 00225 00227 inline uint128_t operator & ( const uint128_t& a, const uint128_t& b ) 00228 { 00229 uint128_t result = a; 00230 result.low() &= b.low(); 00231 result.high() &= b.high(); 00232 return result; 00233 } 00234 00236 inline uint128_t operator | ( const uint128_t& a, const uint128_t& b ) 00237 { 00238 uint128_t result = a; 00239 result.low() |= b.low(); 00240 result.high() |= b.high(); 00241 return result; 00242 } 00243 00252 LUNCHBOX_API uint128_t make_uint128( const char* string ); 00253 } 00254 #endif // LUNCHBOX_UINT128_H
1.3.1-git by
1.8.0