haiku/headers/private/media/TMap.h

99 lines
1.8 KiB
C
Raw Normal View History

#ifndef _MEDIA_T_MAP_H
#define _MEDIA_T_MAP_H
template<class key, class value> class Map
{
public:
Map() : count(0) {}
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;
}
}
bool Insert(const key &k, const value &v)
{
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++;
return true;
}
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;
}
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;
}
// 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;
}
bool IsEmpty()
{
return count == 0;
}
private:
enum { MAXENT = 64 };
struct ent {
key k;
value v;
};
ent list[MAXENT];
int count;
};
#endif // _MEDIA_T_MAP_H