File: list.h

package info (click to toggle)
vdr-plugin-freecell 0.0.2-48
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 684 kB
  • ctags: 343
  • sloc: ansic: 2,413; sh: 152; makefile: 56
file content (560 lines) | stat: -rw-r--r-- 11,114 bytes parent folder | download | duplicates (4)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#ifndef TOOLBOX_LIST_H
#define TOOLBOX_LIST_H

#include "tools/tools.h"
#include "tools/collection.h"
#include "tools/string.h"

template <class T>
class cTBList: public cTBCollection {
protected:
	struct cItem {
		cItem *Next;
		cItem *Prev;
		T      Data;
	};

	cItem *NewItem (cItem *i1, cItem *i2) const;
	void DelItem (cItem *i) const;

private:
	cItem *m_First;
	cItem *m_Last;
	cItem *m_Current;
	int    m_CurPos;
	uint   m_Count;
	bool   m_Delete;

	cItem *Locate (uint idx);
	cItem *Unlink ();
	cItem *GetCurrent () const { return m_Current; }

	void Initialize ();

public:
	cTBList ();
	cTBList (const cTBList<T> &src);
	virtual ~cTBList ();

	void SetAutoDelete(bool on) { m_Delete = on; }

	cTBList<T> &operator= (const cTBList<T> &src);

	uint Count () const { return m_Count; }

	void Append   (const T &obj);
	void Prepend  (const T &obj);
	bool Insert (uint pos, const T &obj);

	T &Append  ();
	T &Prepend ();

	void Append   (const cTBList<T> &src);
	void Prepend  (const cTBList<T> &src);
	bool Insert (uint pos, const cTBList<T> &src);

	bool Remove   (const T &obj);
	bool Remove   ();
	bool Remove (uint idx);

	bool RemoveFirst () { SetFirstCurrent(); return Remove(); }
	bool RemoveLast  () { SetLastCurrent();  return Remove(); }

	T Take      ();
	T Take    (uint idx);

	T TakeFirst () { SetFirstCurrent(); return Take(); }
	T TakeLast  () { SetLastCurrent();  return Take(); }

	void Clear ();

	int Find     (const T &obj);
	int Contains (const T &obj) const;
	
	int Current  () const { return m_CurPos; }

	T  At (uint idx) const;
	T &At (uint idx);

	T  operator[] (uint idx) const { return At(idx); }
	T &operator[] (uint idx) { return At(idx); }

	const T &Get   () const;
	const T &First () const;
	const T &Last  () const;
	const T &Next  () const;
	const T &Prev  () const;
	
	T &SetFirstCurrent ();
	T &SetLastCurrent  ();
	T &SetNextCurrent  ();
	T &SetPrevCurrent  ();

	friend cTBList<T> &operator+= <> (cTBList<T> &list1, const cTBList<T> &list2);
	friend cTBList<T> &operator+= <> (cTBList<T> &list, const T &element);

	friend cTBList<T> operator+ <> (const cTBList<T> &list1, const cTBList<T> &list2);
	friend cTBList<T> operator+ <> (const T &element, const cTBList<T> &list);
	friend cTBList<T> operator+ <> (const cTBList<T> &list, const T &element);

	/*friend cSource &operator<< <> (cSource &dest, const cTBList<T> &list);
	friend cSource &operator>> <> (cSource &dest, cTBList<T> &list);*/
};

// Template Implementation (inline funcs)

template<class T>
inline int cTBList<T>::Find (const T & i) {
	cTBList<T>::cItem *item = m_First;
	int index = 0;

	while (item && (item->Data != i)) {
		item = item->Next;
		index++;
	}

	m_Current = item;
	return (m_CurPos = item ? index : -1);
}

template<class T>
inline int cTBList<T>::Contains (const T &i) const {
	cItem *item = m_First;
	uint Count = 0;

	while (item) {
		if (item->Data == i)
			Count++;
		item = item->Next;
	}
	return Count;
}

template<class T>
inline T &cTBList<T>::At (uint idx) {
	ASSERT(idx >= m_Count);
	return Locate(idx)->Data;
}

template<class T>
inline T cTBList<T>::At (uint idx) const {
	ASSERT(idx >= m_Count);
	return Locate(idx)->Data;
}

template<class T>
inline const T &cTBList<T>::Get () const {
	ASSERT(m_Current == NULL);
	return m_Current->Data;
}

template<class T>
inline const T &cTBList<T>::First () const {
	ASSERT(m_First == NULL);
	return m_First->Data;
}

template<class T>
inline const T &cTBList<T>::Last () const {
	ASSERT(m_Last == NULL);
	return m_Last->Data;
}

template<class T>
inline const T &cTBList<T>::Next () const {
	ASSERT(m_Current == NULL);
	ASSERT(m_Current->Next == NULL);
	return m_Current->Next->Data;
}

