Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: Source/core/dom/DocumentOrderedMap.cpp

Issue 117313002: Simplify DocumentOrderedMap (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/dom/DocumentOrderedMap.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 } 58 }
59 59
60 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element) 60 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element)
61 { 61 {
62 return isHTMLLabelElement(element) && element->getAttribute(forAttr).impl() == key; 62 return isHTMLLabelElement(element) && element->getAttribute(forAttr).impl() == key;
63 } 63 }
64 64
65 void DocumentOrderedMap::clear() 65 void DocumentOrderedMap::clear()
66 { 66 {
67 m_map.clear(); 67 m_map.clear();
68 m_duplicateCounts.clear();
69 } 68 }
70 69
71 void DocumentOrderedMap::add(StringImpl* key, Element* element) 70 void DocumentOrderedMap::add(StringImpl* key, Element* element)
72 { 71 {
73 ASSERT(key); 72 ASSERT(key);
74 ASSERT(element); 73 ASSERT(element);
75 74
76 if (!m_duplicateCounts.contains(key)) { 75 Map::AddResult addResult = m_map.add(key, MapEntry(element));
77 // Fast path. The key is not already in m_duplicateCounts, so we assume that it's 76 if (addResult.isNewEntry)
78 // also not already in m_map and try to add it. If that add succeeds, we 're done. 77 return;
79 Map::AddResult addResult = m_map.add(key, element);
80 if (addResult.isNewEntry)
81 return;
82 78
83 // The add failed, so this key was already cached in m_map. 79 MapEntry& entry = addResult.iterator->value;
84 // There are multiple elements with this key. Remove the m_map 80 ASSERT(entry.count);
85 // cache for this key so get searches for it next time it is called. 81 entry.element = 0;
86 m_map.remove(addResult.iterator); 82 entry.count++;
87 m_duplicateCounts.add(key);
88 } else {
89 // There are multiple elements with this key. Remove the m_map
90 // cache for this key so get will search for it next time it is called.
91 Map::iterator cachedItem = m_map.find(key);
92 if (cachedItem != m_map.end()) {
93 m_map.remove(cachedItem);
94 m_duplicateCounts.add(key);
95 }
96 }
97
98 m_duplicateCounts.add(key);
99 } 83 }
100 84
101 void DocumentOrderedMap::remove(StringImpl* key, Element* element) 85 void DocumentOrderedMap::remove(StringImpl* key, Element* element)
102 { 86 {
103 ASSERT(key); 87 ASSERT(key);
104 ASSERT(element); 88 ASSERT(element);
105 89
106 Map::iterator cachedItem = m_map.find(key); 90 Map::iterator it = m_map.find(key);
107 if (cachedItem != m_map.end() && cachedItem->value == element) 91 if (it == m_map.end())
Inactive 2013/12/17 15:34:50 This is the fix for the clusterfuzz crash. We need
108 m_map.remove(cachedItem); 92 return;
109 else 93
110 m_duplicateCounts.remove(key); 94 MapEntry& entry = it->value;
95 ASSERT(entry.count);
96 if (entry.count == 1) {
97 ASSERT(!entry.element || entry.element == element);
98 m_map.remove(it);
99 } else {
100 if (entry.element == element)
101 entry.element = 0;
102 entry.count--;
103 }
111 } 104 }
112 105
113 template<bool keyMatches(StringImpl*, Element*)> 106 template<bool keyMatches(StringImpl*, Element*)>
114 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const 107 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const
115 { 108 {
116 ASSERT(key); 109 ASSERT(key);
117 ASSERT(scope); 110 ASSERT(scope);
118 111
119 Element* element = m_map.get(key); 112 Map::iterator it = m_map.find(key);
120 if (element) 113 if (it == m_map.end())
114 return 0;
115
116 MapEntry& entry = it->value;
117 ASSERT(entry.count);
118 if (entry.element)
119 return entry.element;
120
121 // We know there's at least one node that matches; iterate to find the first one.
122 for (Element* element = ElementTraversal::firstWithin(*scope->rootNode()); e lement; element = ElementTraversal::next(*element)) {
123 if (!keyMatches(key, element))
124 continue;
125 entry.element = element;
121 return element; 126 return element;
122
123 if (m_duplicateCounts.contains(key)) {
124 // We know there's at least one node that matches; iterate to find the f irst one.
125 ASSERT(scope->rootNode());
126 for (element = ElementTraversal::firstWithin(*scope->rootNode()); elemen t; element = ElementTraversal::next(*element)) {
127 if (!keyMatches(key, element))
128 continue;
129 m_duplicateCounts.remove(key);
130 m_map.set(key, element);
131 return element;
132 }
133 ASSERT_NOT_REACHED();
134 } 127 }
135 128 ASSERT_NOT_REACHED();
136 return 0; 129 return 0;
137 } 130 }
138 131
139 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc ope) const 132 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc ope) const
140 { 133 {
141 return get<keyMatchesId>(key, scope); 134 return get<keyMatchesId>(key, scope);
142 } 135 }
143 136
144 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop e* scope) const 137 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop e* scope) const
145 { 138 {
146 return get<keyMatchesMapName>(key, scope); 139 return get<keyMatchesMapName>(key, scope);
147 } 140 }
148 141
149 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons t TreeScope* scope) const 142 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons t TreeScope* scope) const
150 { 143 {
151 return get<keyMatchesLowercasedMapName>(key, scope); 144 return get<keyMatchesLowercasedMapName>(key, scope);
152 } 145 }
153 146
154 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons t TreeScope* scope) const 147 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons t TreeScope* scope) const
155 { 148 {
156 return get<keyMatchesLabelForAttribute>(key, scope); 149 return get<keyMatchesLabelForAttribute>(key, scope);
157 } 150 }
158 151
159 } // namespace WebCore 152 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/dom/DocumentOrderedMap.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698