Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lunchbox::uint128_t Class Reference

A base type for 128 bit unsigned integer values. More...

#include <uint128_t.h>

+ Collaboration diagram for lunchbox::uint128_t:

Public Member Functions

 uint128_t (const unsigned long long low_=0)
 Construct a new 128 bit integer with a default value. More...
 
 uint128_t (const unsigned long low_)
 Construct a new 128 bit integer with a default value. More...
 
 uint128_t (const int low_)
 Construct a new 128 bit integer with a default value. More...
 
 uint128_t (const uint64_t high_, const uint64_t low_)
 Construct a new 128 bit integer with default values. More...
 
 uint128_t (const std::string &string)
 Construct a new 128 bit integer from a string representation. More...
 
bool isUUID () const
 
uint128_toperator= (const lunchbox::uint128_t &rhs)
 Assign another 128 bit value. More...
 
uint128_toperator= (const uint64_t rhs)
 Assign another 64 bit value. More...
 
uint128_toperator= (const int rhs)
 Assign an integer value. More...
 
LUNCHBOX_API uint128_toperator= (const std::string &from)
 Assign an 128 bit value from a std::string. More...
 
bool operator== (const lunchbox::uint128_t &rhs) const
 
bool operator!= (const lunchbox::uint128_t &rhs) const
 
bool operator== (const unsigned long long &low_) const
 
bool operator!= (const unsigned long long &low_) const
 
bool operator< (const lunchbox::uint128_t &rhs) const
 
bool operator> (const lunchbox::uint128_t &rhs) const
 
bool operator<= (const lunchbox::uint128_t &rhs) const
 
bool operator>= (const lunchbox::uint128_t &rhs) const
 
uint128_toperator++ ()
 Increment the value. More...
 
uint128_toperator-- ()
 Decrement the value. More...
 
uint128_toperator+= (const lunchbox::uint128_t &rhs)
 Add value and return the new value. More...
 
const uint64_t & low () const
 
const uint64_t & high () const
 
uint64_t & low ()
 
uint64_t & high ()
 
std::string getShortString () const
 
std::string getString () const
 
template<class Archive >
void serialize (Archive &ar, const unsigned int)
 Serialize this object to a boost archive. More...
 

Detailed Description

A base type for 128 bit unsigned integer values.

Example:

