OverlayKey Class Reference

#include <OverlayKey.h>

List of all members.


Detailed Description

A common overlay key class.

Wraps common functions from Gnu MP library.

Author:
Sebastian Mies.


Public Member Functions

 OverlayKey ()
 Default constructor.
 OverlayKey (uint32_t num)
 Constructs an overlay key initialized with a common integer.
 OverlayKey (const char *str, uint radix=16)
 Constructs a key out of a string number.
 OverlayKey (const OverlayKey &rhs)
 Copy contructor.
 ~OverlayKey ()
 Default destructor.
std::string toString (uint base=16) const
 Returns a string representation of this key.
bool isUnspecified () const
 Returns true, if the key is unspecified.
bool operator< (const OverlayKey &compKey) const
 compares this to a given OverlayKey
bool operator> (const OverlayKey &compKey) const
 compares this to a given OverlayKey
bool operator<= (const OverlayKey &compKey) const
 compares this to a given OverlayKey
bool operator>= (const OverlayKey &compKey) const
 compares this to a given OverlayKey
bool operator== (const OverlayKey &compKey) const
 compares this to a given OverlayKey
bool operator!= (const OverlayKey &compKey) const
 compares this to a given OverlayKey
int compareTo (const OverlayKey &compKey) const
 Unifies all compare operations in one method.
OverlayKeyoperator= (const OverlayKey &rhs)
 assigns OverlayKey of rhs to this->key
OverlayKeyoperator-- ()
 substracts 1 from this->key
OverlayKeyoperator++ ()
 adds 1 to this->key
OverlayKeyoperator+= (const OverlayKey &rhs)
 adds rhs->key to this->key
OverlayKeyoperator-= (const OverlayKey &rhs)
 substracts rhs->key from this->key
OverlayKey operator+ (const OverlayKey &rhs) const
 adds rhs->key to this->key
OverlayKey operator- (const OverlayKey &rhs) const
 substracts rhs->key from this->key
OverlayKey operator-- (int)
 substracts 1 from this->key
OverlayKey operator++ (int)
 adds 1 to this->key
OverlayKey operator>> (uint num) const
 bitwise shift right
OverlayKey operator<< (uint num) const
 bitwise shift left
OverlayKey operator & (const OverlayKey &rhs) const
 bitwise AND of rhs->key and this->key
OverlayKey operator| (const OverlayKey &rhs) const
 bitwise OR of rhs->key and this->key
OverlayKey operator^ (const OverlayKey &rhs) const
 bitwise XOR of rhs->key and this->key
OverlayKey operator~ () const
 bitwise NOT of this->key
bool operator[] (uint n) const
 returns the n-th bit of this->key
uint32_t get (uint p, uint n) const
 Returns a sub integer at position p with n-bits.
size_t hash () const
 Returns a hash value for the key.
int log_2 () const
 Returns the position of the msb in this key, which represents just the logarithm to base 2.
OverlayKey randomSuffix (uint pos) const
 Fills the suffix starting at pos with random bits to lsb.
OverlayKey randomPrefix (uint pos) const
 Fills the prefix starting at pos with random bits to msb.
uint sharedPrefixLength (const OverlayKey &compKey) const
 Calculates the number of equal bits from the left with another Key (shared prefix length).
bool isBetween (const OverlayKey &keyA, const OverlayKey &keyB) const
 Returns true, if this key is element of the interval (keyA, keyB) on the ring.
bool isBetweenR (const OverlayKey &keyA, const OverlayKey &keyB) const
 Returns true, if this key is element of the interval (keyA, keyB] on the ring.
bool isBetweenL (const OverlayKey &keyA, const OverlayKey &keyB) const
 Returns true, if this key is element of the interval [keyA, keyB) on the ring.
bool isBetweenLR (const OverlayKey &keyA, const OverlayKey &keyB) const
 Returns true, if this key is element of the interval [keyA, keyB] on the ring.
int bitAtPlace (int step) const
void netPack (cCommBuffer *b)
 serializes the object into a buffer
void netUnpack (cCommBuffer *b)
 deserializes the object from a buffer

Static Public Member Functions

static void setKeyLength (uint length)
 Set the length of an OverlayKey.
