summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/plists.h
blob: c2f758d0d705e7aac3484f7390a1800df8e6d768 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * plists.h
 *
 */

#pragma once

#ifndef PLISTS_H_
#define PLISTS_H_

#include "nl_config.h"
#include "pstring.h"

// ----------------------------------------------------------------------------------------
// plinearlist_t: a simple list
// ----------------------------------------------------------------------------------------

template <class _ListClass, int _NumElem = 0>
class plinearlist_t
{
public:

	ATTR_COLD plinearlist_t(int numElements = _NumElem)
	{
		m_num_elements = numElements;
		if (m_num_elements == 0)
			m_list = NULL;
		else
			m_list = nl_alloc_array(_ListClass, m_num_elements);
		m_count = 0;
	}

	ATTR_COLD plinearlist_t(const plinearlist_t &rhs)
	{
		m_num_elements = rhs.capacity();
		if (m_num_elements == 0)
			m_list = NULL;
		else
			m_list = nl_alloc_array(_ListClass, m_num_elements);
		m_count = 0;
		for (int i=0; i<rhs.count(); i++)
		{
			this->add(rhs[i]);
		}
	}

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


	ATTR_COLD ~plinearlist_t()
	{
		if (m_list != NULL)
		    nl_free_array(m_list);
		m_list = NULL;
	}

	ATTR_HOT inline operator _ListClass *  () { return m_list; }
	ATTR_HOT inline operator const _ListClass * () const { return m_list; }

	/* using the [] operator will not allow gcc to vectorize code because
	 * basically a pointer is returned.
	 * array works around this.
	 */

	ATTR_HOT inline _ListClass *array() { return m_list; }

	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]; }

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

		m_list[m_count++] = elem;
	}

	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 void remove_at(const int pos)
	{
		nl_assert((pos>=0) && (pos<m_count));
		m_count--;
		for (int i = pos; i < m_count; i++)
		{
			m_list[i] = m_list[i+1];
		}
	}

	ATTR_HOT inline void swap(const int pos1, const int pos2)
	{
		nl_assert((pos1>=0) && (pos1<m_count));
		nl_assert((pos2>=0) && (pos2<m_count));
		_ListClass tmp = m_list[pos1];
		m_list[pos1] = m_list[pos2];
		m_list[pos2] =tmp;
	}

	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 int indexof(const _ListClass &elem) const
	{
		for (int i = 0; i < m_count; i++)
		{
			if (m_list[i] == elem)
				return i;
		}
		return -1;
	}

	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 is_empty() const { return (m_count == 0); }
	ATTR_HOT inline void clear() { m_count = 0; }
	ATTR_HOT inline int capacity() const { return m_num_elements; }

	ATTR_COLD void clear_and_free()
	{
		for (_ListClass *i = m_list; i < m_list + m_count; i++)
		{
			nl_free(*i);
		}
		clear();
	}

private:
	ATTR_COLD void resize(const int new_size)
	{
		int cnt = count();
		if (new_size > 0)
		{
			_ListClass *m_new = nl_alloc_array(_ListClass, new_size);
			_ListClass *pd = m_new;

			if (cnt > new_size)
				cnt = new_size;
			for (_ListClass *ps = m_list; ps < m_list + cnt; ps++, pd++)
				*pd = *ps;
			if (m_list != NULL)
				nl_free_array(m_list);
			m_list = m_new;
			m_count = cnt;
		}
		else
		{
			if (m_list != NULL)
			    nl_free_array(m_list);
			m_list = NULL;
			m_count = 0;
		}
		m_num_elements = new_size;
	}

	int m_count;
	_ListClass * m_list /* ATTR_ALIGN */;
	int m_num_elements;
};

// ----------------------------------------------------------------------------------------
// pnamedlist_t: a simple list
// ----------------------------------------------------------------------------------------

#ifdef SDLMAME_SOLARIS
#undef _C
#endif

template <class _C>
class pnamedlist_t : public plinearlist_t<_C>
{
public:
	_C find(const pstring name) const
	{
		for (int i=0; i < this->count(); i++)
			if (get_name((*this)[i]) == name)
				return (*this)[i];
		return _C(NULL);
	}

