OverlayKey.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #ifndef __OVERLAYKEY_H_
00025 #define __OVERLAYKEY_H_
00026 
00027 #include <gmp.h>
00028 #include <stdint.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     // constants
00051     //-------------------------------------------------------------------------
00052 
00053     static const OverlayKey UNSPECIFIED_KEY; 
00054     static const OverlayKey ZERO; 
00055     static const OverlayKey ONE; 
00057     //-------------------------------------------------------------------------
00058     // construction and destruction
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     // string representations & node key attributes
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     // operators
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     // additional math
00320     //-------------------------------------------------------------------------
00321 
00331     uint32_t getBitRange(uint32_t p, uint32_t n) const;
00332 
00333     double toDouble() const;
00334 
00335     inline bool getBit(uint32_t p) const
00336     {
00337         return getBitRange(p, 1);
00338     };
00339 
00345     size_t hash() const;
00346 
00353     int log_2() const;
00354 
00361     OverlayKey randomSuffix(uint32_t pos) const;
00362 
00369     OverlayKey randomPrefix(uint32_t pos) const;
00370 
00379     uint32_t sharedPrefixLength(const OverlayKey& compKey,
00380                                 uint32_t bitsPerDigit = 1) const;
00381 
00390     bool isBetween(const OverlayKey& keyA, const OverlayKey& keyB) const;
00391 
00400     bool isBetweenR(const OverlayKey& keyA, const OverlayKey& keyB) const;
00401 
00410     bool isBetweenL(const OverlayKey& keyA, const OverlayKey& keyB) const;
00411 
00420     bool isBetweenLR(const OverlayKey& keyA, const OverlayKey& keyB) const;
00421 
00422     //-------------------------------------------------------------------------
00423     // static methods
00424     //-------------------------------------------------------------------------
00425 
00431     static void setKeyLength(uint32_t length);
00432 
00438     static uint32_t getLength();
00439 
00445     static OverlayKey random();
00446 
00452     static OverlayKey getMax();
00453 
00461     static OverlayKey sha1(const BinaryValue& value);
00462 
00469     static OverlayKey pow2(uint32_t exponent);
00470 
00475     static void test();
00476 
00477 private:
00478     // private constants
00479 
00480     static const uint32_t MAX_KEYLENGTH = 160; 
00481     static uint32_t keyLength; 
00482     static uint32_t aSize; 
00483     static mp_limb_t GMP_MSB_MASK; 
00486     // private fields
00487     bool isUnspec; 
00489     mp_limb_t key[MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00490               (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0)];
00494     // private "helper" methods
00498     void trim();
00499 
00503     void clear();
00504 
00505 public:
00506 
00512     void netPack(cCommBuffer *b);
00513 
00519     void netUnpack(cCommBuffer *b);
00520 };
00521 
00527 class OverlayKeyBit
00528 {
00529  public:
00530 
00531     OverlayKeyBit(bool value, uint32_t pos, OverlayKey* key)
00532         : bit(value), pos(pos), key(key)
00533     {};
00534 
00536     inline operator bool()
00537     {
00538         return bit;
00539     };
00540 
00541     inline OverlayKeyBit& operator=(const OverlayKeyBit& value)
00542     {
00543         key->setBit(pos, value.bit);
00544         return *this;
00545     };
00546 
00550     inline OverlayKeyBit& operator=(bool value)
00551     {
00552         key->setBit(pos, value);
00553         return *this;
00554     };
00555 
00556     inline OverlayKeyBit& operator^=(bool value)
00557     {
00558         key->setBit(pos, (*key)[pos] ^ value);
00559         return *this;
00560     };
00561 
00562  private:
00563 
00564     bool bit;
00565     uint32_t pos;
00566     OverlayKey* key;
00567 };
00568 
00569 
00576 inline void doPacking(cCommBuffer *b, OverlayKey& obj) {obj.netPack(b);}
00577 
00584 inline void doUnpacking(cCommBuffer *b, OverlayKey& obj) {obj.netUnpack(b);}
00585 
00586 #endif
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3