static uint getLength ()
 Returns the length in number of bits.
static OverlayKey random ()
 Returns a random key.
static OverlayKey max ()
 Returns the maximum key, i.e.
static OverlayKey sha1 (const BinaryValue &value)
 Returns a key with the SHA1 cryptographic hash of a BinaryValue.
static OverlayKey pow2 (uint exponent)
 Returns a key 2^exponent.
static void test ()
 A pseudo regression test method.

Static Public Attributes

static const OverlayKey UNSPECIFIED_KEY
 OverlayKey without defined key.
static const OverlayKey ZERO
 OverlayKey with key initialized as 0.
static const OverlayKey ONE
 OverlayKey with key initialized as 1.

Private Member Functions

void trim ()
 trims key after key operations
void clear ()
 set this->key to 0 and isUnspec to false

Private Attributes

bool isUnspec
 is this->key unspecified?
mp_limb_t key [MAX_KEYLENGTH/(8 *sizeof(mp_limb_t))+(MAX_KEYLENGTH%(8 *sizeof(mp_limb_t))!=0?1:0)]
 the overlay key this object represents

Static Private Attributes

static const uint MAX_KEYLENGTH = 160
 maximum length of the key
static uint keyLength
 actual length of the key
static uint aSize
 number of needed machine words to hold the key
static mp_limb_t GMP_MSB_MASK
 bits to fill up if key does not exactly fit in one or more machine words

Friends

std::ostream & operator<< (std::ostream &os, const OverlayKey &c)
 Common stdc++ console output method.


Constructor & Destructor Documentation

OverlayKey::OverlayKey (  ) 

Default constructor.

Contructs an unspecified overlay key

00060 {
00061     isUnspec = true;
00062 }

OverlayKey::OverlayKey ( uint32_t  num  ) 

Constructs an overlay key initialized with a common integer.

Parameters:
num The integer to initialize this key with
00066 {
00067     clear();
00068     key[0] = num;
00069     trim();
00070 }

OverlayKey::OverlayKey ( const char *  str,
uint  radix = 16 
)

Constructs a key out of a string number.

00074 {
00075     throw "NOT IMPLEMENTED YET!!";
00076 }

OverlayKey::OverlayKey ( const OverlayKey rhs  ) 

Copy contructor.

Parameters:
rhs The key to copy.
00080 {
00081     (*this) = rhs;
00082 }

OverlayKey::~OverlayKey (  ) 

Default destructor.

Does nothing ATM.

00086 {}


Member Function Documentation

std::string OverlayKey::toString ( uint  base = 16  )  const

Returns a string representation of this key.

Returns:
String representation of this key
00123 {
00124     if (isUnspec)
00125         return std::string("<unspec>");
00126     else {
00127         char temp[256];
00128 
00129         if (base==16) {
00130             int k=0;
00131             for (int i=(keyLength-1)/4; i>=0; i--, k++)
00132                 temp[k] = HEX[this->get
00133                               (4*i,4)];
00134 
00135             temp[k] = 0;
00136             return std::string((const char*)temp);
00137         } else if (base==2) {
00138             int k=0;
00139             for (int i=keyLength-1; i>=0; i-=1, k++)
00140                 temp[k] = HEX[this->get
00141                               (i,1)];
00142             temp[k] = 0;
00143             return std::string((const char*)temp);
00144         }
00145 
00146         mp_size_t last = mpn_get_str((unsigned char*)temp, base,
00147                                      (mp_limb_t*)this->key, aSize);
00148         for (int i=0; i<last; i++) {
00149             temp[i] = HEX[temp[i]];
00150         }
00151         temp[last] = 0;
00152         return std::string((const char*)temp);
00153     }
00154 }

bool OverlayKey::isUnspecified (  )  const

Returns true, if the key is unspecified.

Returns:
Returns true, if key is unspecified
00118 {
00119     return isUnspec;
00120 }

bool OverlayKey::operator< ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is smaller than this->key, else false
00232 {
00233     return compareTo(compKey) < 0;
00234 }

bool OverlayKey::operator> ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is greater than this->key, else false
00236 {
00237     return compareTo(compKey) > 0;
00238 }

