00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef IDENTIFIER_H_
00040 #define IDENTIFIER_H_
00041
00042 #include <memory>
00043 #include <string>
00044 #include <gmp.h>
00045 #include <boost/cstdint.hpp>
00046 #include "ariba/utility/logging/Logging.h"
00047 #include "ariba/utility/types/Address.h"
00048 #include "ariba/utility/serialization.h"
00049
00051 #define MAX_KEYLENGTH 192
00052
00053 namespace ariba {
00054 namespace utility {
00055
00056 using_serialization;
00057
00058 class IdentifierBit;
00059
00065 class Identifier: public Address {
00066 VSERIALIZEABLE;
00067 use_logging_h( Identifier );
00068 public:
00069
00070
00071
00072
00073
00074 virtual void clearAddress();
00075 virtual bool setAddress(string address);
00076 virtual string getAddress() const;
00077
00078
00079
00080
00081
00082 static const Identifier UNSPECIFIED_KEY;
00083 static const Identifier ZERO;
00084 static const Identifier ONE;
00086
00087
00088
00089
00095 Identifier();
00096
00102 Identifier(uint32_t num);
00103
00109 Identifier(const unsigned char* buffer, uint size);
00110
00114 Identifier(const std::string& str, uint base = 16);
00115
00121 Identifier(const Identifier& rhs);
00122
00128 virtual ~Identifier();
00129
00130
00131
00132
00133
00139 virtual std::string toString(uint base = 16) const;
00140
00144 friend std::ostream& operator<<(std::ostream& os, const Identifier& c);
00145
00151 bool isUnspecified() const;
00152
00153
00154
00155
00156
00163 bool operator<(const Identifier& compKey) const;
00164
00171 bool operator>(const Identifier& compKey) const;
00172
00179 bool operator<=(const Identifier& compKey) const;
00180
00187 bool operator>=(const Identifier& compKey) const;
00188
00195 bool operator==(const Identifier& compKey) const;
00196
00203 bool operator!=(const Identifier& compKey) const;
00204
00211 int compareTo(const Identifier& compKey) const;
00212
00219 Identifier& operator=(const Identifier& rhs);
00220
00226 Identifier& operator--();
00227
00233 Identifier& operator++();
00234
00241 Identifier& operator+=(const Identifier& rhs);
00242
00249 Identifier& operator-=(const Identifier& rhs);
00250
00257 Identifier operator+(const Identifier& rhs) const;
00258
00265 Identifier operator-(const Identifier& rhs) const;
00266
00272 Identifier operator--(int);
00273
00279 Identifier operator++(int);
00280
00287 Identifier operator>>(uint num) const;
00288
00295 Identifier operator<<(uint num) const;
00296
00303 Identifier operator&(const Identifier& rhs) const;
00304
00311 Identifier operator|(const Identifier& rhs) const;
00312
00319 Identifier operator^(const Identifier& rhs) const;
00320
00326 Identifier operator~() const;
00327
00334 IdentifierBit operator[](uint n);
00335
00343 Identifier& setBitAt(uint pos, bool value);
00344
00345
00346
00347
00348
00358 uint32_t get(uint p, uint n) const;
00359
00365 size_t hash() const;
00366
00373 int log_2() const;
00374
00381 Identifier randomSuffix(uint pos) const;
00382
00389 Identifier randomPrefix(uint pos) const;
00390
00398 uint sharedPrefixLength(const Identifier& compKey) const;
00399
00408 bool isBetween(const Identifier& keyA, const Identifier& keyB) const;
00409
00418 bool isBetweenR(const Identifier& keyA, const Identifier& keyB) const;
00419
00428 bool isBetweenL(const Identifier& keyA, const Identifier& keyB) const;
00429
00438 bool isBetweenLR(const Identifier& keyA, const Identifier& keyB) const;
00439
00440
00441
00442
00443
00449 static void setKeyLength(uint length);
00450
00456 static uint getLength();
00457
00463 static Identifier random();
00464
00470 static Identifier max();
00471
00472
00473
00474
00475
00476
00484 static Identifier sha1(const string& value);
00485
00486 static Identifier sha1(const uint8_t* value, size_t length );
00487
00494 static Identifier pow2(uint exponent);
00495
00496 private:
00497
00498
00499 static uint keyLength;
00500 static uint aSize;
00501 static mp_limb_t GMP_MSB_MASK;
00504
00505 bool isUnspec;
00507 void seed();
00508
00509 static const size_t array_size = MAX_KEYLENGTH / (8 * sizeof(mp_limb_t))
00510 + (MAX_KEYLENGTH % (8 * sizeof(mp_limb_t)) != 0 ? 1 : 0);
00511
00513 mp_limb_t key[array_size + 1];
00514
00515
00519 void trim();
00520
00524 void clear();
00525 };
00526
00532 class IdentifierBit {
00533 public:
00534
00535 IdentifierBit(bool value, uint pos, Identifier* key) :
00536 bit(value), pos(pos), key(key) {
00537 }
00538
00540 inline operator bool() {
00541 return bit;
00542 }
00543
00547 inline IdentifierBit& operator=(bool value) {
00548 key->setBitAt(pos, value);
00549 return *this;
00550 }
00551
00552 inline IdentifierBit& operator^=(bool value) {
00553 key->setBitAt(pos, (*key)[pos] ^ value);
00554 return *this;
00555 }
00556
00557 private:
00558
00559 bool bit;
00560 uint pos;
00561 Identifier* key;
00562 };
00563
00564 }
00565 }
00566
00567
00568 sznBeginDefault( ariba::utility::Identifier, X ) {
00569
00570 uint16_t len = array_size*sizeof(mp_limb_t);
00571 uint8_t unspec = isUnspec;
00572
00573
00574 X && unspec && len;
00575
00576
00577 for (int i=array_size-1; i>=0; i--) X && key[i];
00578
00579
00580 if (X.isDeserializer()) isUnspec = unspec;
00581 } sznEnd();
00582
00583 #endif