Couenne  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CouenneSparseBndVec.hpp
Go to the documentation of this file.
1 /* $Id: CouenneSparseBndVec.hpp 490 2011-01-14 16:07:12Z pbelotti $
2  *
3  * Name: CouenneSparseBndVec.hpp
4  * Author: Pietro Belotti
5  * Purpose: Keep track of tightened bounds with a sparse vector
6  *
7  * (C) Pietro Belotti, 2010.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #ifndef COUENNESPARSEBNDVEC_HPP
12 #define COUENNESPARSEBNDVEC_HPP
13 
14 namespace Couenne {
15 
16  template <class T> class CouenneSparseBndVec {
17 
39 
40  private:
41 
42  unsigned int size_;
43  unsigned int n_;
44  unsigned int *dInd_;
45  unsigned int *sInd_;
46  T *data_;
47 
48  public:
49 
51  CouenneSparseBndVec (unsigned int size):
52 
53  size_ (size),
54  n_ (0) {
55 
56  dInd_ = new unsigned int [size_];
57  sInd_ = new unsigned int [size_];
58  data_ = new T [size_];
59  }
60 
63 
64  size_ (src.size_),
65  n_ (src.n_) {
66 
67  for (register unsigned int i=0; i<n_; i++) {
68 
69  register unsigned int ind = (dInd_ [i] = src.dInd_ [i]);
70  data_ [ind] = src.data_ [ind];
71  sInd_ [ind] = i;
72  }
73  }
74 
77  delete [] sInd_;
78  delete [] dInd_;
79  delete [] data_;
80  }
81 
83  void reset ()
84  {n_ = 0;}
85 
91  T &operator[] (register unsigned int index) {
92 
93  register unsigned int &sind = sInd_ [index];
94 
95  if ((sind >= n_) ||
96  (dInd_ [sind] != index))
97  dInd_ [sind = n_++] = index; // this entry is new and has to be initialized
98 
99  return data_ [sind];
100  }
101 
103  T *data ()
104  {return data_;}
105 
107  unsigned int *indices ()
108  {return dInd_;}
109 
111  unsigned int nElements ()
112  {return n_;}
113 
115  void resize (unsigned int newsize)
116  {size_ = newsize;}
117  };
118 }
119 
120 #endif
121 
122 // #include <stdio.h>
123 // #include <stdlib.h>
124 
125 // int main () {
126 
127 // Couenne::CouenneSparseBndVec <int> v (100);
128 
129 // v[84] = 10;
130 // v[0] = 60;
131 // v[99] = 63;
132 // v[72] = 16;
133 // v[84] = 70;
134 // v[25] = 33;
135 // v[21] = 15;
136 // v[21] = 12;
137 // v[21] = 12;
138 // v[8] = 22;
139 // v[4] = 66;
140 
141 // srand48(1243235);
142 // for (int i=0; i<1e9; i++)
143 // v [(int)(99.999 * drand48())] = (int)(10000 * drand48());
144 
145 // for (int i=0; i< v.nElements(); i++)
146 // printf ("v [%d] = %d\n", v.indices () [i], v.data () [i]);
147 // }
unsigned int * sInd_
indices vector, sparse (lots of garbage in between entries)
void resize(unsigned int newsize)
Resize.
unsigned int nElements()
Return current size.
void reset()
Reset (eeeeasy!)
CouenneSparseBndVec(CouenneSparseBndVec &src)
Copy constructor.
T * data_
data vector, sparse (lots of garbage in between entries)
T * data()
Return data in DENSE format – use with care.
unsigned int n_
current number of elements
unsigned int * dInd_
indices vector, dense (garbage exists past n_)
CouenneSparseBndVec(unsigned int size)
Constructor.
T & operator[](register unsigned int index)
Access – the only chance for garbage to be returned (and for valgrind to complain) is when object[ind...
unsigned int * indices()
Return indices in DENSE format – for use with data()
unsigned int size_
Implements a fast sparse+dense vector data structure, with a size of n and a number k of nonzero elem...