2009-11-13 16:45:49 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2011-01-30 18:05:38 +03:00
|
|
|
#ifndef _PACKAGE__HPKG__PRIVATE__STRINGS_H_
|
|
|
|
#define _PACKAGE__HPKG__PRIVATE__STRINGS_H_
|
2009-11-13 16:45:49 +03:00
|
|
|
|
|
|
|
|
2011-02-08 01:25:16 +03:00
|
|
|
#include <new>
|
|
|
|
|
2009-11-13 16:45:49 +03:00
|
|
|
#include <util/OpenHashTable.h>
|
|
|
|
|
|
|
|
|
2011-01-28 02:17:03 +03:00
|
|
|
namespace BPackageKit {
|
|
|
|
|
2011-01-30 18:05:38 +03:00
|
|
|
namespace BHPKG {
|
2011-01-28 02:17:03 +03:00
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
|
2009-11-13 16:45:49 +03:00
|
|
|
uint32 hash_string(const char* string);
|
|
|
|
|
|
|
|
|
|
|
|
struct CachedString {
|
|
|
|
char* string;
|
|
|
|
int32 index;
|
|
|
|
uint32 usageCount;
|
|
|
|
CachedString* next; // hash table link
|
|
|
|
|
|
|
|
CachedString()
|
|
|
|
:
|
|
|
|
string(NULL),
|
|
|
|
index(-1),
|
|
|
|
usageCount(1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~CachedString()
|
|
|
|
{
|
|
|
|
free(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Init(const char* string)
|
|
|
|
{
|
|
|
|
this->string = strdup(string);
|
|
|
|
if (this->string == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CachedStringHashDefinition {
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef CachedString ValueType;
|
|
|
|
|
|
|
|
size_t HashKey(const char* key) const
|
|
|
|
{
|
|
|
|
return hash_string(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Hash(const CachedString* value) const
|
|
|
|
{
|
|
|
|
return HashKey(value->string);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Compare(const char* key, const CachedString* value) const
|
|
|
|
{
|
|
|
|
return strcmp(value->string, key) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedString*& GetLink(CachedString* value) const
|
|
|
|
{
|
|
|
|
return value->next;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef BOpenHashTable<CachedStringHashDefinition> CachedStringTable;
|
|
|
|
|
|
|
|
|
|
|
|
struct CachedStringUsageGreater {
|
|
|
|
bool operator()(const CachedString* a, const CachedString* b)
|
|
|
|
{
|
|
|
|
return a->usageCount > b->usageCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-08 01:25:16 +03:00
|
|
|
struct StringCache : public CachedStringTable {
|
|
|
|
StringCache();
|
|
|
|
~StringCache();
|
|
|
|
|
|
|
|
CachedString* Get(const char* value);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-28 02:17:03 +03:00
|
|
|
} // namespace BPrivate
|
|
|
|
|
2011-01-30 18:05:38 +03:00
|
|
|
} // namespace BHPKG
|
2011-01-28 02:17:03 +03:00
|
|
|
|
|
|
|
} // namespace BPackageKit
|
|
|
|
|
|
|
|
|
2011-01-30 18:05:38 +03:00
|
|
|
#endif // _PACKAGE__HPKG__PRIVATE__STRINGS_H_
|