20 #ifndef GALOIS_GDEQUE_H
21 #define GALOIS_GDEQUE_H
23 #include "galois/config.h"
28 #include <boost/iterator/iterator_facade.hpp>
29 #include <boost/iterator/reverse_iterator.hpp>
41 template <
typename T,
unsigned ChunkSize = 64,
42 typename ContainerTy = FixedSizeRing<T, ChunkSize>>
52 template <
typename InputIterator>
54 : ContainerTy(first, second),
next(),
prev() {}
60 :
public boost::iterator_facade<outer_iterator<U>, U,
61 boost::bidirectional_traversal_tag> {
62 friend class boost::iterator_core_access;
63 template <
typename,
unsigned,
typename>
68 void increment() { cur = cur->next; }
77 template <
typename OtherTy>
78 bool equal(
const outer_iterator<OtherTy>& o)
const {
82 U& dereference()
const {
return *cur; }
85 outer_iterator(Block* b = 0, Block* l = 0) : cur(b), last(l) {}
87 template <
typename OtherTy>
88 outer_iterator(
const outer_iterator<OtherTy>& o)
89 : cur(o.cur), last(o.last) {}
92 typedef typename Block::iterator inner_iterator;
93 typedef typename Block::const_iterator const_inner_iterator;
105 template <
typename... Args>
106 Block* alloc_block(Args&&... args) {
109 Block* b = heap.allocate(1);
110 return new (b) Block(std::forward<Args>(args)...);
113 void free_block(Block* b) {
115 heap.deallocate(b, 1);
119 bool precondition()
const {
120 return (num == 0 &&
first == NULL && last == NULL) ||
121 (num > 0 &&
first != NULL && last != NULL);
124 Block* extend_first() {
125 Block* b = alloc_block();
135 Block* extend_last() {
136 Block* b = alloc_block();
146 void shrink(Block* b) {
158 template <
typename... Args>
159 std::pair<Block*, typename Block::iterator>
160 emplace(Block* b,
typename Block::iterator ii, Args&&... args) {
168 }
else if (b ==
first && ii == b->begin()) {
174 }
else if (b->full()) {
175 auto d = std::distance(ii, b->end());
176 Block* n = alloc_block(std::make_move_iterator(ii),
177 std::make_move_iterator(b->end()));
187 unsigned boff = std::distance(b->begin(), ii);
188 b->emplace(ii, std::forward<Args>(args)...);
189 return std::make_pair(b, b->begin() + boff);
195 std::random_access_iterator_tag,
196 GetBegin<Block>, GetEnd<Block>>
199 const_inner_iterator,
200 std::random_access_iterator_tag,
201 GetBegin<const Block>, GetEnd<const Block>>
204 #ifndef _NEW_ITERATOR
205 template <
typename U>
207 :
public boost::iterator_facade<Iterator<U>, U,
208 boost::bidirectional_traversal_tag> {
218 if (
offset == b->size()) {
237 template <
typename OtherTy>
238 bool equal(
const Iterator<OtherTy>& o)
const {
239 return b == o.b &&
offset == o.offset;
242 U& dereference()
const {
return b->getAt(
offset); }
248 template <
typename OtherTy>
268 std::swap(
first, o.first);
269 std::swap(last, o.last);
270 std::swap(num, o.num);
274 std::swap(
first, o.first);
275 std::swap(last, o.last);
276 std::swap(num, o.num);
286 assert(precondition());
290 outer_iterator<Block>{
nullptr, last},
299 assert(precondition());
302 outer_iterator<Block>{
nullptr, last},
311 assert(precondition());
315 outer_iterator<const Block>{
nullptr, last},
316 outer_iterator<const Block>{
first, last},
327 outer_iterator<const Block>{
nullptr, last},
328 outer_iterator<const Block>{
nullptr, last},
349 assert(precondition());
354 assert(precondition());
360 return first->front();
365 return first->front();
395 assert(precondition());
408 template <
typename... Args>
411 Block* b = pos.get_outer_reference().cur;
412 inner_iterator ii = pos.get_inner_reference();
415 typename Block::iterator ii;
417 ii = b->begin() + pos.offset;
419 auto p = emplace(b, ii, std::forward<Args>(args)...);
422 outer_iterator<Block>{
nullptr, last},
423 outer_iterator<Block>{p.first, last},
428 return iterator(p.first, last, std::distance(p.first->begin(), p.second));
461 template <
typename... Args>
463 assert(precondition());
465 if (!last || last->full())
468 pointer p = last->emplace_back(std::forward<Args>(args)...);
471 last->emplace_back(std::forward<Args>(args)...);
475 template <
typename ValueTy>
480 template <
typename... Args>
482 assert(precondition());
486 pointer p =
first->emplace_front(std::forward<Args>(args)...);
490 template <
typename ValueTy>
Alternate implementation of ChooseTwoLevelIterator.
Definition: TwoLevelIteratorA.h:40
void push_front(ValueTy &&v)
Definition: gdeque.h:491
Block()
Definition: gdeque.h:50
Iterator< T > iterator
Definition: gdeque.h:252
Block * first
Definition: gdeque.h:96
reverse_iterator rend()
Definition: gdeque.h:338
iterator::pointer pointer
Definition: gdeque.h:259
Helper functor, returns t.end()
Definition: TwoLevelIteratorA.h:383
size_t size_type
Definition: gdeque.h:263
Block * next
Definition: gdeque.h:47
Iterator< const T > const_iterator
Definition: gdeque.h:253
gdeque(gdeque &&o)
Definition: gdeque.h:267
const_iterator::reference const_reference
Definition: gdeque.h:261
iterator::difference_type difference_type
Definition: gdeque.h:262
Block(InputIterator first, InputIterator second)
Definition: gdeque.h:53
size_t size() const
Definition: gdeque.h:348
iterator end()
Definition: gdeque.h:298
iterator::reference reference
Definition: gdeque.h:260
iterator erase(iterator pos)
Definition: gdeque.h:432
const_reference back() const
Definition: gdeque.h:373
reference back()
Definition: gdeque.h:368
gdeque & operator=(gdeque &&o)
Definition: gdeque.h:273
void emplace_front(Args &&...args)
Definition: gdeque.h:481
#define GALOIS_DIE(...)
Definition: gIO.h:96
bool empty() const
Definition: gdeque.h:353
void clear()
Definition: gdeque.h:394
Block * b
Definition: gdeque.h:211
void emplace_back(Args &&...args)
Definition: gdeque.h:462
const_reverse_iterator rbegin() const
Definition: gdeque.h:340
void push_back(ValueTy &&v)
Definition: gdeque.h:476
reference front()
Definition: gdeque.h:358
Block * last
Definition: gdeque.h:212
gdeque()
Definition: gdeque.h:265
~gdeque()
Definition: gdeque.h:283
void pop_front()
Definition: gdeque.h:386
void pop_back()
Definition: gdeque.h:378
iterator begin()
Definition: gdeque.h:285
reverse_iterator rbegin()
Definition: gdeque.h:336
Iterator(const Iterator< OtherTy > &o)
Definition: gdeque.h:249
boost::reverse_iterator< const_iterator > const_reverse_iterator
Definition: gdeque.h:257
const_iterator end() const
Definition: gdeque.h:324
unsigned offset
Definition: gdeque.h:213
const_reference front() const
Definition: gdeque.h:363
friend class boost::iterator_core_access
Definition: gdeque.h:209
iterator emplace(iterator pos, Args &&...args)
Invalidates pointers.
Definition: gdeque.h:409
Block * prev
Definition: gdeque.h:48
Like std::deque but use Galois memory management functionality.
Definition: gdeque.h:43
T value_type
Definition: Executor_ParaMeter.h:111
iterator::value_type value_type
Definition: gdeque.h:258
const_reverse_iterator rend() const
Definition: gdeque.h:344
const_iterator begin() const
Definition: gdeque.h:310
A fixed size block allocator.
Definition: runtime/Mem.h:703
Iterator(Block *_b=0, Block *_l=0, unsigned _off=0)
Definition: gdeque.h:245
boost::reverse_iterator< iterator > reverse_iterator
Definition: gdeque.h:256
Helper functor, returns t.end()
Definition: TwoLevelIteratorA.h:391