All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Array.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/datatype/Array.cpp
22 
23  \brief Class for dealing with arrays of abstract data.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "Array.h"
29 
30 // STL
31 #include <cassert>
32 #include <iostream>
33 #include <sstream>
34 #include <string>
35 
36 te::dt::Array::Array(std::size_t d, int t)
37  : m_dimension(d),
38  m_type(t)
39 {
40  for (std::size_t i = 0; i < m_dimension; i++)
41  m_dimensionSizes.push_back(0);
42 }
43 
45  : m_dimension(rhs.m_dimension),
46  m_dimensionSizes(rhs.m_dimensionSizes),
47  m_type(rhs.m_type)
48 {
49  std::map<std::vector<std::size_t>, te::dt::AbstractData*>::const_iterator it = rhs.m_data.begin();
50 
51  while (it != rhs.m_data.end())
52  {
53  m_data[it->first] = it->second->clone();
54 
55  ++it;
56  }
57 }
58 
60 {
61  if(this != &rhs)
62  {
64 
65  m_data.clear();
66 
67  m_dimension = rhs.m_dimension;
68 
69  m_dimensionSizes = rhs.m_dimensionSizes;
70 
71  std::map<std::vector<std::size_t>, te::dt::AbstractData*>::const_iterator it = rhs.m_data.begin();
72 
73  while (it != rhs.m_data.end())
74  {
75  m_data[it->first] = it->second->clone();
76 
77  ++it;
78  }
79 
80  m_type = rhs.m_type;
81  }
82 
83  return *this;
84 }
85 
87 {
89 }
90 
91 std::size_t te::dt::Array::getDimension() const
92 {
93  return m_dimension;
94 }
95 
97 {
98  return m_type;
99 }
100 
101 std::size_t te::dt::Array::getDimensionSize(std::size_t i) const
102 {
103  assert(i < getDimension());
104 
105  return m_dimensionSizes[i];
106 }
107 
108 void te::dt::Array::insert(te::dt::AbstractData* data, const std::vector<std::size_t>& pos)
109 {
110  assert(pos.size() == getDimension());
111 
112 // check if pos is out of current bounds, and expand the array dimension info if needed
113  for (std::size_t i = 0; i < getDimension(); i++)
114  m_dimensionSizes[i] = std::max(m_dimensionSizes[i], pos[i] + 1);
115 
116 // remove previously data on the position
117  delete m_data[pos];
118 
119  m_data[pos] = data;
120 }
121 
122 te::dt::AbstractData* te::dt::Array::getData(const std::vector<std::size_t>& i)
123 {
124  assert(i.size() == getDimension());
125 
126  return te::common::GetPValue(m_data, i);
127 }
128 
129 te::dt::AbstractData& te::dt::Array::operator[](const std::vector<std::size_t>& i)
130 {
131  assert(i.size() == getDimension());
132 
133  return *te::common::GetPValue(m_data, i);
134 }
135 
137 {
138  return new Array(*this);
139 }
140 
141 std::string te::dt::Array::toString() const
142 {
143  std::ostringstream output;
144 
145  std::size_t total_size = 1;
146 
147  std::vector<std::size_t> products_back (getDimension(), 1);
148 
149  std::vector<std::size_t> products_front (getDimension(), 1);
150 
151  for (std::size_t i = 0; i < getDimension(); i++)
152  {
153  total_size *= getDimensionSize(i);
154 
155  for (std::size_t f = i + 1; f < getDimension(); f++)
156  products_front[i] *= getDimensionSize(f);
157 
158  for (std::size_t b = 0; b < i; b++)
159  products_back[i] *= getDimensionSize(b);
160  }
161 
162  std::vector<std::vector<std::size_t> > poses(total_size, std::vector<std::size_t>(getDimension(), 0));
163 
164  for (std::size_t ii = 0; ii < getDimension(); ii++)
165  {
166  std::size_t i = getDimension() - ii - 1;
167 
168  std::size_t pi = 0;
169  for (std::size_t j = 0; j < products_back[i]; j++)
170  for (std::size_t k = 0; k < getDimensionSize(i); k++)
171  for (std::size_t l = 0; l < products_front[i]; l++)
172  {
173  poses[pi++][i] = k;
174  }
175  }
176 
177  for (std::size_t i = 0; i < poses.size(); i++)
178  {
179  std::map<std::vector<std::size_t>, te::dt::AbstractData*>::const_iterator it = m_data.find(poses[i]);
180 
181  if(it != m_data.end())
182  output << it->second->toString() << " ";
183  }
184  output << std::endl;
185 
186  return output.str();
187 }
188 
int m_type
The data type of this array.
Definition: Array.h:154
V * GetPValue(const std::map< K, V * > &m, const K &k)
It finds for a given key in the map and returns a pointer if something is found or NULL otherwise...
Definition: STLUtils.h:179
AbstractData * clone() const
It creates a new clone of the array.
Definition: Array.cpp:136
std::vector< std::size_t > m_dimensionSizes
The vector of sizes for the dimensions.
Definition: Array.h:153
int getElementsTypeCode()
Returns the data type of the elements of the array.
Definition: Array.cpp:96
Array & operator=(const Array &rhs)
Assignment operator.
Definition: Array.cpp:59
virtual std::string toString() const =0
It returns the data value in a string notation.
~Array()
Destructor.
Definition: Array.cpp:86
Class for dealing with arrays of abstract data.
The type for variable-length multidimensional arrays.
Definition: Array.h:59
std::size_t getDimension() const
Returns the numbe of dimensions of the array.
Definition: Array.cpp:91
te::dt::AbstractData & operator[](const std::vector< std::size_t > &i)
Access data in i index.
Definition: Array.cpp:129
std::string toString() const
Return a string with all the data inside array.
Definition: Array.cpp:141
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
std::size_t m_dimension
The number of dimensions.
Definition: Array.h:152
te::dt::AbstractData * getData(const std::vector< std::size_t > &i)
Returns data from specified position.
Definition: Array.cpp:122
Array(std::size_t d, int t)
Multi-dimensional array constructor.
Definition: Array.cpp:36
std::size_t getDimensionSize(std::size_t i) const
Gets the number of elements in the i-th dimension.
Definition: Array.cpp:101
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
void insert(te::dt::AbstractData *data, const std::vector< std::size_t > &pos)
Inserts data into specified position.
Definition: Array.cpp:108
std::map< std::vector< std::size_t >, te::dt::AbstractData * > m_data
A map from positions to data.
Definition: Array.h:151