19b9d18dcSbeveloper #ifndef _MEDIA_T_MAP_H 29b9d18dcSbeveloper #define _MEDIA_T_MAP_H 352a38012Sejakowatz 43ca7b9c9Sbeveloper 5*b84955d4SBarrett17 #include "MediaDebug.h" 63ca7b9c9Sbeveloper 752a38012Sejakowatz template<class key, class value> class Map 852a38012Sejakowatz { 952a38012Sejakowatz public: Map()103ca7b9c9Sbeveloper Map() 113ca7b9c9Sbeveloper : item_max(INIT_COUNT), 123ca7b9c9Sbeveloper item_count(0), 133ca7b9c9Sbeveloper item_iter(-1), 143ca7b9c9Sbeveloper items((ent **)malloc(sizeof(ent *) * INIT_COUNT)) 153ca7b9c9Sbeveloper { 163ca7b9c9Sbeveloper ASSERT(items); 173ca7b9c9Sbeveloper } 183ca7b9c9Sbeveloper ~Map()193ca7b9c9Sbeveloper ~Map() 203ca7b9c9Sbeveloper { 21aef8495fSbeveloper MakeEmpty(); 22aef8495fSbeveloper free(items); 233ca7b9c9Sbeveloper } 2452a38012Sejakowatz Map(const Map<key,value> & other)25353b9f6bSbeveloper Map(const Map<key, value> &other) 26353b9f6bSbeveloper { 273ca7b9c9Sbeveloper *this = other; 283ca7b9c9Sbeveloper } 293ca7b9c9Sbeveloper 303ca7b9c9Sbeveloper Map<key, value> &operator=(const Map<key, value> &other) 313ca7b9c9Sbeveloper { 323ca7b9c9Sbeveloper MakeEmpty(); 333ca7b9c9Sbeveloper free(items); 343ca7b9c9Sbeveloper item_max = other.item_max; 353ca7b9c9Sbeveloper item_count = other.item_count; 363ca7b9c9Sbeveloper items = (ent **)malloc(sizeof(ent *) * item_max); 373ca7b9c9Sbeveloper ASSERT(items); 383ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) { 393ca7b9c9Sbeveloper items[i] = new ent; 403ca7b9c9Sbeveloper items[i]->k = other.items[i]->k; 413ca7b9c9Sbeveloper items[i]->v = other.items[i]->v; 42353b9f6bSbeveloper } 435aab162dSbeveloper return *this; 44353b9f6bSbeveloper } 45353b9f6bSbeveloper Insert(const key & k,const value & v)461299bfb2Sbeveloper bool Insert(const key &k, const value &v) 4752a38012Sejakowatz { 483ca7b9c9Sbeveloper if (item_count == item_max) { 493ca7b9c9Sbeveloper item_max *= 2; 503ca7b9c9Sbeveloper items = (ent **)realloc(items, sizeof(ent *) * item_max); 513ca7b9c9Sbeveloper ASSERT(items); 523ca7b9c9Sbeveloper } 533ca7b9c9Sbeveloper items[item_count] = new ent; 543ca7b9c9Sbeveloper items[item_count]->k = k; 553ca7b9c9Sbeveloper items[item_count]->v = v; 563ca7b9c9Sbeveloper item_count++; 571299bfb2Sbeveloper return true; 5852a38012Sejakowatz } 5952a38012Sejakowatz Get(const key & k,value ** v)60fc601b54SDario Casalinuovo bool Get(const key &k, value **v) const 6152a38012Sejakowatz { 623ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) { 633ca7b9c9Sbeveloper if (items[i]->k == k) { 643ca7b9c9Sbeveloper *v = &items[i]->v; 6552a38012Sejakowatz return true; 6652a38012Sejakowatz } 673ca7b9c9Sbeveloper } 6852a38012Sejakowatz return false; 6952a38012Sejakowatz } 7052a38012Sejakowatz Remove(const key & k)713ca7b9c9Sbeveloper bool Remove(const key &k) { 723ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) 733ca7b9c9Sbeveloper if (items[i]->k == k) 743ca7b9c9Sbeveloper return _Remove(i); 759b9d18dcSbeveloper return false; 769b9d18dcSbeveloper } 779b9d18dcSbeveloper Find(const value & v)78fc601b54SDario Casalinuovo int Find(const value &v) const 799b9d18dcSbeveloper { 803ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) 813ca7b9c9Sbeveloper if (items[i]->v == v) 823ca7b9c9Sbeveloper return i; 833ca7b9c9Sbeveloper return -1; 849b9d18dcSbeveloper } 859b9d18dcSbeveloper Has(const key & k)86fc601b54SDario Casalinuovo bool Has(const key &k) const 8752a38012Sejakowatz { 883ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) 893ca7b9c9Sbeveloper if (items[i]->k == k) 9052a38012Sejakowatz return true; 9152a38012Sejakowatz return false; 9252a38012Sejakowatz } 9352a38012Sejakowatz CountItems()94fc601b54SDario Casalinuovo int CountItems() const 95adcc5beaSJérôme Duval { 96adcc5beaSJérôme Duval return item_count; 97adcc5beaSJérôme Duval } 98adcc5beaSJérôme Duval IsEmpty()99fc601b54SDario Casalinuovo bool IsEmpty() const 100353b9f6bSbeveloper { 1013ca7b9c9Sbeveloper return item_count == 0; 1023ca7b9c9Sbeveloper } 1033ca7b9c9Sbeveloper MakeEmpty()1043ca7b9c9Sbeveloper void MakeEmpty() 1053ca7b9c9Sbeveloper { 1063ca7b9c9Sbeveloper if (items != 0) { 1073ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) { 1083ca7b9c9Sbeveloper delete items[i]; 1093ca7b9c9Sbeveloper } 1103ca7b9c9Sbeveloper item_count = 0; 1113ca7b9c9Sbeveloper } 1123ca7b9c9Sbeveloper } 1133ca7b9c9Sbeveloper Rewind()1143ca7b9c9Sbeveloper void Rewind() 1153ca7b9c9Sbeveloper { 1163ca7b9c9Sbeveloper item_iter = -1; 1173ca7b9c9Sbeveloper } 1183ca7b9c9Sbeveloper GetNext(value ** v)1193ca7b9c9Sbeveloper bool GetNext(value **v) 1203ca7b9c9Sbeveloper { 1213ca7b9c9Sbeveloper item_iter++; 1223ca7b9c9Sbeveloper return _Get(item_iter, v); 1233ca7b9c9Sbeveloper } 1243ca7b9c9Sbeveloper GetCurrentKey(key ** k)1257ad0981bSbeveloper bool GetCurrentKey(key **k) 1267ad0981bSbeveloper { 1277ad0981bSbeveloper if (item_iter < 0 || item_iter >= item_count) 1287ad0981bSbeveloper return false; 1297ad0981bSbeveloper *k = &items[item_iter]->k; 1307ad0981bSbeveloper return true; 1317ad0981bSbeveloper } 1327ad0981bSbeveloper RemoveCurrent()1333ca7b9c9Sbeveloper bool RemoveCurrent() 1343ca7b9c9Sbeveloper { 1353ca7b9c9Sbeveloper return _Remove(item_iter); 136353b9f6bSbeveloper } 137353b9f6bSbeveloper 13852a38012Sejakowatz private: _Get(int32 index,value ** v)139fc601b54SDario Casalinuovo bool _Get(int32 index, value **v) const 1403ca7b9c9Sbeveloper { 1413ca7b9c9Sbeveloper if (index < 0 || index >= item_count) 1423ca7b9c9Sbeveloper return false; 1433ca7b9c9Sbeveloper *v = &items[index]->v; 1443ca7b9c9Sbeveloper return true; 1453ca7b9c9Sbeveloper } 1463ca7b9c9Sbeveloper _Remove(int32 index)1473ca7b9c9Sbeveloper bool _Remove(int32 index) 1483ca7b9c9Sbeveloper { 1493ca7b9c9Sbeveloper if (index < 0 || index >= item_count) 1503ca7b9c9Sbeveloper return false; 1513ca7b9c9Sbeveloper delete items[index]; 1523ca7b9c9Sbeveloper item_count--; 15334068836SStephan Aßmus items[index] = items[item_count]; 15434068836SStephan Aßmus if (index == item_iter) 15534068836SStephan Aßmus item_iter--; 1563ca7b9c9Sbeveloper return true; 1573ca7b9c9Sbeveloper } 1583ca7b9c9Sbeveloper 1593ca7b9c9Sbeveloper private: 1603ca7b9c9Sbeveloper enum { INIT_COUNT=32 }; 1613ca7b9c9Sbeveloper int item_max; 1623ca7b9c9Sbeveloper int item_count; 1633ca7b9c9Sbeveloper int item_iter; 16452a38012Sejakowatz struct ent { 16552a38012Sejakowatz key k; 16652a38012Sejakowatz value v; 16752a38012Sejakowatz }; 1683ca7b9c9Sbeveloper ent **items; 16952a38012Sejakowatz }; 17052a38012Sejakowatz 1719b9d18dcSbeveloper #endif // _MEDIA_T_MAP_H 172