OverSim
OverlayKey.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #ifndef __OVERLAYKEY_H_
25 #define __OVERLAYKEY_H_
26 
27 #include <gmp.h>
28 #include <stdint.h>
29 
30 class BinaryValue;
31 class OverlayKeyBit;
32 class cCommBuffer;
33 
37 inline void omnet_random(mp_limb_t *r1p, mp_size_t r1n);
38 
47 {
48 public:
49  //-------------------------------------------------------------------------
50  // constants
51  //-------------------------------------------------------------------------
52 
53  static const OverlayKey UNSPECIFIED_KEY;
54  static const OverlayKey ZERO;
55  static const OverlayKey ONE;
57  //-------------------------------------------------------------------------
58  // construction and destruction
59  //-------------------------------------------------------------------------
60 
66  OverlayKey();
67 
73  OverlayKey( uint32_t num );
74 
80  OverlayKey( const unsigned char* buffer, uint32_t size);
81 
85  OverlayKey( const std::string& str, uint32_t base = 16 );
86 
92  OverlayKey( const OverlayKey& rhs );
93 
99  ~OverlayKey();
100 
101  //-------------------------------------------------------------------------
102  // string representations & node key attributes
103  //-------------------------------------------------------------------------
104 
105 
106 
112  std::string toString( uint32_t base = 16 ) const;
113 
117  friend std::ostream& operator<<(std::ostream& os, const OverlayKey& c);
118 
124  bool isUnspecified() const;
125 
126  //-------------------------------------------------------------------------
127  // operators
128  //-------------------------------------------------------------------------
129 
136  bool operator< ( const OverlayKey& compKey ) const;
137 
144  bool operator> ( const OverlayKey& compKey ) const;
145 
152  bool operator<=( const OverlayKey& compKey ) const;
153 
160  bool operator>=( const OverlayKey& compKey ) const;
161 
168  bool operator==( const OverlayKey& compKey ) const;
169 
176  bool operator!=( const OverlayKey& compKey ) const;
177 
184  int compareTo( const OverlayKey& compKey ) const;
185 
192  OverlayKey& operator= ( const OverlayKey& rhs );
193 
200 
207 
214  OverlayKey& operator+=( const OverlayKey& rhs );
215 
222  OverlayKey& operator-=( const OverlayKey& rhs );
223 
230  OverlayKey operator+ ( const OverlayKey& rhs ) const;
231 
238  OverlayKey operator- ( const OverlayKey& rhs ) const;
239 
245  OverlayKey operator--( int );
246 
252  OverlayKey operator++( int );
253 
260  OverlayKey operator>>( uint32_t num ) const;
261 
268  OverlayKey operator<<( uint32_t num ) const;
269 
276  OverlayKey operator& ( const OverlayKey& rhs ) const;
277 
284  OverlayKey operator| ( const OverlayKey& rhs ) const;
285 
292  OverlayKey operator^ ( const OverlayKey& rhs ) const;
293 
299  OverlayKey operator~ () const;
300 
307  OverlayKeyBit operator[]( uint32_t n );
308 
316  OverlayKey& setBit(uint32_t pos, bool value);
317 
318  //-------------------------------------------------------------------------
319  // additional math
320  //-------------------------------------------------------------------------
321 
331  uint32_t getBitRange(uint32_t p, uint32_t n) const;
332 
333  double toDouble() const;
334 
335  inline bool getBit(uint32_t p) const
336  {
337  return getBitRange(p, 1);
338  };
339 
345  size_t hash() const;
346 
353  int log_2() const;
354 
361  OverlayKey randomSuffix(uint32_t pos) const;
362 
369  OverlayKey randomPrefix(uint32_t pos) const;
370 
379  uint32_t sharedPrefixLength(const OverlayKey& compKey,
380  uint32_t bitsPerDigit = 1) const;
381 
390  bool isBetween(const OverlayKey& keyA, const OverlayKey& keyB) const;
391 
400  bool isBetweenR(const OverlayKey& keyA, const OverlayKey& keyB) const;
401 
410  bool isBetweenL(const OverlayKey& keyA, const OverlayKey& keyB) const;
411 
420  bool isBetweenLR(const OverlayKey& keyA, const OverlayKey& keyB) const;
421 
422  //-------------------------------------------------------------------------
423  // static methods
424  //-------------------------------------------------------------------------
425 
431  static void setKeyLength(uint32_t length);
432 
438  static uint32_t getLength();
439 
445  static OverlayKey random();
446 
452  static OverlayKey getMax();
453 
461  static OverlayKey sha1(const BinaryValue& value);
462 
469  static OverlayKey pow2(uint32_t exponent);
470 
475  static void test();
476 
477 private:
478  // private constants
479 
480  static const uint32_t MAX_KEYLENGTH = 160;
481  static uint32_t keyLength;
482  static uint32_t aSize;
483  static mp_limb_t GMP_MSB_MASK;
486  // private fields
487  bool isUnspec;
489  mp_limb_t key[MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
490  (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0)];
494  // private "helper" methods
498  void trim();
499 
503  void clear();
504 
505 public:
506 
512  void netPack(cCommBuffer *b);
513 
519  void netUnpack(cCommBuffer *b);
520 };
521 
528 {
529  public:
530 
531  OverlayKeyBit(bool value, uint32_t pos, OverlayKey* key)
532  : bit(value), pos(pos), key(key)
533  {};
534 
536  inline operator bool()
537  {
538  return bit;
539  };
540 
541  inline OverlayKeyBit& operator=(const OverlayKeyBit& value)
542  {
543  key->setBit(pos, value.bit);
544  return *this;
545  };
546 
550  inline OverlayKeyBit& operator=(bool value)
551  {
552  key->setBit(pos, value);
553  return *this;
554  };
555 
556  inline OverlayKeyBit& operator^=(bool value)
557  {
558  key->setBit(pos, (*key)[pos] ^ value);
559  return *this;
560  };
561 
562  private:
563 
564  bool bit;
565  uint32_t pos;
567 };
568 
569 
576 inline void doPacking(cCommBuffer *b, OverlayKey& obj) {obj.netPack(b);}
577 
584 inline void doUnpacking(cCommBuffer *b, OverlayKey& obj) {obj.netUnpack(b);}
585 
586 #endif