TMap.h revision b84955d4
1#ifndef _MEDIA_T_MAP_H
2#define _MEDIA_T_MAP_H
3
4
5#include "MediaDebug.h"
6
7template<class key, class value> class Map
8{
9public:
10	Map()
11	 :	item_max(INIT_COUNT),
12	 	item_count(0),
13	 	item_iter(-1),
14	 	items((ent **)malloc(sizeof(ent *) * INIT_COUNT))
15	{
16		ASSERT(items);
17	}
18
19	~Map()
20	{
21		MakeEmpty();
22		free(items);
23	}
24
25	Map(const Map<key, value> &other)
26	{
27		*this = other;
28	}
29
30	Map<key, value> &operator=(const Map<key, value> &other)
31	{
32		MakeEmpty();
33		free(items);
34		item_max = other.item_max;
35	 	item_count = other.item_count;
36		items = (ent **)malloc(sizeof(ent *) * item_max);
37		ASSERT(items);
38	 	for (int i = 0; i < item_count; i++) {
39	 		items[i] = new ent;
40	 		items[i]->k = other.items[i]->k;
41	 		items[i]->v = other.items[i]->v;
42	 	}
43	 	return *this;
44	}
45
46	bool Insert(const key &k, const value &v)
47	{
48		if (item_count == item_max) {
49			item_max *= 2;
50			items = (ent **)realloc(items, sizeof(ent *) * item_max);
51			ASSERT(items);
52		}
53		items[item_count] = new ent;
54		items[item_count]->k = k;
55		items[item_count]->v = v;
56		item_count++;
57		return true;
58	}
59
60	bool Get(const key &k, value **v) const
61	{
62	 	for (int i = 0; i < item_count; i++) {
63	 		if (items[i]->k == k) {
64	 			*v = &items[i]->v;
65	 			return true;
66	 		}
67	 	}
68		return false;
69	}
70
71	bool Remove(const key &k) {
72		for (int i = 0; i < item_count; i++)
73			if (items[i]->k == k)
74				return _Remove(i);
75		return false;
76	}
77
78	int Find(const value &v) const
79	{
80		for (int i = 0; i < item_count; i++)
81			if (items[i]->v == v)
82				return i;
83		return -1;
84	}
85
86	bool Has(const key &k) const
87	{
88		for (int i = 0; i < item_count; i++)
89			if (items[i]->k == k)
90				return true;
91		return false;
92	}
93
94	int CountItems() const
95	{
96		return item_count;
97	}
98
99	bool IsEmpty() const
100	{
101		return item_count == 0;
102	}
103
104	void MakeEmpty()
105	{
106		if (items != 0) {
107			for (int i = 0; i < item_count; i++) {
108				delete items[i];
109			}
110			item_count = 0;
111		}
112	}
113
114	void Rewind()
115	{
116		item_iter = -1;
117	}
118
119	bool GetNext(value **v)
120	{
121		item_iter++;
122		return _Get(item_iter, v);
123	}
124
125	bool GetCurrentKey(key **k)
126	{
127		if (item_iter < 0 || item_iter >= item_count)
128			return false;
129		*k = &items[item_iter]->k;
130		return true;
131	}
132
133	bool RemoveCurrent()
134	{
135		return _Remove(item_iter);
136	}
137
138private:
139	bool _Get(int32 index, value **v) const
140	{
141		if (index < 0 || index >= item_count)
142			return false;
143		*v = &items[index]->v;
144		return true;
145	}
146
147	bool _Remove(int32 index)
148	{
149		if (index < 0 || index >= item_count)
150			return false;
151		delete items[index];
152		item_count--;
153		items[index] = items[item_count];
154		if (index == item_iter)
155			item_iter--;
156		return true;
157	}
158
159private:
160	enum { INIT_COUNT=32 };
161	int item_max;
162	int item_count;
163	int item_iter;
164	struct ent {
165		key k;
166		value v;
167	};
168	ent **items;
169};
170
171#endif // _MEDIA_T_MAP_H
172