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_ 7 8 9 #include <new> 10 11 #include <util/OpenHashTable.h> 12 13 14 namespace BPackageKit { 15 16 namespace BHPKG { 17 18 namespace BPrivate { 19 20 21 uint32 hash_string(const char* string); 22 23 24 struct CachedString { 25 char* string; 26 int32 index; 27 uint32 usageCount; 28 CachedString* next; // hash table link 29 30 CachedString() 31 : 32 string(NULL), 33 index(-1), 34 usageCount(1) 35 { 36 } 37 38 ~CachedString() 39 { 40 free(string); 41 } 42 43 bool Init(const char* string) 44 { 45 this->string = strdup(string); 46 if (this->string == NULL) 47 return false; 48 49 return true; 50 } 51 }; 52 53 54 struct CachedStringHashDefinition { 55 typedef const char* KeyType; 56 typedef CachedString ValueType; 57 58 size_t HashKey(const char* key) const 59 { 60 return hash_string(key); 61 } 62 63 size_t Hash(const CachedString* value) const 64 { 65 return HashKey(value->string); 66 } 67 68 bool Compare(const char* key, const CachedString* value) const 69 { 70 return strcmp(value->string, key) == 0; 71 } 72 73 CachedString*& GetLink(CachedString* value) const 74 { 75 return value->next; 76 } 77 }; 78 79 80 typedef BOpenHashTable<CachedStringHashDefinition> CachedStringTable; 81 82 83 struct CachedStringUsageGreater { 84 bool operator()(const CachedString* a, const CachedString* b) 85 { 86 return a->usageCount > b->usageCount; 87 } 88 }; 89 90 91 struct StringCache : public CachedStringTable { 92 StringCache(); 93 ~StringCache(); 94 95 CachedString* Get(const char* value); 96 void Put(CachedString* string); 97 }; 98 99 100 } // namespace BPrivate 101 102 } // namespace BHPKG 103 104 } // namespace BPackageKit 105 106 107 #endif // _PACKAGE__HPKG__PRIVATE__STRINGS_H_ 108