10d6b3b20SOliver Tappe /* 2*3f40dcb6SIngo 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 110d6b3b20SOliver Tappe #include <util/OpenHashTable.h> 120d6b3b20SOliver Tappe 130d6b3b20SOliver Tappe 140d6b3b20SOliver Tappe namespace BPackageKit { 150d6b3b20SOliver Tappe 165fb1c6ffSOliver Tappe namespace BHPKG { 170d6b3b20SOliver Tappe 180d6b3b20SOliver Tappe namespace BPrivate { 190d6b3b20SOliver Tappe 200d6b3b20SOliver Tappe 210d6b3b20SOliver Tappe uint32 hash_string(const char* string); 220d6b3b20SOliver Tappe 230d6b3b20SOliver Tappe 240d6b3b20SOliver Tappe struct CachedString { 250d6b3b20SOliver Tappe char* string; 260d6b3b20SOliver Tappe int32 index; 270d6b3b20SOliver Tappe uint32 usageCount; 280d6b3b20SOliver Tappe CachedString* next; // hash table link 290d6b3b20SOliver Tappe 300d6b3b20SOliver Tappe CachedString() 310d6b3b20SOliver Tappe : 320d6b3b20SOliver Tappe string(NULL), 330d6b3b20SOliver Tappe index(-1), 340d6b3b20SOliver Tappe usageCount(1) 350d6b3b20SOliver Tappe { 360d6b3b20SOliver Tappe } 370d6b3b20SOliver Tappe 380d6b3b20SOliver Tappe ~CachedString() 390d6b3b20SOliver Tappe { 400d6b3b20SOliver Tappe free(string); 410d6b3b20SOliver Tappe } 420d6b3b20SOliver Tappe 430d6b3b20SOliver Tappe bool Init(const char* string) 440d6b3b20SOliver Tappe { 450d6b3b20SOliver Tappe this->string = strdup(string); 460d6b3b20SOliver Tappe if (this->string == NULL) 470d6b3b20SOliver Tappe return false; 480d6b3b20SOliver Tappe 490d6b3b20SOliver Tappe return true; 500d6b3b20SOliver Tappe } 510d6b3b20SOliver Tappe }; 520d6b3b20SOliver Tappe 530d6b3b20SOliver Tappe 540d6b3b20SOliver Tappe struct CachedStringHashDefinition { 550d6b3b20SOliver Tappe typedef const char* KeyType; 560d6b3b20SOliver Tappe typedef CachedString ValueType; 570d6b3b20SOliver Tappe 580d6b3b20SOliver Tappe size_t HashKey(const char* key) const 590d6b3b20SOliver Tappe { 600d6b3b20SOliver Tappe return hash_string(key); 610d6b3b20SOliver Tappe } 620d6b3b20SOliver Tappe 630d6b3b20SOliver Tappe size_t Hash(const CachedString* value) const 640d6b3b20SOliver Tappe { 650d6b3b20SOliver Tappe return HashKey(value->string); 660d6b3b20SOliver Tappe } 670d6b3b20SOliver Tappe 680d6b3b20SOliver Tappe bool Compare(const char* key, const CachedString* value) const 690d6b3b20SOliver Tappe { 700d6b3b20SOliver Tappe return strcmp(value->string, key) == 0; 710d6b3b20SOliver Tappe } 720d6b3b20SOliver Tappe 730d6b3b20SOliver Tappe CachedString*& GetLink(CachedString* value) const 740d6b3b20SOliver Tappe { 750d6b3b20SOliver Tappe return value->next; 760d6b3b20SOliver Tappe } 770d6b3b20SOliver Tappe }; 780d6b3b20SOliver Tappe 790d6b3b20SOliver Tappe 800d6b3b20SOliver Tappe typedef BOpenHashTable<CachedStringHashDefinition> CachedStringTable; 810d6b3b20SOliver Tappe 820d6b3b20SOliver Tappe 830d6b3b20SOliver Tappe struct CachedStringUsageGreater { 840d6b3b20SOliver Tappe bool operator()(const CachedString* a, const CachedString* b) 850d6b3b20SOliver Tappe { 860d6b3b20SOliver Tappe return a->usageCount > b->usageCount; 870d6b3b20SOliver Tappe } 880d6b3b20SOliver Tappe }; 890d6b3b20SOliver Tappe 900d6b3b20SOliver Tappe 91f2022173SOliver Tappe struct StringCache : public CachedStringTable { 92f2022173SOliver Tappe StringCache(); 93f2022173SOliver Tappe ~StringCache(); 94f2022173SOliver Tappe 95f2022173SOliver Tappe CachedString* Get(const char* value); 96*3f40dcb6SIngo Weinhold void Put(CachedString* string); 97f2022173SOliver Tappe }; 98f2022173SOliver Tappe 99f2022173SOliver Tappe 1000d6b3b20SOliver Tappe } // namespace BPrivate 1010d6b3b20SOliver Tappe 1025fb1c6ffSOliver Tappe } // namespace BHPKG 1030d6b3b20SOliver Tappe 1040d6b3b20SOliver Tappe } // namespace BPackageKit 1050d6b3b20SOliver Tappe 1060d6b3b20SOliver Tappe 1075fb1c6ffSOliver Tappe #endif // _PACKAGE__HPKG__PRIVATE__STRINGS_H_ 108