bool OverlayKey::operator<= ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is smaller than or equal to this->key, else false
00240 {
00241     return compareTo(compKey) <=0;
00242 }

bool OverlayKey::operator>= ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is greater than or equal to this->key, else false
00244 {
00245     return compareTo(compKey) >=0;
00246 }

bool OverlayKey::operator== ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is equal to this->key, else false
00248 {
00249     return compareTo(compKey) ==0;
00250 }

bool OverlayKey::operator!= ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is not equal to this->key, else false
00252 {
00253     return compareTo(compKey) !=0;
00254 }

int OverlayKey::compareTo ( const OverlayKey compKey  )  const

Unifies all compare operations in one method.

Parameters:
compKey key to compare with
Returns:
int -1 if smaller, 0 if equal, 1 if greater
00706 {
00707     if (compKey.isUnspec || isUnspec)
00708         opp_error("OverlayKey::compareTo(): key is unspecified!");
00709     return mpn_cmp(key,compKey.key,aSize);
00710 }

OverlayKey & OverlayKey::operator= ( const OverlayKey rhs  ) 

assigns OverlayKey of rhs to this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00162 {
00163     isUnspec = rhs.isUnspec;
00164     memcpy( key, rhs.key, aSize*sizeof(mp_limb_t) );
00165     return *this;
00166 }

OverlayKey & OverlayKey::operator-- (  ) 

substracts 1 from this->key

Returns:
this OverlayKey object
00170 {
00171     return (*this -= ONE);
00172 }

OverlayKey & OverlayKey::operator++ (  ) 

adds 1 to this->key

Returns:
this OverlayKey object
00184 {
00185     return (*this += ONE);
00186 }

OverlayKey & OverlayKey::operator+= ( const OverlayKey rhs  ) 

adds rhs->key to this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00198 {
00199     mpn_add_n((mp_limb_t*)key, (mp_limb_t*)key, (mp_limb_t*)rhs.key, aSize);
00200     trim();
00201     isUnspec = false;
00202     return *this;
00203 }

OverlayKey & OverlayKey::operator-= ( const OverlayKey rhs  ) 

substracts rhs->key from this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00207 {
00208     mpn_sub_n((mp_limb_t*)key, (mp_limb_t*)key, (mp_limb_t*)rhs.key, aSize);
00209     trim();
00210     isUnspec = false;
00211     return *this;
00212 }

OverlayKey OverlayKey::operator+ ( const OverlayKey rhs  )  const

adds rhs->key to this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00216 {
00217     OverlayKey result = *this;
00218     result += rhs;
00219     return result;
00220 }

OverlayKey OverlayKey::operator- ( const OverlayKey rhs  )  const

substracts rhs->key from this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00224 {
00225     OverlayKey result = *this;
00226     result -= rhs;
00227     return result;
00228 }

OverlayKey OverlayKey::operator-- ( int   ) 

substracts 1 from this->key

Returns:
this OverlayKey object
00176 {
00177     OverlayKey clone = *this;
00178     *this -= ONE;
00179     return clone;
00180 }

OverlayKey OverlayKey::operator++ ( int   ) 

adds 1 to this->key

Returns:
this OverlayKey object
00190 {
00191     OverlayKey clone = *this;
00192     *this += ONE;
00193     return clone;
00194 }

OverlayKey OverlayKey::operator>> ( uint  num  )  const

bitwise shift right

Parameters:
num number of bits to shift
Returns:
this OverlayKey object
00303 {
00304     OverlayKey result = ZERO;
00305     int i = num/GMP_LIMB_BITS;
00306 
00307     num %= GMP_LIMB_BITS;
00308 
00309     if (i>=(int)aSize)
00310         return result;
00311 
00312     for (int j=0; j<(int)aSize-i; j++) {
00313         result.key[j] = key[j+i];
00314     }
00315     mpn_rshift(result.key,result.key,aSize,num);
00316     result.isUnspec = false;
00317     result.trim();
00318 
00319     return result;
00320 }

OverlayKey OverlayKey::operator<< ( uint  num  )  const

bitwise shift left

