summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/nl_lists.h
blob: 70f0569d0f460868f72eb042d065619124200e19 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// 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, int _NumElem = 128>
class netlist_list_t
{
public:

	ATTR_COLD netlist_list_t(int numElements = _NumElem)
	{
		m_num_elements = numElements;
		m_list = new _ListClass[m_num_elements];
        m_count = 0;
	}

	ATTR_COLD netlist_list_t(const netlist_list_t &rhs)
	{
	    m_num_elements = rhs.capacity();
		m_list = new _ListClass[m_num_elements];
		m_count = 0;
		for (int i=0; i<rhs.count(); i++)
		{
			this->add(rhs[i]);
		}
	}

	ATTR_COLD netlist_list_t &operator=(const netlist_list_t &rhs)
	{
		for (int i=0; i<rhs.count(); i++)
		{
			this->add(rhs[i]);
		}
		return *this;
	}


	ATTR_COLD ~netlist_list_t()
	{
		delete[] m_list;
	}

	ATTR_HOT inline void add(const _ListClass &elem)
	{
		if (m_count >= m_num_elements)
			resize(m_num_elements * 2);

		m_list[m_count++] = elem;
	}

	ATTR_HOT inline void resize(const int new_size)
	{
		int cnt = count();
		_ListClass *m_new = new _ListClass[new_size];
		_ListClass *pd = m_new;

		for (_ListClass *ps = m_list; ps < m_list + cnt; ps++, pd++)
			*pd = *ps;
		delete[] m_list;
		m_list = m_new;
		m_count = cnt;
		m_num_elements = new_size;
	}

	ATTR_HOT inline void remove(const _ListClass &elem)
	{
		for (int i = 0; i < m_count; i++)
		{
			if (m_list[i] == elem)
			{
			    m_count --;
				while (i < m_count)
				{
					m_list[i] = m_list[i+1];
					i++;
				}
				return;
			}
		}
	}

	ATTR_HOT inline bool contains(const _ListClass &elem) const
	{
		for (_ListClass *i = m_list; i < m_list + m_count; i++)
		{
			if (*i == elem)
				return true;
		}
		return false;
	}

	ATTR_HOT inline const _ListClass *first() const { return ((m_count > 0) ? &m_list[0] : NULL ); }
	ATTR_HOT inline const _ListClass *next(const _ListClass *lc) const { return ((lc < last()) ? lc + 1 : NULL ); }
	ATTR_HOT inline const _ListClass *last() const { return &m_list[m_count -1]; }
	ATTR_HOT inline int count() const { return m_count; }
	ATTR_HOT inline bool empty() const { return (m_count == 0); }
	ATTR_HOT inline void reset() { m_count = 0; }
    ATTR_HOT inline int capacity() const { return m_num_elements; }

	ATTR_COLD void reset_and_free()
	{
		for (_ListClass *i = m_list; i < m_list + m_count; i++)
		{
			delete *i;
		}
		reset();
	}

	ATTR_HOT inline _ListClass& operator[](const int & index) { return m_list[index]; }
	ATTR_HOT inline const _ListClass& operator[](const int & index) const { return m_list[index]; }

private:
	int m_count;
	_ListClass * m_list;
	int m_num_elements;
	//_ListClass m_list[_NumElements];
};

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

template <class _Element, class _Time, int _Size>
class netlist_timed_queue
{
	NETLIST_PREVENT_COPYING(netlist_timed_queue)
public:

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

	private:
		_Time m_time;
		_Element m_object;
	};

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

	ATTR_HOT inline int capacity() const { return _Size; }
	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 void push(const entry_t &e)
	{
#if 0
	    // less is more
	    if (is_empty() || (e.time() <= (m_end - 1)->time()))
		{
			*m_end++ = e;
			inc_stat(m_prof_end);
		}
		else
#endif
		{
			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 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];
	}

	// save state support & mame disasm

	ATTR_COLD inline const entry_t *listptr() const { return &m_list[0]; }
	ATTR_HOT inline int count() const { return m_end - m_list; }
	ATTR_HOT inline const entry_t & operator[](const int & index) const { return m_list[index]; }

#if (NL_KEEP_STATISTICS)
	// profiling
	INT32   m_prof_start;
	INT32   m_prof_end;
	INT32   m_prof_sortmove;
	INT32   m_prof_sort;
#endif

private:

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

};


#endif /* NLLISTS_H_ */