11 template<class kind
> HeapRing
<kind
>::HeapRing()
13 head_i
= tail_i
= length
= 0;
17 template<class kind
> HeapRing
<kind
>::HeapRing(unsigned int length
)
20 ring
= new kind
[length
];
21 // TODO: handle allocation failure
22 this->length
= length
;
29 template<class kind
> HeapRing
<kind
>::~HeapRing()
31 head_i
= tail_i
= length
= 0;
38 * index accessors (protected)
41 template<class kind
> unsigned int HeapRing
<kind
>::next(unsigned int item
)
52 template<class kind
> unsigned int HeapRing
<kind
>::prev(unsigned int item
)
67 template<class kind
> kind
& HeapRing
<kind
>::head()
72 template<class kind
> kind
& HeapRing
<kind
>::tail()
77 template<class kind
> kind
& HeapRing
<kind
>::item(unsigned int i
)
82 template<class kind
> void HeapRing
<kind
>::push_front(kind
& item
)
85 head_i
= next(head_i
);
88 template<class kind
> kind
& HeapRing
<kind
>::pop_back()
90 kind
& r
= ring
[tail_i
];
91 tail_i
= next(tail_i
);
99 template<class kind
> kind
* HeapRing
<kind
>::head_ref()
101 return &ring
[head_i
];
104 template<class kind
> kind
* HeapRing
<kind
>::tail_ref()
106 return &ring
[tail_i
];
109 template<class kind
> kind
* HeapRing
<kind
>::item_ref(unsigned int i
)
114 template<class kind
> void HeapRing
<kind
>::produce_head()
116 head_i
= next(head_i
);
119 template<class kind
> void HeapRing
<kind
>::consume_tail()
121 tail_i
= next(tail_i
);
125 * queue status accessors
128 template<class kind
> bool HeapRing
<kind
>::is_full()
130 return (next(head_i
) == tail_i
);
133 template<class kind
> bool HeapRing
<kind
>::is_empty()
135 return (head_i
== tail_i
);
142 template<class kind
> bool HeapRing
<kind
>::resize(unsigned int length
)
150 if (is_empty()) // check again in case something was pushed
152 head_i
= tail_i
= this->length
= 0;
167 // Note: we don't use realloc so we can fall back to the existing ring if allocation fails
168 kind
* newring
= new kind
[length
];
172 kind
* oldring
= ring
;
176 if (is_empty()) // check again in case something was pushed while malloc did its thing
179 this->length
= length
;
199 template<class kind
> bool HeapRing
<kind
>::provide(kind
* buffer
, unsigned int length
)
205 kind
* oldring
= ring
;
207 if ((buffer
!= NULL
) && (length
> 0))
210 this->length
= length
;