Realm
A distributed, event-based tasking library
Loading...
Searching...
No Matches
circ_queue.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Stanford University, NVIDIA Corporation
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18// templated circular queue
19
20#ifndef REALM_CIRC_QUEUE_H
21#define REALM_CIRC_QUEUE_H
22
23#include <iterator>
24
25namespace Realm {
26
27 // a circular queue is similar to a deque, except that it tries to avoid
28 // new/delete during construction and in normal operation by reusing
29 // entries - allocation is only needed if the current capacity is exhausted
30
31 template <typename T, unsigned INTSIZE>
32 class CircularQueueIterator;
33
34 template <typename T, unsigned INTSIZE = 4>
36 public:
37 // default is to allocate just a few entries and then double whenever space runs out
38 CircularQueue(size_t init_capacity = 0, int _growth_factor = -2);
40
41 typedef T ITEMTYPE;
42 static const size_t ITEMSIZE = sizeof(T);
43
44 // using the standard STL contain methoder names and semantics
45 bool empty(void) const;
46 size_t size(void) const;
47 size_t capacity(void) const;
48
49 void reserve(size_t new_capacity);
50 void clear(void);
51
52 T &front(void);
53 const T &front(void) const;
54 void push_front(const T &val);
55 void pop_front(void);
56
57 T &back(void);
58 const T &back(void) const;
59 void push_back(const T &val);
60 void pop_back(void);
61
63
64 template <unsigned INTSIZE2>
66
69
72
73 const_iterator begin(void) const;
74 const_iterator end(void) const;
75
76 protected:
77 friend class CircularQueueIterator<T, INTSIZE>;
78
79 T *item_ptr(char *base, size_t idx) const;
80 const T *item_ptr(const char *base, size_t idx) const;
81
82 // put this first for alignment goodness
83 char internal_buffer[ITEMSIZE * INTSIZE];
85 size_t current_size; // number of elements currently in queue
86 size_t max_size; // size of underlying storage
87 size_t head; // index of first valid element (i.e. front)
88 size_t tail; // index of last valid element (i.e. back)
89 // (when empty, tail = head - 1 (mod capacity) )
90 int growth_factor; // how to grow when more space is needed
91 // if > 0, an additive increase on current capacity
92 // if < 0, a multiplicative increase (i.e. new_cap = cap *
93 // abs(growth) )
94 };
95
96 template <typename T, unsigned INTSIZE>
98 public:
99 // explicitly set iterator traits
100 typedef std::forward_iterator_tag iterator_category;
101 typedef T value_type;
102 typedef std::ptrdiff_t difference_type;
103 typedef T *pointer;
104 typedef T &reference;
105
106 protected:
107 friend class CircularQueue<T, INTSIZE>;
108
109 CircularQueueIterator(CircularQueue<T, INTSIZE> *_cq, size_t _pos, bool _at_end);
110
111 public:
114
117
118 bool operator==(const CircularQueueIterator<T, INTSIZE> &compare_to) const;
119 bool operator!=(const CircularQueueIterator<T, INTSIZE> &compare_to) const;
120
121 T operator*(void);
122 const T *operator->(void);
123
126
127 protected:
129 size_t pos;
130 bool at_end;
131 };
132
133}; // namespace Realm
134
135#include "realm/circ_queue.inl"
136
137#endif // ifndef REALM_CIRC_QUEUE_H
Definition circ_queue.h:97
T value_type
Definition circ_queue.h:101
CircularQueue< T, INTSIZE > * cq
Definition circ_queue.h:128
std::ptrdiff_t difference_type
Definition circ_queue.h:102
size_t pos
Definition circ_queue.h:129
CircularQueueIterator(CircularQueue< T, INTSIZE > *_cq, size_t _pos, bool _at_end)
bool operator==(const CircularQueueIterator< T, INTSIZE > &compare_to) const
T & reference
Definition circ_queue.h:104
CircularQueueIterator(const CircularQueueIterator< T, INTSIZE > &copy_from)
CircularQueueIterator< T, INTSIZE > & operator++()
CircularQueueIterator< T, INTSIZE > & operator=(const CircularQueueIterator< T, INTSIZE > &copy_from)
CircularQueueIterator< T, INTSIZE > operator++(int)
bool at_end
Definition circ_queue.h:130
std::forward_iterator_tag iterator_category
Definition circ_queue.h:100
bool operator!=(const CircularQueueIterator< T, INTSIZE > &compare_to) const
T * pointer
Definition circ_queue.h:103
Definition circ_queue.h:35
void reserve(size_t new_capacity)
size_t tail
Definition circ_queue.h:88
void swap(CircularQueue< T, INTSIZE > &swap_with)
CircularQueue(size_t init_capacity=0, int _growth_factor=-2)
T * item_ptr(char *base, size_t idx) const
CircularQueueIterator< T, INTSIZE > iterator
Definition circ_queue.h:67
void push_front(const T &val)
const_iterator begin(void) const
bool empty(void) const
const T * item_ptr(const char *base, size_t idx) const
CircularQueueIterator< T const, INTSIZE > const_iterator
Definition circ_queue.h:68
iterator end(void)
char internal_buffer[ITEMSIZE *INTSIZE]
Definition circ_queue.h:83
size_t max_size
Definition circ_queue.h:86
int growth_factor
Definition circ_queue.h:90
char * external_buffer
Definition circ_queue.h:84
size_t size(void) const
size_t head
Definition circ_queue.h:87
size_t capacity(void) const
void swap(CircularQueue< T, INTSIZE2 > &swap_with)
void push_back(const T &val)
size_t current_size
Definition circ_queue.h:85
iterator begin(void)
const T & front(void) const
T ITEMTYPE
Definition circ_queue.h:41
static const size_t ITEMSIZE
Definition circ_queue.h:42
const T & back(void) const
Definition activemsg.h:38