23#ifndef BLOCK_VECTOR_H_
24#define BLOCK_VECTOR_H_
34template <
typename value_type_ >
36template <
typename value_type_,
typename ref_,
typename ptr_ >
51template <
typename value_type_,
typename ref_,
typename ptr_ >
58 template <
typename,
typename,
typename >
62 template <
typename cv_value_type_ >
70 typename std::vector< value_type_ >::const_iterator
block_it_;
110 const typename std::vector< value_type_ >::const_iterator,
111 const typename std::vector< value_type_ >::const_iterator );
154template <
typename value_type_ >
157 template <
typename cv_value_type_,
typename ref_,
typename ptr_ >
323template <
typename value_type_ >
326 std::vector< std::vector< value_type_ > >( 1, std::move( std::vector< value_type_ >(
max_block_size ) ) ) )
331template <
typename value_type_ >
334 std::vector< std::vector< value_type_ > >( 1, std::move( std::vector< value_type_ >(
max_block_size ) ) ) )
337 size_t num_blocks_needed = std::ceil(
static_cast< double >( n ) /
max_block_size );
338 for (
size_t i = 0; i < num_blocks_needed - 1; ++i )
346template <
typename value_type_ >
348 : blockmap_( other.blockmap_ )
349 , finish_( begin() + ( other.finish_ - other.begin() ) )
353template <
typename value_type_ >
356template <
typename value_type_ >
364 return blockmap_[ block_index ][ element_index ];
367template <
typename value_type_ >
368inline const value_type_&
375 return blockmap_[ block_index ][ element_index ];
378template <
typename value_type_ >
385template <
typename value_type_ >
392template <
typename value_type_ >
399template <
typename value_type_ >
406template <
typename value_type_ >
411 if ( finish_.block_it_ == finish_.current_block_end_ - 1 )
417 finish_.block_vector_it_ = finish_.block_vector_->blockmap_.begin() + current_block;
423template <
typename value_type_ >
428 if ( finish_.block_it_ == finish_.current_block_end_ - 1 )
432 const auto current_block = finish_.block_vector_it_ - finish_.block_vector_->blockmap_.begin();
434 finish_.block_vector_it_ = finish_.block_vector_->blockmap_.begin() + current_block;
436 *finish_ = std::move( value );
440template <
typename value_type_ >
444 for (
auto it = blockmap_.begin(); it != blockmap_.end(); ++it )
454template <
typename value_type_ >
458 size_t element_index;
459 if ( finish_.block_vector_it_ >= blockmap_.end() )
466 element_index = finish_.block_it_ - finish_.block_vector_it_->begin();
468 return ( finish_.block_vector_it_ - finish_.block_vector_->blockmap_.begin() ) *
max_block_size + element_index;
471template <
typename value_type_ >
477 assert( last < finish_ or last == finish_ );
482 else if ( first == begin() and last == end() )
490 for (
auto element = last; element != end(); ++element )
492 *repl_it = std::move( *element );
497 auto& new_final_block = blockmap_[ repl_it.block_vector_it_ - blockmap_.begin() ];
502 auto element_index = repl_it.block_it_ - new_final_block.begin();
504 new_final_block.erase( new_final_block.begin() + element_index, new_final_block.end() );
508 for (
int i = 0; i < num_default_init; ++i )
510 new_final_block.emplace_back();
514 blockmap_.erase( repl_it.block_vector_it_ + 1, blockmap_.end() );
524template <
typename value_type_ >
528 std::cerr <<
"this: \t\t" <<
this <<
"\n";
529 std::cerr <<
"finish block_vector: \t" << finish_.
block_vector_ <<
"\n";
530 std::cerr <<
"Blockmap size: " << blockmap_.size() <<
"\n";
531 std::cerr <<
"==============================================\n";
532 auto seq_iter = begin();
533 for (
size_t block_index = 0; block_index != blockmap_.size() and seq_iter != end(); ++block_index )
535 std::cerr <<
"----------------------------------------------\n";
536 auto& block = blockmap_[ block_index ];
537 std::cerr <<
"Block size: " << block.size() <<
"\n";
538 for (
auto block_it = block.begin(); block_it != block.end() and seq_iter != end(); ++block_it )
540 std::cerr << *block_it <<
" ";
543 std::cerr <<
"\n----------------------------------------------\n";
545 std::cerr <<
"==============================================\n";
548template <
typename value_type_ >
555template <
typename value_type_ >
562template <
typename value_type_ >
569template <
typename value_type_ >
576template <
typename value_type_ >
583template <
typename value_type_ >
594template <
typename value_type_,
typename ref_,
typename ptr_ >
603template <
typename value_type_,
typename ref_,
typename ptr_ >
612template <
typename value_type_,
typename ref_,
typename ptr_ >
614 const typename std::vector< std::vector< value_type_ > >
::const_iterator block_vector_it,
615 const typename std::vector< value_type_ >::const_iterator block_it,
616 const typename std::vector< value_type_ >::const_iterator current_block_end )
624template <
typename value_type_,
typename ref_,
typename ptr_ >
645template <
typename value_type_,
typename ref_,
typename ptr_ >
672template <
typename value_type_,
typename ref_,
typename ptr_ >
687template <
typename value_type_,
typename ref_,
typename ptr_ >
702template <
typename value_type_,
typename ref_,
typename ptr_ >
710template <
typename value_type_,
typename ref_,
typename ptr_ >
718template <
typename value_type_,
typename ref_,
typename ptr_ >
727template <
typename value_type_,
typename ref_,
typename ptr_ >
736template <
typename value_type_,
typename ref_,
typename ptr_ >
742 return const_cast< reference >( *block_it_ );
745template <
typename value_type_,
typename ref_,
typename ptr_ >
751 return const_cast< pointer >( &( *block_it_ ) );
754template <
typename value_type_,
typename ref_,
typename ptr_ >
759 const auto other_element_index = other.block_it_ - other.block_vector_it_->begin();
763template <
typename value_type_,
typename ref_,
typename ptr_ >
768 const auto other_element_index = other.block_it_ - other.block_vector_it_->begin();
772template <
typename value_type_,
typename ref_,
typename ptr_ >
783template <
typename value_type_,
typename ref_,
typename ptr_ >
787 return *( *
this + n );
790template <
typename value_type_,
typename ref_,
typename ptr_ >
797template <
typename value_type_,
typename ref_,
typename ptr_ >
804template <
typename value_type_,
typename ref_,
typename ptr_ >
812template <
typename value_type_,
typename ref_,
typename ptr_ >
820template <
typename value_type_,
typename ref_,
typename ptr_ >
827template <
typename value_type_,
typename ref_,
typename ptr_ >
834template <
typename value_type_,
typename ref_,
typename ptr_ >
841template <
typename value_type_,
typename ref_,
typename ptr_ >
constexpr int max_block_size_sub_1
Definition block_vector.h:41
constexpr int max_block_size
Definition block_vector.h:40
constexpr int block_size_shift
max_block_size = 2^block_size_shift
Definition block_vector.h:39
bv_iterator< value_type_, ref_, ptr_ > operator+(typename bv_iterator< value_type_, ref_, ptr_ >::difference_type n, bv_iterator< value_type_, ref_, ptr_ > &x)
Definition block_vector.h:843
Container with a vector-of-vectors structure.
Definition block_vector.h:156
iterator finish_
Iterator pointing to one past the last element.
Definition block_vector.h:316
const_iterator begin() const
Returns a read-only (constant) iterator that points to the first element in the BlockVector.
Definition block_vector.h:387
reverse_iterator rend() const
Returns a read-only (constant) reverse iterator that points to one before the first element in the Bl...
Definition block_vector.h:585
const value_type_ & operator[](const size_t pos) const
Subscript access to the data contained in the BlockVector.
Definition block_vector.h:369
typename std::vector< value_type >::difference_type difference_type
Definition block_vector.h:162
size_t size_type
Definition block_vector.h:169
iterator begin()
Returns a read/write iterator that points to the first element in the BlockVector.
Definition block_vector.h:380
reverse_iterator rbegin()
Returns a read/write reverse iterator that points to the last element in the BlockVector.
Definition block_vector.h:564
void clear()
Erases all the elements.
Definition block_vector.h:442
reverse_iterator rend()
Returns a read/write reverse iterator that points to one before the first element in the BlockVector.
Definition block_vector.h:578
const value_type & const_reference
Definition block_vector.h:163
size_type max_size() const
Returns the size() of the largest possible BlockVector.
Definition block_vector.h:557
void print_blocks() const
Writes the contents of the BlockVector, separated into blocks, to cerr.
Definition block_vector.h:526
const_iterator end() const
Returns a read-only (constant) iterator that points one past the last element in the BlockVector.
Definition block_vector.h:401
iterator end()
Returns a read/write iterator that points one past the last element in the BlockVector.
Definition block_vector.h:394
int get_max_block_size() const
Returns the block-size.
Definition block_vector.h:550
iterator erase(const_iterator, const_iterator)
Remove a range of elements.
Definition block_vector.h:473
size_t size() const
Returns the number of elements in the BlockVector.
Definition block_vector.h:456
value_type_ & operator[](const size_t pos)
Subscript access to the data contained in the BlockVector.
Definition block_vector.h:358
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition block_vector.h:168
std::vector< std::vector< value_type_ > > blockmap_
Vector holding blocks containing data.
Definition block_vector.h:315
BlockVector()
Creates an empty BlockVector.
Definition block_vector.h:324
value_type_ value_type
Definition block_vector.h:161
void push_back(value_type_ &&value)
Move data to the end of the BlockVector.
Definition block_vector.h:425
void push_back(const value_type_ &value)
Add data to the end of the BlockVector.
Definition block_vector.h:408
BlockVector(size_t)
Creates a BlockVector containing a number of elements.
Definition block_vector.h:332
BlockVector(const BlockVector< value_type_ > &)
BlockVector copy constructor.
Definition block_vector.h:347
std::reverse_iterator< iterator > reverse_iterator
Definition block_vector.h:167
const value_type * const_pointer
Definition block_vector.h:164
reverse_iterator rbegin() const
Returns a read-only (constant) reverse iterator that points to the last element in the BlockVector.
Definition block_vector.h:571
A BlockVector::iterator.
Definition block_vector.h:53
bv_iterator operator--(int)
Definition block_vector.h:729
std::random_access_iterator_tag iterator_category
Definition block_vector.h:78
bool operator<=(const bv_iterator &) const
Definition block_vector.h:822
value_type_ value_type
Definition block_vector.h:79
ptr_ pointer
Definition block_vector.h:80
bool operator==(const bv_iterator &) const
Definition block_vector.h:792
bv_iterator()
Definition block_vector.h:84
bv_iterator & operator-=(difference_type)
Definition block_vector.h:689
bv_iterator operator++(int)
Definition block_vector.h:720
iterator const_cast_() const
Converts the iterator to a non-const iterator.
Definition block_vector.h:836
ref_ reference
Definition block_vector.h:81
iterator & operator=(const iterator &)
Definition block_vector.h:774
const BlockVector< value_type_ > * block_vector_
BlockVector to which this iterator points.
Definition block_vector.h:66
bv_iterator operator-(difference_type) const
Definition block_vector.h:712
difference_type operator-(const const_iterator &) const
Definition block_vector.h:765
reference operator*() const
Definition block_vector.h:738
iter_< const value_type_ > const_iterator
Definition block_vector.h:76
pointer operator->() const
Definition block_vector.h:747
friend class bv_iterator
Definition block_vector.h:59
bv_iterator(const BlockVector< value_type_ > &)
Creates an iterator pointing to the first element in a BlockVector.
Definition block_vector.h:595
bv_iterator operator+(difference_type) const
Definition block_vector.h:704
typename BlockVector< value_type >::difference_type difference_type
Definition block_vector.h:82
reference operator[](difference_type n) const
Definition block_vector.h:785
bv_iterator(const iterator &)
Iterator copy constructor.
Definition block_vector.h:604
bool operator<(const bv_iterator &) const
Definition block_vector.h:806
difference_type operator-(const iterator &) const
Definition block_vector.h:756
std::vector< value_type_ >::const_iterator block_it_
Iterator pointing to the current element in the current block.
Definition block_vector.h:70
std::vector< std::vector< value_type_ > >::const_iterator block_vector_it_
Iterator for the current block in the blockmap.
Definition block_vector.h:68
bv_iterator(const BlockVector< value_type_ > *, const typename std::vector< std::vector< value_type_ > >::const_iterator, const typename std::vector< value_type_ >::const_iterator, const typename std::vector< value_type_ >::const_iterator)
Creates an iterator with specified parameters.
Definition block_vector.h:613
iter_< value_type_ > iterator
Definition block_vector.h:75
bool operator!=(const bv_iterator &) const
Definition block_vector.h:799
bv_iterator & operator+=(difference_type)
Definition block_vector.h:674
bool operator>=(const bv_iterator &) const
Definition block_vector.h:829
bool operator>(const bv_iterator &) const
Definition block_vector.h:814
bv_iterator & operator++()
Definition block_vector.h:626
std::vector< value_type_ >::const_iterator current_block_end_
Iterator pointing to the end of the current block.
Definition block_vector.h:72
bv_iterator & operator--()
Definition block_vector.h:647
Exception to be thrown if a feature is unavailable.
Definition exceptions.h:108