6 #ifndef CNOID_UTIL_DEQUE_2D_H 7 #define CNOID_UTIL_DEQUE_2D_H 9 #include <Eigen/StdVector> 15 template <
typename ElementType,
typename Allocator = std::allocator<ElementType> >
23 class const_iterator :
public std::iterator<std::random_access_iterator_tag, ElementType> {
25 friend class Deque2D<ElementType, Allocator>;
35 term = buf + owner.capacity_;
67 current = buf + (current -
term);
74 current = term - (buf -
current);
89 return (current == rhs.
current);
92 return (current != rhs.
current);
98 friend class Deque2D<ElementType, Allocator>;
136 return iterator(*
this,
buf + offset);
162 size_ = owner.colSize_;
164 if(owner.capacity_ > 0){
165 top += (owner.offset + rowIndex * owner.colSize_) % owner.capacity_;
185 Element&
at(
int index) {
208 offset = owner.offset;
210 capacity = owner.capacity_;
212 end_ =
iterator(*
this, top + ((owner.capacity_ > 0) ? ((offset + owner.size_) % owner.capacity_) : 0));
224 return top[(offset + rowIndex *
colSize) % capacity];
228 return top[(offset + rowIndex *
colSize) % capacity];
231 Element&
at(
int index) {
235 class iterator :
public std::iterator<std::bidirectional_iterator_tag, ElementType, int> {
249 term = top + column.capacity;
250 colSize = column.colSize;
269 return (current == rhs.current);
272 return (current != rhs.current);
277 return iterator(*
this, top + offset);
310 resizeMain(rowSize, colSize,
false);
314 : allocator(org.allocator) {
317 rowSize_ = org.rowSize_;
318 colSize_ = org.colSize_;
319 capacity_ = size_ + colSize_;
323 buf = allocator.allocate(capacity_);
325 ElementType* p =
buf;
326 ElementType* pend =
buf + size_;
327 ElementType* q = org.buf + org.offset;
328 ElementType* qterm = org.buf + org.capacity_;
330 allocator.construct(p++, *q++);
336 end_ = iterator(*
this,
buf + ((capacity_ > 0) ? ((offset + size_) % capacity_) : 0));
341 resizeMain(rhs.rowSize_, rhs.colSize_,
false);
342 iterator p =
begin();
356 ElementType* p =
buf + offset;
357 const ElementType* pend =
buf + (offset + size_) % capacity_;
361 allocator.destroy(p++);
364 for(ElementType* q =
buf; q != pend; ++q){
365 allocator.destroy(q);
367 const ElementType* pterm =
buf + capacity_;
368 for(ElementType* q = p; q != pterm; ++q){
369 allocator.destroy(q);
372 allocator.deallocate(
buf, capacity_);
377 return !rowSize_ || !colSize_;
381 void reallocMemory(
int newColSize,
int newSize,
int newCapacity,
bool doCopy) {
385 newBuf = allocator.allocate(newCapacity);
389 ElementType* p = newBuf;
390 ElementType* pend = newBuf + newSize;
393 ElementType* qend =
buf + (offset + size_) % capacity_;
396 if(newCapacity > 0 && newColSize == colSize_ && doCopy){
397 ElementType* q =
buf + offset;
399 while(q != qend && p != pend){
400 allocator.construct(p++, *q++);
403 for(ElementType* r =
buf; r != qend && p != pend; ++r){
404 allocator.construct(p++, *r);
406 ElementType* qterm =
buf + capacity_;
407 for(ElementType* r = q; r != qterm && p != pend; ++r){
408 allocator.construct(p++, *r);
413 ElementType* q =
buf + offset;
416 allocator.destroy(q++);
419 for(ElementType* r =
buf; r != qend; ++r){
420 allocator.destroy(r);
422 ElementType* qterm =
buf + capacity_;
423 for(ElementType* r = q; r != qterm; ++r){
424 allocator.destroy(r);
431 allocator.construct(p++, ElementType());
435 allocator.deallocate(
buf, capacity_);
438 capacity_ = newCapacity;
442 void resizeMain(
int newRowSize,
int newColSize,
bool doCopy) {
444 const int newSize = newRowSize * newColSize;
447 reallocMemory(newColSize, newSize, 0,
false);
451 const int minCapacity = newSize + newColSize;
453 if(capacity_ > 0 && minCapacity <= capacity_){
454 if(newColSize != colSize_ && (capacity_ % newColSize > 0)){
455 reallocMemory(newColSize, newSize, capacity_ - (capacity_ % newColSize), doCopy);
457 }
else if(newSize > size_){
458 ElementType* p =
buf + (offset + size_) % capacity_;
459 const ElementType* pend =
buf + (offset + newSize) % capacity_;
462 allocator.construct(p++, ElementType());
465 for(ElementType* r =
buf; r != pend; ++r){
466 allocator.construct(r, ElementType());
468 const ElementType* pterm =
buf + capacity_;
469 for(ElementType* r = p; r != pterm; ++r){
470 allocator.construct(r, ElementType());
473 }
else if(newSize < size_){
474 ElementType* p =
buf + (offset + newSize) % capacity_;
475 ElementType* pend =
buf + (offset + size_) % capacity_;
478 allocator.destroy(p++);
481 for(ElementType* r =
buf; r != pend; ++r){
482 allocator.destroy(r);
484 const ElementType* pterm =
buf + capacity_;
485 for(ElementType* r = p; r != pterm; ++r){
486 allocator.destroy(r);
492 capacity_ = minCapacity;
494 buf = allocator.allocate(minCapacity);
495 ElementType* p =
buf;
496 ElementType* pend =
buf + newSize;
499 allocator.construct(p++, ElementType());
504 const int expandedSize = size_ * 3 / 2;
505 if(expandedSize > newSize){
506 newCapacity = expandedSize - (expandedSize % newColSize) + newColSize;
508 newCapacity = minCapacity;
510 reallocMemory(newColSize, newSize, newCapacity, doCopy);
515 rowSize_ = newRowSize;
516 colSize_ = newColSize;
518 end_ = iterator(*
this,
buf + ((capacity_ > 0) ? ((offset + size_) % capacity_) : 0));
522 void resize(
int newRowSize,
int newColSize) {
523 resizeMain(newRowSize, newColSize,
true);
527 resize(rowSize_, newColSize);
538 resize(newRowSize, colSize_);
549 const Element&
operator()(
int rowIndex,
int colIndex)
const {
550 return buf[(offset + (rowIndex * colSize_)) % capacity_ + colIndex];
554 return buf[(offset + (rowIndex * colSize_)) % capacity_ + colIndex];
557 const Element&
at(
int rowIndex,
int colIndex)
const {
558 return buf[(offset + (rowIndex * colSize_)) % capacity_ + colIndex];
561 Element&
at(
int rowIndex,
int colIndex) {
562 return buf[(offset + (rowIndex * colSize_)) % capacity_ + colIndex];
566 return Row(*
this, rowIndex);
570 return Row(*
this, rowIndex);
574 return Row(*
this, rowIndex);
577 const Row
row(
int rowIndex)
const {
578 return Row(*
this, rowIndex);
582 return Row(*
this, rowSize_ - 1);
586 return Row(*
this, rowSize_ - 1);
590 return Column(*
this, colIndex);
593 const Column
column(
int colIndex)
const {
594 return Column(*
this, colIndex);
598 resize(rowSize_ + 1, colSize_);
599 return Row(*
this, rowSize_ - 1);
603 resize(rowSize_ - 1, colSize_);
610 if(numRows > rowSize_){
613 const size_t popSize = numRows * colSize_;
614 ElementType* p =
buf + offset;
615 const ElementType* pend =
buf + (offset + popSize) % capacity_;
619 allocator.destroy(p++);
622 for(ElementType* r =
buf; r != pend; ++r){
623 allocator.destroy(r);
625 const ElementType* pterm =
buf + capacity_;
626 for(ElementType* r = p; r != pterm; ++r){
627 allocator.destroy(r);
630 offset = (offset + popSize) % capacity_;
Deque2D(const Deque2D< ElementType, Allocator > &org)
Definition: Deque2D.h:313
bool operator!=(iterator rhs) const
Definition: Deque2D.h:271
const_iterator(const const_iterator &org)
Definition: Deque2D.h:41
Column()
Definition: Deque2D.h:201
void operator++()
Definition: Deque2D.h:255
Element * end()
Definition: Deque2D.h:193
iterator(const iterator &org)
Definition: Deque2D.h:104
int size() const
Definition: Deque2D.h:219
const Element & operator[](int index) const
Definition: Deque2D.h:181
Column(const Deque2D< ElementType, Allocator > &owner, int column)
Definition: Deque2D.h:206
Element & at(int index)
Definition: Deque2D.h:231
Row()
Definition: Deque2D.h:157
Element * begin()
Definition: Deque2D.h:189
ElementType * term
Definition: Deque2D.h:29
void resizeRow(int newRowSize)
Definition: Deque2D.h:537
const Element & operator[](int rowIndex) const
Definition: Deque2D.h:227
const Row operator[](int rowIndex) const
Definition: Deque2D.h:569
iterator()
Definition: Deque2D.h:244
void pop_front()
Definition: Deque2D.h:635
Element & operator*()
Definition: Deque2D.h:252
bool empty() const
Definition: Deque2D.h:376
const_iterator cbegin() const
Definition: Deque2D.h:139
bool empty() const
Definition: Deque2D.h:215
iterator end()
Definition: Deque2D.h:143
const Element & at(int rowIndex, int colIndex) const
Definition: Deque2D.h:557
Row last()
Definition: Deque2D.h:581
bool empty() const
Definition: Deque2D.h:169
const Row last() const
Definition: Deque2D.h:585
void pop_back()
Definition: Deque2D.h:602
const Element & operator*() const
Definition: Deque2D.h:47
Deque2D(int rowSize, int colSize)
Definition: Deque2D.h:301
Definition: Deque2D.h:235
void resizeColumn(int newColSize)
Definition: Deque2D.h:526
void pop_front(int numRows)
Definition: Deque2D.h:606
void clear()
Definition: Deque2D.h:545
Element & operator[](int rowIndex)
Definition: Deque2D.h:223
iterator(Column &column, Element *pos)
Definition: Deque2D.h:246
iterator operator+(size_t n)
Definition: Deque2D.h:123
const_iterator(const Deque2DType &owner, ElementType *pos)
Definition: Deque2D.h:32
const Element & operator()(int rowIndex, int colIndex) const
Definition: Deque2D.h:549
ElementType * buf
Definition: Deque2D.h:30
const Column column(int colIndex) const
Definition: Deque2D.h:593
Deque2D()
Definition: Deque2D.h:292
Row(const Deque2D< ElementType, Allocator > &owner, int rowIndex)
Definition: Deque2D.h:161
void resize(int newRowSize, int newColSize)
Definition: Deque2D.h:522
const_iterator & operator-=(size_t n)
Definition: Deque2D.h:71
const_iterator()
Definition: Deque2D.h:39
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
bool operator==(iterator rhs) const
Definition: Deque2D.h:268
virtual ~Deque2D()
Definition: Deque2D.h:354
const Row row(int rowIndex) const
Definition: Deque2D.h:577
Element & operator*()
Definition: Deque2D.h:106
int size() const
Definition: Deque2D.h:173
const_iterator & operator+=(size_t n)
Definition: Deque2D.h:64
Row operator[](int rowIndex)
Definition: Deque2D.h:565
Element & operator[](int index)
Definition: Deque2D.h:177
Row row(int rowIndex)
Definition: Deque2D.h:573
Definition: Deque2D.h:198
int colSize() const
Definition: Deque2D.h:541
void operator--()
Definition: Deque2D.h:261
iterator begin()
Definition: Deque2D.h:135
const_iterator operator-(size_t n)
Definition: Deque2D.h:83
int rowSize() const
Definition: Deque2D.h:530
iterator & operator-=(size_t n)
Definition: Deque2D.h:116
Element & at(int rowIndex, int colIndex)
Definition: Deque2D.h:561
iterator()
Definition: Deque2D.h:103
iterator begin()
Definition: Deque2D.h:276
bool operator==(const const_iterator &rhs) const
Definition: Deque2D.h:88
bool operator!=(const const_iterator &rhs) const
Definition: Deque2D.h:91
Column column(int colIndex)
Definition: Deque2D.h:589
ElementType * current
Definition: Deque2D.h:28
iterator end()
Definition: Deque2D.h:279
const_iterator & operator++()
Definition: Deque2D.h:50
Definition: Deque2D.h:151
Deque2DType & operator=(const Deque2DType &rhs)
Definition: Deque2D.h:339
iterator & operator+=(size_t n)
Definition: Deque2D.h:109
ElementType Element
Definition: Deque2D.h:21
iterator operator-(size_t n)
Definition: Deque2D.h:128
const_iterator operator+(size_t n)
Definition: Deque2D.h:78
const_iterator & operator--()
Definition: Deque2D.h:57
Row append()
Definition: Deque2D.h:597
const_iterator cend() const
Definition: Deque2D.h:147
Element & at(int index)
Definition: Deque2D.h:185
Element & operator()(int rowIndex, int colIndex)
Definition: Deque2D.h:553