voxelman/containers/voxelman_queue.cpp

155 lines
2.3 KiB
C++
Raw Normal View History

#include "voxelman_queue.h"
/// VMQUeue
2020-01-09 04:29:05 +01:00
template <typename T>
void VMQueue<T>::enqueue(T obj) {
2020-01-09 04:29:05 +01:00
_lock->write_lock();
2020-01-09 04:29:05 +01:00
if (_size == _count) {
_resize(_size + 10);
}
2020-01-09 04:29:05 +01:00
_items[(_head + _count) % _size] = obj;
++_count;
2020-01-09 04:29:05 +01:00
_lock->write_unlock();
}
2020-01-09 04:29:05 +01:00
template <typename T>
T VMQueue<T>::dequeue() {
2020-01-09 04:29:05 +01:00
if (_size == 0)
return T();
2020-01-09 04:29:05 +01:00
if (_count == 0)
return T();
2020-01-09 04:29:05 +01:00
T obj;
2020-01-09 04:29:05 +01:00
_lock->write_lock();
2020-01-09 04:29:05 +01:00
obj = _items[_head];
2020-01-09 04:29:05 +01:00
_items[_head] = T();
2020-01-09 04:29:05 +01:00
++_head;
--_count;
2020-01-09 04:29:05 +01:00
if (_head >= _size)
_head -= _size;
2020-01-09 04:29:05 +01:00
_lock->write_unlock();
2020-01-09 04:29:05 +01:00
return obj;
}
2020-01-09 04:29:05 +01:00
template <typename T>
T VMQueue<T>::peek() {
2020-01-09 04:29:05 +01:00
T obj;
2020-01-09 04:29:05 +01:00
_lock->read_lock();
obj = _items[_head];
_lock->read_unlock();
2020-01-09 04:29:05 +01:00
return obj;
}
2020-01-09 04:29:05 +01:00
template <typename T>
void VMQueue<T>::resize(int new_size) {
2020-01-09 04:29:05 +01:00
_lock->write_lock();
2020-01-09 04:29:05 +01:00
_resize(new_size);
2020-01-09 04:29:05 +01:00
_lock->write_unlock();
}
2020-01-09 04:29:05 +01:00
template <typename T>
void VMQueue<T>::_resize(int new_size) {
2020-01-09 04:29:05 +01:00
if (new_size == _size)
return;
2020-01-09 04:29:05 +01:00
if (new_size > _size) {
2020-01-09 04:29:05 +01:00
T *narr = memnew_arr(T, new_size);
//copymem(narr, _items, sizeof(T) * _size);
2020-01-09 04:29:05 +01:00
for (int i = 0; i < _size; ++i) {
narr[i] = _items[i];
}
2020-01-09 04:29:05 +01:00
memdelete_arr(_items);
_items = narr;
2020-01-09 04:29:05 +01:00
_size = new_size;
2020-01-09 04:29:05 +01:00
return;
}
}
2020-01-09 04:29:05 +01:00
template <typename T>
VMQueue<T>::VMQueue(int initial_size) {
2020-01-09 04:29:05 +01:00
_items = NULL;
_head = 0;
_count = 0;
_size = initial_size;
2020-01-09 04:29:05 +01:00
if (unlikely(initial_size <= 0)) {
_size = 10;
2020-01-09 04:29:05 +01:00
print_error("initial_size <= 0");
}
2020-01-09 04:29:05 +01:00
_lock = RWLock::create();
_items = memnew_arr(T, _size);
}
2020-01-09 04:29:05 +01:00
template <typename T>
VMQueue<T>::VMQueue() {
2020-01-09 04:29:05 +01:00
_items = NULL;
_head = 0;
_count = 0;
_size = 10;
2020-01-09 04:29:05 +01:00
_lock = RWLock::create();
2020-01-09 04:29:05 +01:00
_items = memnew_arr(T, _size);
}
2020-01-09 04:29:05 +01:00
template <typename T>
VMQueue<T>::~VMQueue() {
2020-01-09 04:29:05 +01:00
if (_items)
memdelete_arr(_items);
2020-01-09 04:29:05 +01:00
memdelete(_lock);
}
///VoxelmanQueue
void VoxelmanQueue::enqueue(Variant obj) {
2020-01-09 04:29:05 +01:00
_queue.enqueue(obj);
}
Variant VoxelmanQueue::dequeue() {
2020-01-09 04:29:05 +01:00
return _queue.dequeue();
}
Variant VoxelmanQueue::peek() {
2020-01-09 04:29:05 +01:00
return _queue.peek();
}
void VoxelmanQueue::resize(int new_size) {
2020-01-09 04:29:05 +01:00
_queue.resize(new_size);
}
VoxelmanQueue::VoxelmanQueue() {
}
VoxelmanQueue::~VoxelmanQueue() {
}
void VoxelmanQueue::_bind_methods() {
2020-01-09 04:29:05 +01:00
ClassDB::bind_method(D_METHOD("enqueue", "obj"), &VoxelmanQueue::enqueue);
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);
}