| 1 |
/* |
| 2 |
* Copyright 2013 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkTMultiMap_DEFINED |
| 9 |
#define SkTMultiMap_DEFINED |
| 10 |
|
| 11 |
#include "src/core/SkTDynamicHash.h" |
| 12 |
|
| 13 |
/** A set that contains pointers to instances of T. Instances can be looked up with key Key. |
| 14 |
* Multiple (possibly same) values can have the same key. |
| 15 |
*/ |
| 16 |
template <typename T, |
| 17 |
typename Key, |
| 18 |
typename HashTraits=T> |
| 19 |
class SkTMultiMap { |
| 20 |
struct ValueList { |
| 21 |
explicit ValueList(T* value) : fValue(value), fNext(nullptr), fCount(1) {} |
| 22 |
|
| 23 |
static const Key& GetKey(const ValueList& e) { return HashTraits::GetKey(*e.fValue); } |
| 24 |
static uint32_t Hash(const Key& key) { return HashTraits::Hash(key); } |
| 25 |
T* fValue; |
| 26 |
ValueList* fNext; |
| 27 |
|
| 28 |
// TODO(b/407062399): Debugging information holding count of elements in ValueList. |
| 29 |
// Only maintained in the head of the linked list to view in LLDB dumps. |
| 30 |
uint32_t fCount; |
| 31 |
}; |
| 32 |
public: |
| 33 |
SkTMultiMap() : fCount(0) {} |
| 34 |
|
| 35 |
~SkTMultiMap() { |
| 36 |
this->reset(); |
| 37 |
} |
| 38 |
|
| 39 |
void reset() { |
| 40 |
fHash.foreach([&](ValueList* vl) { |
| 41 |
ValueList* next; |
| 42 |
for (ValueList* it = vl; it; it = next) { |
| 43 |
HashTraits::OnFree(it->fValue); |
| 44 |
next = it->fNext; |
| 45 |
delete it; |
| 46 |
} |
| 47 |
}); |
| 48 |
fHash.reset(); |
| 49 |
fCount = 0; |
| 50 |
} |
| 51 |
|
| 52 |
void insert(const Key& key, T* value) { |
| 53 |
ValueList* list = fHash.find(key); |
| 54 |
if (list) { |
| 55 |
// The new ValueList entry is inserted as the second element in the |
| 56 |
// linked list, and it will contain the value of the first element. |
| 57 |
ValueList* newEntry = new ValueList(list->fValue); |
| 58 |
newEntry->fNext = list->fNext; |
| 59 |
// The existing first ValueList entry is updated to contain the |
| 60 |
// inserted value. |
| 61 |
list->fNext = newEntry; |
| 62 |
list->fValue = value; |
| 63 |
list->fCount++; |
| 64 |
} else { |
| 65 |
fHash.add(new ValueList(value)); |
| 66 |
} |
| 67 |
|
| 68 |
++fCount; |
| 69 |
} |
| 70 |
|
| 71 |
void remove(const Key& key, const T* value) { |
| 72 |
ValueList* root = fHash.find(key); |
| 73 |
ValueList* list = root; |
| 74 |
// Temporarily making this safe for remove entries not in the map because of |
| 75 |
// crbug.com/877915. |
| 76 |
#if 0 |
| 77 |
// Since we expect the caller to be fully aware of what is stored, just |
| 78 |
// assert that the caller removes an existing value. |
| 79 |
SkASSERT(list); |
| 80 |
ValueList* prev = nullptr; |
| 81 |
while (list->fValue != value) { |
| 82 |
prev = list; |
| 83 |
list = list->fNext; |
| 84 |
} |
| 85 |
this->internalRemove(root, prev, list, key); |
| 86 |
#else |
| 87 |
ValueList* prev = nullptr; |
| 88 |
while (list && list->fValue != value) { |
| 89 |
prev = list; |
| 90 |
list = list->fNext; |
| 91 |
} |
| 92 |
// Crash in Debug since it'd be great to detect a repro of 877915. |
| 93 |
SkASSERT(list); |
| 94 |
if (list) { |
| 95 |
this->internalRemove(root, prev, list, key); |
| 96 |
} |
| 97 |
#endif |
| 98 |
} |
| 99 |
|
| 100 |
T* find(const Key& key) const { |
| 101 |
ValueList* list = fHash.find(key); |
| 102 |
if (list) { |
| 103 |
return list->fValue; |
| 104 |
} |
| 105 |
return nullptr; |
| 106 |
} |
| 107 |
|
| 108 |
template<class FindPredicate> |
| 109 |
T* find(const Key& key, const FindPredicate f) { |
| 110 |
ValueList* list = fHash.find(key); |
| 111 |
while (list) { |
| 112 |
if (f(list->fValue)){ |
| 113 |
return list->fValue; |
| 114 |
} |
| 115 |
list = list->fNext; |
| 116 |
} |
| 117 |
return nullptr; |
| 118 |
} |
| 119 |
|
| 120 |
template<class FindPredicate> |
| 121 |
T* findAndRemove(const Key& key, const FindPredicate f) { |
| 122 |
ValueList* root = fHash.find(key); |
| 123 |
|
| 124 |
ValueList* list = root; |
| 125 |
ValueList* prev = nullptr; |
| 126 |
while (list) { |
| 127 |
if (f(list->fValue)){ |
| 128 |
T* value = list->fValue; |
| 129 |
this->internalRemove(root, prev, list, key); |
| 130 |
return value; |
| 131 |
} |
| 132 |
prev = list; |
| 133 |
list = list->fNext; |
| 134 |
} |
| 135 |
return nullptr; |
| 136 |
} |
| 137 |
|
| 138 |
int count() const { return fCount; } |
| 139 |
|
| 140 |
#ifdef SK_DEBUG |
| 141 |
template <typename Fn> // f(T) or f(const T&) |
| 142 |
void foreach(Fn&& fn) const { |
| 143 |
fHash.foreach([&](const ValueList& vl) { |
| 144 |
for (const ValueList* it = &vl; it; it = it->fNext) { |
| 145 |
fn(*it->fValue); |
| 146 |
} |
| 147 |
}); |
| 148 |
} |
| 149 |
|
| 150 |
bool has(const T* value, const Key& key) const { |
| 151 |
for (ValueList* list = fHash.find(key); list; list = list->fNext) { |
| 152 |
if (list->fValue == value) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
} |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
// This is not particularly fast and only used for validation, so debug only. |
| 160 |
int countForKey(const Key& key) const { |
| 161 |
int count = 0; |
| 162 |
ValueList* list = fHash.find(key); |
| 163 |
while (list) { |
| 164 |
list = list->fNext; |
| 165 |
++count; |
| 166 |
} |
| 167 |
return count; |
| 168 |
} |
| 169 |
#endif |
| 170 |
|
| 171 |
private: |
| 172 |
SkTDynamicHash<ValueList, Key> fHash; |
| 173 |
int fCount; |
| 174 |
|
| 175 |
void internalRemove(ValueList* root, ValueList* prev, ValueList* elem, const Key& key) { |
| 176 |
root->fCount--; |
| 177 |
if (elem->fNext) { |
| 178 |
ValueList* next = elem->fNext; |
| 179 |
elem->fValue = next->fValue; |
| 180 |
elem->fNext = next->fNext; |
| 181 |
delete next; |
| 182 |
} else if (prev) { |
| 183 |
prev->fNext = nullptr; |
| 184 |
delete elem; |
| 185 |
} else { |
| 186 |
fHash.remove(key); |
| 187 |
delete elem; |
| 188 |
} |
| 189 |
|
| 190 |
--fCount; |
| 191 |
} |
| 192 |
|
| 193 |
}; |
| 194 |
|
| 195 |
#endif |