mirror of
https://github.com/holub/mame
synced 2025-07-04 17:38:08 +03:00
Merge pull request #515 from SuperV1234/master
Modernize `simple_list` code [Vittorio Romeo]
This commit is contained in:
commit
ebdc4a9414
@ -62,25 +62,25 @@ inline std::unique_ptr<T> make_unique_clear()
|
|||||||
// a simple_list is a singly-linked list whose 'next' pointer is owned
|
// a simple_list is a singly-linked list whose 'next' pointer is owned
|
||||||
// by the object
|
// by the object
|
||||||
template<class _ElementType>
|
template<class _ElementType>
|
||||||
class simple_list
|
class simple_list final
|
||||||
{
|
{
|
||||||
// we don't support deep copying
|
|
||||||
simple_list(const simple_list &);
|
|
||||||
simple_list &operator=(const simple_list &);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
// we don't support deep copying
|
||||||
|
simple_list(const simple_list &) = delete;
|
||||||
|
simple_list &operator=(const simple_list &) = delete;
|
||||||
|
|
||||||
// construction/destruction
|
// construction/destruction
|
||||||
simple_list()
|
simple_list() noexcept
|
||||||
: m_head(nullptr),
|
: m_head(nullptr),
|
||||||
m_tail(nullptr),
|
m_tail(nullptr),
|
||||||
m_count(0) { }
|
m_count(0) { }
|
||||||
|
|
||||||
virtual ~simple_list() { reset(); }
|
~simple_list() noexcept { reset(); }
|
||||||
|
|
||||||
// simple getters
|
// simple getters
|
||||||
_ElementType *first() const { return m_head; }
|
_ElementType *first() const noexcept { return m_head; }
|
||||||
_ElementType *last() const { return m_tail; }
|
_ElementType *last() const noexcept { return m_tail; }
|
||||||
int count() const { return m_count; }
|
int count() const noexcept { return m_count; }
|
||||||
|
|
||||||
// remove (free) all objects in the list, leaving an empty list
|
// remove (free) all objects in the list, leaving an empty list
|
||||||
void reset()
|
void reset()
|
||||||
@ -101,7 +101,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add the given list to the head of the list
|
// add the given list to the head of the list
|
||||||
void prepend_list(simple_list<_ElementType> &list)
|
void prepend_list(simple_list<_ElementType> &list) noexcept
|
||||||
{
|
{
|
||||||
int count = list.count();
|
int count = list.count();
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
@ -116,7 +116,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add the given object to the tail of the list
|
// add the given object to the tail of the list
|
||||||
_ElementType &append(_ElementType &object)
|
_ElementType &append(_ElementType &object) noexcept
|
||||||
{
|
{
|
||||||
object.m_next = nullptr;
|
object.m_next = nullptr;
|
||||||
if (m_tail != nullptr)
|
if (m_tail != nullptr)
|
||||||
@ -128,7 +128,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add the given list to the tail of the list
|
// add the given list to the tail of the list
|
||||||
void append_list(simple_list<_ElementType> &list)
|
void append_list(simple_list<_ElementType> &list) noexcept
|
||||||
{
|
{
|
||||||
int count = list.count();
|
int count = list.count();
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
@ -144,7 +144,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// insert the given object after a particular object (NULL means prepend)
|
// insert the given object after a particular object (NULL means prepend)
|
||||||
_ElementType &insert_after(_ElementType &object, _ElementType *insert_after)
|
_ElementType &insert_after(_ElementType &object, _ElementType *insert_after) noexcept
|
||||||
{
|
{
|
||||||
if (insert_after == nullptr)
|
if (insert_after == nullptr)
|
||||||
return prepend(object);
|
return prepend(object);
|
||||||
@ -157,7 +157,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// insert the given object before a particular object (NULL means append)
|
// insert the given object before a particular object (NULL means append)
|
||||||
_ElementType &insert_before(_ElementType &object, _ElementType *insert_before)
|
_ElementType &insert_before(_ElementType &object, _ElementType *insert_before) noexcept
|
||||||
{
|
{
|
||||||
if (insert_before == nullptr)
|
if (insert_before == nullptr)
|
||||||
return append(object);
|
return append(object);
|
||||||
@ -175,7 +175,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// replace an item in the list at the same location, and remove it
|
// replace an item in the list at the same location, and remove it
|
||||||
_ElementType &replace_and_remove(_ElementType &object, _ElementType &toreplace)
|
_ElementType &replace_and_remove(_ElementType &object, _ElementType &toreplace) noexcept
|
||||||
{
|
{
|
||||||
_ElementType *prev = nullptr;
|
_ElementType *prev = nullptr;
|
||||||
for (_ElementType *cur = m_head; cur != nullptr; prev = cur, cur = cur->m_next)
|
for (_ElementType *cur = m_head; cur != nullptr; prev = cur, cur = cur->m_next)
|
||||||
@ -195,7 +195,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// detach the head item from the list, but don't free its memory
|
// detach the head item from the list, but don't free its memory
|
||||||
_ElementType *detach_head()
|
_ElementType *detach_head() noexcept
|
||||||
{
|
{
|
||||||
_ElementType *result = m_head;
|
_ElementType *result = m_head;
|
||||||
if (result != nullptr)
|
if (result != nullptr)
|
||||||
@ -209,7 +209,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// detach the given item from the list, but don't free its memory
|
// detach the given item from the list, but don't free its memory
|
||||||
_ElementType &detach(_ElementType &object)
|
_ElementType &detach(_ElementType &object) noexcept
|
||||||
{
|
{
|
||||||
_ElementType *prev = nullptr;
|
_ElementType *prev = nullptr;
|
||||||
for (_ElementType *cur = m_head; cur != nullptr; prev = cur, cur = cur->m_next)
|
for (_ElementType *cur = m_head; cur != nullptr; prev = cur, cur = cur->m_next)
|
||||||
@ -228,7 +228,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deatch the entire list, returning the head, but don't free memory
|
// deatch the entire list, returning the head, but don't free memory
|
||||||
_ElementType *detach_all()
|
_ElementType *detach_all() noexcept
|
||||||
{
|
{
|
||||||
_ElementType *result = m_head;
|
_ElementType *result = m_head;
|
||||||
m_head = m_tail = nullptr;
|
m_head = m_tail = nullptr;
|
||||||
@ -237,13 +237,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// remove the given object and free its memory
|
// remove the given object and free its memory
|
||||||
void remove(_ElementType &object)
|
void remove(_ElementType &object) noexcept
|
||||||
{
|
{
|
||||||
global_free(&detach(object));
|
global_free(&detach(object));
|
||||||
}
|
}
|
||||||
|
|
||||||
// find an object by index in the list
|
// find an object by index in the list
|
||||||
_ElementType *find(int index) const
|
_ElementType *find(int index) const noexcept
|
||||||
{
|
{
|
||||||
for (_ElementType *cur = m_head; cur != nullptr; cur = cur->m_next)
|
for (_ElementType *cur = m_head; cur != nullptr; cur = cur->m_next)
|
||||||
if (index-- == 0)
|
if (index-- == 0)
|
||||||
@ -252,7 +252,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// return the index of the given object in the list
|
// return the index of the given object in the list
|
||||||
int indexof(const _ElementType &object) const
|
int indexof(const _ElementType &object) const noexcept
|
||||||
{
|
{
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (_ElementType *cur = m_head; cur != nullptr; cur = cur->m_next)
|
for (_ElementType *cur = m_head; cur != nullptr; cur = cur->m_next)
|
||||||
|
Loading…
Reference in New Issue
Block a user