File size: 1,927 Bytes
9f5b176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
Copyright 2015 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "core/float_matrix.h"

#include <string.h>



FloatMatrix::FloatMatrix() {
  data_ = NULL;
  x_size_ = 0;
  y_size_ = 0;
}

FloatMatrix::FloatMatrix(const FloatMatrix &m) {
  clear();
  resize(m.x_size_, m.y_size_);
  for (uint32_t i = 0; i < x_size_ * y_size_; i++) {
    data_[i] = m.data_[i];
  }
}

FloatMatrix::~FloatMatrix() {
  clear();
}

void FloatMatrix::operator=(const FloatMatrix &m) {
  clear();
  resize(m.x_size_, m.y_size_);
  for (uint32_t i = 0; i < x_size_ * y_size_; i++) {
    data_[i] = m.data_[i];
  }
}

void FloatMatrix::clear() {
  if (data_ != NULL) {
    delete [] data_;
    data_ = NULL;
  }
  x_size_ = 0;
  y_size_ = 0;
}

void FloatMatrix::resize(uint32_t x_size, uint32_t y_size) {
  if (data_ == NULL) {
    data_ = new float [x_size * y_size];
    memset(data_, 0, sizeof(float)*x_size * y_size);
  } else if (x_size == x_size_ && y_size == y_size_) {
    return;
  } else {
    float *newdata = new float [x_size * y_size];
    memset(newdata, 0, sizeof(float) * x_size * y_size);
    int min_x = x_size < x_size_ ? x_size : x_size_;
    int min_y = y_size < y_size_ ? y_size : y_size_;
    for (int y = 0; y < min_y; y++) {
      memcpy(newdata + y * x_size, data_ + y * x_size_, min_x * sizeof(float));
    }
    delete [] data_;
    data_ = newdata;
  }

  x_size_ = x_size;
  y_size_ = y_size;
}