Parameters:
num number of bits to shift
Returns:
this OverlayKey object
00324 {
00325     OverlayKey result = ZERO;
00326     int i = num/GMP_LIMB_BITS;
00327 
00328     num %= GMP_LIMB_BITS;
00329 
00330     if (i>=(int)aSize)
00331         return result;
00332 
00333     for (int j=0; j<(int)aSize-i; j++) {
00334         result.key[j+i] = key[j];
00335     }
00336     mpn_lshift(result.key,result.key,aSize,num);
00337     result.isUnspec = false;
00338     result.trim();
00339 
00340     return result;
00341 }

OverlayKey OverlayKey::operator & ( const OverlayKey rhs  )  const

bitwise AND of rhs->key and this->key

Parameters:
rhs the OverlayKey AND is calculated with
Returns:
this OverlayKey object
00280 {
00281     OverlayKey result = *this;
00282     for (uint i=0; i<aSize; i++) {
00283         result.key[i] &= rhs.key[i];
00284     }
00285 
00286     return result;
00287 }

OverlayKey OverlayKey::operator| ( const OverlayKey rhs  )  const

bitwise OR of rhs->key and this->key

Parameters:
rhs the OverlayKey OR is calculated with
Returns:
this OverlayKey object
00269 {
00270     OverlayKey result = *this;
00271     for (uint i=0; i<aSize; i++) {
00272         result.key[i] |= rhs.key[i];
00273     }
00274 
00275     return result;
00276 }

OverlayKey OverlayKey::operator^ ( const OverlayKey rhs  )  const

bitwise XOR of rhs->key and this->key

Parameters:
rhs the OverlayKey XOR is calculated with
Returns:
this OverlayKey object
00258 {
00259     OverlayKey result = *this;
00260     for (uint i=0; i<aSize; i++) {
00261         result.key[i] ^= rhs.key[i];
00262     }
00263 
00264     return result;
00265 }

OverlayKey OverlayKey::operator~ (  )  const

bitwise NOT of this->key

Returns:
this OverlayKey object
00291 {
00292     OverlayKey result = *this;
00293     for (uint i=0; i<aSize; i++) {
00294         result.key[i] = ~key[i];
00295     }
00296     result.trim();
00297 
00298     return result;
00299 }

bool OverlayKey::operator[] ( uint  n  )  const

returns the n-th bit of this->key

Parameters:
n the position of the returned bit
Returns:
the bit on position n in this->key
00345 {
00346     return (key[n / GMP_LIMB_BITS] >> (n % GMP_LIMB_BITS)) & 1;
00347 }

uint32_t OverlayKey::get ( uint  p,
uint  n 
) const

Returns a sub integer at position p with n-bits.

p is counted starting from the least significant bit of the key as bit 0. Bit p of the key becomes bit 0 of the returned integer.

Parameters:
p the position of the sub-integer
n the number of bits to be returned (max.32)
Returns:
The sub-integer.
00356     {
00357         // easy ...
00358         int i = p / GMP_LIMB_BITS, f = p % GMP_LIMB_BITS,
00359             f2 = n+f-GMP_LIMB_BITS;
00360         // ... and nasty stuff ... :)
00361 
00362         return ((key[i] >> f) | (f2>0 ? (key[i+1]<<(GMP_LIMB_BITS-f)) : 0)) &
00363                (((uint32_t)(~0))>>(GMP_LIMB_BITS-n));
00364     }

size_t OverlayKey::hash (  )  const

Returns a hash value for the key.

Returns:
size_t The hash value
00461 {
00462     return (size_t)key[0];
00463 }

int OverlayKey::log_2 (  )  const

Returns the position of the msb in this key, which represents just the logarithm to base 2.

Returns:
The logarithm to base 2 of this key.
00438 {
00439     int16_t i = aSize-1;
00440 
00441     while (i>=0 && key[i]==0) {
00442         i--;
00443     }
00444 
00445     if (i<0) {
00446         return -1;
00447     }
00448 
00449     mp_limb_t j = key[i];
00450     i *= GMP_LIMB_BITS;
00451     while (j!=0) {
00452         j >>= 1;
00453         i++;
00454     }
00455 
00456     return i-1;
00457 }

OverlayKey OverlayKey::randomSuffix ( uint  pos  )  const

Fills the suffix starting at pos with random bits to lsb.

