[go: up one dir, main page]

File: Matrices.h

package info (click to toggle)
timbl 6.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 17,211; ansic: 425; sh: 70; makefile: 63
file content (124 lines) | stat: -rw-r--r-- 3,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
  Copyright (c) 1998 - 2024
  ILK   - Tilburg University
  CLST  - Radboud University
  CLiPS - University of Antwerp

  This file is part of timbl

  timbl is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  timbl is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.

  For questions and suggestions, see:
      https://github.com/LanguageMachines/timbl/issues
  or send mail to:
      lamasoftware (at ) science.ru.nl

*/

#ifndef TIMBL_MATRICES_H
#define TIMBL_MATRICES_H

template <class T>  class SparseSymetricMatrix;
template <class T> std::ostream& operator << (std::ostream&,
					      const SparseSymetricMatrix<T>& );

template <class Class>
class SparseSymetricMatrix {
  using CDmap = std::map< Class, double >;
  using CCDmap = std::map< Class, CDmap >;
  friend std::ostream& operator << <> ( std::ostream&,
					const SparseSymetricMatrix<Class>& );

 public:
  void Clear() { my_mat.clear(); };
  void Assign( Class i, Class j, double d ){
    if ( i == j )
      return;
    if ( i <j )
      my_mat[j][i] = d;
      else
	my_mat[i][j] = d;
  };
  double Extract( Class i, Class j ) const {
    if ( i == j ){
      return 0.0;
    }
    if ( i < j ){
      typename CCDmap::const_iterator it1 = my_mat.find(j);
      if ( it1 != my_mat.end() ){
	typename CDmap::const_iterator it2 = it1->second.find(i);
	if ( it2 != it1->second.end() ){
	  return it2->second;
	}
      }
    }
    else {
      typename CCDmap::const_iterator it1 = my_mat.find(i);
      if ( it1 != my_mat.end() ){
	typename CDmap::const_iterator it2 = it1->second.find(j);
	if ( it2 != it1->second.end() ){
	  return it2->second;
	}
      }
    }
    return 0.0;
  };
  unsigned int NumBytes(void) const{
    unsigned int tot = sizeof(std::map<Class, CDmap>);
    typename CCDmap::const_iterator it1 = my_mat.begin();
    while ( it1 != my_mat.end() ){
      tot +=  sizeof(CDmap);
      typename CDmap::const_iterator it2 = it1->second.begin();
      while ( it2 != it1->second.end() ){
	tot += sizeof(double);
	++it2;
      }
      ++it1;
    }
    return tot;
  };
  SparseSymetricMatrix<Class> *copy(void) const{
    SparseSymetricMatrix<Class> *res = new SparseSymetricMatrix<Class>();
    typename CCDmap::const_iterator it1 = my_mat.begin();
    while ( it1 != my_mat.end() ){
      typename CDmap::const_iterator it2 = it1->second.begin();
      while ( it2 != it1->second.end() ){
	res->my_mat[it1->first][it2->first] = it2->second;
	++it2;
      }
      ++it1;
    }
    return res;
  }
 private:
  CCDmap my_mat;
};

template <class T>
inline std::ostream& operator << (std::ostream& os,
				  const SparseSymetricMatrix<T>& m ){
  typename SparseSymetricMatrix<T>::CCDmap::const_iterator it1 = m.my_mat.begin();
  while ( it1 != m.my_mat.end() ){
    typename SparseSymetricMatrix<T>::CDmap::const_iterator it2 = it1->second.begin();
    while ( it2 != it1->second.end() ){
      os << "[" << it1->first << ",\t" << it2->first << "] "
	<< it2->second << std::endl;
      ++it2;
    }
    ++it1;
  }
  return os;
}

#endif // TIMBL_MATRICES_H