/* Copyright (c) 2010-2014, Stefan Eilemann <eile@equalizergraphics.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// Tests the functionality of universally unique identifiers and 128 bit ints
#include <test.h>
#include <lunchbox/clock.h>
#include <lunchbox/init.h>
#include <lunchbox/rng.h>
#include <lunchbox/thread.h>
#include <lunchbox/uint128_t.h>
#define N_UUIDS 10000
#define N_THREADS 10
typedef stde::hash_map< lunchbox::uint128_t, bool > TestHash;
void testConvertUint128ToUUID();
void testIncrement();
class Thread : public lunchbox::Thread
{
public:
virtual void run()
{
size_t i = N_UUIDS;
while( i-- )
{
TEST( uuid.isUUID( ));
TESTINFO( hash.find( uuid ) == hash.end(),
"Iteration " << N_UUIDS - i );
hash[ uuid ] = true;
}
}
TestHash hash;
};
int main( int argc, char **argv )
{
TEST( lunchbox::init( argc, argv ));
// basic tests
TEST( id1 != lunchbox::uint128_t( ));
TEST( id1 != id2 );
TEST( id1.isUUID( ));
TEST( !id2.isUUID( ));
TEST( id1 != id2 );
TEST( id2.isUUID( ));
id1 = id2;
TEST( id1 == id2 );
TEST( id1 == *id3 );
TEST( *id4 != *id3 );
*id4 = *id3;
TEST( *id4 == *id3 );
delete id3;
delete id4;
TEST( id5 == lunchbox::uint128_t( ));
TEST( id5 == id6 );
"The quick brown fox jumps over the lazy dog." );
// Values from http://en.wikipedia.org/wiki/MD5#MD5_hashes
TEST( empty != fox );
TESTINFO( empty == lunchbox::uint128_t( 0xD41D8CD98F00B204ull,
0xE9800998ECF8427Eull ),
empty );
TESTINFO( fox == lunchbox::uint128_t( 0xE4D909C290D0FB1Cull,
0xA068FFADDF22CBD0ull ),
fox );
const uint16_t high = rng.get< uint16_t >();
const int32_t low = rng.get< int32_t >();
id6 = lunchbox::uint128_t( high, low );
TEST( id6.high() == high );
TEST( id6.low() == uint64_t( low ));
id6 = lunchbox::uint128_t( low );
TEST( id6.high() == 0 );
TEST( id6.low() == uint64_t( low ));
id6 = std::string( "0xD41D8CD98F00B204" );
TEST( id6.high() == 0 );
TEST( id6.low() == 0xD41D8CD98F00B204ull );
id6 = std::string( "0xD41D8CD98F00B204:0xE9800998ECF8427E" );
TESTINFO( id6.high() == 0xD41D8CD98F00B204ull, id6 );
TEST( id6.low() == 0xE9800998ECF8427Eull );
// Load tests
Thread threads[ N_THREADS ];
for( size_t i = 0; i < N_THREADS; ++i )
threads[ i ].start();
for( size_t i = 0; i < N_THREADS; ++i )
threads[ i ].join();
LBINFO << N_UUIDS * N_THREADS /clock.getTimef()
<< " UUID generations and hash ops / ms" << std::endl;
TestHash& first = threads[0].hash;
for( size_t i = 1; i < N_THREADS; ++i )
{
TestHash& current = threads[i].hash;
for( TestHash::const_iterator j = current.begin();
j != current.end(); ++j )
{
lunchbox::uint128_t uuid( j->first );
TESTINFO( uuid == j->first, j->first << " = " << uuid );
std::ostringstream stream;
stream << uuid;
uuid = stream.str();
TESTINFO( uuid == j->first,
j->first << " -> " << stream.str() << " -> " << uuid );
TEST( first.find( uuid ) == first.end( ));
first[ uuid ] = true;
}
}
testConvertUint128ToUUID();
testIncrement();
TEST( lunchbox::exit( ));
return EXIT_SUCCESS;
}
void testConvertUint128ToUUID()
{
const uint64_t low = 1212;
const uint64_t high = 2314;
lunchbox::uint128_t test128( high, low );
TEST( test128.low() == low && test128.high() == high );
testUUID = test128;
const lunchbox::uint128_t compare128 = testUUID;
TEST( compare128 == test128 );
}
void testIncrement()
{
{
lunchbox::uint128_t test128( 0, 0 );
++test128;
TEST( test128.high() == 0 && test128.low() == 1 );
--test128;
TEST( test128.high() == 0 && test128.low() == 0 );
test128 = test128 + 1;
TEST( test128.high() == 0 && test128.low() == 1 );
test128 = test128 - 1;
TEST( test128.high() == 0 && test128.low() == 0 );
}
{
lunchbox::uint128_t test128( 0, std::numeric_limits<uint64_t>::max() );
++test128;
TEST( test128.high() == 1 && test128.low() == 0 );
--test128;
TEST( test128.high() == 0 &&
test128.low() == std::numeric_limits< uint64_t >::max() );
test128 = test128 + 1;
TEST( test128.high() == 1 && test128.low() == 0 );
test128 = test128 - 1;
TEST( test128.high() == 0 &&
test128.low() == std::numeric_limits< uint64_t >::max() );
}
{
lunchbox::uint128_t test128( 0, 0 );
++test128;
TEST( test128.high() == 0 && test128.low() == 1 );
--test128;
TEST( test128.high() == 0 && test128.low() == 0 );
test128 = test128 + 1;
TEST( test128.high() == 0 && test128.low() == 1 );
test128 = test128 - 1;
TEST( test128.high() == 0 && test128.low() == 0 );
}
{
lunchbox::uint128_t test128( 0, std::numeric_limits< uint64_t >::max() );
++test128;
TEST( test128.high() == 1 && test128.low() == 0 );
--test128;
TEST( test128.high() == 0 &&
test128.low() == std::numeric_limits< uint64_t >::max() );
test128 = test128 + 1;
TEST( test128.high() == 1 && test128.low() == 0 );
test128 = test128 - 1;
TEST( test128.high() == 0 &&
test128.low() == std::numeric_limits< uint64_t >::max() );
}
}

Definition at line 46 of file uint128_t.h.

Constructor & Destructor Documentation

lunchbox::uint128_t::uint128_t ( const unsigned long long  low_ = 0)
inlineexplicit

Construct a new 128 bit integer with a default value.

Version
1.0

Definition at line 53 of file uint128_t.h.

Referenced by operator!=(), and operator==().

+ Here is the caller graph for this function:

lunchbox::uint128_t::uint128_t ( const unsigned long  low_)
inlineexplicit

Construct a new 128 bit integer with a default value.

Version
1.9.1

Definition at line 60 of file uint128_t.h.

lunchbox::uint128_t::uint128_t ( const int  low_)
inlineexplicit

Construct a new 128 bit integer with a default value.

Version
1.9.1

Definition at line 66 of file uint128_t.h.

lunchbox::uint128_t::uint128_t ( const uint64_t  high_,
const uint64_t  low_ 
)
inline

Construct a new 128 bit integer with default values.

Version
1.0

Definition at line 84 of file uint128_t.h.

lunchbox::uint128_t::uint128_t ( const std::string &  string)
inlineexplicit

Construct a new 128 bit integer from a string representation.

Version
1.3.2

Definition at line 91 of file uint128_t.h.

Member Function Documentation

std::string lunchbox::uint128_t::getShortString ( ) const
inline
Returns
a short, but not necessarily unique, string of the value.

Definition at line 252 of file uint128_t.h.

std::string lunchbox::uint128_t::getString ( ) const
inline
Returns
the full string representation of the value.

Definition at line 262 of file uint128_t.h.

const uint64_t& lunchbox::uint128_t::high ( ) const
inline
Returns
the reference to the high 64 bits of this 128 bit value.

Definition at line 244 of file uint128_t.h.

Referenced by isUUID(), lunchbox::operator&(), lunchbox::operator+(), lunchbox::operator-(), lunchbox::operator|(), and serialize().

+ Here is the caller graph for this function:

uint64_t& lunchbox::uint128_t::high ( )
inline
Returns
the reference to the high 64 bits of this 128 bit value.

Definition at line 249 of file uint128_t.h.

bool lunchbox::uint128_t::isUUID ( ) const
inline
Returns
true if the uint128_t is a generated universally unique identifier.
Version
1.9.1

Definition at line 99 of file uint128_t.h.

References high().

+ Here is the call graph for this function:

const uint64_t& lunchbox::uint128_t::low ( ) const
inline
Returns
the reference to the lower 64 bits of this 128 bit value.

Definition at line 242 of file uint128_t.h.

Referenced by lunchbox::operator&(), lunchbox::operator+(), lunchbox::operator-(), lunchbox::operator|(), and serialize().

+ Here is the caller graph for this function:

uint64_t& lunchbox::uint128_t::low ( )
inline
Returns
the reference to the lower 64 bits of this 128 bit value.

Definition at line 247 of file uint128_t.h.

bool lunchbox::uint128_t::operator!= ( const lunchbox::uint128_t rhs) const
inline
Returns
true if the values are different, false otherwise.
Version
1.0

Definition at line 139 of file uint128_t.h.

bool lunchbox::uint128_t::operator!= ( const unsigned long long &  low_) const
inline
Returns
true if the values are different, false otherwise.
Version
1.9.1

Definition at line 153 of file uint128_t.h.

References uint128_t().

+ Here is the call graph for this function:

uint128_t& lunchbox::uint128_t::operator++ ( void  )
inline

Increment the value.

Version
1.0

Definition at line 211 of file uint128_t.h.

uint128_t& lunchbox::uint128_t::operator+= ( const lunchbox::uint128_t rhs)
inline

Add value and return the new value.

Version
1.5.1

Definition at line 230 of file uint128_t.h.

uint128_t& lunchbox::uint128_t::operator-- ( void  )
inline

Decrement the value.

Version
1.0

Definition at line 221 of file uint128_t.h.

bool lunchbox::uint128_t::operator< ( const lunchbox::uint128_t rhs) const
inline
Returns
true if this value is smaller than the RHS value.
Version
1.0

Definition at line 160 of file uint128_t.h.

bool lunchbox::uint128_t::operator<= ( const lunchbox::uint128_t rhs) const
inline
Returns
true if this value is smaller or equal than the RHS value.
Version
1.0

Definition at line 187 of file uint128_t.h.

uint128_t& lunchbox::uint128_t::operator= ( const lunchbox::uint128_t rhs)
inline

Assign another 128 bit value.

Version
1.0

Definition at line 102 of file uint128_t.h.

uint128_t& lunchbox::uint128_t::operator= ( const uint64_t  rhs)
inline

Assign another 64 bit value.

Version
1.1.1

Definition at line 110 of file uint128_t.h.

uint128_t& lunchbox::uint128_t::operator= ( const int  rhs)
inline

Assign an integer value.

Version
1.7.1

Definition at line 118 of file uint128_t.h.

LUNCHBOX_API uint128_t& lunchbox::uint128_t::operator= ( const std::string &  from)

Assign an 128 bit value from a std::string.

Version
1.0
bool lunchbox::uint128_t::operator== ( const lunchbox::uint128_t rhs) const
inline
Returns
true if the values are equal, false if not.
Version
1.0

Definition at line 132 of file uint128_t.h.

bool lunchbox::uint128_t::operator== ( const unsigned long long &  low_) const
inline
Returns
true if the values are equal, false otherwise.
Version
1.9.1

Definition at line 146 of file uint128_t.h.

References uint128_t().

+ Here is the call graph for this function:

bool lunchbox::uint128_t::operator> ( const lunchbox::uint128_t rhs) const
inline
Returns
true if this value is bigger than the rhs value.
Version
1.0

Definition at line 173 of file uint128_t.h.

bool lunchbox::uint128_t::operator>= ( const lunchbox::uint128_t rhs) const
inline
Returns
true if this value is smaller or equal than the RHS value.
Version
1.0

Definition at line 201 of file uint128_t.h.

template<class Archive >
void lunchbox::uint128_t::serialize ( Archive &  ar,
const unsigned  int 
)
inline

Serialize this object to a boost archive.

Version
1.3.1

Definition at line 271 of file uint128_t.h.

References high(), and low().

+ Here is the call graph for this function:


The documentation for this class was generated from the following file: