bes  Updated for version 3.20.6
Shape.h
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 {
58 class Shape {
59 public:
60  // Indices into the space of the shape.
61  typedef std::vector<unsigned int> IndexTuple;
62 
63 public:
64  // Inner Classes
65 
71  class IndexIterator: public std::iterator<std::forward_iterator_tag, IndexTuple> {
72  public:
74  IndexIterator(); // for uninitialized. Don't use it!
75  IndexIterator(const Shape& shape, bool isEnd = false);
76  IndexIterator(const IndexIterator& proto);
77  ~IndexIterator();
78  IndexIterator& operator=(const IndexIterator& rhs);
79  bool operator==(const IndexIterator& rhs) const;
80 
81  inline bool operator!=(const IndexIterator& rhs) const
82  {
83  return (!((*this) == rhs));
84  }
85 
86  inline IndexIterator& operator++() //prefix
87  {
88  advanceCurrent();
89  return *this;
90  }
91 
92  inline IndexIterator operator++(int) // postfix...
93  {
94  // Copy it, this is why prefix increment is preferred in STL...
95  Shape::IndexIterator tmp(*this);
96  ++(*this);
97  return tmp;
98  }
99 
100  // don't mutate the return, we use it to compute next element!
101  inline const Shape::IndexTuple& operator*()
102  {
103  NCML_ASSERT_MSG(!_end, "Can't reference end iterator!");
104  return _current;
105  }
106 
107  private:
109  void advanceCurrent();
110 
112  void setCurrentToStart();
113 
114  private:
115  const Shape* _shape; // the shape of the space we are iterating on. It cannot change during an iteration!
116  IndexTuple _current; // the current point.
117  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.
118  }; // class IndexIterator
119 
120 public:
121  friend class IndexIterator;
122 
123 public:
125  Shape();
126  Shape(const Shape& proto);
128  Shape(const Array& copyDimsFrom);
129  ~Shape();
130  Shape& operator=(const Shape& rhs);
131 
135  bool operator==(const Shape& rhs) const;
136 
138  bool operator!=(const Shape& rhs) const
139  {
140  return !(*this == rhs);
141  }
142 
144  bool isConstrained() const;
145 
147  void setToUnconstrained();
148 
149  inline unsigned int getNumDimensions() const
150  {
151  return _dims.size();
152  }
153 
156  static bool areDimensionsEqual(const Array::dimension& lhs, const Array::dimension& rhs);
157 
159  inline unsigned int getUnconstrainedSpaceSize() const
160  {
161  unsigned int size = 1;
162  for (unsigned int i = 0; i < _dims.size(); ++i) {
163  size *= _dims[i].size;
164  }
165  return size;
166  }
167 
169  inline unsigned int getConstrainedSpaceSize() const
170  {
171  unsigned int c_size = 1;
172  for (unsigned int i = 0; i < _dims.size(); ++i) {
173  c_size *= _dims[i].c_size;
174  }
175  return c_size;
176  }
177 
194  unsigned int getRowMajorIndex(const IndexTuple& indices, bool validate = true) const;
195 
207 
211 
213  std::string toString() const;
214 
216  void print(std::ostream& strm) const;
217 
219  static void printDimension(std::ostream& strm, const Array::dimension& dim);
220 
224  bool validateIndices(const IndexTuple& indices) const;
225 
226 private:
227  // Methods
228 
229 private:
230  std::vector<Array::dimension> _dims;
231 
232 };
233 // class Shape
234 
235 }// namespace ncml_module
236 
237 inline std::ostream &
238 operator<<(std::ostream &strm, const ncml_module::Shape& shape)
239 {
240  shape.print(strm);
241  return strm;
242 }
243 #endif /* __NCML_MODULE__SHAPE_H__ */
ncml_module::Shape::isConstrained
bool isConstrained() const
Definition: Shape.cc:96
ncml_module::Shape::getConstrainedSpaceSize
unsigned int getConstrainedSpaceSize() const
Definition: Shape.h:169
ncml_module::Shape::Shape
Shape()
Definition: Shape.cc:38
ncml_module::Shape::operator==
bool operator==(const Shape &rhs) const
Definition: Shape.cc:75
ncml_module::Shape
A wrapper class for a vector of Array::dimension structs.
Definition: Shape.h:58
ncml_module::Shape::print
void print(std::ostream &strm) const
Definition: Shape.cc:173
ncml_module::Shape::validateIndices
bool validateIndices(const IndexTuple &indices) const
Definition: Shape.cc:194
ncml_module::Shape::IndexIterator
Definition: Shape.h:71
ncml_module::Shape::endSpaceEnumeration
Shape::IndexIterator endSpaceEnumeration() const
Definition: Shape.cc:161
ncml_module::Shape::setToUnconstrained
void setToUnconstrained()
Definition: Shape.cc:109
ncml_module::Shape::printDimension
static void printDimension(std::ostream &strm, const Array::dimension &dim)
Definition: Shape.cc:183
ncml_module::Shape::getUnconstrainedSpaceSize
unsigned int getUnconstrainedSpaceSize() const
Definition: Shape.h:159
ncml_module::Shape::areDimensionsEqual
static bool areDimensionsEqual(const Array::dimension &lhs, const Array::dimension &rhs)
Definition: Shape.cc:120
ncml_module
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
Definition: AggregationElement.cc:72
ncml_module::Shape::operator!=
bool operator!=(const Shape &rhs) const
Definition: Shape.h:138
ncml_module::Shape::beginSpaceEnumeration
Shape::IndexIterator beginSpaceEnumeration() const
Definition: Shape.cc:156
ncml_module::Shape::toString
std::string toString() const
Definition: Shape.cc:166
ncml_module::Shape::getRowMajorIndex
unsigned int getRowMajorIndex(const IndexTuple &indices, bool validate=true) const
Definition: Shape.cc:141
ncml_module::Shape::IndexIterator::IndexIterator
IndexIterator()
Definition: Shape.cc:213