~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/doc/swig/test/Array2.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik, Riku Voipio, Tiziano Zito, Carlos Galisteo, Ondrej Certik
  • Date: 2008-07-08 15:08:16 UTC
  • mfrom: (0.1.21 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080708150816-ekf992jcp2k1eua3
Tags: 1:1.1.0-3
[ Riku Voipio ]
* debian/control: atlas is not available on armel, and after a quick look
  neither on alpha. I'd also suggest dropping
  libatlas-sse-dev|libatlas-sse2-dev|libatlas-3dnow-dev alternative combo
  away, these are potentially dangerous on buildd's. Ondrej: dropped.
  (Closes: #489568)

[ Tiziano Zito ]
* patch: build _dotblas.c when ATLAS is not installed, build-conflict with
  atlas, build-depend on blas+lapack only, as it used to be (Closes: #489726)

[ Carlos Galisteo ]
* debian/control
  - Added Homepage field.

[ Ondrej Certik ]
* Checked the package on i386 and amd64, both with and without atlas, all
  tests run and the numpy package is faster if atlas is around. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Array2.h"
 
2
#include <sstream>
 
3
 
 
4
// Default constructor
 
5
Array2::Array2() :
 
6
  _ownData(false), _nrows(0), _ncols(), _buffer(0), _rows(0)
 
7
{ }
 
8
 
 
9
// Size/array constructor
 
10
Array2::Array2(int nrows, int ncols, long* data) :
 
11
  _ownData(false), _nrows(0), _ncols(), _buffer(0), _rows(0)
 
12
{
 
13
  resize(nrows, ncols, data);
 
14
}
 
15
 
 
16
// Copy constructor
 
17
Array2::Array2(const Array2 & source) :
 
18
  _nrows(source._nrows), _ncols(source._ncols)
 
19
{
 
20
  _ownData = true;
 
21
  allocateMemory();
 
22
  *this = source;
 
23
}
 
24
 
 
25
// Destructor
 
26
Array2::~Array2()
 
27
{
 
28
  deallocateMemory();
 
29
}
 
30
 
 
31
// Assignment operator
 
32
Array2 & Array2::operator=(const Array2 & source)
 
33
{
 
34
  int nrows = _nrows < source._nrows ? _nrows : source._nrows;
 
35
  int ncols = _ncols < source._ncols ? _ncols : source._ncols;
 
36
  for (int i=0; i < nrows; ++i)
 
37
  {
 
38
    for (int j=0; j < ncols; ++j)
 
39
    {
 
40
      (*this)[i][j] = source[i][j];
 
41
    }
 
42
  }
 
43
  return *this;
 
44
}
 
45
 
 
46
// Equals operator
 
47
bool Array2::operator==(const Array2 & other) const
 
48
{
 
49
  if (_nrows != other._nrows) return false;
 
50
  if (_ncols != other._ncols) return false;
 
51
  for (int i=0; i < _nrows; ++i)
 
52
  {
 
53
    for (int j=0; j < _ncols; ++j)
 
54
    {
 
55
      if ((*this)[i][j] != other[i][j]) return false;
 
56
    }
 
57
  }
 
58
  return true;
 
59
}
 
60
 
 
61
// Length accessors
 
62
int Array2::nrows() const
 
63
{
 
64
  return _nrows;
 
65
}
 
66
 
 
67
int Array2::ncols() const
 
68
{
 
69
  return _ncols;
 
70
}
 
71
 
 
72
// Resize array
 
73
void Array2::resize(int nrows, int ncols, long* data)
 
74
{
 
75
  if (nrows < 0) throw std::invalid_argument("Array2 nrows less than 0");
 
76
  if (ncols < 0) throw std::invalid_argument("Array2 ncols less than 0");
 
77
  if (nrows == _nrows && ncols == _ncols) return;
 
78
  deallocateMemory();
 
79
  _nrows = nrows;
 
80
  _ncols = ncols;
 
81
  if (!data)
 
82
  {
 
83
    allocateMemory();
 
84
  }
 
85
  else
 
86
  {
 
87
    _ownData = false;
 
88
    _buffer  = data;
 
89
    allocateRows();
 
90
  }
 
91
}
 
92
 
 
93
// Set item accessor
 
94
Array1 & Array2::operator[](int i)
 
95
{
 
96
  if (i < 0 || i > _nrows) throw std::out_of_range("Array2 row index out of range");
 
97
  return _rows[i];
 
98
}
 
99
 
 
100
// Get item accessor
 
101
const Array1 & Array2::operator[](int i) const
 
102
{
 
103
  if (i < 0 || i > _nrows) throw std::out_of_range("Array2 row index out of range");
 
104
  return _rows[i];
 
105
}
 
106
 
 
107
// String output
 
108
std::string Array2::asString() const
 
109
{
 
110
  std::stringstream result;
 
111
  result << "[ ";
 
112
  for (int i=0; i < _nrows; ++i)
 
113
  {
 
114
    if (i > 0) result << "  ";
 
115
    result << (*this)[i].asString();
 
116
    if (i < _nrows-1) result << "," << std::endl;
 
117
  }
 
118
  result << " ]" << std::endl;
 
119
  return result.str();
 
120
}
 
121
 
 
122
// Get view
 
123
void Array2::view(int* nrows, int* ncols, long** data) const
 
124
{
 
125
  *nrows = _nrows;
 
126
  *ncols = _ncols;
 
127
  *data  = _buffer;
 
128
}
 
129
 
 
130
// Private methods
 
131
void Array2::allocateMemory()
 
132
{
 
133
  if (_nrows * _ncols == 0)
 
134
  {
 
135
    _ownData = false;
 
136
    _buffer  = 0;
 
137
    _rows    = 0;
 
138
  }
 
139
  else
 
140
  {
 
141
    _ownData = true;
 
142
    _buffer = new long[_nrows*_ncols];
 
143
    allocateRows();
 
144
  }
 
145
}
 
146
 
 
147
void Array2::allocateRows()
 
148
{
 
149
  _rows = new Array1[_nrows];
 
150
  for (int i=0; i < _nrows; ++i)
 
151
  {
 
152
    _rows[i].resize(_ncols, &_buffer[i*_ncols]);
 
153
  }
 
154
}
 
155
 
 
156
void Array2::deallocateMemory()
 
157
{
 
158
  if (_ownData && _nrows*_ncols && _buffer)
 
159
  {
 
160
    delete [] _rows;
 
161
    delete [] _buffer;
 
162
  }
 
163
  _ownData = false;
 
164
  _nrows   = 0;
 
165
  _ncols   = 0;
 
166
  _buffer  = 0;
 
167
  _rows    = 0;
 
168
}