Parameters:
pos 
Returns:
OverlayKey
00368 {
00369     OverlayKey newKey = *this;
00370     int i = pos/GMP_LIMB_BITS, j = pos%GMP_LIMB_BITS;
00371     mp_limb_t m = ((mp_limb_t)1 << j)-1;
00372     mp_limb_t rnd;
00373 
00374     //  mpn_random(&rnd,1);
00375     omnet_random(&rnd,1);
00376     newKey.key[i] &= ~m;
00377     newKey.key[i] |= (rnd&m);
00378     //  mpn_random(newKey.key,i);
00379     omnet_random(newKey.key,i);
00380     newKey.trim();
00381 
00382     return newKey;
00383 }

OverlayKey OverlayKey::randomPrefix ( uint  pos  )  const

Fills the prefix starting at pos with random bits to msb.

Parameters:
pos 
Returns:
OverlayKey
00387 {
00388     OverlayKey newKey = *this;
00389     int i = pos/GMP_LIMB_BITS, j = pos%GMP_LIMB_BITS;
00390     mp_limb_t m = ((mp_limb_t)1 << j)-1;
00391     mp_limb_t rnd;
00392 
00393     //  mpn_random(&rnd,1);
00394     omnet_random(&rnd,1);
00395 
00396     newKey.key[i] &= m;
00397     newKey.key[i] |= (rnd&~m);
00398     for (int k=aSize-1; k!=i; k--) {
00399         //        mpn_random( &newKey.key[k], 1 );
00400         omnet_random( &newKey.key[k], 1 );
00401     }
00402     newKey.trim();
00403 
00404     return newKey;
00405 }

uint OverlayKey::sharedPrefixLength ( const OverlayKey compKey  )  const

Calculates the number of equal bits from the left with another Key (shared prefix length).

Parameters:
compKey the Key to compare with
Returns:
length of shared prefix
00409 {
00410     if (compareTo(compKey) == 0) return keyLength;
00411 
00412     uint length = 0;
00413     int i;
00414     uint j;
00415     bool msb = true;
00416 
00417     // count equal limbs first:
00418     for (i=aSize-1; i>=0; --i)
00419     {
00420         if (this->key[i] != compKey.key[i])
00421         {
00422             // XOR first differing limb for easy counting of the bits:
00423             mp_limb_t d = this->key[i] ^ compKey.key[i];
00424             if (msb) d <<= ( GMP_LIMB_BITS - (keyLength % GMP_LIMB_BITS) );
00425             for (j = GMP_LIMB_BITS-1; d >>= 1; --j);
00426             length += j;
00427             break;
00428         }
00429         length += GMP_LIMB_BITS;
00430         msb = false;
00431     }
00432     
00433     return length;
00434 }

bool OverlayKey::isBetween ( const OverlayKey keyA,
const OverlayKey keyB 
) const

Returns true, if this key is element of the interval (keyA, keyB) on the ring.

Parameters:
keyA The left border of the interval
keyB The right border of the interval
Returns:
True, if the key is element of the interval (keyA, keyB)
00468 {
00469     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00470         return false;
00471 
00472     if (*this == keyA)
00473         return false;
00474     else if (keyA < keyB)
00475         return ((*this > keyA) && (*this < keyB));
00476     else
00477         return ((*this > keyA) || (*this < keyB));
00478 }

bool OverlayKey::isBetweenR ( const OverlayKey keyA,
const OverlayKey keyB 
) const

Returns true, if this key is element of the interval (keyA, keyB] on the ring.

Parameters:
keyA The left border of the interval
keyB The right border of the interval
Returns:
True, if the key is element of the interval (keyA, keyB]
00483 {
00484     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00485         return false;
00486 
00487     if ((keyA == keyB) && (*this == keyA))
00488         return true;
00489     else if (keyA <= keyB)
00490         return ((*this > keyA) && (*this <= keyB));
00491     else
00492         return ((*this > keyA) || (*this <= keyB));
00493 }

bool OverlayKey::isBetweenL ( const OverlayKey keyA,
const OverlayKey keyB 
) const

Returns true, if this key is element of the interval [keyA, keyB) on the ring.

