diff options
author | chai <chaifix@163.com> | 2018-10-23 12:23:58 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-23 12:23:58 +0800 |
commit | 40fc27154fe754181934dc7ee31375e6bdfb33fc (patch) | |
tree | 897ad98d759bc308ef66561181ba88b85f2ccd47 /src/libjin/Common/StringMap.hpp | |
parent | 1480c9445100075c9e1a894eb07c0ef727b509a1 (diff) |
*merge from minimal
Diffstat (limited to 'src/libjin/Common/StringMap.hpp')
-rw-r--r-- | src/libjin/Common/StringMap.hpp | 143 |
1 files changed, 0 insertions, 143 deletions
diff --git a/src/libjin/Common/StringMap.hpp b/src/libjin/Common/StringMap.hpp deleted file mode 100644 index bebd94d..0000000 --- a/src/libjin/Common/StringMap.hpp +++ /dev/null @@ -1,143 +0,0 @@ -#ifndef __JIN_COMMON_SREINGMAP_H -#define __JIN_COMMON_SREINGMAP_H - -namespace jin -{ - - template<typename T, unsigned SIZE> - class StringMap - { - private: - - struct Record - { - const char * key; - T value; - bool set; - Record() : set(false) {} - }; - - const static unsigned MAX = SIZE * 2; - - Record records[MAX]; - const char * reverse[SIZE]; - - public: - - struct Entry - { - const char * key; - T value; - }; - - StringMap(Entry * entries, unsigned num) - { - - for (unsigned i = 0; i < SIZE; ++i) - reverse[i] = 0; - - unsigned n = num / sizeof(Entry); - - for (unsigned i = 0; i < n; ++i) - { - add(entries[i].key, entries[i].value); - } - } - - bool streq(const char * a, const char * b) - { - while (*a != 0 && *b != 0) - { - if (*a != *b) - return false; - ++a; - ++b; - } - - return (*a == 0 && *b == 0); - } - - bool find(const char * key, T & t) - { - //unsigned str_hash = djb2(key); - - for (unsigned i = 0; i < MAX; ++i) - { - //unsigned str_i = (str_hash + i) % MAX; //this isn't used, is this intentional? - - if (records[i].set && streq(records[i].key, key)) - { - t = records[i].value; - return true; - } - } - - return false; - } - - bool find(T key, const char *& str) - { - unsigned index = (unsigned)key; - - if (index >= SIZE) - return false; - - if (reverse[index] != 0) - { - str = reverse[index]; - return true; - } - else - { - return false; - } - } - - bool add(const char * key, T value) - { - unsigned str_hash = djb2(key); - bool inserted = false; - - for (unsigned i = 0; i < MAX; ++i) - { - unsigned str_i = (str_hash + i) % MAX; - - if (!records[str_i].set) - { - inserted = true; - records[str_i].set = true; - records[str_i].key = key; - records[str_i].value = value; - break; - } - } - - unsigned index = (unsigned)value; - - if (index >= SIZE) - { - printf("\nConstant %s out of bounds with %i!\n", key, index); - return false; - } - - reverse[index] = key; - - return inserted; - } - - unsigned djb2(const char * key) - { - unsigned hash = 5381; - int c; - - while ((c = *key++)) - hash = ((hash << 5) + hash) + c; - - return hash; - } - - }; // StringMap - -} - -#endif
\ No newline at end of file |