NEST main@caf0ae8
 
Loading...
Searching...
No Matches
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// C++ includes:
27#include <bitset>
28#include <iterator>
29#include <utility>
30#include <vector>
31
32// Includes from spatial:
33#include "position.h"
34
35namespace nest
36{
37
38class AbstractMask;
39
40template < int D >
41class Mask;
42
53template < int D, class T, int max_capacity = 100, int max_depth = 10 >
54class Ntree
55{
56public:
57 static const int N = 1 << D;
58
60 typedef T mapped_type;
61 typedef std::pair< Position< D >, T > value_type;
64
69 {
70 public:
75 : ntree_( 0 )
76 , top_( 0 )
77 , node_( 0 )
78 {
79 }
80
85 explicit iterator( Ntree& q );
86
92 iterator( Ntree& q, size_t n );
93
96 {
97 return ntree_->nodes_[ node_ ];
98 }
101 {
102 return &ntree_->nodes_[ node_ ];
103 }
104
110
116 {
117 iterator tmp = *this;
118 ++*this;
119 return tmp;
120 }
121
126 bool
127 operator==( const iterator& other ) const
128 {
129 return other.ntree_ == ntree_ and ( other.node_ == node_ );
130 }
131 bool
132 operator!=( const iterator& other ) const
133 {
134 return ( other.ntree_ != ntree_ ) or ( other.node_ != node_ );
135 }
136
137 protected:
142 void next_leaf_();
143
146 size_t node_;
147 };
148
153 {
154 public:
155 using iterator_category = std::forward_iterator_tag;
156 using value_type = std::pair< Position< D >, T >;
159 using difference_type = long int;
164 : ntree_( 0 )
165 , top_( 0 )
166 , allin_top_( 0 )
167 , node_( 0 )
168 , mask_( 0 )
169 {
170 }
171
176 masked_iterator( Ntree& q, const Mask< D >& mask, const Position< D >& anchor );
177
180 {
181 return ntree_->nodes_[ node_ ];
182 }
185 {
186 return &ntree_->nodes_[ node_ ];
187 }
188
197
203 {
204 masked_iterator tmp = *this;
205 ++*this;
206 return tmp;
207 }
208
213 bool
214 operator==( const masked_iterator& other ) const
215 {
216 return other.ntree_ == ntree_ and ( other.node_ == node_ );
217 }
218 bool
219 operator!=( const masked_iterator& other ) const
220 {
221 return ( other.ntree_ != ntree_ ) or ( other.node_ != node_ );
222 }
223
224 protected:
228 void init_();
229
233 void next_leaf_();
234
240 void first_leaf_();
241
246 void first_leaf_inside_();
247
251 void next_anchor_();
252
253 bool
255 {
256 // Create anchored position in two steps to avoid creating a new Position object.
257 anchored_position_ = position;
259 return mask_->inside( anchored_position_ );
260 }
261
265 size_t node_;
269 std::vector< Position< D > > anchors_;
271 };
272
280 Ntree( const Position< D >& lower_left,
281 const Position< D >& extent,
282 std::bitset< D > periodic = 0,
283 Ntree* parent = 0,
284 int subquad = 0 );
285
289 ~Ntree();
290
297 iterator insert( Position< D > pos, const T& node );
298
303
308
312 void push_back( const value_type& val );
313
314
318 std::vector< value_type > get_nodes();
319
327 std::vector< value_type > get_nodes( const Mask< D >& mask, const Position< D >& anchor );
328
337 {
338 return iterator( *this );
339 }
340
341 iterator
343 {
344 return iterator();
345 }
346
353 masked_iterator
354 masked_begin( const Mask< D >& mask, const Position< D >& anchor )
355 {
356 return masked_iterator( *this, mask, anchor );
357 }
358
359 masked_iterator
361 {
362 return masked_iterator();
363 }
364
368 bool is_leaf() const;
369
370protected:
375 void split_();
376
380 void append_nodes_( std::vector< value_type >& );
381
385 void append_nodes_( std::vector< value_type >&, const Mask< D >&, const Position< D >& );
386
390 int subquad_( const Position< D >& );
391
394
395 bool leaf_;
396
397 std::vector< value_type > nodes_;
398
403 std::bitset< D > periodic_;
404
405 friend class iterator;
406 friend class masked_iterator;
407};
408
409template < int D, class T, int max_capacity, int max_depth >
411 const Position< D >& extent,
412 std::bitset< D > periodic,
414 int subquad )
415 : lower_left_( lower_left )
416 , extent_( extent )
417 , leaf_( true )
418 , parent_( parent )
419 , my_subquad_( subquad )
420 , my_depth_( parent ? parent->my_depth_ + 1 : 0 )
421 , periodic_( periodic )
422{
423}
424
425template < int D, class T, int max_capacity, int max_depth >
427{
428 if ( leaf_ )
429 {
430 // if T is a vector class, we do not delete the pointees
431 return;
432 }
433
434 for ( size_t n = 0; n < static_cast< size_t >( N ); ++n )
435 {
436 delete children_[ n ]; // calls destructor in child, thus recursing
437 }
438}
439
440template < int D, class T, int max_capacity, int max_depth >
442 : ntree_( &q )
443 , top_( &q )
444 , node_( n )
445{
446 assert( ntree_->leaf_ );
447
448 // First ancestor
449 while ( top_->parent_ )
450 {
451 top_ = top_->parent_;
452 }
453}
454
455template < int D, class T, int max_capacity, int max_depth >
456bool
461
462
463template < int D, class T, int max_capacity, int max_depth >
464std::vector< std::pair< Position< D >, T > >
466{
467 std::vector< std::pair< Position< D >, T > > result;
468 append_nodes_( result );
469 return result;
470}
471
472template < int D, class T, int max_capacity, int max_depth >
473std::vector< std::pair< Position< D >, T > >
475{
476 std::vector< std::pair< Position< D >, T > > result;
477 append_nodes_( result, mask, anchor );
478 return result;
479}
480
481template < int D, class T, int max_capacity, int max_depth >
484{
485 return insert( val.first, val.second );
486}
487
488template < int D, class T, int max_capacity, int max_depth >
489typename Ntree< D, T, max_capacity, max_depth >::iterator
490Ntree< D, T, max_capacity, max_depth >::insert( iterator, const std::pair< Position< D >, T >& val )
491{
492 return insert( val.first, val.second );
493}
494
495template < int D, class T, int max_capacity, int max_depth >
496void
501
502
503} // namespace nest
504
505
506#endif
Abstract base class for masks with given dimension.
Definition mask.h:101
Iterator iterating the nodes in a Quadtree.
Definition ntree.h:69
iterator & operator++()
Move the iterator to the next node within the tree.
Definition ntree_impl.h:61
Ntree * ntree_
Definition ntree.h:144
value_type * operator->()
Definition ntree.h:100
Ntree * top_
Definition ntree.h:145
bool operator==(const iterator &other) const
Iterators are equal if they point to the same node in the same ntree.
Definition ntree.h:127
size_t node_
Definition ntree.h:146
iterator()
Initialize an invalid iterator.
Definition ntree.h:74
bool operator!=(const iterator &other) const
Definition ntree.h:132
iterator operator++(int)
Postfix increment operator.
Definition ntree.h:115
value_type & operator*()
Definition ntree.h:95
void next_leaf_()
Move to the next leaf quadrant, or set ntree_ to 0 if there are no more leaves.
Definition ntree_impl.h:82
Iterator iterating the nodes in a Quadtree inside a Mask.
Definition ntree.h:153
Ntree * ntree_
Definition ntree.h:262
value_type * operator->()
Definition ntree.h:184
std::vector< Position< D > > anchors_
Definition ntree.h:269
masked_iterator operator++(int)
Postfix increment operator.
Definition ntree.h:202
value_type * pointer
Definition ntree.h:157
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:333
void init_()
Initialize.
Definition ntree_impl.h:176
value_type & operator*()
Definition ntree.h:179
masked_iterator & operator++()
Move the iterator to the next node inside the mask within the tree.
Definition ntree_impl.h:346
long int difference_type
Definition ntree.h:159
const Mask< D > * mask_
Definition ntree.h:266
Ntree * allin_top_
Definition ntree.h:264
bool operator!=(const masked_iterator &other) const
Definition ntree.h:219
masked_iterator()
Initialize an invalid iterator.
Definition ntree.h:163
std::pair< Position< D >, T > value_type
Definition ntree.h:156
Position< D > anchor_
Definition ntree.h:267
bool operator==(const masked_iterator &other) const
Iterators are equal if they point to the same node in the same ntree.
Definition ntree.h:214
size_t current_anchor_
Definition ntree.h:270
Ntree * top_
Definition ntree.h:263
void next_leaf_()
Find the next leaf which is not outside the mask.
Definition ntree_impl.h:226
void next_anchor_()
Go to the next anchor image.
Definition ntree_impl.h:208
value_type & reference
Definition ntree.h:158
void first_leaf_()
Find the first leaf which is not outside the mask.
Definition ntree_impl.h:311
Position< D > anchored_position_
Definition ntree.h:268
std::forward_iterator_tag iterator_category
Definition ntree.h:155
size_t node_
Definition ntree.h:265
bool anchored_position_inside_mask(const Position< D > &position)
Definition ntree.h:254
A Ntree object represents a subtree or leaf in a Ntree structure.
Definition ntree.h:55
value_type & reference
Definition ntree.h:62
iterator insert(Position< D > pos, const T &node)
Traverse quadtree structure from current ntree.
Definition ntree_impl.h:453
Position< D > lower_left_
Definition ntree.h:392
Ntree * parent_
Definition ntree.h:399
bool is_leaf() const
Definition ntree.h:457
std::pair< Position< D >, T > value_type
Definition ntree.h:61
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:410
T mapped_type
Definition ntree.h:60
iterator insert(const value_type &val)
std::multimap like insert method
masked_iterator masked_end()
Definition ntree.h:360
int my_subquad_
This Ntree's subquad number within parent.
Definition ntree.h:400
void push_back(const value_type &val)
STL container compatible push_back method.
Definition ntree.h:497
Position< D > extent_
Definition ntree.h:393
void split_()
Change a leaf ntree to a regular ntree with four children regions.
Definition ntree_impl.h:502
int my_depth_
This Ntree's depth in the tree.
Definition ntree.h:401
~Ntree()
Delete Ntree recursively.
Definition ntree.h:426
int subquad_(const Position< D > &)
Definition ntree_impl.h:384
Position< D > key_type
Definition ntree.h:59
static const int N
Definition ntree.h:57
Ntree * children_[N]
Definition ntree.h:402
iterator insert(iterator, const value_type &val)
STL container compatible insert method (the first argument is ignored)
iterator begin()
This function returns a node iterator which will traverse the subtree below this Ntree.
Definition ntree.h:336
std::vector< value_type > nodes_
Definition ntree.h:397
std::vector< value_type > get_nodes()
Definition ntree.h:465
std::bitset< D > periodic_
periodic b.c.
Definition ntree.h:403
void append_nodes_(std::vector< value_type > &, const Mask< D > &, const Position< D > &)
Append this ntree's nodes inside the mask to the vector.
void append_nodes_(std::vector< value_type > &)
Append this ntree's nodes to the vector.
const value_type & const_reference
Definition ntree.h:63
iterator end()
Definition ntree.h:342
bool leaf_
Definition ntree.h:395
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:354
Definition position.h:57
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33