NEST  2.6.0,not_revisioned_source_dir@0
ntree.h
Go to the documentation of this file.
1 /*
2  * ntree.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef NTREE_H
24 #define NTREE_H
25 
26 #include <vector>
27 #include <utility>
28 #include <bitset>
29 #include "position.h"
30 
31 namespace nest
32 {
33 
34  class AbstractMask;
35 
36  template<int D>
37  class Mask;
38 
48  template<int D, class T, int max_capacity=100, int max_depth=10>
49  class Ntree
50  {
51  public:
52 
53  static const int N = 1<<D;
54 
56  typedef T mapped_type;
57  typedef std::pair<Position<D>,T> value_type;
59  typedef const value_type& const_reference;
60 
64  class iterator {
65  public:
69  iterator() : ntree_(0), top_(0), node_(0) {}
70 
75  iterator(Ntree& q);
76 
82  iterator(Ntree& q, index n);
83 
86 
91  iterator & operator++();
92 
97  {
98  iterator tmp = *this;
99  ++*this;
100  return tmp;
101  }
102 
107  bool operator==(const iterator &other) const
108  { return (other.ntree_==ntree_) && (other.node_==node_); }
109  bool operator!=(const iterator &other) const
110  { return (other.ntree_!=ntree_) || (other.node_!=node_); }
111 
112  protected:
113 
118  void next_leaf_();
119 
123  };
124 
129  public:
133  masked_iterator() : ntree_(0), top_(0), allin_top_(0), node_(0), mask_(0) {}
134 
139  masked_iterator(Ntree& q, const Mask<D> &mask, const Position<D> &anchor);
140 
143 
150 
155  {
156  masked_iterator tmp = *this;
157  ++*this;
158  return tmp;
159  }
160 
165  bool operator==(const masked_iterator &other) const
166  { return (other.ntree_==ntree_) && (other.node_==node_); }
167  bool operator!=(const masked_iterator &other) const
168  { return (other.ntree_!=ntree_) || (other.node_!=node_); }
169 
170  protected:
171 
175  void init_();
176 
180  void next_leaf_();
181 
186  void first_leaf_();
187 
192  void first_leaf_inside_();
193 
197  void next_anchor_();
198 
203  const Mask<D> *mask_;
205  std::vector<Position<D> > anchors_;
207  };
208 
216  std::bitset<D> periodic=0, Ntree *parent=0, int subquad=0);
217 
221  ~Ntree();
222 
228  iterator insert(Position<D> pos, const T& node);
229 
233  iterator insert(const value_type& val);
234 
238  iterator insert(iterator, const value_type& val);
239 
243  std::vector<value_type> get_nodes();
244 
251  std::vector<value_type> get_nodes(const Mask<D> &mask, const Position<D> &anchor);
252 
259  { return iterator(*this); }
260 
262  { return iterator(); }
263 
270  { return masked_iterator(*this,mask,anchor); }
271 
273  { return masked_iterator(); }
274 
278  bool is_leaf() const;
279 
280  protected:
285  void split_();
286 
290  void append_nodes_(std::vector<value_type>&);
291 
295  void append_nodes_(std::vector<value_type>&, const Mask<D> &, const Position<D> &);
296 
300  int subquad_(const Position<D>&);
301 
304 
305  bool leaf_;
306 
307  std::vector<value_type> nodes_;
308 
311  int my_depth_;
313  std::bitset<D> periodic_;
314 
315  friend class iterator;
316  friend class masked_iterator;
317  };
318 
319  template<int D, class T, int max_capacity, int max_depth>
321  const Position<D>& extent,
322  std::bitset<D> periodic,
324  int subquad) :
325  lower_left_(lower_left),
326  extent_(extent),
327  leaf_(true),
328  parent_(parent),
329  my_subquad_(subquad),
330  my_depth_(parent?parent->my_depth_+1:0),
331  periodic_(periodic)
332  {
333  }
334 
335  template<int D, class T, int max_capacity, int max_depth>
337  {
338  if ( leaf_ )
339  return; // if T is a vector class, we do not delete the pointees
340 
341  for ( size_t n = 0 ; n < static_cast<size_t>(N) ; ++n )
342  delete children_[n]; // calls destructor in child, thus recursing
343  }
344 
345  template<int D, class T, int max_capacity, int max_depth>
347  ntree_(&q), top_(&q), node_(n)
348  {
349  assert(ntree_->leaf_);
350 
351  // First ancestor
352  while(top_->parent_)
353  top_ = top_->parent_;
354  }
355 
356  template<int D, class T, int max_capacity, int max_depth>
358  {
359  return leaf_;
360  }
361 
362 
363  template<int D, class T, int max_capacity, int max_depth>
364  std::vector<std::pair<Position<D>,T> > Ntree<D,T,max_capacity,max_depth>::get_nodes()
365  {
366  std::vector<std::pair<Position<D>,T> > result;
367  append_nodes_(result);
368  return result;
369  }
370 
371  template<int D, class T, int max_capacity, int max_depth>
372  std::vector<std::pair<Position<D>,T> > Ntree<D,T,max_capacity,max_depth>::get_nodes(const Mask<D> &mask, const Position<D> &anchor)
373  {
374  std::vector<std::pair<Position<D>,T> > result;
375  append_nodes_(result,mask,anchor);
376  return result;
377  }
378 
379  template<int D, class T, int max_capacity, int max_depth>
381  {
382  return insert(val.first,val.second);
383  }
384 
385  template<int D, class T, int max_capacity, int max_depth>
386  typename Ntree<D,T,max_capacity,max_depth>::iterator Ntree<D,T,max_capacity,max_depth>::insert(iterator, const std::pair<Position<D>,T>& val)
387  {
388  return insert(val.first,val.second);
389  }
390 
391 } // namespace nest
392 
393 #endif
size_t index
Unsigned long type for enumerations.
Definition: nest.h:109
~Ntree()
Delete Ntree recursively.
Definition: ntree.h:336
masked_iterator & operator++()
Move the iterator to the next node inside the mask within the tree.
Definition: ntree_impl.h:287
const Name N("N")
Specific to population point process model (pp_pop_psc_delta)
Definition: nest_names.h:203
const Mask< D > * mask_
Definition: ntree.h:203
index current_anchor_
Definition: ntree.h:206
bool is_leaf() const
Definition: ntree.h:357
int my_subquad_
This Ntree's subquad number within parent.
Definition: ntree.h:310
iterator begin()
This function returns a node iterator which will traverse the subtree below this Ntree.
Definition: ntree.h:258
Iterator iterating the nodes in a Quadtree inside a Mask.
Definition: ntree.h:128
Ntree * top_
Definition: ntree.h:200
bool leaf_
Definition: ntree.h:305
void next_leaf_()
Move to the next leaf quadrant, or set ntree_ to 0 if there are no more leaves.
Definition: ntree_impl.h:67
value_type & operator*()
Definition: ntree.h:84
const Name extent("extent")
Definition: topology_names.h:47
iterator & operator++()
Move the iterator to the next node within the tree.
Definition: ntree_impl.h:49
T mapped_type
Definition: ntree.h:56
void next_anchor_()
Go to the next anchor image.
Definition: ntree_impl.h:168
const Name lower_left("lower_left")
Definition: topology_names.h:66
index node_
Definition: ntree.h:202
const value_type & const_reference
Definition: ntree.h:59
void split_()
Change a leaf ntree to a regular ntree with four children regions.
Definition: ntree_impl.h:397
void next_leaf_()
Find the next leaf which is not outside the mask.
Definition: ntree_impl.h:182
int subquad_(const Position< D > &)
Definition: ntree_impl.h:316
masked_iterator operator++(int)
Postfix increment operator.
Definition: ntree.h:154
assert(pNet!=0)
masked_iterator masked_end()
Definition: ntree.h:272
const Name parent("parent")
Node parameter.
Definition: nest_names.h:220
void first_leaf_inside_()
Set the allin_top_ to the current quadrant, and find the first leaf below the current quadrant...
Definition: ntree_impl.h:276
Abstract base class for masks with given dimension.
Definition: mask.h:90
bool operator==(const masked_iterator &other) const
Iterators are equal if they point to the same node in the same ntree.
Definition: ntree.h:165
A Ntree object represents a subtree or leaf in a Ntree structure.
Definition: ntree.h:49
const Name anchor("anchor")
Definition: topology_names.h:50
static const int N
Definition: ntree.h:53
Ntree * parent_
Definition: ntree.h:309
bool operator!=(const iterator &other) const
Definition: ntree.h:109
bool operator!=(const masked_iterator &other) const
Definition: ntree.h:167
Position< D > lower_left_
Definition: ntree.h:302
bool operator==(const iterator &other) const
Iterators are equal if they point to the same node in the same ntree.
Definition: ntree.h:107
const Name other("other")
Node type.
Definition: nest_names.h:216
iterator insert(Position< D > pos, const T &node)
Traverse quadtree structure from current ntree.
Definition: ntree_impl.h:362
Iterator iterating the nodes in a Quadtree.
Definition: ntree.h:64
value_type * operator->()
Definition: ntree.h:85
std::pair< Position< D >, T > value_type
Definition: ntree.h:57
void first_leaf_()
Find the first leaf which is not outside the mask.
Definition: ntree_impl.h:257
friend class masked_iterator
Definition: ntree.h:316
Position< D > extent_
Definition: ntree.h:303
Position< D > key_type
Definition: ntree.h:55
value_type * operator->()
Definition: ntree.h:142
int my_depth_
This Ntree's depth in the tree.
Definition: ntree.h:311
Ntree * allin_top_
Definition: ntree.h:201
void init_()
Initialize.
Definition: ntree_impl.h:143
Ntree * ntree_
Definition: ntree.h:120
Ntree * top_
Definition: ntree.h:121
std::vector< Position< D > > anchors_
Definition: ntree.h:205
value_type & reference
Definition: ntree.h:58
index node_
Definition: ntree.h:122
std::bitset< D > periodic_
periodic b.c.
Definition: ntree.h:313
iterator()
Initialize an invalid iterator.
Definition: ntree.h:69
Position< D > anchor_
Definition: ntree.h:204
Ntree(const Position< D > &lower_left, const Position< D > &extent, std::bitset< D > periodic=0, Ntree *parent=0, int subquad=0)
Create a Ntree that covers the region defined by the two input positions.
Definition: ntree.h:320
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
friend class iterator
Definition: ntree.h:315
std::vector< value_type > nodes_
Definition: ntree.h:307
Ntree * ntree_
Definition: ntree.h:199
const Name mask("mask")
Definition: topology_names.h:57
std::vector< value_type > get_nodes()
Definition: ntree.h:364
value_type & operator*()
Definition: ntree.h:141
Ntree * children_[N]
Definition: ntree.h:312
masked_iterator masked_begin(const Mask< D > &mask, const Position< D > &anchor)
This function returns a masked node iterator which will traverse the subtree below this Ntree...
Definition: ntree.h:269
iterator operator++(int)
Postfix increment operator.
Definition: ntree.h:96
void append_nodes_(std::vector< value_type > &)
Append this ntree's nodes to the vector.
iterator end()
Definition: ntree.h:261
masked_iterator()
Initialize an invalid iterator.
Definition: ntree.h:133