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

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

Issue 123333003: Use a pointer type for values in DocumentOrderedMap (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 11 months 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 void DocumentOrderedMap::clear() 64 void DocumentOrderedMap::clear()
65 { 65 {
66 m_map.clear(); 66 m_map.clear();
67 } 67 }
68 68
69 void DocumentOrderedMap::add(StringImpl* key, Element* element) 69 void DocumentOrderedMap::add(StringImpl* key, Element* element)
70 { 70 {
71 ASSERT(key); 71 ASSERT(key);
72 ASSERT(element); 72 ASSERT(element);
73 73
74 Map::AddResult addResult = m_map.add(key, MapEntry(element)); 74 Map::AddResult addResult = m_map.add(key, adoptPtr(new MapEntry(element)));
75 if (addResult.isNewEntry) 75 if (addResult.isNewEntry)
76 return; 76 return;
77 77
78 MapEntry& entry = addResult.iterator->value; 78 OwnPtr<MapEntry>& entry = addResult.iterator->value;
79 ASSERT(entry.count); 79 ASSERT(entry->count);
80 entry.element = 0; 80 entry->element = 0;
81 entry.count++; 81 entry->count++;
82 entry.orderedList.clear(); 82 entry->orderedList.clear();
83 } 83 }
84 84
85 void DocumentOrderedMap::remove(StringImpl* key, Element* element) 85 void DocumentOrderedMap::remove(StringImpl* key, Element* element)
86 { 86 {
87 ASSERT(key); 87 ASSERT(key);
88 ASSERT(element); 88 ASSERT(element);
89 89
90 Map::iterator it = m_map.find(key); 90 Map::iterator it = m_map.find(key);
91 if (it == m_map.end()) 91 if (it == m_map.end())
92 return; 92 return;
93 93
94 MapEntry& entry = it->value; 94 OwnPtr<MapEntry>& entry = it->value;
95 ASSERT(entry.count); 95 ASSERT(entry->count);
96 if (entry.count == 1) { 96 if (entry->count == 1) {
97 ASSERT(!entry.element || entry.element == element); 97 ASSERT(!entry->element || entry->element == element);
98 m_map.remove(it); 98 m_map.remove(it);
99 } else { 99 } else {
100 if (entry.element == element) { 100 if (entry->element == element) {
101 ASSERT(entry.orderedList.isEmpty() || entry.orderedList.first() == e lement); 101 ASSERT(entry->orderedList.isEmpty() || entry->orderedList.first() == element);
102 entry.element = entry.orderedList.size() > 1 ? entry.orderedList[1] : 0; 102 entry->element = entry->orderedList.size() > 1 ? entry->orderedList[ 1] : 0;
103 } 103 }
104 entry.count--; 104 entry->count--;
105 entry.orderedList.clear(); 105 entry->orderedList.clear();
106 } 106 }
107 } 107 }
108 108
109 template<bool keyMatches(StringImpl*, Element*)> 109 template<bool keyMatches(StringImpl*, Element*)>
110 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const 110 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const
111 { 111 {
112 ASSERT(key); 112 ASSERT(key);
113 ASSERT(scope); 113 ASSERT(scope);
114 114
115 Map::iterator it = m_map.find(key); 115 MapEntry* entry = m_map.get(key);
116 if (it == m_map.end()) 116 if (!entry)
117 return 0; 117 return 0;
118 118
119 MapEntry& entry = it->value; 119 ASSERT(entry->count);
120 ASSERT(entry.count); 120 if (entry->element)
121 if (entry.element) 121 return entry->element;
122 return entry.element;
123 122
124 // We know there's at least one node that matches; iterate to find the first one. 123 // We know there's at least one node that matches; iterate to find the first one.
125 for (Element* element = ElementTraversal::firstWithin(*scope->rootNode()); e lement; element = ElementTraversal::next(*element)) { 124 for (Element* element = ElementTraversal::firstWithin(*scope->rootNode()); e lement; element = ElementTraversal::next(*element)) {
126 if (!keyMatches(key, element)) 125 if (!keyMatches(key, element))
127 continue; 126 continue;
128 entry.element = element; 127 entry->element = element;
129 return element; 128 return element;
130 } 129 }
131 ASSERT_NOT_REACHED(); 130 ASSERT_NOT_REACHED();
132 return 0; 131 return 0;
133 } 132 }
134 133
135 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc ope) const 134 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc ope) const
136 { 135 {
137 return get<keyMatchesId>(key, scope); 136 return get<keyMatchesId>(key, scope);
138 } 137 }
139 138
140 const Vector<Element*>& DocumentOrderedMap::getAllElementsById(StringImpl* key, const TreeScope* scope) const 139 const Vector<Element*>& DocumentOrderedMap::getAllElementsById(StringImpl* key, const TreeScope* scope) const
141 { 140 {
142 ASSERT(key); 141 ASSERT(key);
143 ASSERT(scope); 142 ASSERT(scope);
144 DEFINE_STATIC_LOCAL(Vector<Element*>, emptyVector, ()); 143 DEFINE_STATIC_LOCAL(Vector<Element*>, emptyVector, ());
145 144
146 Map::iterator it = m_map.find(key); 145 Map::iterator it = m_map.find(key);
147 if (it == m_map.end()) 146 if (it == m_map.end())
148 return emptyVector; 147 return emptyVector;
149 148
150 MapEntry& entry = it->value; 149 OwnPtr<MapEntry>& entry = it->value;
151 ASSERT(entry.count); 150 ASSERT(entry->count);
152 151
153 if (entry.orderedList.isEmpty()) { 152 if (entry->orderedList.isEmpty()) {
154 entry.orderedList.reserveCapacity(entry.count); 153 entry->orderedList.reserveCapacity(entry->count);
155 for (Element* element = entry.element ? entry.element : ElementTraversal ::firstWithin(*scope->rootNode()); entry.orderedList.size() < entry.count; eleme nt = ElementTraversal::next(*element)) { 154 for (Element* element = entry->element ? entry->element : ElementTravers al::firstWithin(*scope->rootNode()); entry->orderedList.size() < entry->count; e lement = ElementTraversal::next(*element)) {
156 ASSERT(element); 155 ASSERT(element);
157 if (!keyMatchesId(key, element)) 156 if (!keyMatchesId(key, element))
158 continue; 157 continue;
159 entry.orderedList.uncheckedAppend(element); 158 entry->orderedList.uncheckedAppend(element);
160 } 159 }
161 if (!entry.element) 160 if (!entry->element)
162 entry.element = entry.orderedList.first(); 161 entry->element = entry->orderedList.first();
163 } 162 }
164 163
165 return entry.orderedList; 164 return entry->orderedList;
166 } 165 }
167 166
168 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop e* scope) const 167 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop e* scope) const
169 { 168 {
170 return get<keyMatchesMapName>(key, scope); 169 return get<keyMatchesMapName>(key, scope);
171 } 170 }
172 171
173 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons t TreeScope* scope) const 172 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons t TreeScope* scope) const
174 { 173 {
175 return get<keyMatchesLowercasedMapName>(key, scope); 174 return get<keyMatchesLowercasedMapName>(key, scope);
176 } 175 }
177 176
178 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons t TreeScope* scope) const 177 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons t TreeScope* scope) const
179 { 178 {
180 return get<keyMatchesLabelForAttribute>(key, scope); 179 return get<keyMatchesLabelForAttribute>(key, scope);
181 } 180 }
182 181
183 } // namespace WebCore 182 } // 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