2002-08-13 00:10:24 +04:00
|
|
|
#ifndef _MEDIA_T_MAP_H
|
|
|
|
#define _MEDIA_T_MAP_H
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-09 02:47:55 +03:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
template<class key, class value> class Map
|
|
|
|
{
|
|
|
|
public:
|
2002-12-09 02:47:55 +03:00
|
|
|
Map()
|
|
|
|
: item_max(INIT_COUNT),
|
|
|
|
item_count(0),
|
|
|
|
item_iter(-1),
|
|
|
|
items((ent **)malloc(sizeof(ent *) * INIT_COUNT))
|
|
|
|
{
|
|
|
|
ASSERT(items);
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-09 02:47:55 +03:00
|
|
|
~Map()
|
|
|
|
{
|
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
|
|
|
Map(const Map<key, 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
|
|
|
Map<key, value> &operator=(const Map<key, value> &other)
|
|
|
|
{
|
|
|
|
MakeEmpty();
|
|
|
|
free(items);
|
|
|
|
item_max = other.item_max;
|
|
|
|
item_count = other.item_count;
|
|
|
|
items = (ent **)malloc(sizeof(ent *) * item_max);
|
|
|
|
ASSERT(items);
|
|
|
|
for (int i = 0; i < item_count; i++) {
|
|
|
|
items[i] = new ent;
|
|
|
|
items[i]->k = other.items[i]->k;
|
|
|
|
items[i]->v = other.items[i]->v;
|
|
|
|
}
|
2002-12-10 23:15:29 +03:00
|
|
|
return *this;
|
2002-12-09 02:47:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Insert(const key &k, 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 = (ent **)realloc(items, sizeof(ent *) * item_max);
|
|
|
|
ASSERT(items);
|
|
|
|
}
|
|
|
|
items[item_count] = new ent;
|
|
|
|
items[item_count]->k = k;
|
|
|
|
items[item_count]->v = v;
|
|
|
|
item_count++;
|
2002-10-09 03:59:43 +04:00
|
|
|
return true;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-12-09 02:47:55 +03:00
|
|
|
|
|
|
|
bool Get(const key &k, value **v)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < item_count; i++) {
|
|
|
|
if (items[i]->k == k) {
|
|
|
|
*v = &items[i]->v;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2002-08-13 00:10:24 +04:00
|
|
|
|
2002-12-09 02:47:55 +03:00
|
|
|
bool Remove(const key &k) {
|
|
|
|
for (int i = 0; i < item_count; i++)
|
|
|
|
if (items[i]->k == k)
|
|
|
|
return _Remove(i);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Find(const value &v)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
for (int i = 0; i < item_count; i++)
|
|
|
|
if (items[i]->v == v)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Has(const key &k)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < item_count; i++)
|
|
|
|
if (items[i]->k == k)
|
2002-07-09 16:24:59 +04:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2002-12-09 02:47:55 +03:00
|
|
|
|
|
|
|
bool IsEmpty()
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
return item_count == 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-12-09 02:47:55 +03:00
|
|
|
void MakeEmpty()
|
2002-08-13 00:10:24 +04:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
if (items != 0) {
|
|
|
|
for (int i = 0; i < item_count; i++) {
|
|
|
|
delete items[i];
|
2002-08-13 00:10:24 +04:00
|
|
|
}
|
2002-12-09 02:47:55 +03:00
|
|
|
item_count = 0;
|
|
|
|
}
|
2002-08-13 00:10:24 +04:00
|
|
|
}
|
|
|
|
|
2002-12-09 02:47:55 +03:00
|
|
|
void Rewind()
|
2002-08-13 00:10:24 +04:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
item_iter = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetNext(value **v)
|
|
|
|
{
|
|
|
|
item_iter++;
|
|
|
|
return _Get(item_iter, v);
|
2002-08-13 00:10:24 +04:00
|
|
|
}
|
|
|
|
|
2002-12-10 21:22:16 +03:00
|
|
|
bool GetCurrentKey(key **k)
|
|
|
|
{
|
|
|
|
if (item_iter < 0 || item_iter >= item_count)
|
|
|
|
return false;
|
|
|
|
*k = &items[item_iter]->k;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-12-09 02:47:55 +03:00
|
|
|
bool RemoveCurrent()
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
return _Remove(item_iter);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-12-09 02:47:55 +03:00
|
|
|
private:
|
|
|
|
bool _Get(int32 index, value **v)
|
2002-12-03 03:59:42 +03:00
|
|
|
{
|
2002-12-09 02:47:55 +03:00
|
|
|
if (index < 0 || index >= item_count)
|
|
|
|
return false;
|
|
|
|
*v = &items[index]->v;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _Remove(int32 index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= item_count)
|
|
|
|
return false;
|
|
|
|
delete items[index];
|
|
|
|
item_count--;
|
|
|
|
items[index] = items[item_count];
|
|
|
|
if (index == item_iter)
|
|
|
|
item_iter--;
|
|
|
|
return true;
|
2002-12-03 03:59:42 +03: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;
|
2002-07-09 16:24:59 +04:00
|
|
|
struct ent {
|
|
|
|
key k;
|
|
|
|
value v;
|
|
|
|
};
|
2002-12-09 02:47:55 +03:00
|
|
|
ent **items;
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2002-08-13 00:10:24 +04:00
|
|
|
#endif // _MEDIA_T_MAP_H
|