Lunchbox  1.4.0
uint128_t.h
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 {
00036     class uint128_t;
00037     std::ostream& operator << ( std::ostream& os, const uint128_t& id );
00038 
00040     class uint128_t
00041     {
00042     public:
00047         uint128_t( const uint64_t low_ = 0 ) 
00048             : _high( 0 ), _low( low_ ) {}
00049 
00054         explicit uint128_t( const uint64_t high_, const uint64_t low_ ) 
00055             : _high( high_ ), _low( low_ ) {}
00056 
00061         explicit uint128_t( const std::string& string ) 
00062             : _high( 0 ), _low( 0 ) { *this = string; }
00063 
00065         uint128_t& operator = ( const uint128_t& rhs )
00066             {
00067                 _high = rhs._high;
00068                 _low = rhs._low;
00069                 return *this;
00070             }
00071 
00073         uint128_t& operator = ( const uint64_t rhs )
00074             {
00075                 _high = 0;
00076                 _low = rhs;
00077                 return *this;
00078             }
00079 
00081         LUNCHBOX_API uint128_t& operator = ( const std::string& from );
00082 
00087         bool operator == ( const uint128_t& rhs ) const
00088             { return _high == rhs._high && _low == rhs._low; }
00089 
00094         bool operator != ( const uint128_t& rhs ) const
00095             { return _high != rhs._high || _low != rhs._low; }
00096 
00101         bool operator < ( const uint128_t& rhs ) const
00102             { 
00103                 if( _high < rhs._high )
00104                     return true;
00105                 if( _high > rhs._high )
00106                     return false;
00107                 return _low < rhs._low; 
00108             }
00109 
00114         bool operator > ( const uint128_t& rhs ) const
00115             { 
00116                 if( _high > rhs._high )
00117                     return true;
00118                 if( _high < rhs._high )
00119                     return false;
00120                 return _low > rhs._low; 
00121             }
00122 
00128         bool operator <= ( const uint128_t& rhs ) const
00129             { 
00130                 if( _high < rhs._high )
00131                     return true;
00132                 if( _high > rhs._high )
00133                     return false;
00134                 return _low <= rhs._low; 
00135             }
00136 
00142         bool operator >= ( const uint128_t& rhs ) const
00143             { 
00144                 if( _high > rhs._high )
00145                     return true;
00146                 if( _high < rhs._high )
00147                     return false;
00148                 return _low >= rhs._low; 
00149             }
00150 
00152         uint128_t& operator ++()
00153             { 
00154                 ++_low;
00155                 if( !_low )
00156                     ++_high;
00157                 
00158                 return *this;
00159             }
00160         
00162         uint128_t& operator --()
00163             { 
00164                 if( !_low )
00165                     --_high;
00166                 --_low;
00167                 return *this;
00168             }
00169 
00171         const uint64_t& low() const { return _low; }
00173         const uint64_t& high() const { return _high; }
00174 
00176         uint64_t& low() { return _low; }
00178         uint64_t& high() { return _high; }
00179 
00181         std::string getShortString() const
00182             {
00183                 std::stringstream stream;
00184                 stream << std::hex << _high << _low;
00185                 const std::string str = stream.str();
00186                 return str.substr( 0, 3 ) + ".." +
00187                     str.substr( str.length() - 3, std::string::npos );
00188             }
00189 
00191         std::string getString() const
00192             {
00193                 std::stringstream stream;
00194                 stream << *this;
00195                 return stream.str();
00196             }
00197 
00199         template< class Archive >
00200         void serialize( Archive& ar, const unsigned int version )
00201         {
00202             ar & low();
00203             ar & high();
00204         }
00205 
00207         static LUNCHBOX_API const uint128_t ZERO;
00208 
00209     private:
00210         uint64_t _high;
00211         uint64_t _low;
00212     };
00213 
00215     inline std::ostream& operator << ( std::ostream& os, const uint128_t& id )
00216     {
00217         if( id.high() == 0 )
00218             os << std::hex << id.low() << std::dec;
00219         else
00220             os << std::hex << id.high() << ':' << id.low() << std::dec;
00221         return os;
00222     }
00223 
00225     inline uint128_t operator+ ( const uint128_t& a, const uint64_t& b ) 
00226     {
00227         uint128_t result = a;
00228         result.low() += b;
00229         if( result.low() < a.low( ))
00230             ++result.high();
00231         return result;
00232     };
00233 
00235     inline uint128_t operator- ( const uint128_t& a, const uint64_t& b ) 
00236     {
00237         uint128_t result = a;
00238         result.low() -= b;
00239         if( result.low() > a.low( ))
00240             --result.high();
00241         return result;
00242     };
00243 
00245     inline uint128_t operator & ( const uint128_t& a, const uint128_t& b )
00246     {
00247         uint128_t result = a;
00248         result.low() &= b.low();
00249         result.high() &= b.high();
00250         return result;
00251     }
00252 
00254     inline uint128_t operator | ( const uint128_t& a, const uint128_t& b )
00255     {
00256         uint128_t result = a;
00257         result.low() |= b.low();
00258         result.high() |= b.high();
00259         return result;
00260     }
00261 
00270      LUNCHBOX_API uint128_t make_uint128( const char* string );
00271 }
00272 #endif // LUNCHBOX_UINT128_H