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 <cassert>
00045 #include <iostream>
00046 #include <gmp.h>
00047 #include <boost/cstdint.hpp>
00048 #include "ariba/utility/logging/Logging.h"
00049 #include "ariba/utility/types/Address.h"
00050 #include "ariba/utility/serialization.h"
00051
00053 #define MAX_KEYLENGTH 192
00054
00055 namespace ariba {
00056 namespace utility {
00057
00058 using_serialization;
00059
00060 class IdentifierBit;
00061
00067 class Identifier: public Address {
00068 VSERIALIZEABLE;
00069 use_logging_h( Identifier );
00070 public:
00071
00072
00073
00074
00075
00076 virtual void clearAddress();
00077 virtual bool setAddress(string address);
00078 virtual string getAddress() const;
00079
00080
00081
00082
00083
00084 static const Identifier UNSPECIFIED_KEY;
00085 static const Identifier ZERO;
00086 static const Identifier ONE;
00088
00089
00090
00091
00097 Identifier();
00098
00104 Identifier(uint32_t num);
00105
00111 Identifier(const unsigned char* buffer, uint size);
00112
00116 Identifier(const std::string& str, uint base = 16);
00117
00123 Identifier(const Identifier& rhs);
00124
00130 virtual ~Identifier();
00131
00132
00133
00134
00135
00141 virtual std::string toString(uint base = 16) const;
00142
00146 friend std::ostream& operator<<(std::ostream& os, const Identifier& c);
00147
00153 bool isUnspecified() const;
00154
00155
00156
00157
00158
00165 bool operator<(const Identifier& compKey) const;
00166
00173 bool operator>(const Identifier& compKey) const;
00174
00181 bool operator<=(const Identifier& compKey) const;
00182
00189 bool operator>=(const Identifier& compKey) const;
00190
00197 bool operator==(const Identifier& compKey) const;
00198
00205 bool operator!=(const Identifier& compKey) const;
00206
00213 int compareTo(const Identifier& compKey) const;
00214
00221 Identifier& operator=(const Identifier& rhs);
00222
00228 Identifier& operator--();
00229
00235 Identifier& operator++();
00236
00243 Identifier& operator+=(const Identifier& rhs);
00244
00251 Identifier& operator-=(const Identifier& rhs);
00252
00259 Identifier operator+(const Identifier& rhs) const;
00260
00267 Identifier operator-(const Identifier& rhs) const;
00268
00274 Identifier operator--(int);
00275
00281 Identifier operator++(int);
00282
00289 Identifier operator>>(uint num) const;
00290
00297 Identifier operator<<(uint num) const;
00298
00305 Identifier operator&(const Identifier& rhs) const;
00306
00313 Identifier operator|(const Identifier& rhs) const;
00314
00321 Identifier operator^(const Identifier& rhs) const;
00322
00328 Identifier operator~() const;
00329
00336 IdentifierBit operator[](uint n);
00337
00345 Identifier& setBitAt(uint pos, bool value);
00346
00347
00348
00349
00350
00360 uint32_t get(uint p, uint n) const;
00361
00367 size_t hash() const;
00368
00375 int log_2() const;
00376
00383 Identifier randomSuffix(uint pos) const;
00384
00391 Identifier randomPrefix(uint pos) const;
00392
00400 uint sharedPrefixLength(const Identifier& compKey) const;
00401
00410 bool isBetween(const Identifier& keyA, const Identifier& keyB) const;
00411
00420 bool isBetweenR(const Identifier& keyA, const Identifier& keyB) const;
00421
00430 bool isBetweenL(const Identifier& keyA, const Identifier& keyB) const;
00431
00440 bool isBetweenLR(const Identifier& keyA, const Identifier& keyB) const;
00441
00442
00443
00444
00445
00451 static void setKeyLength(uint length);
00452
00458 static uint getLength();
00459
00465 static Identifier random();
00466
00472 static Identifier max();
00473
00474
00475
00476
00477
00478
00486 static Identifier sha1(const string& value);
00487
00488 static Identifier sha1(const uint8_t* value, size_t length );
00489
00496 static Identifier pow2(uint exponent);
00497
00498 private:
00499
00500
00501 static uint keyLength;
00502 static uint aSize;
00503 static mp_limb_t GMP_MSB_MASK;
00506
00507 bool isUnspec;
00509 void seed();
00510
00511 static const size_t array_size = MAX_KEYLENGTH / (8 * sizeof(mp_limb_t))
00512 + (MAX_KEYLENGTH % (8 * sizeof(mp_limb_t)) != 0 ? 1 : 0);
00513
00515 mp_limb_t key[array_size + 1];
00516
00517
00521 void trim();
00522
00526 void clear();
00527 };
00528
00534 class IdentifierBit {
00535 public:
00536
00537 IdentifierBit(bool value, uint pos, Identifier* key) :
00538 bit(value), pos(pos), key(key) {
00539 }
00540
00542 inline operator bool() {
00543 return bit;
00544 }
00545
00549 inline IdentifierBit& operator=(bool value) {
00550 key->setBitAt(pos, value);
00551 return *this;
00552 }
00553
00554 inline IdentifierBit& operator^=(bool value) {
00555 key->setBitAt(pos, (*key)[pos] ^ value);
00556 return *this;
00557 }
00558
00559 private:
00560
00561 bool bit;
00562 uint pos;
00563 Identifier* key;
00564 };
00565
00566 }
00567 }
00568
00569
00570 sznBeginDefault( ariba::utility::Identifier, X ) {
00571
00572
00573 uint16_t len = array_size*sizeof(mp_limb_t);
00574 uint8_t unspec = isUnspec;
00575
00576
00577 X && unspec && len;
00578
00579
00580 for (int i=array_size-1; i>=0; i--) X && key[i];
00581
00582
00583 if (X.isDeserializer()){
00584 isUnspec = unspec;
00585 }
00586
00587 } sznEnd();
00588
00589 #endif