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

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

Issue 22968004: Revert "Simplify DocumentOrderedMap" (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | Source/core/dom/DocumentOrderedMap.cpp » ('j') | 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, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
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 15 matching lines...) Expand all
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef DocumentOrderedMap_h 31 #ifndef DocumentOrderedMap_h
32 #define DocumentOrderedMap_h 32 #define DocumentOrderedMap_h
33 33
34 #include "wtf/HashCountedSet.h" 34 #include "wtf/HashCountedSet.h"
35 #include "wtf/HashMap.h" 35 #include "wtf/HashMap.h"
36 #include "wtf/Vector.h"
37 #include "wtf/text/StringImpl.h" 36 #include "wtf/text/StringImpl.h"
38 37
39 namespace WebCore { 38 namespace WebCore {
40 39
41 class Element; 40 class Element;
42 class TreeScope; 41 class TreeScope;
43 42
44 class DocumentOrderedMap { 43 class DocumentOrderedMap {
45 public: 44 public:
46 void add(StringImpl*, Element*); 45 void add(StringImpl*, Element*);
47 void remove(StringImpl*, Element*); 46 void remove(StringImpl*, Element*);
48 void clear(); 47 void clear();
49 48
50 bool contains(StringImpl*) const; 49 bool contains(StringImpl*) const;
51 bool containsSingle(StringImpl*) const;
52 bool containsMultiple(StringImpl*) const; 50 bool containsMultiple(StringImpl*) const;
53
54 // concrete instantiations of the get<>() method template 51 // concrete instantiations of the get<>() method template
55 Element* getElementById(StringImpl*, const TreeScope*) const; 52 Element* getElementById(StringImpl*, const TreeScope*) const;
56 Element* getElementByName(StringImpl*, const TreeScope*) const;
57 Element* getElementByMapName(StringImpl*, const TreeScope*) const; 53 Element* getElementByMapName(StringImpl*, const TreeScope*) const;
58 Element* getElementByLowercasedMapName(StringImpl*, const TreeScope*) const; 54 Element* getElementByLowercasedMapName(StringImpl*, const TreeScope*) const;
59 Element* getElementByLabelForAttribute(StringImpl*, const TreeScope*) const; 55 Element* getElementByLabelForAttribute(StringImpl*, const TreeScope*) const;
60 56
61 const Vector<Element*>* getAllElementsById(StringImpl*, const TreeScope*) co nst;
62
63 void checkConsistency() const; 57 void checkConsistency() const;
64 58
65 private: 59 private:
66 template<bool keyMatches(StringImpl*, Element*)> Element* get(StringImpl*, c onst TreeScope*) const; 60 template<bool keyMatches(StringImpl*, Element*)> Element* get(StringImpl*, c onst TreeScope*) const;
67 61
68 struct MapEntry { 62 typedef HashMap<StringImpl*, Element*> Map;
69 MapEntry()
70 : element(0)
71 , count(0)
72 {
73 }
74 explicit MapEntry(Element* firstElement)
75 : element(firstElement)
76 , count(1)
77 {
78 }
79 63
80 Element* element; 64 // We maintain the invariant that m_duplicateCounts is the count of all elem ents with a given key
81 unsigned count; 65 // excluding the one referenced in m_map, if any. This means it one less tha n the total count
82 Vector<Element*> orderedList; 66 // when the first node with a given key is cached, otherwise the same as the total count.
83 };
84
85 typedef HashMap<StringImpl*, MapEntry> Map;
86
87 mutable Map m_map; 67 mutable Map m_map;
68 mutable HashCountedSet<StringImpl*> m_duplicateCounts;
88 }; 69 };
89 70
90 inline bool DocumentOrderedMap::containsSingle(StringImpl* id) const
91 {
92 Map::const_iterator it = m_map.find(id);
93 return it != m_map.end() && it->value.count == 1;
94 }
95
96 inline bool DocumentOrderedMap::contains(StringImpl* id) const 71 inline bool DocumentOrderedMap::contains(StringImpl* id) const
97 { 72 {
98 return m_map.contains(id); 73 return m_map.contains(id) || m_duplicateCounts.contains(id);
99 } 74 }
100 75
101 inline bool DocumentOrderedMap::containsMultiple(StringImpl* id) const 76 inline bool DocumentOrderedMap::containsMultiple(StringImpl* id) const
102 { 77 {
103 Map::const_iterator it = m_map.find(id); 78 return m_duplicateCounts.contains(id);
104 return it != m_map.end() && it->value.count > 1;
105 } 79 }
106 80
107 } // namespace WebCore 81 } // namespace WebCore
108 82
109 #endif // DocumentOrderedMap_h 83 #endif // DocumentOrderedMap_h
OLDNEW
« no previous file with comments | « no previous file | Source/core/dom/DocumentOrderedMap.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698