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 14:28:32 +01:00
|
|
|
#include "voxelman_unbounded_queue.h"
|
|
|
|
|
|
|
|
/// VMQUeue
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 14:28:32 +01:00
|
|
|
void VMUBQueue<T>::enqueue(T obj) {
|
2020-01-09 04:29:05 +01:00
|
|
|
_enqueue_lock->write_lock();
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
UBQueueNode<T> *n = memnew(UBQueueNode<T>(obj));
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_tail->next = n;
|
|
|
|
_tail = n;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_enqueue_lock->write_unlock();
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 14:28:32 +01:00
|
|
|
T VMUBQueue<T>::dequeue() {
|
2020-01-09 04:29:05 +01:00
|
|
|
T result;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_dequeue_lock->write_lock();
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
if (_head->next == NULL) {
|
|
|
|
return result;
|
|
|
|
}
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
result = _head->next->data;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
UBQueueNode<T> *h = _head;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_head = _head->next;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
memdelete(h);
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_dequeue_lock->write_unlock();
|
|
|
|
|
|
|
|
return result;
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 14:28:32 +01:00
|
|
|
T VMUBQueue<T>::peek() {
|
2020-01-09 04:29:05 +01:00
|
|
|
return _head->data;
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 14:28:32 +01:00
|
|
|
VMUBQueue<T>::VMUBQueue() {
|
2020-01-09 04:29:05 +01:00
|
|
|
_head = memnew(UBQueueNode<T>);
|
|
|
|
_tail = _head;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
_enqueue_lock = RWLock::create();
|
|
|
|
_dequeue_lock = RWLock::create();
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
template <typename T>
|
2019-12-03 14:28:32 +01:00
|
|
|
VMUBQueue<T>::~VMUBQueue() {
|
2020-01-09 04:29:05 +01:00
|
|
|
memdelete(_enqueue_lock);
|
|
|
|
memdelete(_dequeue_lock);
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
UBQueueNode<T> *h = _head;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
while (h) {
|
|
|
|
UBQueueNode<T> *n = h->next;
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
memdelete(h);
|
2019-12-03 14:28:32 +01:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
h = n;
|
|
|
|
}
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///VoxelmanUnboundedQueue
|
|
|
|
|
|
|
|
void VoxelmanUnboundedQueue::enqueue(Variant obj) {
|
2020-01-09 04:29:05 +01:00
|
|
|
_queue.enqueue(obj);
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|
|
|
|
Variant VoxelmanUnboundedQueue::dequeue() {
|
2020-01-09 04:29:05 +01:00
|
|
|
return _queue.dequeue();
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|
|
|
|
Variant VoxelmanUnboundedQueue::peek() {
|
2020-01-09 04:29:05 +01:00
|
|
|
return _queue.peek();
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelmanUnboundedQueue::VoxelmanUnboundedQueue() {
|
|
|
|
}
|
|
|
|
|
|
|
|
VoxelmanUnboundedQueue::~VoxelmanUnboundedQueue() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelmanUnboundedQueue::_bind_methods() {
|
2020-01-09 04:29:05 +01:00
|
|
|
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);
|
2019-12-03 14:28:32 +01:00
|
|
|
}
|