Fixed the emplace function implementations for stack and queue (#2657)

* Fixed the emplace function implementations for stack and queue

Cleaned up several functions in the stack, queue and priority_queue
classes that were non-standard or weren't needed.

Updated the "style" of the code to use more modern concepts: "typedef" ->
"using", empty constructor body -> default keyword.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Replaced the custom implementations of AZStd stack, (proirity)queue

Theses classes now have a template alias to the standard library version
of the classes

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
This commit is contained in:
lumberyard-employee-dm
2021-07-30 18:20:21 -05:00
committed by GitHub
parent 0318419932
commit bb372f05cd
11 changed files with 55 additions and 317 deletions
+3 -3
View File
@@ -268,7 +268,7 @@ namespace AZ
m_messages.pop();
if (numMessages == 1)
{
m_messages.get_container().clear(); // If it was the last message, free all memory.
m_messages = {};
}
}
//////////////////////////////////////////////////////////////////////////
@@ -280,7 +280,7 @@ namespace AZ
void Clear()
{
AZStd::lock_guard<MutexType> lock(m_messagesMutex);
m_messages.get_container().clear();
m_messages = {};
}
void SetActive(bool isActive)
@@ -289,7 +289,7 @@ namespace AZ
m_isActive = isActive;
if (!m_isActive)
{
m_messages.get_container().clear();
m_messages = {};
}
};
@@ -42,8 +42,6 @@ namespace AZStd
class unordered_multiset;
template<AZStd::size_t NumBits>
class bitset;
template<class T, class Container/* = AZStd::deque<T>*/ >
class stack;
template<class T>
class intrusive_ptr;
@@ -5,206 +5,17 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#ifndef AZSTD_QUEUE_H
#define AZSTD_QUEUE_H 1
#pragma once
#include <AzCore/std/containers/deque.h>
#include <AzCore/std/containers/vector.h>
#include <AzCore/std/functional_basic.h>
#include <queue>
namespace AZStd
{
/**
* FIFO queue complaint with \ref CStd (23.2.3.1)
* The only extension we have is that we allow access
* to the underlying container via: get_container function.
* Check the queue \ref AZStdExamples.
*/
template<class T, class Container = AZStd::deque<T> >
class queue
{
enum
{
CONTAINER_VERSION = 1
};
public:
typedef queue<T, Container> this_type;
typedef Container container_type;
typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type;
typedef typename Container::reference reference;
typedef typename Container::const_reference const_reference;
AZ_FORCE_INLINE queue() {}
AZ_FORCE_INLINE explicit queue(const container_type& container)
: m_container(container) {}
AZ_FORCE_INLINE bool empty() const { return m_container.empty(); }
AZ_FORCE_INLINE size_type size() const { return m_container.size(); }
AZ_FORCE_INLINE reference front() { return m_container.front(); }
AZ_FORCE_INLINE const_reference front() const { return m_container.front(); }
AZ_FORCE_INLINE reference back() { return m_container.back(); }
AZ_FORCE_INLINE const_reference back() const { return m_container.back(); }
AZ_FORCE_INLINE void push(const value_type& value) { m_container.push_back(value); }
AZ_FORCE_INLINE void pop() { m_container.pop_front(); }
AZ_FORCE_INLINE void push() { m_container.push_back(); }
AZ_FORCE_INLINE queue(this_type&& rhs)
: m_container(AZStd::move(rhs.m_container)) {}
AZ_FORCE_INLINE explicit queue(Container&& container)
: m_container(AZStd::move(container)) {}
this_type& operator=(this_type&& rhs)
{
m_container = AZStd::move(rhs.m_container);
return (*this);
}
void push(value_type&& value) { m_container.push_back(AZStd::move(value)); }
template<class... Args>
void emplace(Args&&... args) { m_container.emplace_back(AZStd::forward<Args>(args)...); }
void swap(this_type& rhs) { AZStd::swap(m_container, rhs.m_container); }
AZ_FORCE_INLINE Container& get_container() { return m_container; }
AZ_FORCE_INLINE const Container& get_container() const { return m_container; }
protected:
Container m_container;
};
// queue TEMPLATE FUNCTIONS
template<class T, class Container>
AZ_FORCE_INLINE bool operator==(const AZStd::queue<T, Container>& left, const AZStd::queue<T, Container>& right)
{
return left.get_container() == right.get_container();
}
template<class T, class Container>
AZ_FORCE_INLINE bool operator!=(const AZStd::queue<T, Container>& left, const AZStd::queue<T, Container>& right)
{
return left.get_container() != right.get_container();
}
/* template<class T, class Container>
AZ_FORCE_INLINE bool operator<(const queue<T,Container>& left, const queue<T,Container>& right)
{
return left.get_container() < right.get_container();
}
template<class T, class Container>
AZ_FORCE_INLINE bool operator>(const queue<T,Container>& left, const queue<T,Container>& right)
{
return left.get_container() > right.get_container();
}
template<class T, class Container>
AZ_FORCE_INLINE operator<=(const queue<T,Container>& left, const queue<T,Container>& right)
{
return left.get_container() <= right.get_container();
}
template<class T, class Container>
AZ_FORCE_INLINE bool operator>=(const queue<T,Container>& left, const queue<T,Container>& right)
{
return left.get_container() >= right.get_container();
}*/
/**
* Priority queue is complaint with \ref CStd (23.2.3.2)
* The only extension we have is that we allow access
* to the underlying container via: get_container function.
* Check the priority_queue \ref AZStdExamples.
*/
template<class T, class Container = AZStd::vector<T>, class Predicate = AZStd::less<typename Container::value_type> >
class priority_queue
{
enum
{
CONTAINER_VERSION = 1
};
public:
typedef priority_queue<T, Container, Predicate> this_type;
typedef Container container_type;
typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type;
typedef typename Container::reference reference;
typedef typename Container::const_reference const_reference;
AZ_FORCE_INLINE priority_queue() {}
AZ_FORCE_INLINE explicit priority_queue(const Predicate& comp)
: m_comp(comp) {}
AZ_FORCE_INLINE priority_queue(const Predicate& comp, const container_type& container)
: m_container(container)
, m_comp(comp)
{
// construct by copying specified container, comparator
AZStd::make_heap(m_container.begin(), m_container.end(), comp);
}
template<class InputIterator>
AZ_FORCE_INLINE priority_queue(InputIterator first, InputIterator last)
: m_container(first, last)
, m_comp()
{
AZStd::make_heap(m_container.begin(), m_container.end(), m_comp);
}
template<class InputIterator>
AZ_FORCE_INLINE priority_queue(InputIterator first, InputIterator last, const Predicate& comp)
: m_container(first, last)
, m_comp(comp)
{ // construct by copying [_First, _Last), specified comparator
AZStd::make_heap(m_container.begin(), m_container.end(), m_comp);
}
template<class InputIterator>
AZ_FORCE_INLINE priority_queue(InputIterator first, InputIterator last, const Predicate& comp, const container_type& container)
: m_container(container)
, m_comp(comp)
{ // construct by copying [_First, _Last), container, and comparator
m_container.insert(m_container.end(), first, last);
AZStd::make_heap(m_container.begin(), m_container.end(), m_comp);
}
AZ_FORCE_INLINE bool empty() const { return m_container.empty(); }
AZ_FORCE_INLINE size_type size() const { return m_container.size(); }
AZ_FORCE_INLINE const_reference top() const { return m_container.front(); }
AZ_FORCE_INLINE reference top() { return m_container.front(); }
AZ_FORCE_INLINE void push(const value_type& value)
{
m_container.push_back(value);
AZStd::push_heap(m_container.begin(), m_container.end(), m_comp);
}
AZ_FORCE_INLINE void pop()
{
AZStd::pop_heap(m_container.begin(), m_container.end(), m_comp);
m_container.pop_back();
}
AZ_FORCE_INLINE priority_queue(this_type&& rhs)
: m_container(AZStd::move(rhs.m_container))
, m_comp(AZStd::move(rhs.m_comp)) {}
AZ_FORCE_INLINE explicit priority_queue(const Predicate& pred, Container&& container)
: m_container(AZStd::move(container))
, m_comp(pred) {}
this_type& operator=(this_type&& rhs)
{
m_container = AZStd::move(rhs.m_container);
m_comp = AZStd::move(rhs.m_comp);
return (*this);
}
void push(value_type&& value) { m_container.push_back(AZStd::move(value)); AZStd::push_heap(m_container.begin(), m_container.end(), m_comp); }
template<class Args>
void emplace(Args&& args) { m_container.emplace_back(AZStd::forward<Args>(args)); AZStd::push_heap(m_container.begin(), m_container.end(), m_comp); }
void swap(this_type& rhs) { AZStd::swap(m_container, rhs.m_container); AZStd::swap(m_comp, rhs.m_comp); }
AZ_FORCE_INLINE Container& get_container() { return m_container; }
AZ_FORCE_INLINE const Container& get_container() const { return m_container; }
protected:
Container m_container;
Predicate m_comp;
};
template<class T, class Container = AZStd::deque<T>>
using queue = std::queue<T, Container>;
template<class T, class Container = AZStd::vector<T>, class Compare = AZStd::less<typename Container::value_type>>
using priority_queue = std::priority_queue<T, Container, Compare>;
}
#endif // AZSTD_QUEUE_H
#pragma once
@@ -5,103 +5,13 @@
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#ifndef AZSTD_STACK_H
#define AZSTD_STACK_H 1
#pragma once
#include <AzCore/std/containers/deque.h>
#include <stack>
namespace AZStd
{
/**
* Stack container is complaint with \ref CStd (23.2.3.3)
* The only extension we have is that we allow access
* to the underlying container via: get_container function.
* Check the stack \ref AZStdExamples.
*/
template<class T, class Container = AZStd::deque<T> >
class stack
{
enum
{
CONTAINER_VERSION = 1
};
public:
typedef stack<T, Container> this_type;
typedef Container container_type;
typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type;
typedef typename Container::reference reference;
typedef typename Container::const_reference const_reference;
AZ_FORCE_INLINE stack() {}
AZ_FORCE_INLINE explicit stack(const container_type& container)
: m_container(container) {}
AZ_FORCE_INLINE bool empty() const { return m_container.empty(); }
AZ_FORCE_INLINE size_type size() const { return m_container.size(); }
AZ_FORCE_INLINE reference top() { return m_container.back(); }
AZ_FORCE_INLINE const_reference top() const { return m_container.back(); }
AZ_FORCE_INLINE reference back() { return m_container.back(); }
AZ_FORCE_INLINE const_reference back() const { return m_container.back(); }
AZ_FORCE_INLINE void push(const value_type& value) { m_container.push_back(value); }
AZ_FORCE_INLINE void pop() { m_container.pop_back(); }
AZ_FORCE_INLINE void push() { m_container.push_back(); }
AZ_FORCE_INLINE stack(this_type&& rhs)
: m_container(AZStd::move(rhs.m_container)) {}
AZ_FORCE_INLINE explicit stack(Container&& container)
: m_container(AZStd::move(container)) {}
this_type& operator=(this_type&& rhs) { m_container = AZStd::move(rhs.m_container); return *this; }
void push(value_type&& value) { m_container.push_back(AZStd::move(value)); }
template<class Args>
void emplace(Args&& args) { m_container.emplace_back(AZStd::forward<Args>(args)); }
void swap(this_type&& rhs) { m_container.swap(AZStd::move(rhs.m_container)); }
void swap(this_type& rhs) { AZStd::swap(m_container, rhs.m_container); }
AZ_FORCE_INLINE Container& get_container() { return m_container; }
AZ_FORCE_INLINE const Container& get_container() const { return m_container; }
protected:
Container m_container;
};
// queue TEMPLATE FUNCTIONS
template<class T, class Container>
AZ_FORCE_INLINE bool operator==(const AZStd::stack<T, Container>& left, const AZStd::stack<T, Container>& right)
{
return left.get_container() == right.get_container();
}
template<class T, class Container>
AZ_FORCE_INLINE bool operator!=(const AZStd::stack<T, Container>& left, const AZStd::stack<T, Container>& right)
{
return left.get_container() != right.get_container();
}
/* template<class T, class Container>
AZ_FORCE_INLINE bool operator<(const queue<T,Container>& left, const queue<T,Container>& right)
{
return left.get_container() < right.get_container();
}
template<class T, class Container>
AZ_FORCE_INLINE bool operator>(const queue<T,Container>& left, const queue<T,Container>& right)
{
return left.get_container() > right.get_container();
}
template<class T, class Container>
AZ_FORCE_INLINE operator<=(const queue<T,Container>& left, const queue<T,Container>& right)
{
return left.get_container() <= right.get_container();
}
template<class T, class Container>
AZ_FORCE_INLINE bool operator>=(const queue<T,Container>& left, const queue<T,Container>& right)
{
return left.get_container() >= right.get_container();
}*/
template<class T, class Container = AZStd::deque<T>>
using stack = std::stack<T, Container>;
}
#endif // AZSTD_STACK_H
#pragma once
@@ -298,7 +298,7 @@ namespace UnitTest
AZ_TEST_ASSERT(int_queue.empty());
AZ_TEST_ASSERT(int_queue.size() == 0);
// Queue uses deque as default container, so try to contruct to queue from a deque.
// Queue uses deque as default container, so try to construct to queue from a deque.
deque<int> container(40, 10);
int_queue_type int_queue2(container);
AZ_TEST_ASSERT(!int_queue2.empty());
@@ -324,7 +324,7 @@ namespace UnitTest
AZ_TEST_ASSERT(int_queue2.size() == 40);
AZ_TEST_ASSERT(int_queue2.back() == 20);
int_queue.push();
int_queue.emplace();
AZ_TEST_ASSERT(!int_queue.empty());
AZ_TEST_ASSERT(int_queue.size() == 1);
@@ -423,7 +423,7 @@ namespace UnitTest
AZ_TEST_ASSERT(int_stack2.size() == 40);
AZ_TEST_ASSERT(int_stack2.top() == 10);
int_stack.push();
int_stack.emplace();
AZ_TEST_ASSERT(!int_stack.empty());
AZ_TEST_ASSERT(int_stack.size() == 1);
// StackContainerTest-End
@@ -669,4 +669,19 @@ namespace UnitTest
++iteration;
}
}
using StackContainerTestFixture = ScopedAllocatorSetupFixture;
TEST_F(StackContainerTestFixture, StackEmplaceOperator_SupportsZeroOrMoreArguments)
{
using TestPairType = AZStd::pair<int, int>;
AZStd::stack<TestPairType> testStack;
testStack.emplace();
testStack.emplace(1);
testStack.emplace(2, 3);
using ContainerType = typename AZStd::stack<TestPairType>::container_type;
AZStd::stack<TestPairType> expectedStack(ContainerType{ TestPairType{ 0, 0 }, TestPairType{ 1, 0 }, TestPairType{ 2, 3 } });
EXPECT_EQ(expectedStack, testStack);
}
}