1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkTDict.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#ifndef SkTDict_DEFINED 1.14 +#define SkTDict_DEFINED 1.15 + 1.16 +#include "SkChunkAlloc.h" 1.17 +#include "SkTSearch.h" 1.18 +#include "SkTDArray.h" 1.19 + 1.20 +template <typename T> class SkTDict : SkNoncopyable { 1.21 +public: 1.22 + SkTDict(size_t minStringAlloc) : fStrings(minStringAlloc) {} 1.23 + 1.24 + void reset() 1.25 + { 1.26 + fArray.reset(); 1.27 + fStrings.reset(); 1.28 + } 1.29 + 1.30 + int count() const { return fArray.count(); } 1.31 + 1.32 + bool set(const char name[], const T& value) 1.33 + { 1.34 + return set(name, strlen(name), value); 1.35 + } 1.36 + 1.37 + bool set(const char name[], size_t len, const T& value) 1.38 + { 1.39 + SkASSERT(name); 1.40 + 1.41 + int index = this->find_index(name, len); 1.42 + 1.43 + if (index >= 0) 1.44 + { 1.45 + fArray[index].fValue = value; 1.46 + return false; 1.47 + } 1.48 + else 1.49 + { 1.50 + Pair* pair = fArray.insert(~index); 1.51 + char* copy = (char*)fStrings.alloc(len + 1, SkChunkAlloc::kThrow_AllocFailType); 1.52 + memcpy(copy, name, len); 1.53 + copy[len] = '\0'; 1.54 + pair->fName = copy; 1.55 + pair->fValue = value; 1.56 + return true; 1.57 + } 1.58 + } 1.59 + 1.60 + bool find(const char name[]) const 1.61 + { 1.62 + return this->find_index(name) >= 0; 1.63 + } 1.64 + 1.65 + bool find(const char name[], size_t len) const 1.66 + { 1.67 + return this->find_index(name, len) >= 0; 1.68 + } 1.69 + 1.70 + bool find(const char name[], T* value) const 1.71 + { 1.72 + return find(name, strlen(name), value); 1.73 + } 1.74 + 1.75 + bool find(const char name[], size_t len, T* value) const 1.76 + { 1.77 + int index = this->find_index(name, len); 1.78 + 1.79 + if (index >= 0) 1.80 + { 1.81 + if (value) 1.82 + *value = fArray[index].fValue; 1.83 + return true; 1.84 + } 1.85 + return false; 1.86 + } 1.87 + 1.88 + bool findKey(T& value, const char** name) const 1.89 + { 1.90 + const Pair* end = fArray.end(); 1.91 + for (const Pair* pair = fArray.begin(); pair < end; pair++) { 1.92 + if (pair->fValue != value) 1.93 + continue; 1.94 + *name = pair->fName; 1.95 + return true; 1.96 + } 1.97 + return false; 1.98 + } 1.99 + 1.100 +public: 1.101 + struct Pair { 1.102 + const char* fName; 1.103 + T fValue; 1.104 + 1.105 + friend int operator<(const Pair& a, const Pair& b) 1.106 + { 1.107 + return strcmp(a.fName, b.fName); 1.108 + } 1.109 + friend int operator!=(const Pair& a, const Pair& b) 1.110 + { 1.111 + return strcmp(a.fName, b.fName); 1.112 + } 1.113 + }; 1.114 + friend class Iter; 1.115 + 1.116 +public: 1.117 + class Iter { 1.118 + public: 1.119 + Iter(const SkTDict<T>& dict) 1.120 + { 1.121 + fIter = dict.fArray.begin(); 1.122 + fStop = dict.fArray.end(); 1.123 + } 1.124 + const char* next(T* value) 1.125 + { 1.126 + const char* name = NULL; 1.127 + if (fIter < fStop) 1.128 + { 1.129 + name = fIter->fName; 1.130 + if (value) 1.131 + *value = fIter->fValue; 1.132 + fIter += 1; 1.133 + } 1.134 + return name; 1.135 + } 1.136 + private: 1.137 + const Pair* fIter; 1.138 + const Pair* fStop; 1.139 + }; 1.140 + 1.141 +private: 1.142 + SkTDArray<Pair> fArray; 1.143 + SkChunkAlloc fStrings; 1.144 + 1.145 + int find_index(const char name[]) const 1.146 + { 1.147 + return find_index(name, strlen(name)); 1.148 + } 1.149 + 1.150 + int find_index(const char name[], size_t len) const 1.151 + { 1.152 + SkASSERT(name); 1.153 + 1.154 + int count = fArray.count(); 1.155 + int index = ~0; 1.156 + 1.157 + if (count) 1.158 + index = SkStrSearch(&fArray.begin()->fName, count, name, len, sizeof(Pair)); 1.159 + return index; 1.160 + } 1.161 + friend class Iter; 1.162 +}; 1.163 + 1.164 +#endif