src/work/alpar/matrix.cc
author hegyi
Fri, 01 Apr 2005 13:42:31 +0000
changeset 1290 082fc511c2b9
permissions -rw-r--r--
To run graph-displayer with sample input, type make run, but do not move the nodes, YET
alpar@994
     1
#include <lemon/time_measure.h>
alpar@994
     2
#include <vector>
alpar@994
     3
alpar@994
     4
template<class T>
alpar@994
     5
class Matrix 
alpar@994
     6
{
alpar@994
     7
public:
alpar@994
     8
  int m,n;
alpar@994
     9
  std::vector<T> values;
alpar@994
    10
  
alpar@994
    11
  Matrix(int _m, int _n) : m(_m), n(_n), values(n*m) {}
alpar@994
    12
  typename std::vector<T>::reference operator()(int i, int j)
alpar@994
    13
  { return values[i*n+j];}
alpar@994
    14
alpar@994
    15
  class Row 
alpar@994
    16
  {
alpar@994
    17
  public:
alpar@994
    18
    Matrix<T> &mat;
alpar@994
    19
    int row;
alpar@994
    20
    
alpar@994
    21
    Row(Matrix<T> &_mat,int _row) : mat(_mat), row(_row) {}
alpar@994
    22
    typename std::vector<T>::reference operator()(int j)
alpar@994
    23
    { return mat.values[row*mat.n+j];}
alpar@994
    24
  };
alpar@994
    25
  class Column 
alpar@994
    26
  {
alpar@994
    27
  public:
alpar@994
    28
    Matrix<T> &mat;
alpar@994
    29
    int col;
alpar@994
    30
    
alpar@994
    31
    Column(Matrix<T> &_mat,int _col) : mat(_mat), col(_col) {}
alpar@994
    32
    typename std::vector<T>::reference operator[](int i)
alpar@994
    33
    { return mat.values[i*mat.n+col];}
alpar@994
    34
  };
alpar@994
    35
  
alpar@994
    36
  Row operator[](int i) { return Row(*this,i); }
alpar@994
    37
  Column operator()(int j) { return Column(*this,j); }
alpar@994
    38
};
alpar@994
    39
alpar@994
    40
main() 
alpar@994
    41
{
alpar@994
    42
  const int COUNT=1000000;
alpar@994
    43
  const int SIZE=100;
alpar@994
    44
  
alpar@994
    45
  Matrix<int> A(SIZE,SIZE);
alpar@994
    46
  
alpar@994
    47
  for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) A(i,j)=i+j;
alpar@994
    48
alpar@994
    49
  int s=0;
alpar@994
    50
  lemon::Timer T;
alpar@994
    51
  for(int c=0;c<COUNT;c++) {
alpar@994
    52
    for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A[i](j);
alpar@994
    53
  }
alpar@994
    54
  std::cout << "A[i](j): " << T << ' ' << s <<'\n';
alpar@994
    55
  s=0;
alpar@994
    56
  T.reset();
alpar@994
    57
  for(int c=0;c<COUNT;c++) {
alpar@994
    58
    for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(i,j);
alpar@994
    59
  }
alpar@994
    60
  std::cout << "A(i,j): " << T << ' ' << s << '\n';
alpar@994
    61
  s=0;
alpar@994
    62
  T.reset();
alpar@994
    63
  for(int c=0;c<COUNT;c++) {
alpar@994
    64
    for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(j)[i];
alpar@994
    65
  }
alpar@994
    66
  std::cout << "A(j)[i]: " << T << ' ' << s << '\n';
alpar@994
    67
  s=0;
alpar@994
    68
  T.reset();
alpar@994
    69
  for(int c=0;c<COUNT;c++) {
alpar@994
    70
    for(int i=0;i<SIZE;i++) for(int j=0;j<SIZE;j++) s+=A(i,j);
alpar@994
    71
  }
alpar@994
    72
  std::cout << "A(j,i): " << T << ' ' << s << "\n\n";
alpar@994
    73
alpar@994
    74
  return 0;
alpar@994
    75
}