00001 /* $Id: CouenneSparseBndVec.hpp 490 2011-01-14 16:07:12Z pbelotti $ 00002 * 00003 * Name: CouenneSparseBndVec.hpp 00004 * Author: Pietro Belotti 00005 * Purpose: Keep track of tightened bounds with a sparse vector 00006 * 00007 * (C) Pietro Belotti, 2010. 00008 * This file is licensed under the Eclipse Public License (EPL) 00009 */ 00010 00011 #ifndef COUENNESPARSEBNDVEC_HPP 00012 #define COUENNESPARSEBNDVEC_HPP 00013 00014 namespace Couenne { 00015 00016 template <class T> class CouenneSparseBndVec { 00017 00039 00040 private: 00041 00042 unsigned int size_; 00043 unsigned int n_; 00044 unsigned int *dInd_; 00045 unsigned int *sInd_; 00046 T *data_; 00047 00048 public: 00049 00051 CouenneSparseBndVec (unsigned int size): 00052 00053 size_ (size), 00054 n_ (0) { 00055 00056 dInd_ = new unsigned int [size_]; 00057 sInd_ = new unsigned int [size_]; 00058 data_ = new T [size_]; 00059 } 00060 00062 CouenneSparseBndVec (CouenneSparseBndVec &src): 00063 00064 size_ (src.size_), 00065 n_ (src.n_) { 00066 00067 for (register unsigned int i=0; i<n_; i++) { 00068 00069 register unsigned int ind = (dInd_ [i] = src.dInd_ [i]); 00070 data_ [ind] = src.data_ [ind]; 00071 sInd_ [ind] = i; 00072 } 00073 } 00074 00076 ~CouenneSparseBndVec () { 00077 delete [] sInd_; 00078 delete [] dInd_; 00079 delete [] data_; 00080 } 00081 00083 void reset () 00084 {n_ = 0;} 00085 00091 T &operator[] (register unsigned int index) { 00092 00093 register unsigned int &sind = sInd_ [index]; 00094 00095 if ((sind >= n_) || 00096 (dInd_ [sind] != index)) 00097 dInd_ [sind = n_++] = index; // this entry is new and has to be initialized 00098 00099 return data_ [sind]; 00100 } 00101 00103 T *data () 00104 {return data_;} 00105 00107 unsigned int *indices () 00108 {return dInd_;} 00109 00111 unsigned int nElements () 00112 {return n_;} 00113 00115 void resize (unsigned int newsize) 00116 {size_ = newsize;} 00117 }; 00118 } 00119 00120 #endif 00121 00122 // #include <stdio.h> 00123 // #include <stdlib.h> 00124 00125 // int main () { 00126 00127 // Couenne::CouenneSparseBndVec <int> v (100); 00128 00129 // v[84] = 10; 00130 // v[0] = 60; 00131 // v[99] = 63; 00132 // v[72] = 16; 00133 // v[84] = 70; 00134 // v[25] = 33; 00135 // v[21] = 15; 00136 // v[21] = 12; 00137 // v[21] = 12; 00138 // v[8] = 22; 00139 // v[4] = 66; 00140 00141 // srand48(1243235); 00142 // for (int i=0; i<1e9; i++) 00143 // v [(int)(99.999 * drand48())] = (int)(10000 * drand48()); 00144 00145 // for (int i=0; i< v.nElements(); i++) 00146 // printf ("v [%d] = %d\n", v.indices () [i], v.data () [i]); 00147 // }