More c++. (nw)

This commit is contained in:
couriersud 2016-06-05 18:25:50 +02:00
parent 91be6978cb
commit 498a560ec7
6 changed files with 59 additions and 151 deletions

View File

@ -602,7 +602,7 @@ net_t::~net_t()
void net_t::inc_active(core_terminal_t &term) void net_t::inc_active(core_terminal_t &term)
{ {
m_active++; m_active++;
m_list_active.insert(term); m_list_active.push_front(&term);
nl_assert(m_active <= num_cons()); nl_assert(m_active <= num_cons());
if (m_active == 1) if (m_active == 1)
{ {
@ -633,7 +633,7 @@ void net_t::dec_active(core_terminal_t &term)
{ {
--m_active; --m_active;
nl_assert(m_active >= 0); nl_assert(m_active >= 0);
m_list_active.remove(term); m_list_active.remove(&term);
if (m_active == 0 && netlist().use_deactivate()) if (m_active == 0 && netlist().use_deactivate())
railterminal().device().do_dec_active(); railterminal().device().do_dec_active();
} }
@ -647,7 +647,7 @@ void net_t::rebuild_list()
for (auto & term : m_core_terms) for (auto & term : m_core_terms)
if (term->state() != logic_t::STATE_INP_PASSIVE) if (term->state() != logic_t::STATE_INP_PASSIVE)
{ {
m_list_active.add(*term); m_list_active.push_back(term);
cnt++; cnt++;
} }
m_active = cnt; m_active = cnt;
@ -671,11 +671,11 @@ void net_t::update_devs()
m_in_queue = 2; /* mark as taken ... */ m_in_queue = 2; /* mark as taken ... */
m_cur_Q = m_new_Q; m_cur_Q = m_new_Q;
for (auto p = m_list_active.first(); p != nullptr; p = p->next()) for (auto & p : m_list_active)
{ {
inc_stat(p->device().stat_call_count); inc_stat(p.device().stat_call_count);
if ((p->state() & mask) != 0) if ((p.state() & mask) != 0)
p->device().update_dev(); p.device().update_dev();
} }
} }
@ -693,7 +693,7 @@ void net_t::reset()
m_list_active.clear(); m_list_active.clear();
for (core_terminal_t *ct : m_core_terms) for (core_terminal_t *ct : m_core_terms)
m_list_active.add(*ct); m_list_active.push_back(ct);
for (core_terminal_t *ct : m_core_terms) for (core_terminal_t *ct : m_core_terms)
ct->reset(); ct->reset();
@ -805,7 +805,7 @@ void analog_net_t::process_net(plib::pvector_t<list_t> &groups)
core_terminal_t::core_terminal_t(core_device_t &dev, const pstring &aname, const type_t atype) core_terminal_t::core_terminal_t(core_device_t &dev, const pstring &aname, const type_t atype)
: device_object_t(dev, dev.name() + "." + aname, atype) : device_object_t(dev, dev.name() + "." + aname, atype)
, plinkedlist_element_t() , plib::linkedlist_t<core_terminal_t>::element_t()
, m_net(nullptr) , m_net(nullptr)
, m_state(STATE_NONEX) , m_state(STATE_NONEX)
{ {

View File

@ -403,7 +403,7 @@ namespace netlist
// core_terminal_t // core_terminal_t
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
class core_terminal_t : public device_object_t, public plib::plinkedlist_element_t<core_terminal_t> class core_terminal_t : public device_object_t, public plib::linkedlist_t<core_terminal_t>::element_t
{ {
P_PREVENT_COPYING(core_terminal_t) P_PREVENT_COPYING(core_terminal_t)
public: public:

View File

@ -93,7 +93,7 @@
//============================================================ //============================================================
#define NL_DEBUG (false) #define NL_DEBUG (false)
#define NL_KEEP_STATISTICS (1) #define NL_KEEP_STATISTICS (0)
//============================================================ //============================================================
// General Macros // General Macros

View File

@ -9,6 +9,7 @@
#define NLSETUP_H_ #define NLSETUP_H_
#include <memory> #include <memory>
#include <stack>
#include "nl_base.h" #include "nl_base.h"
#include "nl_factory.h" #include "nl_factory.h"

View File

@ -11,11 +11,10 @@
#define PLISTS_H_ #define PLISTS_H_
#include <algorithm> #include <algorithm>
#include <stack>
#include <vector> #include <vector>
#include <type_traits> #include <type_traits>
#include <cstring> //#include <cstring>
#include <array> //#include <array>
#include "palloc.h" #include "palloc.h"
#include "pstring.h" #include "pstring.h"
@ -118,171 +117,78 @@ public:
// the list allows insertions / deletions if used properly // the list allows insertions / deletions if used properly
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template <class LC>
class linkedlist_t;
#if 1
template <class LC>
struct plinkedlist_element_t
{
public:
friend class linkedlist_t<LC>;
plinkedlist_element_t() : m_next(nullptr) {}
LC *next() const { return m_next; }
//private:
LC * m_next;
};
template <class LC> template <class LC>
class linkedlist_t class linkedlist_t
{ {
public: public:
struct element_t
{
public:
friend class linkedlist_t<LC>;
element_t() : m_next(nullptr) {}
LC *next() const { return m_next; }
private:
LC * m_next;
};
struct iter_t final : public std::iterator<std::forward_iterator_tag, LC>
{
LC* p;
public:
constexpr iter_t(LC* x) noexcept : p(x) {}
iter_t(const iter_t &rhs) noexcept = default;
iter_t(iter_t &&rhs) noexcept = default;
iter_t& operator++() noexcept {p = p->next();return *this;}
iter_t operator++(int) noexcept {iter_t tmp(*this); operator++(); return tmp;}
bool operator==(const iter_t& rhs) noexcept {return p==rhs.p;}
bool operator!=(const iter_t& rhs) noexcept {return p!=rhs.p;}
LC& operator*() noexcept {return *p;}
LC* operator->() noexcept {return p;}
};
linkedlist_t() : m_head(nullptr) {} linkedlist_t() : m_head(nullptr) {}
#if 0
void insert(const LC &before, LC &elem) iter_t begin() const { return iter_t(m_head); }
constexpr iter_t end() const { return iter_t(nullptr); }
void push_front(LC *elem)
{ {
if (m_head == &before) elem->m_next = m_head;
{ m_head = elem;
elem.m_next = m_head;
m_head = &elem;
}
else
{
LC *p = m_head;
while (p != nullptr)
{
if (p->m_next == &before)
{
elem->m_next = &before;
p->m_next = &elem;
return;
}
p = p->m_next;
}
//throw pexception("element not found");
}
}
#endif
void insert(LC &elem)
{
elem.m_next = m_head;
m_head = &elem;
} }
void add(LC &elem) void push_back(LC *elem)
{ {
LC **p = &m_head; LC **p = &m_head;
while (*p != nullptr) while (*p != nullptr)
{ {
p = &((*p)->m_next); p = &((*p)->m_next);
} }
*p = &elem; *p = elem;
elem.m_next = nullptr; elem->m_next = nullptr;
} }
void remove(const LC &elem) void remove(const LC *elem)
{ {
auto p = &m_head; auto p = &m_head;
for ( ; *p != &elem; p = &((*p)->m_next)) for ( ; *p != elem; p = &((*p)->m_next))
{ {
//nl_assert(*p != nullptr); //nl_assert(*p != nullptr);
} }
(*p) = elem.m_next; (*p) = elem->m_next;
} }
LC *first() const { return m_head; } LC *front() const { return m_head; }
void clear() { m_head = nullptr; } void clear() { m_head = nullptr; }
bool is_empty() const { return (m_head == nullptr); } bool empty() const { return (m_head == nullptr); }
//private: //private:
LC *m_head; LC *m_head;
}; };
#else
template <class LC>
struct plinkedlist_element_t
{
public:
friend class linkedlist_t<LC>;
plinkedlist_element_t() : m_next(nullptr), m_prev(nullptr) {}
LC *next() const { return m_next; }
private:
LC * m_next;
LC * m_prev;
};
template <class LC>
class linkedlist_t
{
public:
linkedlist_t() : m_head(nullptr), m_tail(nullptr) {}
void insert(LC &elem)
{
if (m_head != nullptr)
m_head->m_prev = &elem;
elem.m_next = m_head;
elem.m_prev = nullptr;
m_head = &elem;
if (m_tail == nullptr)
m_tail = &elem;
}
void add(LC &elem)
{
if (m_tail != nullptr)
m_tail->m_next = &elem;
elem.m_prev = m_tail;
m_tail = &elem;
elem.m_next = nullptr;
if (m_head == nullptr)
m_head = &elem;
}
void remove(const LC &elem)
{
if (prev(elem) == nullptr)
{
m_head = next(elem);
if (m_tail == &elem)
m_tail = nullptr;
}
else
prev(elem)->m_next = next(elem);
if (next(elem) == nullptr)
{
m_tail = prev(elem);
if (m_head == &elem)
m_head = nullptr;
}
else
next(elem)->m_prev = prev(elem);
}
static LC *next(const LC &elem) { return static_cast<LC *>(elem.m_next); }
static LC *next(const LC *elem) { return static_cast<LC *>(elem->m_next); }
static LC *prev(const LC &elem) { return static_cast<LC *>(elem.m_prev); }
static LC *prev(const LC *elem) { return static_cast<LC *>(elem->m_prev); }
LC *first() const { return m_head; }
void clear() { m_head = m_tail = nullptr; }
bool is_empty() const { return (m_head == nullptr); }
private:
LC *m_head;
LC *m_tail;
};
#endif
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// string list // string list

View File

@ -11,6 +11,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
#include <algorithm> #include <algorithm>
#include <stack>
#include "pstring.h" #include "pstring.h"
#include "palloc.h" #include "palloc.h"