QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
symmatrix.h
Go to the documentation of this file.
1 /*
2  * QuickRank - A C++ suite of Learning to Rank algorithms
3  * Webpage: http://quickrank.isti.cnr.it/
4  * Contact: quickrank@isti.cnr.it
5  *
6  * Unless explicitly acquired and licensed from Licensor under another
7  * license, the contents of this file are subject to the Reciprocal Public
8  * License ("RPL") Version 1.5, or subsequent versions as allowed by the RPL,
9  * and You may not copy or use this file in either source code or executable
10  * form, except in compliance with the terms and conditions of the RPL.
11  *
12  * All software distributed under the RPL is provided strictly on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND
14  * LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
15  * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16  * PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific
17  * language governing rights and limitations under the RPL.
18  *
19  * Contributor:
20  * HPC. Laboratory - ISTI - CNR - http://hpc.isti.cnr.it/
21  */
22 #pragma once
23 
24 #include <cstdlib>
25 
26 /*! \def sm2v(i,j,size)
27  * \brief map a 2D square matrix coorinate pair (\a i, \a j) into a 1D array coordinate; \a size denotes the size of the matrix.
28  */
29 #define sm2v(i, j, size) ((i)*(size)-((i)-1)*(i)/2+(j)-(i))
30 
31 /*! \class symmatrix
32  * \brief symmetric matrix implementation
33  */
34 template<typename T>
35 class SymMatrix {
36  public:
37  /** \brief default constructor: allocate an array of size \a size*(\a size+1)/2.
38  * @param size order of the matrix.
39  */
40  SymMatrix(size_t size)
41  : size(size) {
42  data = size > 0 ? new T[size * (size + 1) / 2]() : NULL;
43  }
45  delete[] data;
46  }
47  /** \brief return the element at position ( \a i, \a j ) for left-hand operation.
48  * @param i row in [0 .. \a size -1]
49  * @param j column in [0 .. \a size -1]
50  */
51  T &at(const size_t i, const size_t j) {
52  return data[i < j ? sm2v(i, j, size) : sm2v(j, i, size)];
53  }
54  /** \brief return the element at position ( \a i, \a j ) for right-hand operation.
55  * @param i row in [0 .. \a size -1]
56  * @param j column in [0 .. \a size -1]
57  */
58  T at(const size_t i, const size_t j) const {
59  return data[i < j ? sm2v(i, j, size) : sm2v(j, i, size)];
60  }
61  /** \brief return the element at position \a i of the array representing the matrix for left-hand operation.
62  * @param i index in [0 .. \a size -1]
63  */
64  T &at(const size_t i) {
65  return data[i];
66  }
67  /** \brief return the element at position \a i of the array representing the matrix for right-hand operation.
68  * @param i index in [0 .. \a size -1]
69  */
70  T at(const size_t i) const {
71  return data[i];
72  }
73  /** \brief return the pointer to the element at position ( \a i, \a j ) of the array representing the matrix.
74  * @param i row in [0 .. \a size -1]
75  * @param j column in [0 .. \a size -1]
76  */
77  T *vectat(const size_t i, const size_t j) {
78  return &data[i < j ? sm2v(i, j, size) : sm2v(j, i, size)];
79  }
80  /** \brief return matrix size.
81  */
82  size_t get_size() const {
83  return size;
84  }
85  private:
86  T *data;
87  size_t size;
88 };
89 
90 #undef sm2v
#define sm2v(i, j, size)
map a 2D square matrix coorinate pair (i, j) into a 1D array coordinate; size denotes the size of the...
Definition: symmatrix.h:29
size_t size
Definition: symmatrix.h:87
T * vectat(const size_t i, const size_t j)
return the pointer to the element at position ( i, j ) of the array representing the matrix...
Definition: symmatrix.h:77
T at(const size_t i) const
return the element at position i of the array representing the matrix for right-hand operation...
Definition: symmatrix.h:70
T * data
Definition: symmatrix.h:86
Definition: symmatrix.h:35
T at(const size_t i, const size_t j) const
return the element at position ( i, j ) for right-hand operation.
Definition: symmatrix.h:58
SymMatrix(size_t size)
default constructor: allocate an array of size size*(size+1)/2.
Definition: symmatrix.h:40
T & at(const size_t i, const size_t j)
return the element at position ( i, j ) for left-hand operation.
Definition: symmatrix.h:51
T & at(const size_t i)
return the element at position i of the array representing the matrix for left-hand operation...
Definition: symmatrix.h:64
size_t get_size() const
return matrix size.
Definition: symmatrix.h:82
~SymMatrix()
Definition: symmatrix.h:44