tools.h

Go to the documentation of this file.
00001 // Copyright (C) 2006 Ivo Nowak and Stefan Vigerske
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // Author: Ivo Nowak, Stefan Vigerske
00006 
00007 // tools
00008 
00009 #ifndef TOOLS_H
00010 #define TOOLS_H
00011 
00012 #include <sys/resource.h> // for Timer-class
00013 //#include <time.h>
00014 #include <cassert>
00015 
00016 // IO
00017 #include <iomanip>
00018 #include <iostream>
00019 #include <fstream>
00020 
00021 // data structures
00022 #include <vector>
00023 #include <map>
00024 #include <list>
00025 #include <set>
00026 
00027 using namespace std;
00028 
00029 #define rtol 1e-10          // 1e-12 tolerance of real numbers
00030 
00031 #ifndef MIN
00032 #define MIN(a,b) ((a) <= (b) ? (a) : (b))
00033 #endif
00034 #ifndef MAX
00035 #define MAX(a,b) ((a) >= (b) ? (a) : (b))
00036 #endif
00037 
00038 // simply used as a really large number
00039 #ifdef INFINITY
00040 #undef INFINITY
00041 #endif
00042 #define INFINITY 1e99
00043 
00044 // similarly for integers
00045 #define INF 65535
00046 
00049 extern map<const void*, int> refcount;
00050 
00054 extern void print_all_Pointer(ostream& out);
00055 
00059 template <class Type> class Pointer {
00060   public:
00066     static int count(const Type* obj_) {
00067       map<const void*, int>::iterator p(refcount.find((const void*)obj_));
00068       if (p==refcount.end()) return 0;
00069       return p->second;
00070     }
00071 
00072   private:
00075     Type* obj;
00076 
00081     void inc_count() {
00082       pair<map<const void*, int>::iterator, bool> p(refcount.insert(pair<const void*, int>(obj, 1)));
00083       if (!p.second) p.first->second++;  // obj existed before
00084     }
00085 
00089     int dec_count() {
00090       map<const void*, int>::iterator p(refcount.find(obj));
00091 #ifndef NO_NULLPOINTER_CHECK
00092       assert(p!=refcount.end());
00093 #endif
00094       int ret=--p->second;
00095       if (!ret) refcount.erase(p);
00096       return ret;
00097     }
00098 
00102     bool delete_obj;
00103 
00104   public:
00105 
00106 
00108     Pointer()
00109     : obj(NULL), delete_obj(true)
00110     { }
00111 
00116     Pointer(Type* obj_, bool del_=true)
00117     : obj(obj_), delete_obj(del_)
00118     { if (obj && delete_obj) inc_count();
00119     }
00120 
00124     Pointer(const Pointer<Type>& p)
00125     : obj(p.obj), delete_obj(p.delete_obj)
00126     { if (obj && delete_obj) inc_count();
00127     }
00128 
00132     ~Pointer() {
00133       if (obj && delete_obj && (!dec_count())) delete obj;
00134     }
00135 
00139     Pointer<Type>& operator=(const Pointer<Type>& p) {
00140       if (obj==p.obj) return *this;
00141       if (obj && delete_obj && (!dec_count())) delete obj;
00142       obj=p.obj; delete_obj=p.delete_obj;
00143       if (obj) inc_count();
00144       return *this;
00145     }
00146 
00151     Pointer<Type>& operator=(Type* obj_) {
00152       if (obj==obj_) return *this;
00153       if (obj && delete_obj && (!dec_count())) delete obj;
00154       if (obj=obj_) inc_count();
00155       delete_obj=true;
00156       return *this;
00157     }
00158 
00162     operator Type*() const { return obj; }
00163 
00167 //    operator const Type*() const { return (const Type*)obj; }
00168 
00172 //    operator bool() const { return (bool)obj; }
00173 
00177     Type& operator*() const {
00178 #ifndef NO_NULLPOINTER_CHECK
00179       assert(obj);
00180 #endif
00181       return *obj;
00182     }
00183 
00187     Type* operator->() const {
00188 #ifndef NO_NULLPOINTER_CHECK
00189       assert(obj);
00190 #endif
00191       return obj;
00192     }
00193 
00198 /*    bool operator==(const Type* obj_) const {
00199       return (obj==obj_);
00200     }
00201 */
00206     bool operator!=(const Type* obj_) const {
00207       return (obj!=obj_);
00208     }
00209 
00213     int count() const {
00214       return Pointer<Type>::count(obj);
00215     }
00216 
00217 };
00218 
00219 extern Pointer<ostream> out_out_p;  // defined in tools.cc
00220 #define out_out if (out_out_p) (*out_out_p)
00221 
00222 extern Pointer<ostream> out_log_p;  // defined in tools.cc
00223 #define out_log if (out_log_p) (*out_log_p)
00224 
00225 extern Pointer<ostream> out_err_p;  // defined in tools.cc
00226 #define out_err if (out_err_p) (*out_err_p)
00227 
00233 extern int random(int lb, int ub);
00234 
00240 extern double random(double lb, double ub);
00241 
00244 //extern int init_random();
00245 
00249 class Timer {
00256   friend ostream& operator<<(ostream& out, Timer& w) {
00257                 print(out, (double)w);
00258 //    w.print(out);
00259     return out;
00260   }
00261 
00262   private:
00265     struct timeval starttime;
00268     struct timeval endtime;
00269 
00270 //              clock_t starttime;
00271 //              clock_t endtime;
00272 
00273   public:
00278     Timer() {
00279       start();
00280     }
00281 
00286     void start();
00287 
00293     Timer& stop();
00294 
00298     operator double() const;
00299 
00305                 static void print(ostream& out, double time);
00306 //    void print(ostream& out) const;
00307 
00308 };
00309 
00310 
00311 unsigned int get_mem();
00312 
00313 int start_process(const char* name, char*const* const args, int timelimit=0, const char* envvarname=NULL, const char* envvarvalue=NULL);
00314 
00315 #endif // TOOLS_H

Generated on Wed Oct 22 03:12:39 2008 for LaGO by  doxygen 1.4.7