All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ObjectIdSet.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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/dataaccess/dataset/ObjectIdSet.cpp
22 
23  \brief This class represents a set of unique ids created in the same context. i.e. from the same data set.
24 */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../common/Translator.h"
29 #include "../../datatype/Enums.h"
30 #include "../query/And.h"
31 #include "../query/In.h"
32 #include "../query/Literal.h"
33 #include "../query/LiteralString.h"
34 #include "../Exception.h"
35 #include "ObjectId.h"
36 #include "ObjectIdSet.h"
37 
38 // STL
39 #include <cassert>
40 
42 {
43 }
44 
46  : m_pnames(rhs.m_pnames),
47  m_ppos(rhs.m_ppos),
48  m_ptypes(rhs.m_ptypes)
49 {
50  if(copyOids)
51  {
52  std::set<ObjectId*, te::common::LessCmp<ObjectId*> >::const_iterator it;
53  for(it = rhs.m_oids.begin(); it != rhs.m_oids.end(); ++it)
54  m_oids.insert((*it)->clone());
55  }
56 }
57 
59 {
61 }
62 
63 void te::da::ObjectIdSet::addProperty(const std::string& name, std::size_t pos, int type)
64 {
65  assert(!name.empty());
66  m_pnames.push_back(name);
67  m_ppos.push_back(pos);
68  m_ptypes.push_back(type);
69 }
70 
72 {
73  assert(oid);
74  m_oids.insert(oid);
75 }
76 
78 {
79  assert(m_pnames.size() == m_ptypes.size());
80 
81  Expression* ins = 0;
82  Expression* tmp = 0;
83 
84  // for each property used to be part of the object identification builds a IN clause
85  for(std::size_t i = 0; i < m_pnames.size(); ++i)
86  {
87  In* in = 0;
88  if(source.empty())
89  in = new In(m_pnames[i]);
90  else
91  in = new In(source + "." + m_pnames[i]);
92 
93  // for each object in the set include its property value in the IN clause
94  std::set<ObjectId*, te::common::LessCmp<ObjectId*> >::const_iterator it;
95  for(it = m_oids.begin(); it != m_oids.end(); ++it)
96  {
97  const boost::ptr_vector<te::dt::AbstractData>& data = (*it)->getValue();
98 
99  if(m_ptypes[i] == te::dt::STRING_TYPE)
100  in->add(new LiteralString(data[i].toString()));
101  else
102  in->add(new Literal(data[i]));
103  }
104 
105  if(i > 0)
106  {
107  tmp = *ins && *in;
108  delete ins;
109  delete in;
110  ins = tmp;
111  }
112  else
113  ins = in;
114  }
115 
116  return ins;
117 }
118 
120 {
121  te::common::FreeContents(m_oids);
122  m_oids.clear();
123 }
124 
125 std::size_t te::da::ObjectIdSet::size() const
126 {
127  return m_oids.size();
128 }
129 
130 const std::vector<std::string>& te::da::ObjectIdSet::getPropertyNames() const
131 {
132  return m_pnames;
133 }
134 
135 const std::vector<std::size_t>& te::da::ObjectIdSet::getPropertyPos() const
136 {
137  return m_ppos;
138 }
139 
140 const std::vector<int>& te::da::ObjectIdSet::getPropertyTypes() const
141 {
142  return m_ptypes;
143 }
144 
146 {
147  assert(oid);
148  return m_oids.find(oid) != m_oids.end();
149 }
150 
152 {
153  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator it = m_oids.find(oid);
154 
155  if(it != m_oids.end())
156  m_oids.erase(it);
157 }
158 
160 {
161  assert(rhs);
162 
163  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& newOids = rhs->m_oids;
164 
165  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator it;
166  for(it = newOids.begin(); it != newOids.end(); ++it)
167  m_oids.find(*it) == m_oids.end() ? add(*it) : delete *it;
168 
169  newOids.clear();
170 
171  delete rhs;
172  rhs = 0;
173 }
174 
176 {
177  assert(rhs);
178 
179  if(m_oids.empty())
180  return;
181 
182  const std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& oidsToRemove = rhs->m_oids;
183 
184  if(oidsToRemove.empty())
185  return;
186 
187  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
188  for(it = oidsToRemove.begin(); it != oidsToRemove.end(); ++it)
189  {
190  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator itSearch = m_oids.find(*it);
191 
192  if(itSearch == m_oids.end())
193  continue;
194 
195  delete *itSearch;
196  m_oids.erase(itSearch);
197  }
198 }
199 
201 {
202  assert(rhs);
203 
204  const std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& oidsToInsert = rhs->m_oids;
205 
206  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
207  for(it = oidsToInsert.begin(); it != oidsToInsert.end(); ++it)
208  {
209  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator itSearch = m_oids.find(*it);
210 
211  if(itSearch != m_oids.end())
212  {
213  delete *itSearch;
214  m_oids.erase(itSearch);
215  }
216  else
217  m_oids.insert((*it)->clone());
218  }
219 }
220 
221 std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator te::da::ObjectIdSet::begin() const
222 {
223  return m_oids.begin();
224 }
225 
226 std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator te::da::ObjectIdSet::end() const
227 {
228  return m_oids.end();
229 }
230 
232 {
233  return new ObjectIdSet(*this);
234 }
const std::vector< int > & getPropertyTypes() const
It returns the property types used to generated the oids.
void add(Expression *arg)
It adds the argument to the function list of arguments.
Definition: Function.cpp:79
Expression * getExpression(const std::string source="") const
It returns the expression that can be used to retrieve the data set that contains the all indentified...
Definition: ObjectIdSet.cpp:77
This class represents an unique id for a data set element.
void addProperty(const std::string &name, std::size_t pos, int type)
It adds a property that will be used to generate the unique ids.
Definition: ObjectIdSet.cpp:63
This is an abstract class that models a query expression.
Definition: Expression.h:47
This class represents a set of unique ids created in the same context. i.e. from the same data set...
This class models a literal value.
Definition: Literal.h:53
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
std::size_t size() const
It returns the object id set size.
ObjectIdSet()
Constructor.
Definition: ObjectIdSet.cpp:41
std::set< ObjectId *, te::common::LessCmp< ObjectId * > > m_oids
The set of unique ids.
Definition: ObjectIdSet.h:199
void symDifference(const ObjectIdSet *rhs)
It performs the symmetric difference operation between this ObjectIdSet and the given ObjectIdSet...
void remove(ObjectId *oid)
Removes the object id from set.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
void Union(ObjectIdSet *rhs)
It performs the union operation between this ObjectIdSet and the given ObjectIdSet.
void clear()
It clears this object id set.
bool contains(ObjectId *oid) const
It returns if the object id set contains the given oid.
const std::vector< std::string > & getPropertyNames() const
It returns the property names used to generated the oids.
void add(ObjectId *oid)
It adds an object id to this object id set.
Definition: ObjectIdSet.cpp:71
A class that represents the IN operator.
Definition: In.h:52
const std::vector< std::size_t > & getPropertyPos() const
It returns the property positions used to generated the oids.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
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
ObjectIdSet * clone() const
void difference(const ObjectIdSet *rhs)
It performs the difference operation between this ObjectIdSet and the given ObjectIdSet.
~ObjectIdSet()
Destructor.
Definition: ObjectIdSet.cpp:58
This class models a string Literal value.
Definition: LiteralString.h:46