A virtual currency betting bot for Twitch chat. https://ddark.net/better
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

154 lines
3.0 KiB

#ifndef BETTER_QUEUE_H
#define BETTER_QUEUE_H
#include <stb_ds.h>
template<typename T, void(*destructor)(T*)>
struct RingBuffer
{
T* data; // dynamic array
size_t first;
size_t count;
void init(size_t size)
{
data = NULL;
first = 0;
count = 0;
arrsetlen(data, size);
}
void destroy()
{
clear();
arrfree(data);
}
inline size_t index(size_t i)
{
return (first + i) % arrlen(data);
}
inline T get(size_t i)
{
assert(i < count);
return data[index(i)];
}
inline T* ref(size_t i)
{
assert(i < count);
return &data[index(i)];
}
void clear()
{
if (destructor != NULL)
for (int i = 0; i < count; ++i)
destructor(&data[index(i)]);
first = 0;
count = 0;
}
bool contains(T item)
{
for (int i = 0; i < count; ++i)
if (item == data[index(i)])
return true;
return false;
}
T* push_back_discarding(T item)
{
T* pos = &data[index(count)];
if (count == arrlen(data))
{
if (destructor != NULL)
destructor(pos);
first = index(1);
}
else ++count;
*pos = item;
return pos;
}
T* push_back_growing(T item)
{
if (count == arrlen(data))
{
arrinsn(data, index(count), 1);
first = index(1);
}
++count;
T* pos = &data[index(count-1)];
*pos = item;
return pos;
}
T pop_front()
{
assert(count > 0);
T item = data[first];
first = index(1);
--count;
return item;
}
T pop_back()
{
assert(count > 0);
T item = data[index(count-1)];
--count;
return item;
}
};
/*
{
RingBuffer<int, NULL> rb;
rb.init(8);
for (int i = 0; i < 15; ++i)
{
rb.push_back_discarding(i);
printf("pbd %i -- arr:", i);
for (int i = 0; i < rb.count; ++i)
{
printf(" %i", rb.get(i));
}
printf("\n");
}
for (int i = 0; i < 4; ++i)
{
printf("pop_front %i -- arr:", rb.pop_front());
for (int i = 0; i < rb.count; ++i)
{
printf(" %i", rb.get(i));
}
printf("\n");
}
for (int i = 0; i < 16; ++i)
{
rb.push_back_growing(i);
printf("pbg %i -- arr:", i);
for (int i = 0; i < rb.count; ++i)
{
printf(" %i", rb.get(i));
}
printf("\n");
}
for (int i = 0; i < 4; ++i)
{
printf("pop_back %i -- arr:", rb.pop_back());
for (int i = 0; i < rb.count; ++i)
{
printf(" %i", rb.get(i));
}
printf("\n");
}
}
*/
#endif // BETTER_QUEUE_H