11 template<class kind
> HeapRing
<kind
>::HeapRing()
13 head_i
= tail_i
= length
= 0;
18 template<class kind
> HeapRing
<kind
>::HeapRing(unsigned int length
)
22 ring
= new kind
[length
];
23 // TODO: handle allocation failure
24 this->length
= length
;
31 template<class kind
> HeapRing
<kind
>::~HeapRing()
33 head_i
= tail_i
= length
= 0;
41 * index accessors (protected)
44 template<class kind
> unsigned int HeapRing
<kind
>::next(unsigned int item
) const
55 template<class kind
> unsigned int HeapRing
<kind
>::prev(unsigned int item
) const
70 template<class kind
> kind
& HeapRing
<kind
>::head()
75 template<class kind
> kind
& HeapRing
<kind
>::tail()
80 template<class kind
> kind
& HeapRing
<kind
>::item(unsigned int i
)
85 template<class kind
> void HeapRing
<kind
>::push_front(kind
& item
)
88 head_i
= next(head_i
);
91 template<class kind
> kind
& HeapRing
<kind
>::pop_back()
93 kind
& r
= ring
[tail_i
];
94 tail_i
= next(tail_i
);
102 template<class kind
> kind
* HeapRing
<kind
>::head_ref()
104 return &ring
[head_i
];
107 template<class kind
> kind
* HeapRing
<kind
>::tail_ref()
109 return &ring
[tail_i
];
112 template<class kind
> kind
* HeapRing
<kind
>::item_ref(unsigned int i
)
117 template<class kind
> void HeapRing
<kind
>::produce_head()
120 head_i
= next(head_i
);
123 template<class kind
> void HeapRing
<kind
>::consume_tail()
126 tail_i
= next(tail_i
);
130 * queue status accessors
133 template<class kind
> bool HeapRing
<kind
>::is_full() const
136 bool r
= (next(head_i
) == tail_i
);
142 template<class kind
> bool HeapRing
<kind
>::is_empty() const
145 bool r
= (head_i
== tail_i
);
155 template<class kind
> bool HeapRing
<kind
>::resize(unsigned int length
)
163 if (is_empty()) // check again in case something was pushed
165 head_i
= tail_i
= this->length
= 0;
181 // Note: we don't use realloc so we can fall back to the existing ring if allocation fails
182 kind
* newring
= new kind
[length
];
186 kind
* oldring
= ring
;
190 if (is_empty()) // check again in case something was pushed while malloc did its thing
193 this->length
= length
;
213 template<class kind
> bool HeapRing
<kind
>::provide(kind
* buffer
, unsigned int length
)
219 kind
* oldring
= ring
;
221 if ((buffer
!= NULL
) && (length
> 0))
224 this->length
= length
;