Parameters:
keyA The left border of the interval
keyB The right border of the interval
Returns:
True, if the key is element of the interval [keyA, keyB)
00498 {
00499     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00500         return false;
00501 
00502     if ((keyA == keyB) && (*this == keyA))
00503         return true;
00504     else if (keyA <= keyB)
00505         return ((*this >= keyA) && (*this < keyB));
00506     else
00507         return ((*this >= keyA) || (*this < keyB));
00508 }

bool OverlayKey::isBetweenLR ( const OverlayKey keyA,
const OverlayKey keyB 
) const

Returns true, if this key is element of the interval [keyA, keyB] on the ring.

Parameters:
keyA The left border of the interval
keyB The right border of the interval
Returns:
True, if the key is element of the interval [keyA, keyB]
00513 {
00514     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00515         return false;
00516 
00517     if ((keyA == keyB) && (*this == keyA))
00518         return true;
00519     else if (keyA <= keyB)
00520         return ((*this >= keyA) && (*this <= keyB));
00521     else
00522         return ((*this >= keyA) || (*this <= keyB));
00523 }

void OverlayKey::setKeyLength ( uint  length  )  [static]

Set the length of an OverlayKey.

Parameters:
length keylength in bits
00093 {
00094     if ((length < 1) || (length > OverlayKey::keyLength)) {
00095         opp_error("OverlayKey::setKeyLength(): length must be <= %i "
00096                   "and setKeyLength() must not be called twice "
00097                   "with different length!", MAX_KEYLENGTH);
00098     }
00099 
00100     keyLength = length;
00101 
00102     aSize = keyLength / (8*sizeof(mp_limb_t)) +
00103             (keyLength % (8*sizeof(mp_limb_t))!=0 ? 1 : 0);
00104 
00105     GMP_MSB_MASK = (keyLength % GMP_LIMB_BITS)
00106         != 0 ? (((mp_limb_t)1 << (keyLength % GMP_LIMB_BITS))-1)
00107         : (mp_limb_t)-1;
00108 }

uint OverlayKey::getLength (  )  [static]

Returns the length in number of bits.

Returns:
The length in number of bits.
00113 {
00114     return OverlayKey::keyLength;
00115 }

OverlayKey OverlayKey::random (  )  [static]

Returns a random key.

Returns:
A random key.
00553 {
00554     OverlayKey newKey = ZERO;
00555 
00556     //    mpn_random(newKey.key,aSize);
00557     omnet_random(newKey.key,aSize);
00558 
00559     newKey.trim();
00560 
00561     return newKey;
00562 }

OverlayKey OverlayKey::max (  )  [static]

Returns the maximum key, i.e.

a key filled with bit 1

Returns:
The maximum key, i.e. a key filled with bit 1
00539 {
00540     OverlayKey newKey;
00541 
00542     for (uint i=0; i<aSize; i++) {
00543         newKey.key[i] = ~0;
00544     }
00545     newKey.isUnspec = false;
00546     newKey.trim();
00547 
00548     return newKey;
00549 }

OverlayKey OverlayKey::sha1 ( const BinaryValue value  )  [static]

Returns a key with the SHA1 cryptographic hash of a BinaryValue.

Parameters:
value A BinaryValue object.
Returns:
SHA1 of value
00566 {
00567     OverlayKey newKey = OverlayKey();
00568     uint8_t temp[20];
00569     CSHA1 sha1;
00570 
00571     sha1.Reset();
00572     sha1.Update((uint8_t*)(&(*input.begin())), input.size());
00573     sha1.Final();
00574     sha1.GetHash(temp);
00575     mpn_set_str(newKey.key, (const uint8_t*)temp,
00576                 (int)min(aSize * sizeof(mp_limb_t), 20), 256);
00577     newKey.isUnspec = false;
00578     newKey.trim();
00579 
00580     return newKey;
00581 }

OverlayKey OverlayKey::pow2 ( uint  exponent  )  [static]

Returns a key 2^exponent.

Parameters:
exponent The exponent.
Returns:
Key=2^exponent.
00585 {
00586     OverlayKey newKey = ZERO;
00587 
00588     newKey.key[exponent/GMP_LIMB_BITS] =
00589         (mp_limb_t)1 << (exponent % GMP_LIMB_BITS);
00590 
00591     return newKey;
00592 }

