OverlayKey.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #ifndef __OVERLAYKEY_H_
00025 #define __OVERLAYKEY_H_
00026
00027 #include <gmp.h>
00028 #include <sys/types.h>
00029
00030 class BinaryValue;
00031 class OverlayKeyBit;
00032 class cCommBuffer;
00033
00037 inline void omnet_random(mp_limb_t *r1p, mp_size_t r1n);
00038
00046 class OverlayKey
00047 {
00048 public:
00049
00050
00051
00052
00053 static const OverlayKey UNSPECIFIED_KEY;
00054 static const OverlayKey ZERO;
00055 static const OverlayKey ONE;
00057
00058
00059
00060
00066 OverlayKey();
00067
00073 OverlayKey( uint32_t num );
00074
00080 OverlayKey( const unsigned char* buffer, uint32_t size);
00081
00085 OverlayKey( const std::string& str, uint32_t base = 16 );
00086
00092 OverlayKey( const OverlayKey& rhs );
00093
00099 ~OverlayKey();
00100
00101
00102
00103
00104
00105
00106
00112 std::string toString( uint32_t base = 16 ) const;
00113
00117 friend std::ostream& operator<<(std::ostream& os, const OverlayKey& c);
00118
00124 bool isUnspecified() const;
00125
00126
00127
00128
00129
00136 bool operator< ( const OverlayKey& compKey ) const;
00137
00144 bool operator> ( const OverlayKey& compKey ) const;
00145
00152 bool operator<=( const OverlayKey& compKey ) const;
00153
00160 bool operator>=( const OverlayKey& compKey ) const;
00161
00168 bool operator==( const OverlayKey& compKey ) const;
00169
00176 bool operator!=( const OverlayKey& compKey ) const;
00177
00184 int compareTo( const OverlayKey& compKey ) const;
00185
00192 OverlayKey& operator= ( const OverlayKey& rhs );
00193
00199 OverlayKey& operator--();
00200
00206 OverlayKey& operator++();
00207
00214 OverlayKey& operator+=( const OverlayKey& rhs );
00215
00222 OverlayKey& operator-=( const OverlayKey& rhs );
00223
00230 OverlayKey operator+ ( const OverlayKey& rhs ) const;
00231
00238 OverlayKey operator- ( const OverlayKey& rhs ) const;
00239
00245 OverlayKey operator--( int );
00246
00252 OverlayKey operator++( int );
00253
00260 OverlayKey operator>>( uint32_t num ) const;
00261
00268 OverlayKey operator<<( uint32_t num ) const;
00269
00276 OverlayKey operator& ( const OverlayKey& rhs ) const;
00277
00284 OverlayKey operator| ( const OverlayKey& rhs ) const;
00285
00292 OverlayKey operator^ ( const OverlayKey& rhs ) const;
00293
00299 OverlayKey operator~ () const;
00300
00307 OverlayKeyBit operator[]( uint32_t n );
00308
00316 OverlayKey& setBit(uint32_t pos, bool value);
00317
00318
00319
00320
00321
00331 uint32_t getBitRange(uint32_t p, uint32_t n) const;
00332
00333 inline bool getBit(uint32_t p) const
00334 {
00335 return getBitRange(p, 1);
00336 };
00337
00343 size_t hash() const;
00344
00351 int log_2() const;
00352
00359 OverlayKey randomSuffix(uint32_t pos) const;
00360
00367 OverlayKey randomPrefix(uint32_t pos) const;
00368
00377 uint32_t sharedPrefixLength(const OverlayKey& compKey,
00378 uint32_t bitsPerDigit = 1) const;
00379
00388 bool isBetween(const OverlayKey& keyA, const OverlayKey& keyB) const;
00389
00398 bool isBetweenR(const OverlayKey& keyA, const OverlayKey& keyB) const;
00399
00408 bool isBetweenL(const OverlayKey& keyA, const OverlayKey& keyB) const;
00409
00418 bool isBetweenLR(const OverlayKey& keyA, const OverlayKey& keyB) const;
00419
00420
00421
00422
00423
00429 static void setKeyLength(uint32_t length);
00430
00436 static uint32_t getLength();
00437
00443 static OverlayKey random();
00444
00450 static OverlayKey getMax();
00451
00459 static OverlayKey sha1(const BinaryValue& value);
00460
00467 static OverlayKey pow2(uint32_t exponent);
00468
00473 static void test();
00474
00475 private:
00476
00477
00478 static const uint32_t MAX_KEYLENGTH = 160;
00479 static uint32_t keyLength;
00480 static uint32_t aSize;
00481 static mp_limb_t GMP_MSB_MASK;
00484
00485 bool isUnspec;
00487 mp_limb_t key[MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00488 (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0)];
00492
00496 void trim();
00497
00501 void clear();
00502
00503 public:
00504
00510 void netPack(cCommBuffer *b);
00511
00517 void netUnpack(cCommBuffer *b);
00518 };
00519
00525 class OverlayKeyBit
00526 {
00527 public:
00528
00529 OverlayKeyBit(bool value, uint32_t pos, OverlayKey* key)
00530 : bit(value), pos(pos), key(key)
00531 {};
00532
00534 inline operator bool()
00535 {
00536 return bit;
00537 };
00538
00539 inline OverlayKeyBit& operator=(const OverlayKeyBit& value)
00540 {
00541 key->setBit(pos, value.bit);
00542 return *this;
00543 };
00544
00548 inline OverlayKeyBit& operator=(bool value)
00549 {
00550 key->setBit(pos, value);
00551 return *this;
00552 };
00553
00554 inline OverlayKeyBit& operator^=(bool value)
00555 {
00556 key->setBit(pos, (*key)[pos] ^ value);
00557 return *this;
00558 };
00559
00560 private:
00561
00562 bool bit;
00563 uint32_t pos;
00564 OverlayKey* key;
00565 };
00566
00567
00574 inline void doPacking(cCommBuffer *b, OverlayKey& obj) {obj.netPack(b);}
00575
00582 inline void doUnpacking(cCommBuffer *b, OverlayKey& obj) {obj.netUnpack(b);}
00583
00584 #endif