rewritten to provide better iteration functionality, etc.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2192 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
beveloper 2002-12-08 23:47:55 +00:00
parent 6a3b2694fd
commit 3ca7b9c952
2 changed files with 207 additions and 102 deletions

View File

@ -1,78 +1,126 @@
#ifndef _MEDIA_T_LIST_H #ifndef _MEDIA_T_LIST_H
#define _MEDIA_T_LIST_H #define _MEDIA_T_LIST_H
#include "debug.h"
template<class value> class List template<class value> class List
{ {
public: public:
List() : count(0) {} List()
: item_max(INIT_COUNT),
item_count(0),
item_iter(-1),
items((value **)malloc(sizeof(value *) * INIT_COUNT))
{
ASSERT(items);
}
~List()
{
}
List(const List<value> &other) List(const List<value> &other)
{ {
printf("template<class value> class List copy constructor\n"); *this = other;
count = other.count;
for (int i = 0; i < count; i++)
list[i] = other.list[i];
hmpt;
}
void Insert(const value &v)
{
value temp;
if (count == MAXENT) debugger("template List out of memory");
list[count] = v;
count++;
}
// you can't Remove() while iterating through the list using GetAt()
bool GetAt(int32 index, value *v)
{
if (index < 0 || index >= count)
return false;
*v = list[index];
return true;
} }
bool GetPointerAt(int32 index, value **v) List<value> &operator=(const List<value> &other)
{ {
if (index < 0 || index >= count) MakeEmpty();
return false; free(items);
*v = &list[index]; 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];
}
}
bool Insert(const value &v)
{
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++;
return true;
}
bool Get(int32 index, value **v)
{
if (index < 0 || index >= item_count)
return false;
*v = items[index];
return true; return true;
} }
// you can't Remove() while iterating through the map using GetAt()
bool Remove(int32 index) bool Remove(int32 index)
{ {
if (index < 0 || index >= count) if (index < 0 || index >= item_count)
return false; return false;
count--; delete items[index];
if (count > 0) item_count--;
list[index] = list[count]; items[index] = items[item_count];
if (index == item_iter)
item_iter--;
return true; return true;
} }
int Find(const value &v) int Find(const value &v)
{ {
for (int i = 0; i < count; i++) for (int i = 0; i < item_count; i++)
if (list[i] == v) if (*items[i] == v)
return i; return i;
return -1; return -1;
} }
int CountItems()
{
return item_count;
}
bool IsEmpty() bool IsEmpty()
{ {
return count == 0; return item_count == 0;
} }
void MakeEmpty() void MakeEmpty()
{ {
count = 0; 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);
} }
private: private:
enum { MAXENT = 64 }; enum { INIT_COUNT=32 };
value list[MAXENT]; int item_max;
int count; int item_count;
int item_iter;
value **items;
}; };
#endif // _MEDIA_T_LIST_H #endif // _MEDIA_T_LIST_H

View File

@ -1,98 +1,155 @@
#ifndef _MEDIA_T_MAP_H #ifndef _MEDIA_T_MAP_H
#define _MEDIA_T_MAP_H #define _MEDIA_T_MAP_H
#include "debug.h"
template<class key, class value> class Map template<class key, class value> class Map
{ {
public: public:
Map() : count(0) {} Map()
: item_max(INIT_COUNT),
item_count(0),
item_iter(-1),
items((ent **)malloc(sizeof(ent *) * INIT_COUNT))
{
ASSERT(items);
}
~Map()
{
}
Map(const Map<key, value> &other) Map(const Map<key, value> &other)
{ {
printf("template<class key, class value> class Map copy constructor\n"); *this = other;
count = other.count; }
for (int i = 0; i < count; i++) {
list[i].v = other.list[i].v; Map<key, value> &operator=(const Map<key, value> &other)
list[i].k = other.list[i].k; {
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;
}
}
bool Insert(const key &k, const value &v)
{
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;
bool Insert(const key &k, const value &v) items[item_count]->v = v;
{ item_count++;
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; return true;
} }
bool Get(const key &k, value *v) bool Get(const key &k, value **v)
{ {
for (int i = 0; i < count; i++) for (int i = 0; i < item_count; i++) {
if (list[i].k == k) { if (items[i]->k == k) {
*v = list[i].v; *v = &items[i]->v;
return true; return true;
} }
}
return false;
}
bool Remove(const key &k) {
for (int i = 0; i < item_count; i++)
if (items[i]->k == k)
return _Remove(i);
return false; return false;
} }
// you can't Remove() while iterating through the map using GetAt() int Find(const value &v)
bool GetAt(int32 index, value *v)
{ {
if (index < 0 || index >= count) for (int i = 0; i < item_count; i++)
return false; if (items[i]->v == v)
*v = list[index].v; return i;
return true; return -1;
} }
bool GetPointer(const key &k, value **v) bool Has(const key &k)
{ {
for (int i = 0; i < count; i++) for (int i = 0; i < item_count; i++)
if (list[i].k == k) { if (items[i]->k == k)
*v = &(list[i].v);
return true; 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; return false;
} }
bool IsEmpty() bool IsEmpty()
{ {
return count == 0; return item_count == 0;
}
void MakeEmpty()
{
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);
}
private:
bool _Get(int32 index, value **v)
{
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;
} }
private: private:
enum { MAXENT = 64 }; enum { INIT_COUNT=32 };
int item_max;
int item_count;
int item_iter;
struct ent { struct ent {
key k; key k;
value v; value v;
}; };
ent list[MAXENT]; ent **items;
int count;
}; };
#endif // _MEDIA_T_MAP_H #endif // _MEDIA_T_MAP_H