10d6b3b20SOliver Tappe /* 23f40dcb6SIngo Weinhold * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de. 30d6b3b20SOliver Tappe * Distributed under the terms of the MIT License. 40d6b3b20SOliver Tappe */ 55fb1c6ffSOliver Tappe #ifndef _PACKAGE__HPKG__PRIVATE__STRINGS_H_ 65fb1c6ffSOliver Tappe #define _PACKAGE__HPKG__PRIVATE__STRINGS_H_ 70d6b3b20SOliver Tappe 80d6b3b20SOliver Tappe 9f2022173SOliver Tappe #include <new> 10f2022173SOliver Tappe 11*e9254dd7SAugustin Cavalier #include <String.h> 120d6b3b20SOliver Tappe #include <util/OpenHashTable.h> 130d6b3b20SOliver Tappe 140d6b3b20SOliver Tappe 150d6b3b20SOliver Tappe namespace BPackageKit { 160d6b3b20SOliver Tappe 175fb1c6ffSOliver Tappe namespace BHPKG { 180d6b3b20SOliver Tappe 190d6b3b20SOliver Tappe namespace BPrivate { 200d6b3b20SOliver Tappe 210d6b3b20SOliver Tappe 220d6b3b20SOliver Tappe struct CachedString { 230d6b3b20SOliver Tappe char* string; 240d6b3b20SOliver Tappe int32 index; 250d6b3b20SOliver Tappe uint32 usageCount; 260d6b3b20SOliver Tappe CachedString* next; // hash table link 270d6b3b20SOliver Tappe CachedStringCachedString280d6b3b20SOliver Tappe CachedString() 290d6b3b20SOliver Tappe : 300d6b3b20SOliver Tappe string(NULL), 310d6b3b20SOliver Tappe index(-1), 320d6b3b20SOliver Tappe usageCount(1) 330d6b3b20SOliver Tappe { 340d6b3b20SOliver Tappe } 350d6b3b20SOliver Tappe ~CachedStringCachedString360d6b3b20SOliver Tappe ~CachedString() 370d6b3b20SOliver Tappe { 380d6b3b20SOliver Tappe free(string); 390d6b3b20SOliver Tappe } 400d6b3b20SOliver Tappe InitCachedString410d6b3b20SOliver Tappe bool Init(const char* string) 420d6b3b20SOliver Tappe { 430d6b3b20SOliver Tappe this->string = strdup(string); 440d6b3b20SOliver Tappe if (this->string == NULL) 450d6b3b20SOliver Tappe return false; 460d6b3b20SOliver Tappe 470d6b3b20SOliver Tappe return true; 480d6b3b20SOliver Tappe } 490d6b3b20SOliver Tappe }; 500d6b3b20SOliver Tappe 510d6b3b20SOliver Tappe 520d6b3b20SOliver Tappe struct CachedStringHashDefinition { 530d6b3b20SOliver Tappe typedef const char* KeyType; 540d6b3b20SOliver Tappe typedef CachedString ValueType; 550d6b3b20SOliver Tappe HashKeyCachedStringHashDefinition560d6b3b20SOliver Tappe size_t HashKey(const char* key) const 570d6b3b20SOliver Tappe { 58*e9254dd7SAugustin Cavalier return BString::HashValue(key); 590d6b3b20SOliver Tappe } 600d6b3b20SOliver Tappe HashCachedStringHashDefinition610d6b3b20SOliver Tappe size_t Hash(const CachedString* value) const 620d6b3b20SOliver Tappe { 630d6b3b20SOliver Tappe return HashKey(value->string); 640d6b3b20SOliver Tappe } 650d6b3b20SOliver Tappe CompareCachedStringHashDefinition660d6b3b20SOliver Tappe bool Compare(const char* key, const CachedString* value) const 670d6b3b20SOliver Tappe { 680d6b3b20SOliver Tappe return strcmp(value->string, key) == 0; 690d6b3b20SOliver Tappe } 700d6b3b20SOliver Tappe GetLinkCachedStringHashDefinition710d6b3b20SOliver Tappe CachedString*& GetLink(CachedString* value) const 720d6b3b20SOliver Tappe { 730d6b3b20SOliver Tappe return value->next; 740d6b3b20SOliver Tappe } 750d6b3b20SOliver Tappe }; 760d6b3b20SOliver Tappe 770d6b3b20SOliver Tappe 780d6b3b20SOliver Tappe typedef BOpenHashTable<CachedStringHashDefinition> CachedStringTable; 790d6b3b20SOliver Tappe 800d6b3b20SOliver Tappe 810d6b3b20SOliver Tappe struct CachedStringUsageGreater { operatorCachedStringUsageGreater820d6b3b20SOliver Tappe bool operator()(const CachedString* a, const CachedString* b) 830d6b3b20SOliver Tappe { 840d6b3b20SOliver Tappe return a->usageCount > b->usageCount; 850d6b3b20SOliver Tappe } 860d6b3b20SOliver Tappe }; 870d6b3b20SOliver Tappe 880d6b3b20SOliver Tappe 89f2022173SOliver Tappe struct StringCache : public CachedStringTable { 90f2022173SOliver Tappe StringCache(); 91f2022173SOliver Tappe ~StringCache(); 92f2022173SOliver Tappe 93f2022173SOliver Tappe CachedString* Get(const char* value); 943f40dcb6SIngo Weinhold void Put(CachedString* string); 95f2022173SOliver Tappe }; 96f2022173SOliver Tappe 97f2022173SOliver Tappe 980d6b3b20SOliver Tappe } // namespace BPrivate 990d6b3b20SOliver Tappe 1005fb1c6ffSOliver Tappe } // namespace BHPKG 1010d6b3b20SOliver Tappe 1020d6b3b20SOliver Tappe } // namespace BPackageKit 1030d6b3b20SOliver Tappe 1040d6b3b20SOliver Tappe 1055fb1c6ffSOliver Tappe #endif // _PACKAGE__HPKG__PRIVATE__STRINGS_H_ 106