void OverlayKey::test (  )  [static]

A pseudo regression test method.

Outputs report to standard output.

00596 {
00597     // add test
00598     cout << endl << "--- Add test ..." << endl;
00599     OverlayKey key = 123456789;
00600     cout << "    key=" << key << endl;
00601     cout << "    key += 987654321 = " << (key+=987654321) << endl;
00602     cout << "    prefix++  : " << (++key) << endl;
00603     cout << "    postfix++ : " << (key++) << endl;
00604     cout << "    key=" << key << endl;
00605 
00606     OverlayKey k1 = 256, k2 = 10, k3 = 3;
00607 
00608     // compare test
00609     cout << endl << "--- Compare test ..." << endl;
00610     cout << "    256 < 10 = "<< (k1 < k2) << " k1="<<k1<<endl;
00611     cout << "    256 > 10 = "<< (k1 > k2) << " k2="<<k2<<endl;
00612 
00613     cout << "    10 isBetween(3, 256)=" << k2.isBetween(k3, k1) << endl;
00614     cout << "    3 isBetween(10, 256)=" << k3.isBetween(k2, k1) << endl;
00615     cout << "    256 isBetween(10, 256)=" << k1.isBetween(k2, k1) << endl;
00616     cout << "    256 isBetweenR(10, 256)=" << k1.isBetweenR(k2, k1) << endl;
00617     cout << "    max isBetween(max-1,0)=" << OverlayKey::max().isBetween(
00618         OverlayKey::max()-1, OverlayKey::ZERO) << endl;
00619     cout << "    max-1 isBetween(max,1)=" << (OverlayKey::max()-1).isBetween(
00620         OverlayKey::max(), OverlayKey::ONE) << endl;
00621     cout << "    max-1 isBetweenL(max-1,1)=" << (OverlayKey::max()-1).
00622     isBetweenL(OverlayKey::max()-1, OverlayKey::ONE) << endl;
00623     cout << "    1 isBetweenL(max-1,1)=" << (OverlayKey::ONE).isBetweenL(
00624         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00625     cout << "    1 isBetweenR(max-1,1)=" << OverlayKey::ONE.isBetweenR(
00626         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00627     cout << "    1 isBetween(max-1,1)=" << OverlayKey::ONE.isBetween(
00628         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00629     cout << "    1 isBetween(max-1,0)=" << OverlayKey::ONE.isBetween(
00630         OverlayKey::max()-1, OverlayKey::ZERO) << endl;
00631     cout << "    256 sharedPrefixLength(3)=" << k1.sharedPrefixLength(k3)
00632         << endl;
00633     cout << "    256 sharedPrefixLength(256)=" << k1.sharedPrefixLength(k1)
00634         << endl;
00635 
00636     // wrap around test
00637     cout << endl << "--- Warp around test ..." << endl;
00638 
00639     k1 = OverlayKey::max();
00640     cout << "k1=max= " << k1.toString(16) << endl;
00641     cout << "k1+1 = " << (k1 + 1).toString(16) << endl;
00642     cout << "k1+2 = " << (k1 + 2).toString(16) << endl;
00643 
00644     k1 = OverlayKey::ZERO;
00645     cout << "k1=0= " << k1.toString(16) << endl;
00646     cout << "k1-1 = " << (k1 - 1).toString(16) << endl;
00647     cout << "k1-2 = " << (k1 - 2).toString(16) << endl;
00648 
00649     cout << "max > ONE=" << (OverlayKey::max() > OverlayKey::ONE) << endl;
00650     cout << "max < ONE=" << (OverlayKey::max() < OverlayKey::ONE) << endl;
00651 
00652 
00653     // suffix and log2 test
00654     cout << endl << "--- RandomSuffix and log2 test ..." << endl;
00655     k1 = OverlayKey::ZERO;
00656     for (uint i=0; i<k1.getLength(); i++) {
00657         k2=k1.randomSuffix(i);
00658         cout << "    " << k2.toString(16) << " log2=" << k2.log_2() << endl;
00659     }
00660     cout << endl << "--- RandomPrefix and log2 test ..." << endl;
00661     k1 = OverlayKey::max();
00662     for (uint i=0; i<k1.getLength(); i++) {
00663         k2=k1.randomPrefix(i);
00664         cout << "    " << k2.toString(16) << " log2=" << k2.log_2() << endl;
00665     }
00666 
00667     cout << endl << "--- pow2 test..." << endl;
00668     for (uint i=0; i<k1.getLength(); i++) {
00669         k2=pow2(i);
00670         cout << " 2^" << i << " = " << k2.toString(16) << "   log2="
00671         << k2.log_2() << endl;
00672     }
00673 
00674     cout << endl << "--- Bits test ..." << endl << "    ";
00675     const char* BITS[] = { "000","001","010","011","100","101","110","111"
00676                          };
00677     k1 = OverlayKey::random();
00678     for (int i=k1.getLength()-1; i>=0; i--)
00679         cout << k1[i];
00680     cout << " = " << endl << "    ";
00681     for (int i=k1.getLength()-3; i>=0; i-=3)
00682         cout << BITS[k1.get(i,3)];
00683     cout << endl;
00684 
00685     cout << endl << "--- SHA1 test ... (verified with test vectors)" << endl;
00686     cout << "    Empty string: " << OverlayKey::sha1("").toString(16)
00687     << " = da39a3ee5e6b4b0d3255bfef95601890afd80709" << endl;
00688     cout << "    'Hello World' string: "
00689     << OverlayKey::sha1("Hello World").toString(16)
00690     << " = 0a4d55a8d778e5022fab701977c5d840bbc486d0" << endl;
00691 }

int OverlayKey::bitAtPlace ( int  step  )  const

Todo:
get rid of this function (Koorde)
00724 {
00725   if ((unsigned int)step > keyLength)
00726   {
00727     //cout << "Bit not in Key!" << endl;
00728     return 0;
00729   }
00730   return ((*this).get((*this).getLength()  - step, 1));
00731 }

void OverlayKey::trim (  )  [inline, private]

trims key after key operations

00699 {
00700     key[aSize-1] &= GMP_MSB_MASK;
00701 }

void OverlayKey::clear (  )  [inline, private]

set this->key to 0 and isUnspec to false

00714 {
00715     memset( key, 0, aSize * sizeof(mp_limb_t) );
00716     isUnspec = false;
00717 }

void OverlayKey::netPack ( cCommBuffer *  b  ) 

serializes the object into a buffer

Parameters:
b the buffer
00795 {
00796     doPacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00797             (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0));
00798     doPacking(b,this->isUnspec);
00799 }

void OverlayKey::netUnpack ( cCommBuffer *  b  ) 

deserializes the object from a buffer

Parameters:
b the buffer
00802 {
00803     doUnpacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00804               (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0));
00805     doUnpacking(b,this->isUnspec);
00806 
00807 }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const OverlayKey c 
) [friend]

Common stdc++ console output method.

00532 {
00533     os << c.toString(16);
00534     return os;
00535 };


Member Data Documentation

const OverlayKey OverlayKey::UNSPECIFIED_KEY [static]

OverlayKey without defined key.

const OverlayKey OverlayKey::ZERO [static]

OverlayKey with key initialized as 0.

const OverlayKey OverlayKey::ONE [static]

OverlayKey with key initialized as 1.

const uint OverlayKey::MAX_KEYLENGTH = 160 [static, private]

maximum length of the key

uint OverlayKey::keyLength [static, private]

actual length of the key

uint OverlayKey::aSize [static, private]

number of needed machine words to hold the key

mp_limb_t OverlayKey::GMP_MSB_MASK [static, private]

bits to fill up if key does not exactly fit in one or more machine words

bool OverlayKey::isUnspec [private]

is this->key unspecified?

mp_limb_t OverlayKey::key[MAX_KEYLENGTH/(8 *sizeof(mp_limb_t))+(MAX_KEYLENGTH%(8 *sizeof(mp_limb_t))!=0?1:0)] [private]

the overlay key this object represents


The documentation for this class was generated from the following files:
Generated on Tue Jul 24 16:51:19 2007 for ITM OverSim by  doxygen 1.5.1