summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/nl_lists.h
blob: 970b5c1c39ff9c640dd28aff92fe6ad6c79c5997 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nllists.h
 *
 */

#ifndef NLLISTS_H_
#define NLLISTS_H_

#include "nl_config.h"

// ----------------------------------------------------------------------------------------
// netlist_list_t: a simple list
// ----------------------------------------------------------------------------------------


template <class _ListClass>
struct netlist_list_t
{
public:
	ATTR_COLD netlist_list_t(int numElements)
	{
		m_num_elements = numElements;
		m_list = new _ListClass[m_num_elements];
		m_ptr = m_list;
		m_ptr--;
	}
	ATTR_COLD ~netlist_list_t()
	{
		delete[] m_list;
	}
	ATTR_HOT inline void add(const _ListClass elem)
	{
		assert(m_ptr-m_list <= m_num_elements - 1);

		*(++m_ptr) = elem;
	}
	ATTR_HOT inline _ListClass *first() { return &m_list[0]; }
	ATTR_HOT inline _ListClass *last()  { return m_ptr; }
	ATTR_HOT inline _ListClass *item(int i) { return &m_list[i]; }
	ATTR_HOT inline int count() const { return m_ptr - m_list + 1; }
	ATTR_HOT inline bool empty() { return (m_ptr < m_list); }
	ATTR_HOT inline void clear() { m_ptr = m_list - 1; }
private:
	_ListClass * m_ptr;
	_ListClass * m_list;
	int m_num_elements;
	//_ListClass m_list[_NumElements];
};

// ----------------------------------------------------------------------------------------
// timed queue
// ----------------------------------------------------------------------------------------

template <class _Element, class _Time, int _Size>
class netlist_timed_queue1
{
public:

	struct entry_t
	{
	public:
		inline entry_t()
		: m_time(), m_object(NULL) {}
		inline entry_t(const _Time atime, _Element &elem) : m_time(atime), m_object(&elem) {}
		ATTR_HOT inline const _Time &time() const { return m_time; }
		ATTR_HOT inline _Element & object() const { return *m_object; }
	private:
		_Time m_time;
		_Element *m_object;
	};

	netlist_timed_queue1()
	{
		//m_list = global_alloc_array(entry_t, SIZE);
		clear();
	}

	ATTR_HOT inline bool is_empty() const { return (m_end == &m_list[0]); }
	ATTR_HOT inline bool is_not_empty() const { return (m_end > &m_list[0]); }

	ATTR_HOT ATTR_ALIGN inline void push(const entry_t &e)
	{
		if (is_empty() || (e.time() <= (m_end - 1)->time()))
		{
			*m_end++ = e;
			//inc_stat(m_prof_end);
		}
		else
		{
			entry_t * RESTRICT i = m_end++;
			while ((i>&m_list[0]) && (e.time() > (i-1)->time()) )
			{
				i--;
				*(i+1) = *i;
				//inc_stat(m_prof_sortmove);
			}
			*i = e;
			//inc_stat(m_prof_sort);
		}
		assert(m_end - m_list < _Size);
	}

	ATTR_HOT inline const entry_t pop()
	{
		return *--m_end;
	}

	ATTR_HOT inline const entry_t peek() const
	{
		return *(m_end-1);
	}

	ATTR_COLD void clear()
	{
		m_end = &m_list[0];
	}
	// profiling

	INT32   m_prof_start;
	INT32   m_prof_end;
	INT32   m_prof_sortmove;
	INT32   m_prof_sort;
private:

	entry_t * RESTRICT m_end;
	//entry_t *m_list;
	entry_t m_list[_Size];

};


#endif /* NLLISTS_H_ */