GHS
Leader Election Based on GHS Minimum Spanning Tree
static_queue_impl.hpp
Go to the documentation of this file.
1 
40 using namespace le;
41 
42 template <typename T, unsigned int N>
44  idx_front(0), idx_back(0), count(0)
45 {
46 }
47 
48 template <typename T, unsigned int N>
50 {
51 }
52 
53 template <typename T, unsigned int N>
55  return size()==N;
56 }
57 
58 template <typename T, unsigned int N>
60  return size()==0;;
61 }
62 
63 template <typename T, unsigned int N>
64 unsigned int StaticQueue<T,N>::size() const{
65  return count;
66 }
67 
68 template <typename T, unsigned int N>
70  if (is_full())
71  {
72  return ERR_QUEUE_FULL;
73  }
74 
75  circle_buf[idx_back]=item;
76 
77  count ++;
78 
79  //ideally, idx_back=(idx_back+1)%N
80  //but we can't assume N = 2^n in general
81  idx_back ++;
82  if (idx_back >= N)
83  {
84  idx_back = 0;
85  }
86 
87  return OK;
88 }
89 
90 template <typename T, unsigned int N>
91 le::Errno StaticQueue<T,N>::front(T &out_item) const {
92  return at(0,out_item);
93 }
94 
95 template <typename T, unsigned int N>
97  if (is_empty())
98  {
99  return ERR_QUEUE_EMPTY;
100  }
101 
102  count --;
103 
104  //ideally, idx_back=(idx_back+1)%N
105  //but we can't assume N = 2^n in general
106  idx_front ++;
107  if (idx_front >= N)
108  {
109  idx_front = 0;
110  }
111  return OK;
112 }
113 
114 template <typename T, unsigned int N>
116  auto r = front(out_item);
117  if (OK!=r){
118  return r;
119  }
120  r = pop();
121  return r;
122 }
123 
124 
125 template <typename T, unsigned int N>
126 le::Errno StaticQueue<T,N>::at(const unsigned int idx, T &out_item) const
127 {
128 
129  if (size()==0){
130  return ERR_QUEUE_EMPTY;
131  }
132 
133  if (idx>=N){
134  return ERR_BAD_IDX;
135  }
136 
137  if (idx>=size()){
138  return ERR_NO_SUCH_ELEMENT;
139  }
140 
141  unsigned int actual_idx = idx+idx_front;
142  if (actual_idx > N){
143  actual_idx -= N;
144  }
145 
146  out_item = circle_buf[actual_idx];
147  return OK;
148 
149 }
150 
151 
152 template <typename T, unsigned int N>
154 {
155  idx_front=0;
156  idx_back =0;
157  count =0;
158  return OK;
159 }
le::Errno
Errno
Definition: errno.h:49
le
Definition: agent.h:43
le::ERR_NO_SUCH_ELEMENT
@ ERR_NO_SUCH_ELEMENT
Operation failed, there are less elements than the given index in the queue.
Definition: errno.h:85
seque::StaticQueue
a static-sized single-ended queue for use in GhsState
Definition: static_queue.h:60
le::ERR_QUEUE_FULL
@ ERR_QUEUE_FULL
Operation failed, the queue is full.
Definition: errno.h:82
le::OK
@ OK
The operation was successful.
Definition: errno.h:50
le::ERR_BAD_IDX
@ ERR_BAD_IDX
Operation failed and is not possible to succeed: that idx is beyond the static size of the queue.
Definition: errno.h:84
le::ERR_QUEUE_EMPTY
@ ERR_QUEUE_EMPTY
Operation failed, the queue is empty.
Definition: errno.h:83