summaryrefslogtreecommitdiff
path: root/utils/array2d.h
blob: e63eda0d2e21b2be305778fcf552e989204cc957 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifndef ARRAY2D_H_
#define ARRAY2D_H_

#include <iostream>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>

template<typename T>
class Array2D {
 public:
  typedef typename std::vector<T>::reference reference;
  typedef typename std::vector<T>::const_reference const_reference;
  typedef typename std::vector<T>::iterator iterator;
  typedef typename std::vector<T>::const_iterator const_iterator;
  Array2D() : width_(0), height_(0) {}
  Array2D(int w, int h, const T& d = T()) :
    width_(w), height_(h), data_(w*h, d) {}
  Array2D(const Array2D& rhs) :
    width_(rhs.width_), height_(rhs.height_), data_(rhs.data_) {}
  bool empty() const { return data_.empty(); }
  void resize(int w, int h, const T& d = T()) {
    data_.resize(w * h, d);
    width_ = w;
    height_ = h;
  }
  const Array2D& operator=(const Array2D& rhs) {
    data_ = rhs.data_;
    width_ = rhs.width_;
    height_ = rhs.height_;
    return *this;
  }
  void fill(const T& v) { data_.assign(data_.size(), v); }
  int width() const { return width_; }
  int height() const { return height_; }
  reference operator()(int i, int j) {
    return data_[offset(i, j)];
  }
  void clear() { data_.clear(); width_=0; height_=0; }
  const_reference operator()(int i, int j) const {
    return data_[offset(i, j)];
  }
  iterator begin_col(int j) {
    return data_.begin() + offset(0,j);
  }
  const_iterator begin_col(int j) const {
    return data_.begin() + offset(0,j);
  }
  iterator end_col(int j) {
    return data_.begin() + offset(0,j) + width_;
  }
  const_iterator end_col(int j) const {
    return data_.begin() + offset(0,j) + width_;
  }
  iterator end() { return data_.end(); }
  const_iterator end() const { return data_.end(); }
  const Array2D<T>& operator*=(const T& x) {
    std::transform(data_.begin(), data_.end(), data_.begin(),
        std::bind2nd(std::multiplies<T>(), x));
  }
  const Array2D<T>& operator/=(const T& x) {
    std::transform(data_.begin(), data_.end(), data_.begin(),
        std::bind2nd(std::divides<T>(), x));
  }
  const Array2D<T>& operator+=(const Array2D<T>& m) {
    std::transform(m.data_.begin(), m.data_.end(), data_.begin(), data_.begin(), std::plus<T>());
  }
  const Array2D<T>& operator-=(const Array2D<T>& m) {
    std::transform(m.data_.begin(), m.data_.end(), data_.begin(), data_.begin(), std::minus<T>());
  }

 private:
  inline int offset(int i, int j) const {
    assert(i<width_);
    assert(j<height_);
    return i + j * width_;
  }

  int width_;
  int height_;

  std::vector<T> data_;
};

template <typename T>
Array2D<T> operator*(const Array2D<T>& l, const T& scalar) {
  Array2D<T> res(l);
  res *= scalar;
  return res;
}

template <typename T>
Array2D<T> operator*(const T& scalar, const Array2D<T>& l) {
  Array2D<T> res(l);
  res *= scalar;
  return res;
}

template <typename T>
Array2D<T> operator/(const Array2D<T>& l, const T& scalar) {
  Array2D<T> res(l);
  res /= scalar;
  return res;
}

template <typename T>
Array2D<T> operator+(const Array2D<T>& l, const Array2D<T>& r) {
  Array2D<T> res(l);
  res += r;
  return res;
}

template <typename T>
Array2D<T> operator-(const Array2D<T>& l, const Array2D<T>& r) {
  Array2D<T> res(l);
  res -= r;
  return res;
}

template <typename T>
inline std::ostream& operator<<(std::ostream& os, const Array2D<T>& m) {
  for (int i=0; i<m.width(); ++i) {
    for (int j=0; j<m.height(); ++j)
      os << '\t' << m(i,j);
    os << '\n';
  }
  return os;
}

inline std::ostream& operator<<(std::ostream& os, const Array2D<bool>& m) {
  os << ' ';
  for (int j=0; j<m.height(); ++j)
    os << (j%10);
  os << "\n";
  for (int i=0; i<m.width(); ++i) {
    os << (i%10);
    for (int j=0; j<m.height(); ++j)
      os << (m(i,j) ? '*' : '.');
    os << (i%10) << "\n";
  }
  os << ' ';
  for (int j=0; j<m.height(); ++j)
    os << (j%10);
  os << "\n";
  return os;
}

inline std::ostream& operator<<(std::ostream& os, const Array2D<std::vector<bool> >& m) {
  os << ' ';
  for (int j=0; j<m.height(); ++j)
    os << (j%10) << "\t";
  os << "\n";
  for (int i=0; i<m.width(); ++i) {
    os << (i%10);
    for (int j=0; j<m.height(); ++j) {
      const std::vector<bool>& ar = m(i,j);
      for (int k=0; k<ar.size(); ++k)
        os << (ar[k] ? '*' : '.');
    }
    os << "\t";
    os << (i%10) << "\n";
  }
  os << ' ';
  for (int j=0; j<m.height(); ++j)
    os << (j%10) << "\t";
  os << "\n";
  return os;
}

#endif