19b9d18dcSbeveloper #ifndef _MEDIA_T_MAP_H 29b9d18dcSbeveloper #define _MEDIA_T_MAP_H 352a38012Sejakowatz 43ca7b9c9Sbeveloper 53ca7b9c9Sbeveloper #include "debug.h" 63ca7b9c9Sbeveloper 752a38012Sejakowatz template<class key, class value> class Map 852a38012Sejakowatz { 952a38012Sejakowatz public: 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 193ca7b9c9Sbeveloper ~Map() 203ca7b9c9Sbeveloper { 213ca7b9c9Sbeveloper } 2252a38012Sejakowatz 23353b9f6bSbeveloper Map(const Map<key, value> &other) 24353b9f6bSbeveloper { 253ca7b9c9Sbeveloper *this = other; 263ca7b9c9Sbeveloper } 273ca7b9c9Sbeveloper 283ca7b9c9Sbeveloper Map<key, value> &operator=(const Map<key, value> &other) 293ca7b9c9Sbeveloper { 303ca7b9c9Sbeveloper MakeEmpty(); 313ca7b9c9Sbeveloper free(items); 323ca7b9c9Sbeveloper item_max = other.item_max; 333ca7b9c9Sbeveloper item_count = other.item_count; 343ca7b9c9Sbeveloper items = (ent **)malloc(sizeof(ent *) * item_max); 353ca7b9c9Sbeveloper ASSERT(items); 363ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) { 373ca7b9c9Sbeveloper items[i] = new ent; 383ca7b9c9Sbeveloper items[i]->k = other.items[i]->k; 393ca7b9c9Sbeveloper items[i]->v = other.items[i]->v; 40353b9f6bSbeveloper } 41353b9f6bSbeveloper } 42353b9f6bSbeveloper 431299bfb2Sbeveloper bool Insert(const key &k, const value &v) 4452a38012Sejakowatz { 453ca7b9c9Sbeveloper if (item_count == item_max) { 463ca7b9c9Sbeveloper item_max *= 2; 473ca7b9c9Sbeveloper items = (ent **)realloc(items, sizeof(ent *) * item_max); 483ca7b9c9Sbeveloper ASSERT(items); 493ca7b9c9Sbeveloper } 503ca7b9c9Sbeveloper items[item_count] = new ent; 513ca7b9c9Sbeveloper items[item_count]->k = k; 523ca7b9c9Sbeveloper items[item_count]->v = v; 533ca7b9c9Sbeveloper item_count++; 541299bfb2Sbeveloper return true; 5552a38012Sejakowatz } 5652a38012Sejakowatz 573ca7b9c9Sbeveloper bool Get(const key &k, value **v) 5852a38012Sejakowatz { 593ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) { 603ca7b9c9Sbeveloper if (items[i]->k == k) { 613ca7b9c9Sbeveloper *v = &items[i]->v; 6252a38012Sejakowatz return true; 6352a38012Sejakowatz } 643ca7b9c9Sbeveloper } 6552a38012Sejakowatz return false; 6652a38012Sejakowatz } 6752a38012Sejakowatz 683ca7b9c9Sbeveloper bool Remove(const key &k) { 693ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) 703ca7b9c9Sbeveloper if (items[i]->k == k) 713ca7b9c9Sbeveloper return _Remove(i); 729b9d18dcSbeveloper return false; 739b9d18dcSbeveloper } 749b9d18dcSbeveloper 753ca7b9c9Sbeveloper int Find(const value &v) 769b9d18dcSbeveloper { 773ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) 783ca7b9c9Sbeveloper if (items[i]->v == v) 793ca7b9c9Sbeveloper return i; 803ca7b9c9Sbeveloper return -1; 819b9d18dcSbeveloper } 829b9d18dcSbeveloper 833ca7b9c9Sbeveloper bool Has(const key &k) 8452a38012Sejakowatz { 853ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) 863ca7b9c9Sbeveloper if (items[i]->k == k) 8752a38012Sejakowatz return true; 8852a38012Sejakowatz return false; 8952a38012Sejakowatz } 9052a38012Sejakowatz 91353b9f6bSbeveloper bool IsEmpty() 92353b9f6bSbeveloper { 933ca7b9c9Sbeveloper return item_count == 0; 943ca7b9c9Sbeveloper } 953ca7b9c9Sbeveloper 963ca7b9c9Sbeveloper void MakeEmpty() 973ca7b9c9Sbeveloper { 983ca7b9c9Sbeveloper if (items != 0) { 993ca7b9c9Sbeveloper for (int i = 0; i < item_count; i++) { 1003ca7b9c9Sbeveloper delete items[i]; 1013ca7b9c9Sbeveloper } 1023ca7b9c9Sbeveloper item_count = 0; 1033ca7b9c9Sbeveloper } 1043ca7b9c9Sbeveloper } 1053ca7b9c9Sbeveloper 1063ca7b9c9Sbeveloper void Rewind() 1073ca7b9c9Sbeveloper { 1083ca7b9c9Sbeveloper item_iter = -1; 1093ca7b9c9Sbeveloper } 1103ca7b9c9Sbeveloper 1113ca7b9c9Sbeveloper bool GetNext(value **v) 1123ca7b9c9Sbeveloper { 1133ca7b9c9Sbeveloper item_iter++; 1143ca7b9c9Sbeveloper return _Get(item_iter, v); 1153ca7b9c9Sbeveloper } 1163ca7b9c9Sbeveloper 117*7ad0981bSbeveloper bool GetCurrentKey(key **k) 118*7ad0981bSbeveloper { 119*7ad0981bSbeveloper if (item_iter < 0 || item_iter >= item_count) 120*7ad0981bSbeveloper return false; 121*7ad0981bSbeveloper *k = &items[item_iter]->k; 122*7ad0981bSbeveloper return true; 123*7ad0981bSbeveloper } 124*7ad0981bSbeveloper 1253ca7b9c9Sbeveloper bool RemoveCurrent() 1263ca7b9c9Sbeveloper { 1273ca7b9c9Sbeveloper return _Remove(item_iter); 128353b9f6bSbeveloper } 129353b9f6bSbeveloper 13052a38012Sejakowatz private: 1313ca7b9c9Sbeveloper bool _Get(int32 index, value **v) 1323ca7b9c9Sbeveloper { 1333ca7b9c9Sbeveloper if (index < 0 || index >= item_count) 1343ca7b9c9Sbeveloper return false; 1353ca7b9c9Sbeveloper *v = &items[index]->v; 1363ca7b9c9Sbeveloper return true; 1373ca7b9c9Sbeveloper } 1383ca7b9c9Sbeveloper 1393ca7b9c9Sbeveloper bool _Remove(int32 index) 1403ca7b9c9Sbeveloper { 1413ca7b9c9Sbeveloper if (index < 0 || index >= item_count) 1423ca7b9c9Sbeveloper return false; 1433ca7b9c9Sbeveloper delete items[index]; 1443ca7b9c9Sbeveloper item_count--; 1453ca7b9c9Sbeveloper items[index] = items[item_count]; 1463ca7b9c9Sbeveloper if (index == item_iter) 1473ca7b9c9Sbeveloper item_iter--; 1483ca7b9c9Sbeveloper return true; 1493ca7b9c9Sbeveloper } 1503ca7b9c9Sbeveloper 1513ca7b9c9Sbeveloper private: 1523ca7b9c9Sbeveloper enum { INIT_COUNT=32 }; 1533ca7b9c9Sbeveloper int item_max; 1543ca7b9c9Sbeveloper int item_count; 1553ca7b9c9Sbeveloper int item_iter; 15652a38012Sejakowatz struct ent { 15752a38012Sejakowatz key k; 15852a38012Sejakowatz value v; 15952a38012Sejakowatz }; 1603ca7b9c9Sbeveloper ent **items; 16152a38012Sejakowatz }; 16252a38012Sejakowatz 1639b9d18dcSbeveloper #endif // _MEDIA_T_MAP_H 164