iRoCS Toolbox  1.1.0
TriangularMatrix.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
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 3 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 Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: Triangular Matrix used to store Models for One Vs. One SMV's
26 ** $RCSfile$
27 ** $Revision: 4820 $$Name$
28 ** $Date: 2011-11-08 10:57:01 +0100 (Tue, 08 Nov 2011) $
29 ** Copyright: GPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.1 2004/08/26 08:36:59 ronneber
38 ** initital import
39 **
40 ** Revision 1.6 2003/05/19 11:24:19 ronneber
41 ** - fixed segfault when calling resize( 0)
42 **
43 ** Revision 1.5 2002/10/28 16:00:52 mechnich
44 ** fixed compatibility problems with gcc3
45 **
46 ** Revision 1.4 2002/05/23 19:06:48 ronneber
47 ** - created Constructor with empty arguments
48 **
49 ** Revision 1.3 2002/05/14 10:17:46 ronneber
50 ** - now representing an _upper_ triangular matrix (to be compatible with
51 ** orig libsvm)
52 ** - added begin() and end() methods to access the raw data
53 **
54 ** Revision 1.2 2002/05/06 12:26:03 ronneber
55 ** - first functional version
56 **
57 ** Revision 1.1 2002/05/03 13:55:52 ronneber
58 ** corrected typo in filename
59 **
60 ** Revision 1.1 2002/05/02 15:43:55 ronneber
61 ** initial revision
62 **
63 **
64 **
65 **************************************************************************/
66 
67 #ifndef TRIANGULARMATRIX_HH
68 #define TRIANGULARMATRIX_HH
69 
70 #ifdef HAVE_CONFIG_H
71 #include <config.hh>
72 #endif
73 
74 #include <vector>
75 
76 namespace svt
77 {
78 
79  /*======================================================================*/
86  /*======================================================================*/
87 
88 
89  template< typename T>
91  {
92  public:
93  typedef T value_type;
94  typedef typename std::vector<T>::size_type size_type;
95  typedef typename std::vector<T>::reference reference;
96  typedef typename std::vector<T>::const_reference const_reference;
97  typedef typename std::vector<T>::iterator iterator;
98  typedef typename std::vector<T>::const_iterator const_iterator;
99 
101  :_width( 0)
102  {
103  }
104 
105  TriangularMatrix( size_type width, const T& defaultVal = T())
106  {
107  resizeWidth( width, defaultVal);
108  }
109  /*======================================================================*/
117  /*======================================================================*/
118  void resizeWidth( size_type width, const T& defaultVal = T())
119  {
120  _width = width;
121  if( width == 0)
122  {
123  _data.resize(0);
124  _rowStart.resize(0);
125  return;
126  }
127 
128  _data.resize( width*(width-1)/2, defaultVal);
129  _rowStart.resize( width-1);
130  int lineWidth = static_cast<int>(width-2);
131  int rowStartIndex = -1;
132  for(int i = 0; i < static_cast<int>(width - 1); ++i)
133  {
134  _rowStart[i] = rowStartIndex;
135  rowStartIndex += lineWidth;
136  --lineWidth;
137  }
138  }
139 
140 
141  size_type width() const
142  {
143  return _width;
144  }
145 
146 
147  size_type nElements() const
148  {
149  return _data.size();
150  }
151 
152  /*======================================================================*/
158  /*======================================================================*/
159  size_type size() const
160  {
161  return _data.size();
162  }
163 
164 
165  /*======================================================================*/
175  /*======================================================================*/
176  reference operator()(size_type row, size_type col)
177  {
178  return _data[_rowStart[row]+col];
179  }
180 
181  /*======================================================================*/
191  /*======================================================================*/
192  const_reference operator()(size_type row, size_type col) const
193  {
194  return _data[_rowStart[row]+col];
195  }
196 
197  /*======================================================================*/
213  /*======================================================================*/
214  reference operator[](size_type index)
215  {
216  return _data[index];
217  }
218 
219  const_reference operator[](size_type index) const
220  {
221  return _data[index];
222  }
223 
224 
225  const_iterator begin() const { return _data.begin(); }
226  iterator begin() { return _data.begin(); }
227  const_iterator end() const { return _data.end(); }
228  iterator end() { return _data.end(); }
229 
230  private:
231  std::vector<int> _rowStart; // indizes of row starts
232  std::vector<T> _data;
233  size_type _width;
234  };
235 }
236 
237 
238 #endif
std::vector< T >::reference reference
const_iterator end() const
The TriangularMatrix class is an upper triangular matrix without diagonal elements.
size_type width() const
size_type size() const
to be compatible with other standard containers
size_type nElements() const
TriangularMatrix(size_type width, const T &defaultVal=T())
std::vector< T >::iterator iterator
reference operator[](size_type index)
1dim element access, accessing data in lexicographical order (e.g.
const_iterator begin() const
std::vector< T >::const_reference const_reference
const_reference operator[](size_type index) const
void resizeWidth(size_type width, const T &defaultVal=T())
resize triangular matrix to new width.
const_reference operator()(size_type row, size_type col) const
const 2dim element access.
std::vector< T >::size_type size_type
std::vector< T >::const_iterator const_iterator
reference operator()(size_type row, size_type col)
2dim element access.