OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
Shape.h
Go to the documentation of this file.
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2009 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 
30 #ifndef __NCML_MODULE__SHAPE_H__
31 #define __NCML_MODULE__SHAPE_H__
32 
33 #include <iostream>
34 #include <iterator>
35 #include <Array.h>
36 #include "NCMLDebug.h"
37 #include <string>
38 #include <vector>
39 
40 using libdap::Array;
41 
42 namespace ncml_module
43 {
59  class Shape
60  {
61  public:
62  // Indices into the space of the shape.
63  typedef std::vector<unsigned int> IndexTuple;
64 
65  public: // Inner Classes
66 
73  : public std::iterator < std::forward_iterator_tag, IndexTuple >
74  {
75  public:
77  IndexIterator(); // for uninitialized. Don't use it!
78  IndexIterator(const Shape& shape, bool isEnd=false);
79  IndexIterator(const IndexIterator& proto);
82  bool operator==(const IndexIterator& rhs) const;
83 
84  inline bool operator!=(const IndexIterator& rhs) const
85  {
86  return (! ((*this) == rhs));
87  }
88 
89  inline IndexIterator& operator++() //prefix
90  {
91  advanceCurrent();
92  return *this;
93  }
94 
95  inline IndexIterator operator++(int) // postfix...
96  {
97  // Copy it, this is why prefix increment is preferred in STL...
98  Shape::IndexIterator tmp(*this);
99  ++(*this);
100  return tmp;
101  }
102 
103  // don't mutate the return, we use it to compute next element!
104  inline const Shape::IndexTuple& operator*()
105  {
106  NCML_ASSERT_MSG(!_end, "Can't reference end iterator!");
107  return _current;
108  }
109 
110  private:
112  void advanceCurrent();
113 
115  void setCurrentToStart();
116 
117  private:
118  const Shape* _shape; // the shape of the space we are iterating on. It cannot change during an iteration!
119  IndexTuple _current; // the current point.
120  bool _end; // set to true when we reach the end since there's no other way to tell if we're at start or end.
121  }; // class IndexIterator
122 
123  public:
124  friend class IndexIterator;
125 
126 
127  public:
129  Shape();
130  Shape(const Shape& proto);
132  Shape(const Array& copyDimsFrom);
133  ~Shape();
134  Shape& operator=(const Shape& rhs);
135 
139  bool operator==(const Shape& rhs) const;
140 
142  bool operator!=(const Shape& rhs) const
143  {
144  return !(*this == rhs);
145  }
146 
148  bool isConstrained() const;
149 
151  void setToUnconstrained();
152 
153  inline unsigned int getNumDimensions() const
154  {
155  return _dims.size();
156  }
157 
160  static bool areDimensionsEqual(const Array::dimension& lhs, const Array::dimension& rhs);
161 
163  inline unsigned int getUnconstrainedSpaceSize() const
164  {
165  unsigned int size = 1;
166  for (unsigned int i=0; i<_dims.size(); ++i)
167  {
168  size *= _dims[i].size;
169  }
170  return size;
171  }
172 
174  inline unsigned int getConstrainedSpaceSize() const
175  {
176  unsigned int c_size = 1;
177  for (unsigned int i=0; i<_dims.size(); ++i)
178  {
179  c_size *= _dims[i].c_size;
180  }
181  return c_size;
182  }
183 
200  unsigned int getRowMajorIndex(const IndexTuple& indices, bool validate=true) const;
201 
213 
217 
219  std::string toString() const;
220 
222  void print(std::ostream& strm) const;
223 
225  static void printDimension(std::ostream& strm, const Array::dimension& dim);
226 
230  bool validateIndices(const IndexTuple& indices) const;
231 
232 
233  private: // Methods
234 
235  private:
236  std::vector<Array::dimension> _dims;
237 
238  }; // class Shape
239 
240 } // namespace ncml_module
241 
242 inline std::ostream &
243 operator<<(std::ostream &strm, const ncml_module::Shape& shape)
244 {
245  shape.print(strm);
246  return strm ;
247 }
248 #endif /* __NCML_MODULE__SHAPE_H__ */
IndexIterator()
isEnd is only set by Shape for creating an end() iterator...
Definition: Shape.cc:239
Shape::IndexIterator endSpaceEnumeration() const
The end of the enumeration for testing end conditions.
Definition: Shape.cc:178
std::ostream & operator<<(std::ostream &strm, const ncml_module::Shape &shape)
Definition: Shape.h:243
bool validateIndices(const IndexTuple &indices) const
Definition: Shape.cc:216
A custom iterator that enumerates all the points in the space defined by a Shape in row major order...
Definition: Shape.h:72
unsigned int getUnconstrainedSpaceSize() const
Get the product of all the dimension sizes.
Definition: Shape.h:163
An abstract superclass for NCMLArray that handles the non-parameterized functionality and allows u...
std::vector< unsigned int > IndexTuple
Definition: Shape.h:63
IndexIterator & operator=(const IndexIterator &rhs)
Definition: Shape.cc:269
void print(std::ostream &strm) const
Print the contents of this to the stream.
Definition: Shape.cc:192
Shape & operator=(const Shape &rhs)
Definition: Shape.cc:67
#define NCML_ASSERT_MSG(cond, msg)
Definition: NCMLDebug.h:83
void setToUnconstrained()
Go through and set all the values to be the unconstrained values.
Definition: Shape.cc:119
A wrapper class for a vector of Array::dimension structs.
Definition: Shape.h:59
IndexIterator & operator++()
Definition: Shape.h:89
Shape()
The empty shape, with no dimensions.
Definition: Shape.cc:39
bool operator!=(const Shape &rhs) const
Definition: Shape.h:142
bool isConstrained() const
Are there constraints on the dimension?
Definition: Shape.cc:103
bool operator==(const IndexIterator &rhs) const
Definition: Shape.cc:283
Shape::IndexIterator beginSpaceEnumeration() const
Create a forward iterator that returns IndexTuple's in a row major order (leftmost dimension slowest ...
Definition: Shape.cc:172
static bool areDimensionsEqual(const Array::dimension &lhs, const Array::dimension &rhs)
Helper:
Definition: Shape.cc:133
bool operator!=(const IndexIterator &rhs) const
Definition: Shape.h:84
IndexIterator operator++(int)
Definition: Shape.h:95
bool operator==(const Shape &rhs) const
Return if this contains the same shape as rhs.
Definition: Shape.cc:79
std::string toString() const
Make a string that prints the contents of this.
Definition: Shape.cc:184
const Shape::IndexTuple & operator*()
Definition: Shape.h:104
unsigned int getNumDimensions() const
Definition: Shape.h:153
unsigned int getRowMajorIndex(const IndexTuple &indices, bool validate=true) const
Return the row major linear index into a slowest varying dimension first flattening of the given UNCO...
Definition: Shape.cc:155
static void printDimension(std::ostream &strm, const Array::dimension &dim)
Helper to print the dimension to a stream.
Definition: Shape.cc:204
unsigned int getConstrainedSpaceSize() const
Get the production of all dimension c_sizes.
Definition: Shape.h:174