template<class T>
inline const T &cTBList<T>::Prev () const {
	ASSERT(m_Current == NULL);
	ASSERT(m_Current->Prev == NULL);
	return m_Current->Prev->Data;
}

// Template implementation (out-of-line)

template<class T>
cTBList<T>::cTBList () : cTBCollection () {
	Initialize();
}

template<class T>
cTBList<T>::cTBList (const cTBList<T> &src) : cTBCollection () {
	Initialize();
	Append(src);
}

template<class T>
cTBList<T>::~cTBList () {
	if (m_Count)
		Clear();
}

template<class T>
void cTBList<T>::Initialize () {
	m_First = m_Last = m_Current = NULL;
	m_Count = 0;
	m_CurPos = -1;
	m_Delete = false;
}

template<class T>
typename cTBList<T>::cItem *cTBList<T>::NewItem (cTBList<T>::cItem *pBefore, cTBList<T>::cItem *pAfter) const {
	cTBList<T>::cItem *item = new cTBList<T>::cItem;

	item->Next = pAfter;
	item->Prev = pBefore;

	if (pBefore)
		pBefore->Next = item;

	if (pAfter)
		pAfter->Prev  = item;

	CreateElements<T>(&item->Data, 1);

	return item;
}

template<class T>
void cTBList<T>::DelItem (cTBList<T>::cItem *item) const {
	ASSERT(item == NULL);
	if (m_Delete)
		delete item->Data;
	DeleteElements<T>(&item->Data, 1);
	delete item;
}

template<class T>
typename cTBList<T>::cItem *cTBList<T>::Locate (uint idx) {
	ASSERT(idx >= m_Count);

	if ((int)idx == m_CurPos)
		return m_Current;

	cTBList<T>::cItem *item;
	int dist = idx - m_CurPos;
	bool direct;

	if (idx >= m_Count)
		return 0;

	if (dist < 0)
		dist = -dist;

	if (((uint)dist < idx) && 
			((uint)dist < m_Count - idx)) {
		item = m_Current;
		direct = idx > (uint)m_CurPos;
	} else if (idx < m_Count - idx) {
		item = m_First;
		dist = idx;
		direct = true;
	} else {
		item = m_Last;
		dist = m_Count - idx - 1;
		if (dist < 0)
			dist = 0;
		direct = false;
	}

	while (dist--)
		item = direct ? item->Next : item->Prev;

	m_Current = item;
	m_CurPos = idx;
	return item;
}

template<class T>
T &cTBList<T>::Prepend () {
	cTBList<T>::cItem *item = NewItem(NULL, m_First);

	m_First = m_Current = item;
	if (!m_Last)
		m_Last = m_First;

	m_Count++;
	m_CurPos = 0;

	return item->Data;
}

template<class T>
T &cTBList<T>::Append () {
	cTBList<T>::cItem *item = NewItem(m_Last, NULL);

	m_Last = m_Current = item;
	if (!m_First)
		m_First = m_Last;

	m_CurPos = m_Count;
	m_Count++;

	return item->Data;
}

template<class T>
void cTBList<T>::Prepend (const T &i) {
	Prepend() = i;
}

template<class T>
void cTBList<T>::Append (const T & i) {
	Append() = i;
}

template<class T>
bool cTBList<T>::Insert (uint idx, const T & i) {
	if (!idx) {
		Prepend(i);
		return true;
	} else if (idx == m_Count) {
		Append(i);
		return true;
	}

	cTBList<T>::cItem *nextItem = Locate(idx);
	if (!nextItem)
		return false;

	cTBList<T>::cItem *prevItem = nextItem->Prev;

	cTBList<T>::cItem *item = NewItem(prevItem, nextItem);
	item->Data = i;

	m_CurPos = idx;
	m_Current = item;
	m_Count++;

	return true;
}

template<class T>
typename cTBList<T>::cItem *cTBList<T>::Unlink () {
	if (!m_Current)
		return NULL;

	cTBList<T>::cItem *item = m_Current;
	if (item == m_First) {
		if (m_First = item->Next)
			m_First->Prev = NULL;
		else {
			m_Last = m_Current = NULL;
			m_CurPos = -1;
		}
	} else if (item == m_Last) {
		m_Last = item->Prev;
		m_Last->Next = NULL;
	} else {
		item->Prev->Next = item->Next;
		item->Next->Prev = item->Prev;
	}

	if (item->Next)
		m_Current = item->Next;
	else if (item->Prev) {
		m_Current = item->Prev;
		m_CurPos--;
	}
	m_Count--;
	return item;
}

template<class T>
bool cTBList<T>::Remove (const T & i) {
	if (i)
		if (Find(i) == -1)
			return false;

	cTBList<T>::cItem *item = Unlink();
	if (!item)
		return false;

	DelItem(item);
	return true;
}

