summaryrefslogtreecommitdiff
path: root/cache.c
blob: eaab82919ff3d565621168961d70743e1893290b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * $Id: cache.c,v 1.1 2004/12/19 22:03:09 lordjaxom Exp $
 */

#include "cache.h"
#include "bitmap.h"

cText2SkinCache::cText2SkinCache(int MaxItems) {
	mMaxItems = MaxItems;
}

cText2SkinCache::~cText2SkinCache() {
	Flush();
}

void cText2SkinCache::Delete(const key_type &Key, data_type &Data) {
	delete Data;
}

void cText2SkinCache::Flush(void) {
	mUsage.clear();
	name_iterator it = mNames.begin();
	for (; it != mNames.end(); ++it) {
		item_iterator it2 = (*it).second.begin();
		for (; it2 != (*it).second.end(); ++it2)
			Delete((*it2).first, (*it2).second);
		(*it).second.clear();
	}
	mNames.clear();
}

void cText2SkinCache::Reset(void) {
	name_iterator it = mNames.begin();
	for (; it != mNames.end(); ++it) {
		item_iterator it2 = (*it).second.begin();
		for (; it2 != (*it).second.end(); ++it2)
			(*it2).second->Reset();
	}
}

cText2SkinCache::data_type &cText2SkinCache::operator[](const key_type &Key) {
	name_iterator it = mNames.find(Key.Filename);
	if (it != mNames.end()) {
		item_iterator it2 = (*it).second.find(Key);
		if (it2 != (*it).second.end()) {
			usage_iterator ut = mUsage.begin();
			for (; ut != mUsage.end(); ++ut) {
				if ((*ut) == Key) {
					mUsage.erase(ut);
					break;
				}
			}
			mUsage.push_back(Key);
			return (*it2).second;
		}
	}

	if (it == mNames.end())
		it = mNames.insert(name_map::value_type(Key.Filename, item_map())).first;

	if ((int)mUsage.size() == mMaxItems) {
		usage_iterator ut = mUsage.begin();
		Delete(*ut, (*it).second[*ut]);
		(*it).second.erase(*ut);
		if ((*it).second.size() == 0)
			mNames.erase((*ut).Filename);
		mUsage.erase(mUsage.begin());
	}

	item_iterator it2 = (*it).second.insert(item_map::value_type(Key, data_type())).first;
	mUsage.push_back(Key);
	return (*it2).second;
}