SNAP Library, Developer Reference  2012-10-02 12:56:23
SNAP, a general purpose network analysis and graph mining library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
bd.h
Go to the documentation of this file.
00001 
00002 // Basic-Macro-Definitions
00003 #define forever for(;;)
00004 
00006 // Basic-Type-Definitions
00007 typedef unsigned char uchar;
00008 typedef unsigned int uint;
00009 typedef unsigned long ulong;
00010 typedef unsigned short ushort;
00011 
00012 typedef float sdouble;
00013 typedef long double ldouble;
00014 typedef FILE* TFileId;
00015 
00016 typedef char int8;
00017 typedef short int16;
00018 typedef int int32;
00019 #ifdef GLib_WIN32
00020 typedef __int64 int64;
00021 #elif defined(GLib_GLIBC)
00022 typedef int64_t int64;
00023 #else
00024 typedef long long int64;
00025 #endif
00026 
00027 typedef unsigned char uint8;
00028 typedef unsigned short uint16;
00029 typedef unsigned int uint32;
00030 #ifdef GLib_WIN32
00031 typedef unsigned __int64 uint64;
00032 #elif defined(GLib_GLIBC)
00033 typedef u_int64_t uint64;
00034 #else
00035 typedef unsigned long long uint64;
00036 #endif
00037 
00038 #if (!defined(__ssize_t_defined) && !defined(GLib_MACOSX))
00039 typedef ptrdiff_t ssize_t;
00040 #endif
00041 
00042 #if defined(GLib_UNIX)
00043 #define _isnan(x) isnan(x)
00044 #if defined (GLib_MACOSX)
00045   #define _finite(x) isfinite(x)
00046 #else
00047   #define _finite(x) finite(x)
00048 #endif
00049 #endif
00050 
00051 #if defined(GLib_WIN32)
00052 #define _vsnprintf vsnprintf
00053 #endif
00054 
00055 typedef size_t TSize;
00056 #define TSizeMx SIZE_MAX
00057 
00059 // Localization
00060 typedef enum {lUndef, lUs, lSi} TLoc;
00061 
00063 // Messages
00064 void WrNotify(const char* CaptionCStr, const char* NotifyCStr);
00065 void SaveToErrLog(const char* MsgCStr);
00066 
00067 inline void InfoNotify(const char* NotifyCStr){
00068   WrNotify("Information", NotifyCStr);}
00069 inline void WarnNotify(const char* NotifyCStr){
00070   WrNotify("Warning", NotifyCStr);}
00071 inline void ErrNotify(const char* NotifyCStr){
00072   WrNotify("Error", NotifyCStr);}
00073 inline void StatNotify(const char* NotifyCStr){
00074   WrNotify("Status", NotifyCStr);}
00075 
00077 // Compiler-Options
00078 #if defined (GLib_WIN)
00079   #define snprintf _snprintf
00080   #define vsnprintf  _vsnprintf
00081   // disable warning 'debug symbol greater than 255 chars'
00082   #pragma warning(disable: 4786)
00083   // disable warning 'return type for 'identifier::operator->'
00084   // is not a UDT or reference to a UDT.
00085   //#pragma warning(disable: 4284)
00086   // 'declaration' : function differs from 'declaration'
00087   // only by calling convention
00088   //#pragma warning(disable: 4666)
00089   #pragma warning(disable: 4996)
00090 #endif
00091 
00092 #if defined (GLib_GCC)
00093   #define _CONSOLE
00094 #endif
00095 
00096 // use console output if console already exists, otherwise
00097 // define GLib_COUT to use console (class COut)
00098 #if defined(_CONSOLE)||defined(__CONSOLE__)
00099   #define GLib_Console
00100 #endif
00101 
00102 // create console if want to output but there is no console allocated
00103 #if defined(GLib_Console) && (! (defined(__CONSOLE__)||defined(_CONSOLE)))
00104   #define GLib_CreateConsole
00105 #endif
00106 
00108 // Class-Definition
00109 #define ClassT(TNm) \
00110 class TNm{
00111 
00112 #define ClassTV(TNm, TNmV) \
00113 class TNm; \
00114 typedef TVec<TNm> TNmV; \
00115 class TNm{
00116 
00117 #define ClassTVQ(TNm, TNmV, TNmQ) \
00118 class TNm; \
00119 typedef TVec<TNm> TNmV; \
00120 typedef TQQueue<TNm> TNmQ; \
00121 class TNm{
00122 
00123 #define ClassTP(TNm, PNm) \
00124 class TNm; \
00125 typedef TPt<TNm> PNm; \
00126 class TNm{ \
00127 private: \
00128   TCRef CRef; \
00129 public: \
00130   friend class TPt<TNm>;
00131 
00132 #define ClassHdTP(TNm, PNm) \
00133 class TNm; \
00134 typedef TPt<TNm> PNm;
00135 
00136 #define ClassTPE(TNm, PNm, ENm) \
00137 class TNm; \
00138 typedef TPt<TNm> PNm; \
00139 class TNm: public ENm{ \
00140 private: \
00141   TCRef CRef; \
00142 public: \
00143   friend class TPt<TNm>;
00144 
00145 #define ClassTPEE(TNm, PNm, ENm1, ENm2) \
00146 class TNm; \
00147 typedef TPt<TNm> PNm; \
00148 class TNm: public ENm1, public ENm2{ \
00149 private: \
00150   TCRef CRef; \
00151 public: \
00152   friend class TPt<TNm>;
00153 
00154 #define ClassTE(TNm, ENm) \
00155 class TNm; \
00156 class TNm: public ENm{ \
00157 public: \
00158 
00159 #define ClassTPV(TNm, PNm, TNmV) \
00160 class TNm; \
00161 typedef TPt<TNm> PNm; \
00162 typedef TVec<PNm> TNmV; \
00163 class TNm{ \
00164 private: \
00165   TCRef CRef; \
00166 public: \
00167   friend class TPt<TNm>;
00168 
00169 #define ClassHdTPV(TNm, PNm, TNmV) \
00170 class TNm; \
00171 typedef TPt<TNm> PNm; \
00172 typedef TVec<PNm> TNmV;
00173 
00174 #define ClassTPVL(TNm, PNm, TNmV, TNmL, TNmLN) \
00175 class TNm; \
00176 typedef TPt<TNm> PNm; \
00177 typedef TVec<PNm> TNmV; \
00178 typedef TLst<PNm> TNmL; \
00179 typedef TLstNd<PNm>* TNmLN; \
00180 class TNm{ \
00181 private: \
00182   TCRef CRef; \
00183 public: \
00184   friend class TPt<TNm>;
00185 
00187 // Forward-Definitions
00188 class TSIn;
00189 class TSOut;
00190 class TStr;
00191 class TXmlObjSer;
00192 class TXmlObjSerTagNm;
00193 template <class TRec> class TPt;
00194 ClassHdTP(TXmlTok, PXmlTok)
00195 ClassHdTP(TExcept, PExcept)
00196 ClassHdTP(TXmlDoc, PXmlDoc)
00197 
00199 // DefaultConstructor/CopyConstructor/AssignmentOperator-UnDefinition
00200 #define UndefDefaultCopyAssign(TNm) \
00201 private: \
00202   TNm(){} \
00203   TNm(const TNm&); \
00204   TNm& operator=(const TNm&)
00205 
00206 #define UndefCopyAssign(TNm) \
00207 private: \
00208   TNm(const TNm&); \
00209   TNm& operator=(const TNm&)
00210 
00211 #define UndefCopy(TNm) \
00212 private: \
00213   TNm(const TNm&)
00214 
00215 #define UndefAssign(TNm) \
00216 private: \
00217   TNm& operator=(const TNm&)
00218 
00220 // Assertions
00221 class TOnExeStop{
00222 private:
00223   typedef bool (*TOnExeStopF)(char* MsgCStr);
00224   static TOnExeStopF OnExeStopF;
00225 public:
00226   static bool IsOnExeStopF(){return OnExeStopF!=NULL;}
00227   static void PutOnExeStopF(TOnExeStopF _OnExeStopF){OnExeStopF=_OnExeStopF;}
00228   static TOnExeStopF GetOnExeStopF(){return OnExeStopF;}
00229 };
00230 
00231 void ExeStop(
00232  const char* MsgStr, const char* ReasonStr,
00233  const char* CondStr, const char* FNm, const int& LnN);
00234 
00235 #define Fail ExeStop(NULL, NULL, "Fail", __FILE__, __LINE__)
00236 
00237 #define FailR(Reason) ExeStop((Reason), NULL, "Fail", __FILE__, __LINE__)
00238 #define FailRA(Reason, ArgStr) \
00239   ExeStop((TStr(Reason)+" ("+ArgStr+")").CStr(), NULL, "Fail", __FILE__, __LINE__)
00240 
00241 #define EFail TExcept::ThrowFull("", TStr("[")+ TStr(__FILE__) + " line " + TInt::GetStr(__LINE__) + "]")
00242 
00243 #define EFailR(Reason) TExcept::ThrowFull(Reason, TStr("[")+TStr(__FILE__)+" line "+TInt::GetStr(__LINE__)+"]")
00244 
00245 #ifdef NDEBUG
00246 #define Assert(Cond)
00247 #else
00248 #define Assert(Cond) \
00249   ((Cond) ? static_cast<void>(0) : ExeStop(NULL, NULL, #Cond, __FILE__, __LINE__))
00250 #endif
00251 
00252 #ifdef NDEBUG
00253 #define AssertR(Cond, Reason)
00254 #else
00255 #define AssertR(Cond, Reason) ((Cond) ? static_cast<void>(0) : \
00256   ExeStop(NULL, Reason.CStr(), #Cond, __FILE__, __LINE__))
00257 #endif
00258 
00259 #define IAssert(Cond) \
00260   ((Cond) ? static_cast<void>(0) : ExeStop(NULL, NULL, #Cond, __FILE__, __LINE__))
00261 
00262 #define IAssertR(Cond, Reason) \
00263   ((Cond) ? static_cast<void>(0) : ExeStop(NULL, TStr(Reason).CStr(), #Cond, __FILE__, __LINE__))
00264 
00265 #define WAssert(Cond, MsgCStr) \
00266   ((Cond) ? static_cast<void>(0) : WarnNotify(MsgCStr))
00267 
00268 #define SAssert(Cond) \
00269   ((Cond) ? static_cast<void>(0) : ExeStop(TSysStr::GetLastMsgCStr(), NULL, #Cond, __FILE__, __LINE__))
00270 
00271 #define FAssert(Cond, MsgCStr) \
00272   ((Cond) ? static_cast<void>(0) : ExeStop(TStr(MsgCStr).CStr(), NULL, NULL, __FILE__, __LINE__))
00273 
00274 #define FSAssert(Cond) \
00275   ((Cond) ? static_cast<void>(0) : ExeStop(TSysStr::GetLastMsgCStr(), NULL, NULL, __FILE__, __LINE__))
00276 
00277 #define EAssert(Cond) \
00278   ((Cond) ? static_cast<void>(0) : TExcept::ThrowFull(#Cond, TStr(__FILE__) + " line " + TInt::GetStr(__LINE__) +": "+ TStr(#Cond)))
00279 
00280 #define EAssertR(Cond, MsgStr) \
00281   ((Cond) ? static_cast<void>(0) : TExcept::ThrowFull(MsgStr, TStr(__FILE__)+" line "+TInt::GetStr(__LINE__)+": "+TStr(#Cond)))
00282 
00283 #define EAssertRA(Cond, MsgStr, ArgStr) \
00284   ((Cond) ? static_cast<void>(0) : TExcept::Throw(MsgStr, ArgStr))
00285 
00286 #define EAssertRAA(Cond, MsgStr, ArgStr1, ArgStr2) \
00287   ((Cond) ? static_cast<void>(0) : TExcept::Throw(MsgStr, ArgStr1, ArgStr2))
00288 
00289 #define ESAssert(Cond) \
00290   ((Cond) ? static_cast<void>(0) : TExcept::Throw(TSysStr::GetLastMsgCStr(), \
00291   TStr(__FILE__) + " line " + TInt::GetStr(__LINE__) +": "+ TStr(#Cond)))
00292 
00293 // compile time assert
00294 // #define STATIC_ASSERT(x) { const char temp[ (((x) == 0) ? 0 : 1) ] = {'\0'}; }
00295 template <bool BoolVal> struct TStaticAssert;
00296 template <> struct TStaticAssert<true> { enum { value = 1 }; };
00297 template<int IntVal> struct TStaticAssertTest{};
00298 
00299 #define CAssert(Cond) \
00300   { typedef TStaticAssertTest<sizeof(TStaticAssert<(Cond)==0?false:true>)> TestStaticAssert; }
00301 
00303 // Xml-Object-Serialization
00304 template <class TRec>
00305 bool IsXLoadFromFileOk(const TStr& FNm, const TStr& Nm, TRec& Rec, TStr& MsgStr);
00306 template <class TRec>
00307 void XLoadFromFile(const TStr& FNm, const TStr& Nm, TRec& Rec);
00308 
00309 #define XLoadHd(Nm) \
00310   {TStr TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \
00311   TXmlObjSer::AssertXmlHd(XmlTok, Nm, TypeNm);}
00312 #define XLoad(Nm) \
00313   Nm.LoadXml(XmlTok->GetTagTok(#Nm), #Nm);
00314 
00315 #define XSaveHd(Nm) \
00316   TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \
00317   TXmlObjSerTagNm XmlObjSerTagNm(SOut, false, Nm, _TypeNm);
00318 #define XSaveHdArg(Nm, ArgNm, ArgVal) \
00319   TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \
00320   TXmlObjSerTagNm XmlObjSerTagNm(SOut, false, Nm, _TypeNm, ArgNm, ArgVal);
00321 #define XSaveBETag(Nm) \
00322   TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \
00323   TXmlObjSerTagNm XmlObjSerTagNm(SOut, true, Nm, _TypeNm);
00324 #define XSaveBETagArg(Nm, ArgNm, ArgVal) \
00325   TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \
00326   TXmlObjSerTagNm XmlObjSerTagNm(SOut, true, Nm, _TypeNm, ArgNm, ArgVal);
00327 #define XSaveBETagArg4(Nm, ArgNm1, ArgVal1, ArgNm2, ArgVal2, ArgNm3, ArgVal3, ArgNm4, ArgVal4) \
00328   TStr _TypeNm=TXmlObjSer::GetTagNm(TStr(typeid(*this).name())); \
00329   TXmlObjSerTagNm XmlObjSerTagNm(SOut, true, Nm, _TypeNm, ArgNm1, ArgVal1, ArgNm2, ArgVal2, ArgNm3, ArgVal3, ArgNm4, ArgVal4);
00330 #define XSave(Nm) \
00331   Nm.SaveXml(SOut, #Nm)
00332 #define XSaveToFile(Nm, FNm) \
00333   {TFOut SOut(FNm); Nm.SaveXml(SOut, #Nm);}
00334 
00336 // Basic-Operators
00337 typedef enum {loUndef, loNot, loAnd, loOr} TLogOp;
00338 typedef enum {roUndef, roLs, roLEq, roEq, roNEq, roGEq, roGt} TRelOp;
00339 
00341 // Comparation-Macros
00342 #ifndef min
00343  #define min(a,b) ((a)<(b)?(a):(b))
00344 #endif
00345 
00346 #ifndef max
00347  #define max(a,b) ((a)>(b)?(a):(b))
00348 #endif
00349 
00351 // Comparator-Definitions
00352 template<class T>
00353 class TEq{
00354 public:
00355   bool operator()(const T& x, const T& y) const {return x==y;}
00356 };
00357 
00358 template<class T>
00359 class TNEq{
00360 public:
00361   bool operator()(const T& x, const T& y) const {return x!=y;}
00362 };
00363 
00364 template<class T>
00365 class TLss{
00366 public:
00367   bool operator()(const T& x, const T& y) const {return x<y;}
00368   int Cmp(const T& d1, const T& d2) const {
00369     if (d1<d2){return -1;}
00370     else if (d2<d1){return 1;}
00371     else {return 0;}
00372   }
00373 };
00374 
00375 template<class T>
00376 class TLEq{
00377 public:
00378   bool operator()(const T& x, const T& y) const {return x<=y;}
00379 };
00380 
00381 template<class T>
00382 class TGtr{
00383 public:
00384   bool operator() (const T& x, const T& y) const { return x>y; }
00385   int Cmp(const T& d1, const T& d2) const { //** vprasaj Janeza za uporabo
00386     if (d1<d2){return 1;}
00387     else if (d2<d1){return -1;}
00388     else {return 0;}
00389   }
00390 };
00391 
00392 template<class T>
00393 class TGEq{
00394 public:
00395   bool operator()(const T& x, const T& y) const {return x>=y;}
00396 };
00397 
00398 template<class T>
00399 class TCmp{
00400 public:
00401   int operator()(const T& x, const T& y) const {
00402     if (x < y){return -1;}
00403     else if (x > y){return 1;}
00404     else {return 0;}
00405   }
00406 };
00407 
00409 // Operator-Definitions
00410 template <class TRec>
00411 bool operator!=(const TRec& Rec1, const TRec& Rec2){return !(Rec1==Rec2);}
00412 
00413 template <class TRec>
00414 bool operator>(const TRec& Rec1, const TRec& Rec2){return Rec2<Rec1;}
00415 
00416 template <class TRec>
00417 bool operator<=(const TRec& Rec1, const TRec& Rec2){return !(Rec2<Rec1);}
00418 
00419 template <class TRec>
00420 bool operator>=(const TRec& Rec1, const TRec& Rec2){return !(Rec1<Rec2);}
00421 
00422 template <class TRec>
00423 bool Cmp(const int& RelOp, const TRec& Rec1, const TRec& Rec2){
00424   switch (RelOp){
00425     case roLs: return Rec1<Rec2;
00426     case roLEq: return Rec1<=Rec2;
00427     case roEq: return Rec1==Rec2;
00428     case roNEq: return Rec1!=Rec2;
00429     case roGEq: return Rec1>=Rec2;
00430     case roGt: return Rec1>Rec2;
00431     default: Fail; return false;
00432   }
00433 }
00434 
00436 // Reference-Count
00437 class TCRef{
00438 private:
00439   int Refs;
00440 private:
00441   TCRef& operator=(const TCRef&);
00442   TCRef(const TCRef&);
00443 public:
00444   TCRef(): Refs(0){}
00445   ~TCRef(){Assert(Refs==0);}
00446 
00447   void MkRef(){Refs++;}
00448   void UnRef(){Assert(Refs>0); Refs--;}
00449   bool NoRef() const {return Refs==0;}
00450   int GetRefs() const {return Refs;}
00451 };
00452 
00454 // Smart-Pointer-With-Reference-Count
00455 template <class TRec>
00456 class TPt{
00457 public:
00458   typedef TRec TObj;
00459 private:
00460   TRec* Addr;
00461   void MkRef() const {
00462     if (Addr!=NULL){
00463       Addr->CRef.MkRef();
00464     }
00465   }
00466   void UnRef() const {
00467     if (Addr!=NULL){
00468       Addr->CRef.UnRef();
00469       if (Addr->CRef.NoRef()){delete Addr;}
00470     }
00471   }
00472 public:
00473   TPt(): Addr(NULL){}
00474   TPt(const TPt& Pt): Addr(Pt.Addr){MkRef();}
00475   TPt(TRec* _Addr): Addr(_Addr){MkRef();}
00476   static TPt New(){return TObj::New();}
00477   ~TPt(){UnRef();}
00478   explicit TPt(TSIn& SIn);
00479   explicit TPt(TSIn& SIn, void* ThisPt);
00480   void Save(TSOut& SOut) const;
00481   void LoadXml(const TPt<TXmlTok>& XmlTok, const TStr& Nm);
00482   void SaveXml(TSOut& SOut, const TStr& Nm) const;
00483 
00484   TPt& operator=(const TPt& Pt){
00485     if (this!=&Pt){Pt.MkRef(); UnRef(); Addr=Pt.Addr;} return *this;}
00486   bool operator==(const TPt& Pt) const {return *Addr==*Pt.Addr;}
00487   bool operator!=(const TPt& Pt) const {return *Addr!=*Pt.Addr;}
00488   bool operator<(const TPt& Pt) const {return *Addr<*Pt.Addr;}
00489 
00490   TRec* operator->() const {Assert(Addr!=NULL); return Addr;}
00491   TRec& operator*() const {Assert(Addr!=NULL); return *Addr;}
00492   TRec& operator[](const int& RecN) const {
00493     Assert(Addr!=NULL); return Addr[RecN];}
00494   TRec* operator()() const {return Addr;}
00495   //const TRec* operator()() const {return Addr;}
00496   //TRec* operator()() {return Addr;}
00497 
00498   bool Empty() const {return Addr==NULL;}
00499   void Clr(){UnRef(); Addr=NULL;}
00500   int GetRefs() const {
00501     if (Addr==NULL){return -1;} else {return Addr->CRef.GetRefs();}}
00502 
00503   int GetPrimHashCd() const {return Addr->GetPrimHashCd();}
00504   int GetSecHashCd() const {return Addr->GetSecHashCd();}
00505 
00506   TPt<TRec> Clone(){return MkClone(*this);}
00507 };
00508 
00510 // Simple-String
00511 class TSStr{
00512 private:
00513   char* Bf;
00514 public:
00515   TSStr(): Bf(NULL){
00516     Bf=new char[0+1]; Bf[0]=0;}
00517   TSStr(const TSStr& SStr): Bf(NULL){
00518     Bf=new char[strlen(SStr.Bf)+1]; strcpy(Bf, SStr.Bf);}
00519   TSStr(const char* _Bf): Bf(NULL){
00520     Bf=new char[strlen(_Bf)+1]; strcpy(Bf, _Bf);}
00521   ~TSStr(){delete[] Bf;}
00522 
00523   TSStr& operator=(const TSStr& SStr){
00524     if (this!=&SStr){
00525       delete[] Bf; Bf=new char[strlen(SStr.Bf)+1]; strcpy(Bf, SStr.Bf);}
00526     return *this;}
00527 
00528   char* CStr() {return Bf;}
00529   const char* CStr() const {return Bf;}
00530   bool Empty() const {return Bf[0]==0;}
00531   int Len() const {return int(strlen(Bf));}
00532 };
00533 
00535 // Conversion-Pointer64-To-Integers32
00536 class TConv_Pt64Ints32{
00537 private:
00538   union{
00539     void* Pt;
00540     uint64 UInt64;
00541     struct {uint Ls; uint Ms;} UInt32;
00542   } Val;
00543   UndefCopyAssign(TConv_Pt64Ints32);
00544 public:
00545   TConv_Pt64Ints32(){
00546     Val.Pt=0; Val.UInt32.Ms=0; Val.UInt32.Ls=0;}
00547   TConv_Pt64Ints32(void* Pt){
00548     Val.UInt32.Ms=0; Val.UInt32.Ls=0; Val.Pt=Pt;}
00549   TConv_Pt64Ints32(const uint& Ms, const uint& Ls){
00550     Val.Pt=0; Val.UInt32.Ms=Ms; Val.UInt32.Ls=Ls;}
00551 
00552   void PutPt(void* Pt){Val.Pt=Pt;}
00553   void* GetPt() const {return Val.Pt;}
00554   void PutUInt64(const uint64& _UInt64){Val.UInt64=_UInt64;}
00555   uint64 GetUInt64() const {return Val.UInt64;}
00556   void PutMsUInt32(const uint& Ms){Val.UInt32.Ms=Ms;}
00557   uint GetMsUInt32() const {return Val.UInt32.Ms;}
00558   void PutLsUInt32(const uint& Ls){Val.UInt32.Ls=Ls;}
00559   uint GetLsUInt32() const {return Val.UInt32.Ls;}
00560 };
00561 
00563 // Swap
00564 template <class TRec>
00565 void Swap(TRec& Rec1, TRec& Rec2){
00566   TRec Rec=Rec1; Rec1=Rec2; Rec2=Rec;
00567 }