template<class T>
bool cTBList<T>::Remove (uint idx) {
	if (!Locate(idx))
		return false;

	cTBList<T>::cItem *item = Unlink();
	if (!item)
		return false;

	DelItem(item);
	return true;
}

template<class T>
T cTBList<T>::Take () {
	ASSERT(m_Current == NULL);

	cItem *item = Unlink();
	T data = item->Data;
	DelItem(item);
	return data;
}

template<class T>
T cTBList<T>::Take (uint idx) {
	ASSERT(idx >= m_Count);

	Locate(idx);

	return Take();
}

template<class T>
void cTBList<T>::Clear () {
	cTBList<T>::cItem *item = m_First;

	m_First = m_Last = m_Current = NULL;
	m_Count = 0;
	m_CurPos = -1;

	cTBList<T>::cItem *Prev;
	while (item) {
		Prev = item;
		item = item->Next;
		DelItem(Prev);
	}
}

template<class T>
T &cTBList<T>::SetFirstCurrent () {
	ASSERT(m_First == NULL);
	m_CurPos = 0;
	return (m_Current = m_First)->Data;
}

template<class T>
T &cTBList<T>::SetLastCurrent () {
	ASSERT(m_Last == NULL);
	m_CurPos = m_Count - 1;
	return (m_Current = m_Last)->Data;
}

template<class T>
T &cTBList<T>::SetNextCurrent () {
	ASSERT(m_Current == NULL);
	ASSERT(m_Current->Next == NULL);
	m_CurPos++;
	return (m_Current = m_Current->Next)->Data;
}

template<class T>
T & cTBList<T>::SetPrevCurrent () {
	ASSERT(m_Current == NULL);
	ASSERT(m_Current->Prev == NULL);
	m_CurPos--;
	return (m_Current = m_Current->Prev)->Data;
}

template<class T>
void cTBList<T>::Append (const cTBList<T> &src) {
	ASSERT(src.m_First == NULL);
	for (cItem *Next = src.m_First; Next; Next = Next->Next)
		Append(Next->Data);
}

template<class T>
void cTBList<T>::Prepend (const cTBList<T> &src) {
	ASSERT(src.m_First == NULL);
	for (cItem *Next = src.m_First; Next; Next = Next->Next)
		Prepend(Next->Data);
}

template<class T>
bool cTBList<T>::Insert (uint idx, const cTBList<T> &src) {
	ASSERT(src.m_First == NULL);

	if (idx >= m_Count)
		return false;

	for (cItem *Next = src.m_First; Next; Next = Next->Next)
		Insert(idx, Next->Data);
}

// Operators (all inline)

template<class T>
inline cTBList<T> &cTBList<T>::operator= (const cTBList<T> &src) {
	Clear();
	Append(src);
	return *this;
}

template<class T>
inline cTBList<T> &operator+= (cTBList<T> &list1, const cTBList<T> &list2) {
	list1.Append(list2);
	return list1;
}

template<class T>
inline cTBList<T> &operator+= (cTBList<T> &list, const T &element) {
	list.Append(element);
	return list;
}

template<class T>
inline cTBList<T> operator+ (const cTBList<T> &list1, const cTBList<T> &list2) {
	cTBList<T> neu = list1;
	neu.Append(list2);
	return neu;
}

template<class T>
inline cTBList<T> operator+ (const T &element, const cTBList<T> &list) {
	cTBList<T> neu = list;
	neu.Prepend(element);
	return neu;
}

template<class T>
inline cTBList<T> operator+ (const cTBList<T> &list, const T &element) {
	cTBList<T> neu = list;
	neu.Append(element);
	return neu;
}

typedef class cTBList<cTBString> cStringList;
typedef class cTBList<void*> cPtrList;

// Special Overrides for String lists
template<class T>
inline cTBList<T> &operator+= (cTBList<T> &src, const char *item); // Normally forbidden

template<>
inline cTBList<cTBString> &operator+= <cTBString> (cTBList<cTBString> &src, const char *item) {
	src.Append((cTBString)item);
	return src;
}

// Special Overrides for Lists of Lists
template<class T>
inline cTBList<cTBList<T> > &operator+= (cTBList<cTBList<T> > &src, const T &item) {
	src.m_Last->Append(item);
	return src;
}

template<class T>
inline cTBList<cTBList<T> > operator+ (const cTBList<cTBList<T> > &src, const T &item) {
	cTBList<cTBList<T> > neu = src;
	neu.m_Last->Append(item);
	return neu;
}

template<class T>
inline cTBList<cTBList<T> > operator+ (const T &item, const cTBList<cTBList<T> > &list) {
	cTBList<cTBList<T> > neu = list;
	list.m_Last->Append(item);
	return neu;
}

#endif // TOOLBOX_LIST_H