Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sparsearray.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __SPARSEARRAY_H_
25 #define __SPARSEARRAY_H_
26 
27 #include <cstdlib>
28 #include "stl_map.h"
29 
30 /**
31  * Array which uses an HashTable for elements stored in this array.
32  * Can be used as an base class of SparseMatrix.
33  * first (fast implemented) version.
34  */
35 namespace matrix
36 {
37 
38  #define COMPARE_EPS 1e-12
39 
40  // forward declaration
41  //template<typename I, typename D> class SparseArray;
42 
43 
44 
45  template<typename I, typename D> class SparseArray
46  {
47  public:
49  {
50  public:
51  ArrayElement() : index(0), value(0), hashData(0), dummy(0) { }
52 
53  inline void reallocate(HashMap<I, D* >* hashData)
54  {
55  this->hashData=hashData;
56  value=0;
57  dummy=0;
58  }
59 
60  inline void set (const I index, D value, D* dummy)
61  {
62  this->index=index;
63  this->value=value;
64  this->dummy=dummy;
65  }
66 
67  // TODO: could be tricky: two ArrayElements at the same time???
68  inline D operator+ (ArrayElement& el2) { return value+el2.value; }
69  inline D operator+ (D el2) { return value+el2; }
70  /* inline D operator++ () { this=(value+1); return value; }*/
71  inline bool operator> (D el2) { return value>el2; }
72  // TODO: could be tricky: two ArrayElements at the same time???
73  inline bool operator> (ArrayElement& el2) { return value>el2.value; }
74  inline D& operator= (D value)
75  {
76  if (dummy==0)
77  {
78  dummy = (D*) malloc(sizeof(D));
79  (*hashData)[index]=dummy;
80  }
81  *dummy = value;
82  return *dummy;
83  }
84  inline D& operator= (ArrayElement& el2) { this=el2.value; }
85 
86  inline operator D() { return value; }
87 
88  protected:
91  HashMap<I, D* >* hashData;
92  D* dummy;
93  };
94 
95  SparseArray(I arraySize) : arraySize(arraySize), hashData(0)
96  {
97  allocate();
98  }
99 
100  virtual ~SparseArray() {
101  freeData();
102  }
103 
104  virtual inline void reallocate(I arraySize)
105  {
106  this->arraySize=arraySize;
107  allocate();
108  }
109 
110  virtual inline I size() { return this->arraySize; }
111 
112  inline ArrayElement& operator[](const I index)
113  {
114  typename HashMap<I,D*>::const_iterator iterator = hashData->find(index);
115  if (iterator!= hashData->end())
116  {
117  D* dummy = (*iterator).second;
118  elementDummy.set(index, *dummy, dummy);
119  } else
120  elementDummy.set(index, 0, 0);
121  return elementDummy;
122  }
123 
124  /** sets the data (row-wise).
125  @param _data if null then matrix elements are set to zero
126  otherwise the field MUST have the length should be getSize()*/
127 /* virtual void inline set(const D* _data)
128  {
129  allocate();
130  for (I i=0;i<arraySize;i++)
131  if (_data[i]>COMPARE_EPS)
132  this[i]=_data[i];
133  }*/
134 
135  virtual inline long getRealSize()
136  {
137  long sumSize = sizeof *this;
138  sumSize += sizeof *hashData;
139  sumSize += hashData->size() * (sizeof(D*)+sizeof(D));
140  return sumSize;
141  }
142 
143 
144  protected:
145  ArrayElement elementDummy;
146  /// set of all array values
148  HashMap<I, D* >* hashData;
149 
150  virtual void inline allocate()
151  {
152  if (hashData)
153  freeData();
154  hashData = new HashMap<I, D* >();
156  }
157 
158  virtual void inline freeData()
159  {
160  // TODO: free all elements in hashData
161 // for (HashMap<I,D*>::iterator iterator = hashData->begin();iterator!=hashData->end();iterator++)
162 // free((*iterator).second);
163  if (hashData)
164  {
165  delete hashData;
166  hashData = 0;
167  }
168  }
169 
170  private:
171  SparseArray() {}
172 
173  };
174 
175 
176 } // namespace matrix
177 
178 #endif /* __SPARSEARRAY_H_ */
virtual I size()
Definition: sparsearray.h:110
D value
Definition: sparsearray.h:90
bool operator>(D el2)
Definition: sparsearray.h:71
void set(const I index, D value, D *dummy)
Definition: sparsearray.h:60
ArrayElement & operator[](const I index)
Definition: sparsearray.h:112
Definition: sparsearray.h:48
virtual ~SparseArray()
Definition: sparsearray.h:100
unsigned int I
type for matrix indices
Definition: matrix.h:36
I index
Definition: sparsearray.h:89
D operator+(ArrayElement &el2)
Definition: sparsearray.h:68
HashMap< I, D * > * hashData
Definition: sparsearray.h:148
virtual void reallocate(I arraySize)
Definition: sparsearray.h:104
D & operator=(D value)
Definition: sparsearray.h:74
SparseArray(I arraySize)
Definition: sparsearray.h:95
HashMap< I, D * > * hashData
Definition: sparsearray.h:91
virtual void allocate()
Definition: sparsearray.h:150
Definition: sparsearray.h:45
ArrayElement elementDummy
Definition: sparsearray.h:145
ArrayElement()
Definition: sparsearray.h:51
virtual long getRealSize()
sets the data (row-wise).
Definition: sparsearray.h:135
D * dummy
Definition: sparsearray.h:92
virtual void freeData()
Definition: sparsearray.h:158
void reallocate(HashMap< I, D * > *hashData)
Definition: sparsearray.h:53
I arraySize
set of all array values
Definition: sparsearray.h:147
double D
type for matrix elements
Definition: matrix.h:38