2020-01-31 19:52:37 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019-2020 Péter Magyar
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-12-03 03:39:53 +01:00
|
|
|
#include "voxelman_queue.h"
|
|
|
|
|
2019-12-03 14:28:32 +01:00
|
|
|
/// VMQUeue
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 03:39:53 +01:00
|
|
|
void VMQueue<T>::enqueue(T obj) {
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock->write_lock();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
if (_size == _count) {
|
|
|
|
_resize(_size + 10);
|
|
|
|
}
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_items[(_head + _count) % _size] = obj;
|
|
|
|
++_count;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock->write_unlock();
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 03:39:53 +01:00
|
|
|
T VMQueue<T>::dequeue() {
|
2020-01-09 04:29:05 +01:00
|
|
|
if (_size == 0)
|
|
|
|
return T();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
if (_count == 0)
|
|
|
|
return T();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
T obj;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock->write_lock();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
obj = _items[_head];
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_items[_head] = T();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
++_head;
|
|
|
|
--_count;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
if (_head >= _size)
|
|
|
|
_head -= _size;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock->write_unlock();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
return obj;
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 03:39:53 +01:00
|
|
|
T VMQueue<T>::peek() {
|
2020-01-09 04:29:05 +01:00
|
|
|
T obj;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock->read_lock();
|
|
|
|
obj = _items[_head];
|
|
|
|
_lock->read_unlock();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
return obj;
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 03:39:53 +01:00
|
|
|
void VMQueue<T>::resize(int new_size) {
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock->write_lock();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_resize(new_size);
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock->write_unlock();
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 03:39:53 +01:00
|
|
|
void VMQueue<T>::_resize(int new_size) {
|
2020-01-09 04:29:05 +01:00
|
|
|
if (new_size == _size)
|
|
|
|
return;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
if (new_size > _size) {
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
T *narr = memnew_arr(T, new_size);
|
|
|
|
//copymem(narr, _items, sizeof(T) * _size);
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
for (int i = 0; i < _size; ++i) {
|
|
|
|
narr[i] = _items[i];
|
|
|
|
}
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
memdelete_arr(_items);
|
|
|
|
_items = narr;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_size = new_size;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 03:39:53 +01:00
|
|
|
VMQueue<T>::VMQueue(int initial_size) {
|
2020-01-09 04:29:05 +01:00
|
|
|
_items = NULL;
|
|
|
|
_head = 0;
|
|
|
|
_count = 0;
|
|
|
|
_size = initial_size;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
if (unlikely(initial_size <= 0)) {
|
|
|
|
_size = 10;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
print_error("initial_size <= 0");
|
|
|
|
}
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock = RWLock::create();
|
|
|
|
|
|
|
|
_items = memnew_arr(T, _size);
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 03:39:53 +01:00
|
|
|
VMQueue<T>::VMQueue() {
|
2020-01-09 04:29:05 +01:00
|
|
|
_items = NULL;
|
|
|
|
_head = 0;
|
|
|
|
_count = 0;
|
|
|
|
_size = 10;
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_lock = RWLock::create();
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_items = memnew_arr(T, _size);
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 03:39:53 +01:00
|
|
|
VMQueue<T>::~VMQueue() {
|
2020-01-09 04:29:05 +01:00
|
|
|
if (_items)
|
|
|
|
memdelete_arr(_items);
|
2019-12-03 03:39:53 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
memdelete(_lock);
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:28:32 +01:00
|
|
|
///VoxelmanQueue
|
2019-12-03 03:39:53 +01:00
|
|
|
|
|
|
|
void VoxelmanQueue::enqueue(Variant obj) {
|
2020-01-09 04:29:05 +01:00
|
|
|
_queue.enqueue(obj);
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
Variant VoxelmanQueue::dequeue() {
|
2020-01-09 04:29:05 +01:00
|
|
|
return _queue.dequeue();
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
Variant VoxelmanQueue::peek() {
|
2020-01-09 04:29:05 +01:00
|
|
|
return _queue.peek();
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelmanQueue::resize(int new_size) {
|
2020-01-09 04:29:05 +01:00
|
|
|
_queue.resize(new_size);
|
2019-12-03 03:39:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|