	void remove_by_name(const pstring name)
	{
		plinearlist_t<_C>::remove(find(name));
	}

	bool add(_C dev, bool allow_duplicate)
	{
		if (allow_duplicate)
			plinearlist_t<_C>::add(dev);
		else
		{
			if (!(this->find(get_name(dev)) == _C(NULL)))
				return false;
			plinearlist_t<_C>::add(dev);
		}
		return true;
	}

private:
	template <typename T> static const pstring get_name(T &elem) { return elem.name(); }
	template <typename T> static const pstring get_name(T *elem) { return elem->name(); }

};


// ----------------------------------------------------------------------------------------
// pstack_t: a simple stack
// ----------------------------------------------------------------------------------------

template <class _StackClass, int _NumElem = 128>
class pstack_t
{
public:

	ATTR_COLD pstack_t(int numElements = _NumElem)
	: m_list(numElements)
	{
	}

	ATTR_COLD pstack_t(const pstack_t &rhs)
	: m_list(rhs.m_list)
	{
	}

	ATTR_COLD pstack_t &operator=(const pstack_t &rhs)
	{
		m_list = rhs.m_list;
		return *this;
	}


	ATTR_COLD ~pstack_t()
	{
	}

	ATTR_HOT inline void push(const _StackClass &elem)
	{
		m_list.add(elem);
	}

	ATTR_HOT inline _StackClass peek() const
	{
		return m_list[m_list.count() - 1];
	}

	ATTR_HOT inline _StackClass pop()
	{
		_StackClass ret = peek();
		m_list.remove_at(m_list.count() - 1);
		return ret;
	}

	ATTR_HOT inline int count() const { return m_list.count(); }
	ATTR_HOT inline bool empty() const { return (m_list.count() == 0); }
	ATTR_HOT inline void reset() { m_list.reset(); }
	ATTR_HOT inline int capacity() const { return m_list.capacity(); }

private:
	plinearlist_t<_StackClass, _NumElem> m_list;
};

template <class _ListClass>
struct plinkedlist_element_t
{
	plinkedlist_element_t() : m_next(NULL) {}
	_ListClass * m_next;

};

// ----------------------------------------------------------------------------------------
// plinkedlist_t: a simple linked list
// ----------------------------------------------------------------------------------------

template <class _ListClass>
class plinkedlist_t
{
public:

	plinkedlist_t() : m_head(NULL) {}

	ATTR_HOT inline void insert(const _ListClass &before, _ListClass &elem)
	{
		if (m_head == &before)
		{
			elem.m_next = m_head;
			m_head = elem;
		}
		else
		{
			_ListClass *p = m_head;
			while (p != NULL)
			{
				if (p->m_next == &before)
				{
					elem->m_next = &before;
					p->m_next = &elem;
					return;
				}
				p = p->m_next;
			}
			nl_assert_always(false, "element not found");
		}
	}

	ATTR_HOT inline void insert(_ListClass &elem)
	{
		elem.m_next = m_head;
		m_head = &elem;
	}

	ATTR_HOT inline void add(_ListClass &elem)
	{
		_ListClass **p = &m_head;
		while (*p != NULL)
		{
			p = &((*p)->m_next);
		}
		*p = &elem;
		elem.m_next = NULL;
	}

	ATTR_HOT inline void remove(const _ListClass &elem)
	{
		_ListClass **p = &m_head;
		while (*p != &elem)
		{
			nl_assert(*p != NULL);
			p = &((*p)->m_next);
		}
		(*p) = elem.m_next;
	}


	ATTR_HOT static inline _ListClass *next(const _ListClass &elem) { return elem.m_next; }
	ATTR_HOT static inline _ListClass *next(const _ListClass *elem) { return elem->m_next; }
	ATTR_HOT inline _ListClass *first() const { return m_head; }
	ATTR_HOT inline void clear() { m_head = NULL; }
	ATTR_HOT inline bool is_empty() const { return (m_head == NULL); }

private:
	_ListClass *m_head;
};

#endif /* PLISTS_H_ */