All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TreeItem.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/common/TreeItem.cpp
22 
23  \brief A base class for data organized as a tree.
24 */
25 
26 // TerraLib
27 #include "TreeItem.h"
28 
29 // STL
30 #include <algorithm>
31 #include <cassert>
32 
33 const std::size_t te::common::TreeItem::npos(static_cast<std::size_t>(-1));
34 
36  : m_parent(parent)
37 {
38  if(m_parent)
39  m_parent->m_children.push_back(TreeItemPtr(this));
40 }
41 
43 {
44  if(parent.get())
45  m_parent->m_children.push_back(TreeItemPtr(this));
46 }
47 
49 {
50  iterator it = begin();
51  iterator itend = end();
52 
53  while(it != itend)
54  {
55  (*it)->m_parent = 0;
56  ++it;
57  }
58 }
59 
61 {
62  return !m_children.empty();
63 }
64 
66 {
67  return m_parent;
68 }
69 
70 const std::list<te::common::TreeItemPtr>& te::common::TreeItem::getChildren() const
71 {
72  return m_children;
73 }
74 
76 {
77  assert(i < m_children.size());
78 
79  const_iterator it = begin();
80 
81  std::advance(it, i);
82 
83  return *it;
84 }
85 
87 {
88  return getChild(i);
89 }
90 
91 void te::common::TreeItem::add(const TreeItemPtr& childItem)
92 {
93  assert(childItem.get());
94 
95  m_children.push_back(childItem);
96 
97  childItem->m_parent = this;
98 }
99 
100 void te::common::TreeItem::insert(std::size_t i, const TreeItemPtr& childItem)
101 {
102  assert(i <= m_children.size());
103  assert(childItem);
104 
105  //childItem->disconnect();
106 
107  childItem->m_parent = this;
108 
109  iterator it = begin();
110 
111  std::advance(it, i);
112 
113  m_children.insert(it, childItem);
114 }
115 
117 {
118  assert(i < m_children.size());
119 
120  iterator it = begin();
121 
122  std::advance(it, i);
123 
124  TreeItemPtr childItem(*it);
125 
126  //childItem->disconnect();
127 
128  m_children.erase(it);
129 
130  childItem.get()->m_parent = 0;
131 
132  return childItem;
133 }
134 
135 std::list<te::common::TreeItemPtr> te::common::TreeItem::remove(std::size_t i, std::size_t count)
136 {
137  assert(i < m_children.size());
138  assert(count > 0);
139  assert((i + count - 1) < m_children.size());
140 
141  iterator firstIt = begin();
142 
143  std::advance(firstIt, i);
144 
145  iterator lastIt = firstIt;
146 
147  std::advance(lastIt, count);
148 
149  std::list<TreeItemPtr> removedItems;
150 
151  for(iterator it = firstIt; it != lastIt; ++it)
152  {
153  TreeItemPtr childItem = *it;
154 
155  //childItem->disconnect();
156 
157  removedItems.push_back(childItem);
158  }
159 
160  m_children.erase(firstIt, lastIt);
161 
162  return removedItems;
163 }
164 
166 {
167  assert(i < m_children.size());
168  assert(childItem);
169 
170  TreeItemPtr replacedItem = getChild(i);
171  replacedItem->disconnect();
172 
173  insert(i, childItem);
174 
175  return replacedItem;
176 }
177 
179 {
180  return getParent() == item->getParent();
181 }
182 
184 {
185  return m_children.size();
186 }
187 
188 void te::common::TreeItem::getDescendantsCount(std::size_t& count) const
189 {
190  count += m_children.size();
191 
192  for(const_iterator it = begin(); it != end(); ++it)
193  {
194  TreeItemPtr item = *it;
195  item->getDescendantsCount(count);
196  }
197 }
198 
200 {
201  if(m_parent == 0)
202  return;
203 
204  int index = getIndex();
205 
206  if(index < 0)
207  return;
208 
209  iterator it = m_parent->begin();
210 
211  std::advance(it, index);
212 
213  m_parent->m_children.erase(it);
214 
215  m_parent = 0;
216 }
217 
219 {
220  if(m_parent == 0)
221  return npos;
222 
223  const std::list<TreeItemPtr>& children = m_parent->m_children;
224 
225  std::size_t k = 0;
226 
227  for(const_iterator it = children.begin(); it != children.end(); ++it)
228  {
229  TreeItemPtr childItem = *it;
230 
231  if(childItem.get() == this)
232  return k;
233 
234  ++k;
235  }
236 
237  return npos;
238 }
239 
240 void te::common::TreeItem::swap(const TreeItemPtr& firstChild, const TreeItemPtr& secondChild)
241 {
242  assert(firstChild->getParent() == this && secondChild->getParent() == this);
243 
244  std::size_t fidx = firstChild->getIndex();
245  std::size_t sidx = secondChild->getIndex();
246 
247  iterator firstIt = begin();
248 
249  std::advance(firstIt, fidx);
250 
251  iterator secondIt = begin();
252 
253  std::advance(secondIt, sidx);
254 
255  *firstIt = secondChild;
256  *secondIt = firstChild;
257 }
258 
260 {
261  assert(m_parent);
262 
263  m_parent->swap(this, sibling);
264 }
265 
267 {
268  return m_children.begin();
269 }
270 
272 {
273  return m_children.begin();
274 }
275 
277 {
278  return m_children.end();
279 }
280 
282 {
283  return m_children.end();
284 }
285 
287 {
288  if(m_parent == parent)
289  return;
290 
291  if(m_parent)
292  disconnect();
293 
294  m_parent = parent;
295 
296  if(parent)
297  parent->m_children.push_back(this);
298 }
299 
const std::list< TreeItemPtr > & getChildren() const
It returns the children of this tree item.
Definition: TreeItem.cpp:70
const TreeItemPtr & getChild(std::size_t i) const
It returns the n-th child.
Definition: TreeItem.cpp:75
std::size_t getChildrenCount() const
It returns the number of children of this node.
Definition: TreeItem.cpp:183
void swap(const TreeItemPtr &firstChild, const TreeItemPtr &secondChild)
It swaps the position of the given children.
Definition: TreeItem.cpp:240
bool isSibling(const TreeItem *item) const
It checks if the given layer is sibling of this one.
Definition: TreeItem.cpp:178
This abstract class describes a basic item to be organized in a tree-oriented way.
Definition: TreeItem.h:62
const TreeItemPtr & operator[](std::size_t i) const
It returns the n-th child.
Definition: TreeItem.cpp:86
bool hasChildren() const
It returns true if the item has descendants.
Definition: TreeItem.cpp:60
TreeItem * m_parent
A pointer to the parent node.
Definition: TreeItem.h:289
virtual ~TreeItem()
Virtual destructor.
Definition: TreeItem.cpp:48
std::size_t getIndex() const
It returns the index of this item in the list of children of its parent item.
Definition: TreeItem.cpp:218
void add(const TreeItemPtr &childItem)
It adds (appends) the item to the end of the children's list.
Definition: TreeItem.cpp:91
TreeItemPtr replace(std::size_t i, const TreeItemPtr &childItem)
It replaces the child item at the given position by the new one. The replaced item will be disconnect...
Definition: TreeItem.cpp:165
TreeItem(TreeItem *parent=0)
It initializes a new item having a parent.
Definition: TreeItem.cpp:35
void setParent(TreeItem *parent)
It sets the parent of this item to the specified one. Don't use this method if you are not sure how t...
Definition: TreeItem.cpp:286
A base class for data organized as a tree.
std::list< TreeItemPtr >::iterator iterator
Definition: TreeItem.h:66
boost::intrusive_ptr< TreeItem > TreeItemPtr
Definition: TreeItem.h:44
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
void disconnect()
It disconnects this item from its parent, if it has one.
Definition: TreeItem.cpp:199
TreeItemPtr remove(std::size_t i)
It removes the i-th child.
Definition: TreeItem.cpp:116
const_iterator begin() const
It returns the constant iterator associated to the first child of this item.
Definition: TreeItem.cpp:266
const_iterator end() const
It returns the constant iterator that refers to one past the end of the children of this item...
Definition: TreeItem.cpp:276
std::list< TreeItemPtr > m_children
Pointers to the items that are children.
Definition: TreeItem.h:290
TreeItem * getParent() const
It returns a pointer to the parent of this node.
Definition: TreeItem.cpp:65
static const std::size_t npos
A value to indicate no match.
Definition: TreeItem.h:285
void getDescendantsCount(std::size_t &count) const
It returns the number of nodes that descends from this node.
Definition: TreeItem.cpp:188
std::list< TreeItemPtr >::const_iterator const_iterator
Definition: TreeItem.h:67
void insert(std::size_t i, const TreeItemPtr &childItem)
It inserts an item in the informed position.
Definition: TreeItem.cpp:100