OLD | NEW |
1 /* | 1 /* |
| 2 * |
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights
reserved. | 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights
reserved. |
3 * | 4 * |
4 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
6 * met: | 7 * met: |
7 * | 8 * |
8 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 11 * * Redistributions in binary form must reproduce the above |
11 * copyright notice, this list of conditions and the following disclaimer | 12 * copyright notice, this list of conditions and the following disclaimer |
(...skipping 14 matching lines...) Expand all Loading... |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 * (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. | 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 30 */ |
30 | 31 |
31 #ifndef DocumentOrderedMap_h | 32 #ifndef DocumentOrderedMap_h |
32 #define DocumentOrderedMap_h | 33 #define DocumentOrderedMap_h |
33 | 34 |
34 #include "wtf/HashCountedSet.h" | 35 #include "wtf/HashCountedSet.h" |
35 #include "wtf/HashMap.h" | 36 #include "wtf/HashMap.h" |
| 37 #include "wtf/Vector.h" |
36 #include "wtf/text/StringImpl.h" | 38 #include "wtf/text/StringImpl.h" |
37 | 39 |
38 namespace WebCore { | 40 namespace WebCore { |
39 | 41 |
40 class Element; | 42 class Element; |
41 class TreeScope; | 43 class TreeScope; |
42 | 44 |
43 class DocumentOrderedMap { | 45 class DocumentOrderedMap { |
44 public: | 46 public: |
45 void add(StringImpl*, Element*); | 47 void add(StringImpl*, Element*); |
46 void remove(StringImpl*, Element*); | 48 void remove(StringImpl*, Element*); |
47 void clear(); | 49 void clear(); |
48 | 50 |
49 bool contains(StringImpl*) const; | 51 bool contains(StringImpl*) const; |
50 bool containsMultiple(StringImpl*) const; | 52 bool containsMultiple(StringImpl*) const; |
51 // concrete instantiations of the get<>() method template | 53 // concrete instantiations of the get<>() method template |
52 Element* getElementById(StringImpl*, const TreeScope*) const; | 54 Element* getElementById(StringImpl*, const TreeScope*) const; |
| 55 const Vector<Element*>* getAllElementsById(StringImpl*, const TreeScope*) co
nst; |
53 Element* getElementByMapName(StringImpl*, const TreeScope*) const; | 56 Element* getElementByMapName(StringImpl*, const TreeScope*) const; |
54 Element* getElementByLowercasedMapName(StringImpl*, const TreeScope*) const; | 57 Element* getElementByLowercasedMapName(StringImpl*, const TreeScope*) const; |
55 Element* getElementByLabelForAttribute(StringImpl*, const TreeScope*) const; | 58 Element* getElementByLabelForAttribute(StringImpl*, const TreeScope*) const; |
56 | 59 |
57 void checkConsistency() const; | 60 void checkConsistency() const; |
58 | 61 |
59 private: | 62 private: |
60 template<bool keyMatches(StringImpl*, Element*)> Element* get(StringImpl*, c
onst TreeScope*) const; | 63 template<bool keyMatches(StringImpl*, Element*)> Element* get(StringImpl*, c
onst TreeScope*) const; |
61 | 64 |
62 typedef HashMap<StringImpl*, Element*> Map; | 65 struct MapEntry { |
| 66 MapEntry() |
| 67 : element(0) |
| 68 , count(0) |
| 69 { } |
63 | 70 |
64 // We maintain the invariant that m_duplicateCounts is the count of all elem
ents with a given key | 71 explicit MapEntry(Element* firstElement) |
65 // excluding the one referenced in m_map, if any. This means it one less tha
n the total count | 72 : element(firstElement) |
66 // when the first node with a given key is cached, otherwise the same as the
total count. | 73 , count(1) |
| 74 { } |
| 75 |
| 76 Element* element; |
| 77 unsigned count; |
| 78 Vector<Element*> orderedList; |
| 79 }; |
| 80 |
| 81 typedef HashMap<StringImpl*, MapEntry> Map; |
| 82 |
| 83 |
67 mutable Map m_map; | 84 mutable Map m_map; |
68 mutable HashCountedSet<StringImpl*> m_duplicateCounts; | |
69 }; | 85 }; |
70 | 86 |
71 inline bool DocumentOrderedMap::contains(StringImpl* id) const | 87 inline bool DocumentOrderedMap::contains(StringImpl* id) const |
72 { | 88 { |
73 return m_map.contains(id) || m_duplicateCounts.contains(id); | 89 return m_map.contains(id); |
74 } | 90 } |
75 | 91 |
76 inline bool DocumentOrderedMap::containsMultiple(StringImpl* id) const | 92 inline bool DocumentOrderedMap::containsMultiple(StringImpl* id) const |
77 { | 93 { |
78 return m_duplicateCounts.contains(id); | 94 Map::const_iterator it = m_map.find(id); |
| 95 return it != m_map.end() && it->value.count > 1; |
79 } | 96 } |
80 | 97 |
81 } // namespace WebCore | 98 } // namespace WebCore |
82 | 99 |
83 #endif // DocumentOrderedMap_h | 100 #endif // DocumentOrderedMap_h |
OLD | NEW |