Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #ifndef SkTDict_DEFINED |
michael@0 | 11 | #define SkTDict_DEFINED |
michael@0 | 12 | |
michael@0 | 13 | #include "SkChunkAlloc.h" |
michael@0 | 14 | #include "SkTSearch.h" |
michael@0 | 15 | #include "SkTDArray.h" |
michael@0 | 16 | |
michael@0 | 17 | template <typename T> class SkTDict : SkNoncopyable { |
michael@0 | 18 | public: |
michael@0 | 19 | SkTDict(size_t minStringAlloc) : fStrings(minStringAlloc) {} |
michael@0 | 20 | |
michael@0 | 21 | void reset() |
michael@0 | 22 | { |
michael@0 | 23 | fArray.reset(); |
michael@0 | 24 | fStrings.reset(); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | int count() const { return fArray.count(); } |
michael@0 | 28 | |
michael@0 | 29 | bool set(const char name[], const T& value) |
michael@0 | 30 | { |
michael@0 | 31 | return set(name, strlen(name), value); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | bool set(const char name[], size_t len, const T& value) |
michael@0 | 35 | { |
michael@0 | 36 | SkASSERT(name); |
michael@0 | 37 | |
michael@0 | 38 | int index = this->find_index(name, len); |
michael@0 | 39 | |
michael@0 | 40 | if (index >= 0) |
michael@0 | 41 | { |
michael@0 | 42 | fArray[index].fValue = value; |
michael@0 | 43 | return false; |
michael@0 | 44 | } |
michael@0 | 45 | else |
michael@0 | 46 | { |
michael@0 | 47 | Pair* pair = fArray.insert(~index); |
michael@0 | 48 | char* copy = (char*)fStrings.alloc(len + 1, SkChunkAlloc::kThrow_AllocFailType); |
michael@0 | 49 | memcpy(copy, name, len); |
michael@0 | 50 | copy[len] = '\0'; |
michael@0 | 51 | pair->fName = copy; |
michael@0 | 52 | pair->fValue = value; |
michael@0 | 53 | return true; |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | bool find(const char name[]) const |
michael@0 | 58 | { |
michael@0 | 59 | return this->find_index(name) >= 0; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | bool find(const char name[], size_t len) const |
michael@0 | 63 | { |
michael@0 | 64 | return this->find_index(name, len) >= 0; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | bool find(const char name[], T* value) const |
michael@0 | 68 | { |
michael@0 | 69 | return find(name, strlen(name), value); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | bool find(const char name[], size_t len, T* value) const |
michael@0 | 73 | { |
michael@0 | 74 | int index = this->find_index(name, len); |
michael@0 | 75 | |
michael@0 | 76 | if (index >= 0) |
michael@0 | 77 | { |
michael@0 | 78 | if (value) |
michael@0 | 79 | *value = fArray[index].fValue; |
michael@0 | 80 | return true; |
michael@0 | 81 | } |
michael@0 | 82 | return false; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | bool findKey(T& value, const char** name) const |
michael@0 | 86 | { |
michael@0 | 87 | const Pair* end = fArray.end(); |
michael@0 | 88 | for (const Pair* pair = fArray.begin(); pair < end; pair++) { |
michael@0 | 89 | if (pair->fValue != value) |
michael@0 | 90 | continue; |
michael@0 | 91 | *name = pair->fName; |
michael@0 | 92 | return true; |
michael@0 | 93 | } |
michael@0 | 94 | return false; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | public: |
michael@0 | 98 | struct Pair { |
michael@0 | 99 | const char* fName; |
michael@0 | 100 | T fValue; |
michael@0 | 101 | |
michael@0 | 102 | friend int operator<(const Pair& a, const Pair& b) |
michael@0 | 103 | { |
michael@0 | 104 | return strcmp(a.fName, b.fName); |
michael@0 | 105 | } |
michael@0 | 106 | friend int operator!=(const Pair& a, const Pair& b) |
michael@0 | 107 | { |
michael@0 | 108 | return strcmp(a.fName, b.fName); |
michael@0 | 109 | } |
michael@0 | 110 | }; |
michael@0 | 111 | friend class Iter; |
michael@0 | 112 | |
michael@0 | 113 | public: |
michael@0 | 114 | class Iter { |
michael@0 | 115 | public: |
michael@0 | 116 | Iter(const SkTDict<T>& dict) |
michael@0 | 117 | { |
michael@0 | 118 | fIter = dict.fArray.begin(); |
michael@0 | 119 | fStop = dict.fArray.end(); |
michael@0 | 120 | } |
michael@0 | 121 | const char* next(T* value) |
michael@0 | 122 | { |
michael@0 | 123 | const char* name = NULL; |
michael@0 | 124 | if (fIter < fStop) |
michael@0 | 125 | { |
michael@0 | 126 | name = fIter->fName; |
michael@0 | 127 | if (value) |
michael@0 | 128 | *value = fIter->fValue; |
michael@0 | 129 | fIter += 1; |
michael@0 | 130 | } |
michael@0 | 131 | return name; |
michael@0 | 132 | } |
michael@0 | 133 | private: |
michael@0 | 134 | const Pair* fIter; |
michael@0 | 135 | const Pair* fStop; |
michael@0 | 136 | }; |
michael@0 | 137 | |
michael@0 | 138 | private: |
michael@0 | 139 | SkTDArray<Pair> fArray; |
michael@0 | 140 | SkChunkAlloc fStrings; |
michael@0 | 141 | |
michael@0 | 142 | int find_index(const char name[]) const |
michael@0 | 143 | { |
michael@0 | 144 | return find_index(name, strlen(name)); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | int find_index(const char name[], size_t len) const |
michael@0 | 148 | { |
michael@0 | 149 | SkASSERT(name); |
michael@0 | 150 | |
michael@0 | 151 | int count = fArray.count(); |
michael@0 | 152 | int index = ~0; |
michael@0 | 153 | |
michael@0 | 154 | if (count) |
michael@0 | 155 | index = SkStrSearch(&fArray.begin()->fName, count, name, len, sizeof(Pair)); |
michael@0 | 156 | return index; |
michael@0 | 157 | } |
michael@0 | 158 | friend class Iter; |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | #endif |