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