xref: /haiku/headers/private/package/hpkg/Strings.h (revision 0d6b3b20e15fe2cbeb5e81749f888c26ca289338)
1*0d6b3b20SOliver Tappe /*
2*0d6b3b20SOliver Tappe  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3*0d6b3b20SOliver Tappe  * Distributed under the terms of the MIT License.
4*0d6b3b20SOliver Tappe  */
5*0d6b3b20SOliver Tappe #ifndef STRINGS_H
6*0d6b3b20SOliver Tappe #define STRINGS_H
7*0d6b3b20SOliver Tappe 
8*0d6b3b20SOliver Tappe 
9*0d6b3b20SOliver Tappe #include <util/OpenHashTable.h>
10*0d6b3b20SOliver Tappe 
11*0d6b3b20SOliver Tappe 
12*0d6b3b20SOliver Tappe namespace BPackageKit {
13*0d6b3b20SOliver Tappe 
14*0d6b3b20SOliver Tappe namespace BHaikuPackage {
15*0d6b3b20SOliver Tappe 
16*0d6b3b20SOliver Tappe namespace BPrivate {
17*0d6b3b20SOliver Tappe 
18*0d6b3b20SOliver Tappe 
19*0d6b3b20SOliver Tappe uint32	hash_string(const char* string);
20*0d6b3b20SOliver Tappe 
21*0d6b3b20SOliver Tappe 
22*0d6b3b20SOliver Tappe struct CachedString {
23*0d6b3b20SOliver Tappe 	char*			string;
24*0d6b3b20SOliver Tappe 	int32			index;
25*0d6b3b20SOliver Tappe 	uint32			usageCount;
26*0d6b3b20SOliver Tappe 	CachedString*	next;	// hash table link
27*0d6b3b20SOliver Tappe 
28*0d6b3b20SOliver Tappe 	CachedString()
29*0d6b3b20SOliver Tappe 		:
30*0d6b3b20SOliver Tappe 		string(NULL),
31*0d6b3b20SOliver Tappe 		index(-1),
32*0d6b3b20SOliver Tappe 		usageCount(1)
33*0d6b3b20SOliver Tappe 	{
34*0d6b3b20SOliver Tappe 	}
35*0d6b3b20SOliver Tappe 
36*0d6b3b20SOliver Tappe 	~CachedString()
37*0d6b3b20SOliver Tappe 	{
38*0d6b3b20SOliver Tappe 		free(string);
39*0d6b3b20SOliver Tappe 	}
40*0d6b3b20SOliver Tappe 
41*0d6b3b20SOliver Tappe 	bool Init(const char* string)
42*0d6b3b20SOliver Tappe 	{
43*0d6b3b20SOliver Tappe 		this->string = strdup(string);
44*0d6b3b20SOliver Tappe 		if (this->string == NULL)
45*0d6b3b20SOliver Tappe 			return false;
46*0d6b3b20SOliver Tappe 
47*0d6b3b20SOliver Tappe 		return true;
48*0d6b3b20SOliver Tappe 	}
49*0d6b3b20SOliver Tappe };
50*0d6b3b20SOliver Tappe 
51*0d6b3b20SOliver Tappe 
52*0d6b3b20SOliver Tappe struct CachedStringHashDefinition {
53*0d6b3b20SOliver Tappe 	typedef const char*		KeyType;
54*0d6b3b20SOliver Tappe 	typedef	CachedString	ValueType;
55*0d6b3b20SOliver Tappe 
56*0d6b3b20SOliver Tappe 	size_t HashKey(const char* key) const
57*0d6b3b20SOliver Tappe 	{
58*0d6b3b20SOliver Tappe 		return hash_string(key);
59*0d6b3b20SOliver Tappe 	}
60*0d6b3b20SOliver Tappe 
61*0d6b3b20SOliver Tappe 	size_t Hash(const CachedString* value) const
62*0d6b3b20SOliver Tappe 	{
63*0d6b3b20SOliver Tappe 		return HashKey(value->string);
64*0d6b3b20SOliver Tappe 	}
65*0d6b3b20SOliver Tappe 
66*0d6b3b20SOliver Tappe 	bool Compare(const char* key, const CachedString* value) const
67*0d6b3b20SOliver Tappe 	{
68*0d6b3b20SOliver Tappe 		return strcmp(value->string, key) == 0;
69*0d6b3b20SOliver Tappe 	}
70*0d6b3b20SOliver Tappe 
71*0d6b3b20SOliver Tappe 	CachedString*& GetLink(CachedString* value) const
72*0d6b3b20SOliver Tappe 	{
73*0d6b3b20SOliver Tappe 		return value->next;
74*0d6b3b20SOliver Tappe 	}
75*0d6b3b20SOliver Tappe };
76*0d6b3b20SOliver Tappe 
77*0d6b3b20SOliver Tappe 
78*0d6b3b20SOliver Tappe typedef BOpenHashTable<CachedStringHashDefinition> CachedStringTable;
79*0d6b3b20SOliver Tappe 
80*0d6b3b20SOliver Tappe 
81*0d6b3b20SOliver Tappe struct CachedStringUsageGreater {
82*0d6b3b20SOliver Tappe 	bool operator()(const CachedString* a, const CachedString* b)
83*0d6b3b20SOliver Tappe 	{
84*0d6b3b20SOliver Tappe 		return a->usageCount > b->usageCount;
85*0d6b3b20SOliver Tappe 	}
86*0d6b3b20SOliver Tappe };
87*0d6b3b20SOliver Tappe 
88*0d6b3b20SOliver Tappe 
89*0d6b3b20SOliver Tappe }	// namespace BPrivate
90*0d6b3b20SOliver Tappe 
91*0d6b3b20SOliver Tappe }	// namespace BHaikuPackage
92*0d6b3b20SOliver Tappe 
93*0d6b3b20SOliver Tappe }	// namespace BPackageKit
94*0d6b3b20SOliver Tappe 
95*0d6b3b20SOliver Tappe 
96*0d6b3b20SOliver Tappe #endif	// STRINGS_H
97