btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / package / hpkg / Strings.h
blobdcd4da9e4d4b016646b42f7a85d8a644389a7e82
1 /*
2 * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _PACKAGE__HPKG__PRIVATE__STRINGS_H_
6 #define _PACKAGE__HPKG__PRIVATE__STRINGS_H_
9 #include <new>
11 #include <util/OpenHashTable.h>
14 namespace BPackageKit {
16 namespace BHPKG {
18 namespace BPrivate {
21 uint32 hash_string(const char* string);
24 struct CachedString {
25 char* string;
26 int32 index;
27 uint32 usageCount;
28 CachedString* next; // hash table link
30 CachedString()
32 string(NULL),
33 index(-1),
34 usageCount(1)
38 ~CachedString()
40 free(string);
43 bool Init(const char* string)
45 this->string = strdup(string);
46 if (this->string == NULL)
47 return false;
49 return true;
54 struct CachedStringHashDefinition {
55 typedef const char* KeyType;
56 typedef CachedString ValueType;
58 size_t HashKey(const char* key) const
60 return hash_string(key);
63 size_t Hash(const CachedString* value) const
65 return HashKey(value->string);
68 bool Compare(const char* key, const CachedString* value) const
70 return strcmp(value->string, key) == 0;
73 CachedString*& GetLink(CachedString* value) const
75 return value->next;
80 typedef BOpenHashTable<CachedStringHashDefinition> CachedStringTable;
83 struct CachedStringUsageGreater {
84 bool operator()(const CachedString* a, const CachedString* b)
86 return a->usageCount > b->usageCount;
91 struct StringCache : public CachedStringTable {
92 StringCache();
93 ~StringCache();
95 CachedString* Get(const char* value);
96 void Put(CachedString* string);
100 } // namespace BPrivate
102 } // namespace BHPKG
104 } // namespace BPackageKit
107 #endif // _PACKAGE__HPKG__PRIVATE__STRINGS_H_