Last active
January 3, 2016 07:09
-
-
Save triffid/8427555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "HeapRing.h" | |
#include <cstdlib> | |
inline static int next(const int item, const int length) __attribute__ ((pure)); | |
inline static int next(const int item, const int length) | |
{ | |
int i = item + 1; | |
if (i >= length) | |
i = 0; | |
return i; | |
} | |
template<class kind> HeapRing<kind>::HeapRing(int length) | |
{ | |
head_i = tail_i = 0; | |
first = static_cast<kind*>(malloc(sizeof(kind) * length)); | |
this->length = length; | |
} | |
template<class kind> HeapRing<kind>::~HeapRing() | |
{ | |
head_i = tail_i = length = 0; | |
free(first); | |
first = NULL; | |
} | |
template<class kind> kind* HeapRing<kind>::head() | |
{ | |
return first[head_i]; | |
} | |
template<class kind> kind* HeapRing<kind>::tail() | |
{ | |
return first[tail_i]; | |
} | |
template<class kind> void HeapRing<kind>::produce_head() | |
{ | |
head_i = next(head_i, length); | |
} | |
template<class kind> void HeapRing<kind>::consume_tail() | |
{ | |
tail_i = next(tail_i, length); | |
} | |
template<class kind> bool HeapRing<kind>::is_full() | |
{ | |
return (next(head_i, length) == tail_i); | |
} | |
template<class kind> bool HeapRing<kind>::is_empty() | |
{ | |
return (head_i == tail_i); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _HEAPRING_H | |
#define _HEAPRING_H | |
template<class kind> class HeapRing { | |
public: | |
HeapRing(int length); | |
~HeapRing(); | |
kind* head(); | |
kind* tail(); | |
void produce_head(void); | |
void consume_tail(void); | |
bool is_empty(void); | |
bool is_full(void); | |
protected: | |
volatile int head_i; | |
volatile int tail_i; | |
int length; | |
kind* first; | |
}; | |
#endif /* _HEAPRING_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment