@@ -208,28 +208,32 @@ namespace sparta
208
208
// / override the comparison operator.
209
209
bool operator <(const BufferIterator& rhs) const
210
210
{
211
- sparta_assert (attached_buffer_ == rhs.attached_buffer_ , " Cannot compare BufferIterators created by different buffers." );
211
+ sparta_assert (attached_buffer_ == rhs.attached_buffer_ ,
212
+ " Cannot compare BufferIterators created by different buffers." );
212
213
return getIndex_ () < rhs.getIndex_ ();
213
214
}
214
215
215
216
// / override the comparison operator.
216
217
bool operator >(const BufferIterator& rhs) const
217
218
{
218
- sparta_assert (attached_buffer_ == rhs.attached_buffer_ , " Cannot compare BufferIterators created by different buffers." );
219
+ sparta_assert (attached_buffer_ == rhs.attached_buffer_ ,
220
+ " Cannot compare BufferIterators created by different buffers." );
219
221
return getIndex_ () > rhs.getIndex_ ();
220
222
}
221
223
222
224
// / override the comparison operator.
223
225
bool operator ==(const BufferIterator& rhs) const
224
226
{
225
- sparta_assert (attached_buffer_ == rhs.attached_buffer_ , " Cannot compare BufferIterators created by different buffers." );
227
+ sparta_assert (attached_buffer_ == rhs.attached_buffer_ ,
228
+ " Cannot compare BufferIterators created by different buffers." );
226
229
return (buffer_entry_ == rhs.buffer_entry_ );
227
230
}
228
231
229
232
// / override the not equal operator.
230
233
bool operator !=(const BufferIterator& rhs) const
231
234
{
232
- sparta_assert (attached_buffer_ == rhs.attached_buffer_ , " Cannot compare BufferIterators created by different buffers." );
235
+ sparta_assert (attached_buffer_ == rhs.attached_buffer_ ,
236
+ " Cannot compare BufferIterators created by different buffers." );
233
237
return !operator ==(rhs);
234
238
}
235
239
@@ -245,39 +249,39 @@ namespace sparta
245
249
246
250
// / override the dereferencing operator
247
251
DataReferenceType operator * () const {
248
- sparta_assert (attached_buffer_, " The iterator is not attached to a buffer. Was it initialized?" );
252
+ sparta_assert (attached_buffer_,
253
+ " The iterator is not attached to a buffer. Was it initialized?" );
249
254
sparta_assert (isValid (), " Iterator is not valid for dereferencing" );
250
255
return *(buffer_entry_->data );
251
256
}
252
257
253
258
// ! Overload the class-member-access operator.
254
259
value_type * operator -> () {
255
- sparta_assert (attached_buffer_, " The iterator is not attached to a buffer. Was it initialized?" );
260
+ sparta_assert (attached_buffer_,
261
+ " The iterator is not attached to a buffer. Was it initialized?" );
256
262
sparta_assert (isValid (), " Iterator is not valid for dereferencing" );
257
263
return buffer_entry_->data ;
258
264
}
259
265
260
266
value_type const * operator -> () const {
261
- sparta_assert (attached_buffer_, " The iterator is not attached to a buffer. Was it initialized?" );
267
+ sparta_assert (attached_buffer_,
268
+ " The iterator is not attached to a buffer. Was it initialized?" );
262
269
sparta_assert (isValid (), " Iterator is not valid for dereferencing" );
263
270
return buffer_entry_->data ;
264
271
}
265
272
266
273
/* * brief Move the iterator forward to point to next element in queue ; PREFIX
267
274
*/
268
275
BufferIterator & operator ++() {
269
- sparta_assert (attached_buffer_, " The iterator is not attached to a buffer. Was it initialized?" );
270
- if (isValid ()) {
271
- uint32_t idx = buffer_entry_->physical_idx ;
272
- ++idx;
273
- if (attached_buffer_->isValid (idx)) {
274
- buffer_entry_ = attached_buffer_->buffer_map_ [idx];
275
- }
276
- else {
277
- buffer_entry_ = nullptr ;
278
- }
279
- } else {
280
- sparta_assert (attached_buffer_->numFree () > 0 , " Incrementing the iterator to entry that is not valid" );
276
+ sparta_assert (attached_buffer_,
277
+ " The iterator is not attached to a buffer. Was it initialized?" );
278
+ sparta_assert (isValid (), " Incrementing an iterator that is not valid" );
279
+ const uint32_t idx = buffer_entry_->physical_idx + 1 ;
280
+ if (attached_buffer_->isValid (idx)) {
281
+ buffer_entry_ = attached_buffer_->buffer_map_ [idx];
282
+ }
283
+ else {
284
+ buffer_entry_ = nullptr ;
281
285
}
282
286
return *this ;
283
287
}
@@ -438,7 +442,7 @@ namespace sparta
438
442
*/
439
443
const value_type & read (const const_reverse_iterator & entry) const
440
444
{
441
- return read (entry.base (). getIndex_ ( ));
445
+ return read (std::prev ( entry.base ()));
442
446
}
443
447
444
448
/* *
@@ -465,7 +469,7 @@ namespace sparta
465
469
* \param entry the BufferIterator to read from.
466
470
*/
467
471
value_type & access (const const_reverse_iterator & entry) {
468
- return access (entry.base (). getIndex_ ( ));
472
+ return access (std::prev ( entry.base ()));
469
473
}
470
474
471
475
/* *
@@ -616,10 +620,11 @@ namespace sparta
616
620
* a BufferIterator has been created, the
617
621
* erase(BufferIterator&) should be used.
618
622
*/
619
- void erase (const uint32_t & idx)
623
+ void erase (uint32_t idx)
620
624
{
621
625
// Make sure we are invalidating an already valid object.
622
- sparta_assert (idx < size (), " Cannot erase an index that is not already valid" );
626
+ sparta_assert (idx < size (),
627
+ " Cannot erase an index that is not already valid" );
623
628
624
629
// Do the invalidation immediately
625
630
// 1. Move the free space pointer to the erased position.
@@ -634,19 +639,18 @@ namespace sparta
634
639
validator_->detachDataPointer (free_position_);
635
640
636
641
// Shift all the positions above the invalidation in the map one space down.
637
- uint32_t i = idx;
638
642
sparta_assert (num_valid_ > 0 );
639
643
const uint32_t top_idx_of_buffer = num_valid_ - 1 ;
640
- while (i < top_idx_of_buffer)
644
+ while (idx < top_idx_of_buffer)
641
645
{
642
646
// assert that we are not going to do an invalid read.
643
- sparta_assert (i + 1 < num_entries_);
644
- buffer_map_[i ] = buffer_map_[i + 1 ];
645
- buffer_map_[i ]->physical_idx = i ;
647
+ sparta_assert (idx + 1 < num_entries_);
648
+ buffer_map_[idx ] = buffer_map_[idx + 1 ];
649
+ buffer_map_[idx ]->physical_idx = idx ;
646
650
647
651
// Shift the indexes in the address map.
648
- address_map_[i ] = address_map_[i + 1 ];
649
- ++i ;
652
+ address_map_[idx ] = address_map_[idx + 1 ];
653
+ ++idx ;
650
654
}
651
655
652
656
// the entry at the old num_valid_ in the map now points to nullptr
@@ -664,22 +668,22 @@ namespace sparta
664
668
* \brief erase the index at which the entry exists in the Buffer.
665
669
* \param entry a reference to the entry to be erased.
666
670
*/
667
- void erase (const const_iterator& entry)
671
+ iterator erase (const const_iterator& entry)
668
672
{
669
- sparta_assert (entry.attached_buffer_ == this , " Cannot erase an entry created by another Buffer" );
673
+ sparta_assert (entry.attached_buffer_ == this ,
674
+ " Cannot erase an entry created by another Buffer" );
670
675
// erase the index in the actual buffer.
671
676
erase (entry.getIndex_ ());
677
+ return {this , buffer_map_[entry.getIndex_ ()]};
672
678
}
673
679
674
680
/* *
675
681
* \brief erase the index at which the entry exists in the Buffer.
676
682
* \param entry a reference to the entry to be erased.
677
683
*/
678
- void erase (const const_reverse_iterator& entry)
684
+ reverse_iterator erase (const const_reverse_iterator& entry)
679
685
{
680
- sparta_assert (entry.base ().attached_buffer_ == this , " Cannot erase an entry created by another Buffer" );
681
- // erase the index in the actual buffer.
682
- erase (entry.base ().getIndex_ ());
686
+ return reverse_iterator{erase (std::prev (entry.base ()))};
683
687
}
684
688
685
689
/* *
@@ -871,7 +875,8 @@ namespace sparta
871
875
void resizeInternalContainers_ () {
872
876
873
877
// Assert that the Buffer class is in Infinite-Mode.
874
- sparta_assert (is_infinite_mode_, " The Buffer class must be in Infinite-Mode in order to resize itself." );
878
+ sparta_assert (is_infinite_mode_,
879
+ " The Buffer class must be in Infinite-Mode in order to resize itself." );
875
880
876
881
// We do not resize if there are available slots in buffer.
877
882
if (numFree () != 0 ) {
@@ -998,14 +1003,15 @@ namespace sparta
998
1003
999
1004
std::string name_;
1000
1005
const Clock * clk_ = nullptr ;
1001
- size_type num_entries_; /* !< The number of entries this buffer can hold */
1002
- PointerList buffer_map_; /* !< A vector list of pointers to all the items active in the buffer */
1003
- size_type data_pool_size_; /* !< The number of elements our data_pool_ can hold*/
1004
- DataPool data_pool_; /* !< A vector twice the size of our Buffer size limit that is filled with pointers for our data.*/
1005
-
1006
- DataPointer* free_position_ = 0 ; /* !< A pointer to a free position in our data_pool_ */
1007
- DataPointer* first_position_ = 0 ; /* !< A pointer to a first position in our data_pool_; used for lower bound check */
1008
- size_type num_valid_ = 0 ; /* !< A tally of valid items */
1006
+ size_type num_entries_ = 0 ; /* !< The number of entries this buffer can hold */
1007
+ PointerList buffer_map_; /* !< A vector list of pointers to all the items active in the buffer */
1008
+ size_type data_pool_size_ = 0 ; /* !< The number of elements our data_pool_ can hold*/
1009
+ DataPool data_pool_; /* !< A vector twice the size of our Buffer size limit
1010
+ that is filled with pointers for our data.*/
1011
+
1012
+ DataPointer* free_position_ = nullptr ; /* !< A pointer to a free position in our data_pool_ */
1013
+ DataPointer* first_position_ = nullptr ; /* !< A pointer to a first position in our data_pool_; used for lower bound check */
1014
+ size_type num_valid_ = 0 ; /* !< A tally of valid items */
1009
1015
std::unique_ptr<DataPointerValidator> validator_; /* !< Checks the validity of DataPointer */
1010
1016
1011
1017
// ////////////////////////////////////////////////////////////////////
0 commit comments