OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #ifndef SkTMultiMap_DEFINED | 9 #ifndef SkTMultiMap_DEFINED |
10 #define SkTMultiMap_DEFINED | 10 #define SkTMultiMap_DEFINED |
(...skipping 19 matching lines...) Expand all Loading... |
30 public: | 30 public: |
31 SkTMultiMap() : fCount(0) {} | 31 SkTMultiMap() : fCount(0) {} |
32 | 32 |
33 ~SkTMultiMap() { | 33 ~SkTMultiMap() { |
34 SkASSERT(fCount == 0); | 34 SkASSERT(fCount == 0); |
35 SkASSERT(fHash.count() == 0); | 35 SkASSERT(fHash.count() == 0); |
36 } | 36 } |
37 | 37 |
38 void insert(const Key& key, T* value) { | 38 void insert(const Key& key, T* value) { |
39 ValueList* list = fHash.find(key); | 39 ValueList* list = fHash.find(key); |
40 if (NULL != list) { | 40 if (list) { |
41 // The new ValueList entry is inserted as the second element in the | 41 // The new ValueList entry is inserted as the second element in the |
42 // linked list, and it will contain the value of the first element. | 42 // linked list, and it will contain the value of the first element. |
43 ValueList* newEntry = SkNEW_ARGS(ValueList, (list->fValue)); | 43 ValueList* newEntry = SkNEW_ARGS(ValueList, (list->fValue)); |
44 newEntry->fNext = list->fNext; | 44 newEntry->fNext = list->fNext; |
45 // The existing first ValueList entry is updated to contain the | 45 // The existing first ValueList entry is updated to contain the |
46 // inserted value. | 46 // inserted value. |
47 list->fNext = newEntry; | 47 list->fNext = newEntry; |
48 list->fValue = value; | 48 list->fValue = value; |
49 } else { | 49 } else { |
50 fHash.add(SkNEW_ARGS(ValueList, (value))); | 50 fHash.add(SkNEW_ARGS(ValueList, (value))); |
51 } | 51 } |
52 | 52 |
53 ++fCount; | 53 ++fCount; |
54 } | 54 } |
55 | 55 |
56 void remove(const Key& key, const T* value) { | 56 void remove(const Key& key, const T* value) { |
57 ValueList* list = fHash.find(key); | 57 ValueList* list = fHash.find(key); |
58 // Since we expect the caller to be fully aware of what is stored, just | 58 // Since we expect the caller to be fully aware of what is stored, just |
59 // assert that the caller removes an existing value. | 59 // assert that the caller removes an existing value. |
60 SkASSERT(NULL != list); | 60 SkASSERT(list); |
61 ValueList* prev = NULL; | 61 ValueList* prev = NULL; |
62 while (list->fValue != value) { | 62 while (list->fValue != value) { |
63 prev = list; | 63 prev = list; |
64 list = list->fNext; | 64 list = list->fNext; |
65 } | 65 } |
66 | 66 |
67 if (NULL != list->fNext) { | 67 if (list->fNext) { |
68 ValueList* next = list->fNext; | 68 ValueList* next = list->fNext; |
69 list->fValue = next->fValue; | 69 list->fValue = next->fValue; |
70 list->fNext = next->fNext; | 70 list->fNext = next->fNext; |
71 SkDELETE(next); | 71 SkDELETE(next); |
72 } else if (NULL != prev) { | 72 } else if (prev) { |
73 prev->fNext = NULL; | 73 prev->fNext = NULL; |
74 SkDELETE(list); | 74 SkDELETE(list); |
75 } else { | 75 } else { |
76 fHash.remove(key); | 76 fHash.remove(key); |
77 SkDELETE(list); | 77 SkDELETE(list); |
78 } | 78 } |
79 | 79 |
80 --fCount; | 80 --fCount; |
81 } | 81 } |
82 | 82 |
83 T* find(const Key& key) const { | 83 T* find(const Key& key) const { |
84 ValueList* list = fHash.find(key); | 84 ValueList* list = fHash.find(key); |
85 if (NULL != list) { | 85 if (list) { |
86 return list->fValue; | 86 return list->fValue; |
87 } | 87 } |
88 return NULL; | 88 return NULL; |
89 } | 89 } |
90 | 90 |
91 template<class FindPredicate> | 91 template<class FindPredicate> |
92 T* find(const Key& key, const FindPredicate f) { | 92 T* find(const Key& key, const FindPredicate f) { |
93 ValueList* list = fHash.find(key); | 93 ValueList* list = fHash.find(key); |
94 while (NULL != list) { | 94 while (list) { |
95 if (f(list->fValue)){ | 95 if (f(list->fValue)){ |
96 return list->fValue; | 96 return list->fValue; |
97 } | 97 } |
98 list = list->fNext; | 98 list = list->fNext; |
99 } | 99 } |
100 return NULL; | 100 return NULL; |
101 } | 101 } |
102 | 102 |
103 int count() const { return fCount; } | 103 int count() const { return fCount; } |
104 | 104 |
105 private: | 105 private: |
106 SkTDynamicHash<ValueList, Key> fHash; | 106 SkTDynamicHash<ValueList, Key> fHash; |
107 int fCount; | 107 int fCount; |
108 }; | 108 }; |
109 | 109 |
110 #endif | 110 #endif |
OLD | NEW |