2002-08-13 00:10:24 +04:00
|
|
|
#ifndef _MEDIA_T_LIST_H
|
|
|
|
#define _MEDIA_T_LIST_H
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2018-10-23 16:19:14 +03:00
|
|
|
#include "MediaDebug.h"
|
2002-12-09 02:47:55 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class value> class List
|
|
|
|
{
|
|
|
|
public:
|
2002-12-09 02:47:55 +03:00
|
|
|
List()
|
|
|
|
: item_max(INIT_COUNT),
|
|
|
|
item_count(0),
|
|
|
|
item_iter(-1),
|
|
|
|
items((value **)malloc(sizeof(value *) * INIT_COUNT))
|
|
|
|
{
|
|
|
|
ASSERT(items);
|
|
|
|
}
|
|
|
|
|
|
|
|
~List()
|
|
|
|
{
|
2003-06-30 04:06:01 +04:00
|
|
|
MakeEmpty();
|
|
|
|
free(items);
|
2002-12-09 02:47:55 +03:00
|
|
|
}
|
2002-12-03 03:59:42 +03:00
|
|
|
|
|
|
|
List(const List<value> &other)
|
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
*this = other;
|
2002-12-03 03:59:42 +03:00
|
|
|
}
|
2002-12-09 02:47:55 +03:00
|
|
|
|
|
|
|
List<value> &operator=(const List<value> &other)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
MakeEmpty();
|
|
|
|
free(items);
|
|
|
|
item_max = other.item_max;
|
|
|
|
item_count = other.item_count;
|
|
|
|
items = (value **)malloc(sizeof(value *) * item_max);
|
|
|
|
ASSERT(items);
|
|
|
|
for (int i = 0; i < item_count; i++) {
|
|
|
|
items[i] = new value;
|
|
|
|
*items[i] = *other.items[i];
|
|
|
|
}
|
2002-12-10 23:15:29 +03:00
|
|
|
return *this;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-12-09 02:47:55 +03:00
|
|
|
|
|
|
|
bool Insert(const value &v)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
if (item_count == item_max) {
|
|
|
|
item_max *= 2;
|
|
|
|
items = (value **)realloc(items, sizeof(value *) * item_max);
|
|
|
|
ASSERT(items);
|
|
|
|
}
|
|
|
|
items[item_count] = new value;
|
|
|
|
*items[item_count] = v;
|
|
|
|
item_count++;
|
2002-07-09 16:24:59 +04:00
|
|
|
return true;
|
|
|
|
}
|
2002-12-09 02:47:55 +03:00
|
|
|
|
2015-03-16 23:03:22 +03:00
|
|
|
bool Get(int32 index, value **v) const
|
2002-10-09 03:59:43 +04:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
if (index < 0 || index >= item_count)
|
2002-10-09 03:59:43 +04:00
|
|
|
return false;
|
2002-12-09 02:47:55 +03:00
|
|
|
*v = items[index];
|
2002-10-09 03:59:43 +04:00
|
|
|
return true;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
bool Remove(int32 index)
|
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
if (index < 0 || index >= item_count)
|
2002-07-09 16:24:59 +04:00
|
|
|
return false;
|
2002-12-09 02:47:55 +03:00
|
|
|
delete items[index];
|
|
|
|
item_count--;
|
2009-06-08 11:01:58 +04:00
|
|
|
items[index] = items[item_count];
|
|
|
|
if (index == item_iter)
|
|
|
|
item_iter--;
|
2002-07-09 16:24:59 +04:00
|
|
|
return true;
|
|
|
|
}
|
2002-10-09 03:59:43 +04:00
|
|
|
|
2015-03-16 23:03:22 +03:00
|
|
|
int Find(const value &v) const
|
2002-12-03 03:59:42 +03:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
for (int i = 0; i < item_count; i++)
|
|
|
|
if (*items[i] == v)
|
2002-12-03 03:59:42 +03:00
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-09 01:50:38 +03:00
|
|
|
int CountItems() const
|
2002-12-09 02:47:55 +03:00
|
|
|
{
|
|
|
|
return item_count;
|
|
|
|
}
|
|
|
|
|
2010-02-09 01:50:38 +03:00
|
|
|
bool IsEmpty() const
|
2002-12-03 03:59:42 +03:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
return item_count == 0;
|
2002-12-03 03:59:42 +03:00
|
|
|
}
|
|
|
|
|
2002-10-09 03:59:43 +04:00
|
|
|
void MakeEmpty()
|
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
if (items != 0) {
|
|
|
|
for (int i = 0; i < item_count; i++) {
|
|
|
|
delete items[i];
|
|
|
|
}
|
|
|
|
item_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rewind()
|
|
|
|
{
|
|
|
|
item_iter = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetNext(value **v)
|
|
|
|
{
|
|
|
|
item_iter++;
|
|
|
|
return Get(item_iter, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoveCurrent()
|
|
|
|
{
|
|
|
|
return Remove(item_iter);
|
2002-10-09 03:59:43 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
private:
|
2002-12-09 02:47:55 +03:00
|
|
|
enum { INIT_COUNT=32 };
|
|
|
|
int item_max;
|
|
|
|
int item_count;
|
|
|
|
int item_iter;
|
|
|
|
value **items;
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2002-08-13 00:10:24 +04:00
|
|
|
#endif // _MEDIA_T_LIST_H
|