All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 = new In(m_pnames[i]);
88 
89  // for each object in the set include its property value in the IN clause
90  std::set<ObjectId*, te::common::LessCmp<ObjectId*> >::const_iterator it;
91  for(it = m_oids.begin(); it != m_oids.end(); ++it)
92  {
93  const boost::ptr_vector<te::dt::AbstractData>& data = (*it)->getValue();
94 
95  if(m_ptypes[i] == te::dt::STRING_TYPE)
96  in->add(new LiteralString(data[i].toString()));
97  else
98  in->add(new Literal(data[i]));
99  }
100 
101  if(i > 0)
102  {
103  tmp = *ins && *in;
104  delete ins;
105  delete in;
106  ins = tmp;
107  }
108  else
109  ins = in;
110  }
111 
112  return ins;
113 }
114 
116 {
117  te::common::FreeContents(m_oids);
118  m_oids.clear();
119 }
120 
121 std::size_t te::da::ObjectIdSet::size() const
122 {
123  return m_oids.size();
124 }
125 
126 const std::vector<std::string>& te::da::ObjectIdSet::getPropertyNames() const
127 {
128  return m_pnames;
129 }
130 
131 const std::vector<std::size_t>& te::da::ObjectIdSet::getPropertyPos() const
132 {
133  return m_ppos;
134 }
135 
136 const std::vector<int>& te::da::ObjectIdSet::getPropertyTypes() const
137 {
138  return m_ptypes;
139 }
140 
142 {
143  assert(oid);
144  return m_oids.find(oid) != m_oids.end();
145 }
146 
148 {
149  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator it = m_oids.find(oid);
150 
151  if(it != m_oids.end())
152  m_oids.erase(it);
153 }
154 
156 {
157  assert(rhs);
158 
159  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& newOids = rhs->m_oids;
160 
161  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator it;
162  for(it = newOids.begin(); it != newOids.end(); ++it)
163  m_oids.find(*it) == m_oids.end() ? add(*it) : delete *it;
164 
165  newOids.clear();
166 
167  delete rhs;
168  rhs = 0;
169 }
170 
172 {
173  assert(rhs);
174 
175  if(m_oids.empty())
176  return;
177 
178  const std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& oidsToRemove = rhs->m_oids;
179 
180  if(oidsToRemove.empty())
181  return;
182 
183  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
184  for(it = oidsToRemove.begin(); it != oidsToRemove.end(); ++it)
185  {
186  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator itSearch = m_oids.find(*it);
187 
188  if(itSearch == m_oids.end())
189  continue;
190 
191  delete *itSearch;
192  m_oids.erase(itSearch);
193  }
194 }
195 
197 {
198  assert(rhs);
199 
200  const std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >& oidsToInsert = rhs->m_oids;
201 
202  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
203  for(it = oidsToInsert.begin(); it != oidsToInsert.end(); ++it)
204  {
205  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::iterator itSearch = m_oids.find(*it);
206 
207  if(itSearch != m_oids.end())
208  {
209  delete *itSearch;
210  m_oids.erase(itSearch);
211  }
212  else
213  m_oids.insert((*it)->clone());
214  }
215 }
216 
217 std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator te::da::ObjectIdSet::begin() const
218 {
219  return m_oids.begin();
220 }
221 
222 std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator te::da::ObjectIdSet::end() const
223 {
224  return m_oids.end();
225 }
226 
228 {
229  return new ObjectIdSet(*this);
230 }
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 * > > m_oids
The set of unique ids.
Definition: ObjectIdSet.h:199
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
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
This class models a string Literal value.
Definition: LiteralString.h:46
void clear()
It clears this object id set.
Expression * getExpression() const
It returns the expression that can be used to retrieve the data set that contains the all indentified...
Definition: ObjectIdSet.cpp:77
void remove(ObjectId *oid)
Removes the object id from set.
This class represents an unique id for a data set element.
const std::vector< std::string > & getPropertyNames() const
It returns the property names used to generated the oids.
std::size_t size() const
It returns the object id set size.
void symDifference(const ObjectIdSet *rhs)
It performs the symmetric difference operation between this ObjectIdSet and the given ObjectIdSet...
void difference(const ObjectIdSet *rhs)
It performs the difference operation between this ObjectIdSet and the given ObjectIdSet.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() 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.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
ObjectIdSet()
Constructor.
Definition: ObjectIdSet.cpp:41
This class models a literal value.
Definition: Literal.h:53
This is an abstract class that models a query expression.
Definition: Expression.h:47
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
bool contains(ObjectId *oid) const
It returns if the object id set contains the given oid.
~ObjectIdSet()
Destructor.
Definition: ObjectIdSet.cpp:58
const std::vector< int > & getPropertyTypes() const
It returns the property types used to generated the oids.
ObjectIdSet * clone() const
void add(Expression *arg)
It adds the argument to the function list of arguments.
Definition: Function.cpp:79
This class represents a set of unique ids created in the same context. i.e. from the same data set...
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