title
Graph Drawing Toolkit

An object-oriented C++ library for handling and drawing graphs

gdtarray.h

Go to the documentation of this file.
00001 
00003 #ifndef __gdtarray__
00004 #define __gdtarray__
00005 
00006 //Qualche header fa undefine di NULL
00007 //Probabilmente quando sara' finito il distacco non servira'
00008 #ifndef NULL 
00009 #define NULL 0
00010 #endif
00011 //
00012 
00013 #include <assert.h>
00014 
00015 namespace gdt {
00016 
00017 #define MAX(a,b) ((a)>(b) ? (a) : (b))
00018 #define MIN(a,b) ((a)<(b) ? (a) : (b))
00019 
00020 template<class E>
00021 class gdtarray {
00022 
00023 private:
00024 
00025         E* table;
00026 
00027         int _low;
00028         int _size;
00029 
00030         void alloc_table(E*& table, int size) {
00031                 table = new E[size];
00032                 assert(table);
00033         }
00034 
00035         void dealloc_table(E*& table, int size) {
00036                 assert(table != NULL);
00037                 delete [] table;
00038                 table = NULL;
00039         }       
00040 
00041         void copy(const gdtarray<E>& a) {
00042                 init(a.low(), a.high());                
00043                 for(int i = low(); i <= high(); ++i) {
00044                         table[i - low()] = a[i];
00045                 }
00046         }
00047 
00048 public:
00049 
00050         typedef void* item;
00051 
00052         /****************
00053          * Constructors * 
00054          ****************/
00055 
00056         gdtarray(int l, int h) : table(NULL) { init(l,h); }
00057         gdtarray(int n) : table(NULL) { init(0, n-1); }
00058         gdtarray() : table(NULL) { }
00059 
00060         gdtarray(const gdtarray<E>& a) : table(NULL) { copy(a); }
00061 
00062         ~gdtarray()  { if (table) dealloc_table(table, _size);  }
00063 
00064         gdtarray<E>& operator = (const gdtarray<E>& a) {
00065                 if (table) dealloc_table(table, _size);
00066                 copy(a);
00067                 return *this;
00068         }
00069 
00070         void init(int l, int h) {
00071                 assert(table == NULL);
00072                 _low = l;
00073                 _size = h - l + 1;
00074                 alloc_table(table, _size);
00075         }
00076 
00077         const E& operator[](int x) const { 
00078                 assert(x >= low() && x <= high());
00079                 return table[x - low()];
00080         }
00081 
00082         E& operator[](int x) {
00083                 assert(x >= low() && x <= high());
00084                 return table[x - low()];
00085         }
00086 
00087         void resize(int a, int b) {             
00088                 E* table2;
00089                 alloc_table(table2, b - a + 1);         
00090                 if (table) {                    
00091                         int l1 = MAX(low(), a);
00092                         int h1 = MIN(high(), b);                
00093                         for (int i = l1; i <= h1; ++i) {
00094                                 table2[i-a] = table[i-_low];
00095                         }       
00096                         dealloc_table(table, size());
00097                 }
00098                 _low = a;
00099                 _size = b - a + 1;
00100                 table = table2;
00101         }       
00102 
00103         void resize(int n) { 
00104                 resize(0, n - 1);               
00105         }
00106 
00107         int low()  const { return _low; }
00108         int high() const { return _low + _size - 1; }
00109         int size() const { return _size; }      
00110 
00111         void swap(int i, int j) {
00112                 assert(false);
00113                 E x = table[i-low];
00114                 table[i-low] = table[j-low];
00115                 table[j-low] = x;
00116         }       
00117 
00118         //*****************
00119         //Iteration methods
00120         //*****************
00121 
00122         const E& inf(item it) const {
00123                 return *(E*)it; 
00124         }
00125 };
00126 
00127 }
00128 
00129 #endif

Generated on Thu Jan 10 14:48:01 2008 for GDToolkit GAPI by  doxygen 1.5.3