SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Array2.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 ///
32 ///@file Array2.h
33 ///@brief Interface to dynamic 2D array class
34 ///
35 ///@author Steven G. Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date March 1994
39 ///
40 
41 #ifndef CORE_CONAINTERS_ARRAY2_H
42 #define CORE_CONAINTERS_ARRAY2_H 1
43 
44 #include <boost/multi_array.hpp>
45 
47 
48 namespace SCIRun {
49 
50 template<class T>
51 class Array2
52 {
53 public:
54  typedef boost::multi_array<T, 2> impl_type;
55  typedef T value_type;
56 
57  Array2() {}
58 
59  Array2(size_t size1, size_t size2)
60  {
61  resize(size1, size2);
62  }
63 
64  void resize(size_t size1, size_t size2)
65  {
66  typename impl_type::extent_gen extents;
67  impl_.resize(extents[size1][size2]);
68  }
69 
70  size_t size() const
71  {
72  return dim1() * dim2();
73  }
74 
75  T& operator[](size_t idx)
76  {
77  return impl_.origin()[idx];
78  }
79 
80  const T& operator[](size_t idx) const
81  {
82  return impl_.origin()[idx];
83  }
84 
85  inline const T& operator()(index_type d1, index_type d2) const
86  {
87  return impl_[d1][d2];
88  }
89 
90  inline T& operator()(index_type d1, index_type d2)
91  {
92  return impl_[d1][d2];
93  }
94 
95  //////////
96  ///Returns number of rows
97  inline size_t dim1() const {return impl_.shape()[0];}
98 
99  //////////
100  ///Returns number of cols
101  inline size_t dim2() const {return impl_.shape()[1];}
102 
103  impl_type& getImpl() { return impl_; }
104 private:
105  impl_type impl_;
106 };
107 
108 template<class T> void Pio(Piostream& stream, Array2<T>& data);
109 template<class T> void Pio(Piostream& stream, Array2<T>*& data);
110 
111 #define ARRAY2_VERSION 2
112 
113 template<class T>
114 void Pio(Piostream& stream, Array2<T>& data)
115 {
116  int version = stream.begin_class("Array2", ARRAY2_VERSION);
117  if(stream.reading())
118  {
119  // Allocate the array...
120  if (version < 2)
121  {
122  int d1, d2;
123  Pio(stream, d1);
124  Pio(stream, d2);
125  data.resize(static_cast<size_type>(d1), static_cast<size_type>(d2));
126  }
127  else
128  {
129  long long d1, d2;
130  Pio(stream, d1);
131  Pio(stream, d2);
132  data.resize(static_cast<size_type>(d1), static_cast<size_type>(d2));
133  }
134  }
135  else
136  {
137  long long d1 = static_cast<long long>(data.dim1());
138  long long d2 = static_cast<long long>(data.dim2());
139  Pio(stream, d1);
140  Pio(stream, d2);
141  }
142  if (stream.supports_block_io())
143  {
144  stream.block_io(&data[0],sizeof(T),data.size());
145  }
146  else
147  {
148  for(index_type i=0;i<data.dim1();i++)
149  {
150  for(index_type j=0;j<data.dim2();j++)
151  {
152  Pio(stream, data.getImpl()[i][j]);
153  }
154  }
155  }
156  stream.end_class();
157 }
158 
159 template<class T>
160 void Pio(Piostream& stream, Array2<T>*& data)
161 {
162  if (stream.reading())
163  {
164  data=new Array2<T>;
165  }
166  Pio(stream, *data);
167 }
168 
169 } // End namespace SCIRun
170 
171 #endif
172 
virtual bool block_io(void *, size_t, size_t)
Definition: Persistent.h:174
bool reading() const
Definition: Persistent.h:164
const T & operator[](size_t idx) const
Definition: Array2.h:80
#define ARRAY2_VERSION
Definition: Array2.h:111
virtual bool supports_block_io()
Definition: Persistent.h:171
Definition: Persistent.h:89
Array2()
Definition: Array2.h:57
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
size_t dim1() const
Returns number of rows.
Definition: Array2.h:97
Base class for persistent objects...
dictionary data
Definition: eabLatVolData.py:11
size_t size() const
Definition: Array2.h:70
const T & operator()(index_type d1, index_type d2) const
Definition: Array2.h:85
Definition: Array2.h:51
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
boost::multi_array< T, 2 > impl_type
Definition: Array2.h:54
virtual void end_class()
Definition: Persistent.cc:178
long long index_type
Definition: Types.h:39
void resize(size_t size1, size_t size2)
Definition: Array2.h:64
impl_type & getImpl()
Definition: Array2.h:103
T value_type
Definition: Array2.h:55
T & operator[](size_t idx)
Definition: Array2.h:75
size_t dim2() const
Returns number of cols.
Definition: Array2.h:101
T & operator()(index_type d1, index_type d2)
Definition: Array2.h:90
Array2(size_t size1, size_t size2)
Definition: Array2.h:59