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