An experimental unbounded queue implementation.

This commit is contained in:
Relintai 2019-12-03 14:28:32 +01:00
parent c386b6850b
commit 22bf841298
5 changed files with 169 additions and 2 deletions

1
SCsub
View File

@ -3,6 +3,7 @@ Import('env')
env.add_source_files(env.modules_sources,"register_types.cpp")
env.add_source_files(env.modules_sources,"containers/voxelman_queue.cpp")
env.add_source_files(env.modules_sources,"containers/voxelman_unbounded_queue.cpp")
env.add_source_files(env.modules_sources,"library/voxelman_library.cpp")
env.add_source_files(env.modules_sources,"library/voxelman_library_simple.cpp")

View File

@ -1,5 +1,7 @@
#include "voxelman_queue.h"
/// VMQUeue
template<typename T>
void VMQueue<T>::enqueue(T obj) {
_lock->write_lock();
@ -91,6 +93,12 @@ VMQueue<T>::VMQueue(int initial_size) {
_count = 0;
_size = initial_size;
if (unlikely(initial_size <= 0)) {
_size = 10;
print_error("initial_size <= 0");
}
_lock = RWLock::create();
_items = memnew_arr(T, _size);
@ -116,6 +124,7 @@ VMQueue<T>::~VMQueue() {
memdelete(_lock);
}
///VoxelmanQueue
void VoxelmanQueue::enqueue(Variant obj) {
_queue.enqueue(obj);
@ -142,4 +151,4 @@ void VoxelmanQueue::_bind_methods() {
ClassDB::bind_method(D_METHOD("dequeue"), &VoxelmanQueue::dequeue);
ClassDB::bind_method(D_METHOD("peek"), &VoxelmanQueue::peek);
ClassDB::bind_method(D_METHOD("resize", "new_size"), &VoxelmanQueue::resize);
}
}

View File

@ -0,0 +1,92 @@
#include "voxelman_unbounded_queue.h"
/// VMQUeue
template<typename T>
void VMUBQueue<T>::enqueue(T obj) {
_enqueue_lock->write_lock();
UBQueueNode<T> *n = memnew(UBQueueNode<T>(obj));
_tail->next = n;
_tail = n;
_enqueue_lock->write_unlock();
}
template<typename T>
T VMUBQueue<T>::dequeue() {
T result;
_dequeue_lock->write_lock();
if (_head->next == NULL) {
return result;
}
result = _head->next->data;
UBQueueNode<T> *h = _head;
_head = _head->next;
memdelete(h);
_dequeue_lock->write_unlock();
return result;
}
template<typename T>
T VMUBQueue<T>::peek() {
return _head->data;
}
template<typename T>
VMUBQueue<T>::VMUBQueue() {
_head = memnew(UBQueueNode<T>);
_tail = _head;
_enqueue_lock = RWLock::create();
_dequeue_lock = RWLock::create();
}
template<typename T>
VMUBQueue<T>::~VMUBQueue() {
memdelete(_enqueue_lock);
memdelete(_dequeue_lock);
UBQueueNode<T> *h = _head;
while (h) {
UBQueueNode<T> *n = h->next;
memdelete(h);
h = n;
}
}
///VoxelmanUnboundedQueue
void VoxelmanUnboundedQueue::enqueue(Variant obj) {
_queue.enqueue(obj);
}
Variant VoxelmanUnboundedQueue::dequeue() {
return _queue.dequeue();
}
Variant VoxelmanUnboundedQueue::peek() {
return _queue.peek();
}
VoxelmanUnboundedQueue::VoxelmanUnboundedQueue() {
}
VoxelmanUnboundedQueue::~VoxelmanUnboundedQueue() {
}
void VoxelmanUnboundedQueue::_bind_methods() {
ClassDB::bind_method(D_METHOD("enqueue", "obj"), &VoxelmanUnboundedQueue::enqueue);
ClassDB::bind_method(D_METHOD("dequeue"), &VoxelmanUnboundedQueue::dequeue);
ClassDB::bind_method(D_METHOD("peek"), &VoxelmanUnboundedQueue::peek);
}

View File

@ -0,0 +1,63 @@
#ifndef VOXELMAN_UNBOUNDED_QUEUE_H
#define VOXELMAN_UNBOUNDED_QUEUE_H
#include "core/reference.h"
#include "core/os/copymem.h"
#include "core/os/memory.h"
#include "core/os/rw_lock.h"
#include "core/variant.h"
template<class T>
struct UBQueueNode {
T data;
UBQueueNode *next;
UBQueueNode() {
next = NULL;
}
UBQueueNode(T value) {
data = value;
next = NULL;
}
};
template<class T>
class VMUBQueue {
public:
void enqueue(T obj);
T dequeue();
T peek();
VMUBQueue();
~VMUBQueue();
private:
RWLock *_enqueue_lock;
RWLock *_dequeue_lock;
UBQueueNode<T> *_head;
UBQueueNode<T> *_tail;
};
class VoxelmanUnboundedQueue : public Reference {
GDCLASS(VoxelmanUnboundedQueue, Reference);
public:
void enqueue(Variant obj);
Variant dequeue();
Variant peek();
VoxelmanUnboundedQueue();
~VoxelmanUnboundedQueue();
protected:
static void _bind_methods();
private:
VMUBQueue<Variant> _queue;
};
#endif

View File

@ -1,6 +1,7 @@
#include "register_types.h"
#include "containers/voxelman_queue.h"
#include "containers/voxelman_unbounded_queue.h"
#include "library/voxel_surface.h"
#include "library/voxel_surface_simple.h"
@ -41,7 +42,8 @@
void register_voxelman_types() {
ClassDB::register_class<VoxelmanQueue>();
ClassDB::register_class<VoxelmanQueue>();\
ClassDB::register_class<VoxelmanUnboundedQueue>();
ClassDB::register_class<VoxelMesher>();
ClassDB::register_class<VoxelMesherTransvoxel>();