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
|
|
|
|
|
|
|
template<class key, class value> class Map
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Map() : count(0) {}
|
|
|
|
|
2002-12-03 03:59:42 +03:00
|
|
|
Map(const Map<key, value> &other)
|
|
|
|
{
|
|
|
|
printf("template<class key, class value> class Map copy constructor\n");
|
|
|
|
count = other.count;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
list[i].v = other.list[i].v;
|
|
|
|
list[i].k = other.list[i].k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-09 03:59:43 +04:00
|
|
|
bool Insert(const key &k, const value &v)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
value temp;
|
|
|
|
if (count == MAXENT) debugger("template Map out of memory");
|
|
|
|
if (Get(k, &temp)) debugger("template Map inserting duplicate key");
|
|
|
|
list[count].k = k;
|
|
|
|
list[count].v = v;
|
|
|
|
count++;
|
2002-10-09 03:59:43 +04:00
|
|
|
return true;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-08-13 00:10:24 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
bool Get(const key &k, value *v)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
if (list[i].k == k) {
|
|
|
|
*v = list[i].v;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// you can't Remove() while iterating through the map using GetAt()
|
|
|
|
bool GetAt(int32 index, value *v)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= count)
|
|
|
|
return false;
|
|
|
|
*v = list[index].v;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-08-13 00:10:24 +04:00
|
|
|
bool GetPointer(const key &k, value **v)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
if (list[i].k == k) {
|
|
|
|
*v = &(list[i].v);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// you can't Remove() while iterating through the map using GetAt()
|
|
|
|
bool GetPointerAt(int32 index, value **v)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= count)
|
|
|
|
return false;
|
|
|
|
*v = &(list[index].v);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// you can't Remove() while iterating through the map using GetAt()
|
|
|
|
bool Remove(const key &k)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
if (list[i].k == k) {
|
|
|
|
count--;
|
|
|
|
if (count > 0) {
|
|
|
|
list[i].v = list[count].v;
|
|
|
|
list[i].k = list[count].k;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-12-03 03:59:42 +03:00
|
|
|
bool IsEmpty()
|
|
|
|
{
|
|
|
|
return count == 0;
|
|
|
|
}
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
private:
|
|
|
|
enum { MAXENT = 64 };
|
|
|
|
struct ent {
|
|
|
|
key k;
|
|
|
|
value v;
|
|
|
|
};
|
|
|
|
ent list[MAXENT];
|
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
2002-08-13 00:10:24 +04:00
|
|
|
#endif // _MEDIA_T_MAP_H
|