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