ROSE 0.11.145.147
Matrix.h
1#ifndef ROSE_BinaryAnalysis_Matrix_H
2#define ROSE_BinaryAnalysis_Matrix_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#ifdef ROSE_HAVE_DLIB
7#include <dlib/matrix.h>
8#endif
9
10#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
11#include <boost/serialization/access.hpp>
12#include <boost/serialization/array.hpp>
13#include <boost/serialization/nvp.hpp>
14#include <boost/serialization/split_member.hpp>
15#endif
16
17namespace Rose {
18namespace BinaryAnalysis {
19
24template<class T>
25class Matrix {
26public:
27 typedef T Value;
28
29#ifdef ROSE_HAVE_DLIB
30 //-------- Dlib implementation --------
31private:
32 dlib::matrix<T> data_;
33public:
34 Matrix(): data_(0, 0) {}
35 explicit Matrix(size_t n): data_(n, n) {}
36 Matrix(size_t nr, size_t nc): data_(nr, nc) {}
37 size_t nr() const { return data_.nr(); }
38 size_t nc() const { return data_.nc(); }
39 T& operator()(size_t i, size_t j) { return data_(i, j); }
40 const T& operator()(size_t i, size_t j) const { return data_(i, j); }
41 const dlib::matrix<T>& dlib() const { return data_; }
42#else
43 //-------- ROSE implementation --------
44private:
45 size_t nr_, nc_;
46 T* data_;
47public:
48 // Construction and destruction
49 Matrix(): nr_(0), nc_(0), data_(NULL) {}
50 explicit Matrix(size_t n): nr_(n), nc_(n), data_(new T[n*n]) {}
51 Matrix(size_t nr, size_t nc): nr_(nr), nc_(nc), data_(new T[nr*nc]) {}
52 ~Matrix() { delete[] data_; }
53
54 // Copying
55 Matrix(const Matrix<T> &other): nr_(other.nr_), nc_(other.nc_), data_(new T[other.size()]) {
56 memcpy(data_, other.data_, size()*sizeof(T));
57 }
58 Matrix& operator=(const Matrix<T> &other) {
59 if (size() != other.size()) {
60 delete[] data_;
61 data_ = new T[other.size()];
62 }
63 nr_ = other.nr_;
64 nc_ = other.nc_;
65 memcpy(data_, other.data_, size()*sizeof(T));
66 return *this;
67 }
68
69 // Size
70 size_t nr() const { return nr_; }
71 size_t nc() const { return nc_; }
72
73 // Element access
74 T& operator()(size_t i, size_t j) {
75 ASSERT_require(i < nr_);
76 ASSERT_require(j < nc_);
77 return data_[i*nc_+j];
78 }
79 const T& operator()(size_t i, size_t j) const {
80 ASSERT_require(i < nr_);
81 ASSERT_require(j < nc_);
82 return data_[i*nc_+j];
83 }
84
85 // dlib::matrix<T> &dlib() -- cannot do this here since there's no dlib
86#endif
87
88 //-------- Common implementation --------
89public:
90 size_t size() const { return nr()*nc(); }
91
92#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
93private:
94 friend class boost::serialization::access;
95
96 template<class S>
97 void save(S &s, const unsigned /*version*/) const {
98 size_t nr = this->nr(), nc = this->nc();
99 s & BOOST_SERIALIZATION_NVP(nr);
100 s & BOOST_SERIALIZATION_NVP(nc);
101 if (nr > 0 && nc > 0) {
102 const T *values = &(*this)(0, 0);
103 s & boost::serialization::make_nvp("values", boost::serialization::make_array(values, nr*nc));
104 }
105 }
106
107 template<class S>
108 void load(S &s, const unsigned /*version*/) {
109 size_t nr, nc;
110 s & BOOST_SERIALIZATION_NVP(nr);
111 s & BOOST_SERIALIZATION_NVP(nc);
112 Matrix<T> tmp(nr, nc);
113 if (nr > 0 && nc > 0) {
114 T *values = &tmp(0, 0);
115 s & boost::serialization::make_nvp("values", boost::serialization::make_array(values, nr*nc));
116 }
117 *this = tmp;
118 }
119
120 BOOST_SERIALIZATION_SPLIT_MEMBER();
121#endif
122};
123
124} // namespace
125} // namespace
126
127#endif
128#endif
The ROSE library.