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

Side by Side Diff: Source/core/rendering/OrderIterator.cpp

Issue 18978010: Setting up OrderIterator shouldn't require an extra Vector (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Updated after cbiesinger's review Created 7 years, 5 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 | « Source/core/rendering/OrderIterator.h ('k') | Source/core/rendering/RenderFlexibleBox.h » ('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) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google 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 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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 #include "config.h" 31 #include "config.h"
32 #include "core/rendering/OrderIterator.h" 32 #include "core/rendering/OrderIterator.h"
33 33
34 #include "core/rendering/RenderFlexibleBox.h" 34 #include "core/rendering/RenderBox.h"
35 #include "core/rendering/RenderGrid.h"
36 35
37 namespace WebCore { 36 namespace WebCore {
38 37
39 OrderIterator::OrderIterator(const RenderBox* containerBox) 38 OrderIterator::OrderIterator(const RenderBox* containerBox)
40 : m_containerBox(containerBox) 39 : m_containerBox(containerBox)
41 , m_currentChild(0) 40 , m_currentChild(0)
42 , m_orderValuesIterator(0) 41 , m_orderValuesIterator(0)
43 { 42 {
44 } 43 }
45 44
46 void OrderIterator::setOrderValues(Vector<int>& orderValues)
47 {
48 reset();
49 m_orderValues.clear();
50
51 if (orderValues.isEmpty())
52 return;
53
54 std::sort(orderValues.begin(), orderValues.end());
55
56
57 // This is inefficient if there are many repeated values, but
58 // saves a lot of allocations when the values are unique. By far,
59 // the common case is that there's exactly one item in the list
60 // (the default order value of 0).
61 m_orderValues.reserveInitialCapacity(orderValues.size());
62
63 int previous = orderValues[0];
64 m_orderValues.append(previous);
65 for (size_t i = 1; i < orderValues.size(); ++i) {
66 int current = orderValues[i];
67 if (current == previous)
68 continue;
69 m_orderValues.append(current);
70 previous = current;
71 }
72 m_orderValues.shrinkToFit();
73 }
74
75 RenderBox* OrderIterator::first() 45 RenderBox* OrderIterator::first()
76 { 46 {
77 reset(); 47 reset();
78 return next(); 48 return next();
79 } 49 }
80 50
81 RenderBox* OrderIterator::next() 51 RenderBox* OrderIterator::next()
82 { 52 {
83 do { 53 do {
84 if (!m_currentChild) { 54 if (!m_currentChild) {
(...skipping 15 matching lines...) Expand all
100 70
101 return m_currentChild; 71 return m_currentChild;
102 } 72 }
103 73
104 void OrderIterator::reset() 74 void OrderIterator::reset()
105 { 75 {
106 m_currentChild = 0; 76 m_currentChild = 0;
107 m_orderValuesIterator = 0; 77 m_orderValuesIterator = 0;
108 } 78 }
109 79
80 OrderIteratorPopulator::~OrderIteratorPopulator()
81 {
82 m_iterator.reset();
83
84 if (m_anyChildHasDefaultOrderValue)
85 m_iterator.m_orderValues.append(0);
86
87 if (m_iterator.m_orderValues.size() > 1)
88 removeDuplicatedOrderValues();
89
90 // Ensure that we release any extra memory we hold onto.
91 m_iterator.m_orderValues.shrinkToFit();
92 }
93
94 void OrderIteratorPopulator::removeDuplicatedOrderValues()
95 {
96 OrderIterator::OrderValues& orderValues = m_iterator.m_orderValues;
97
98 std::sort(orderValues.begin(), orderValues.end());
99
100 int previous = orderValues[0];
101 size_t uniqueItemIndex = 0;
102 for (size_t i = 1; i < orderValues.size(); ++i) {
103 int current = orderValues[i];
104 if (current == previous)
105 continue;
106 ++uniqueItemIndex;
107 std::swap(orderValues[i], orderValues[uniqueItemIndex]);
108 previous = current;
109 }
110 orderValues.shrink(uniqueItemIndex + 1);
111 }
112
113 void OrderIteratorPopulator::collectChild(const RenderBox* child)
114 {
115 // Avoid growing the vector for the common-case default value of 0.
116 if (int order = child->style()->order())
117 m_iterator.m_orderValues.append(order);
118 else
119 m_anyChildHasDefaultOrderValue = true;
120 }
121
110 } // namespace WebCore 122 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/rendering/OrderIterator.h ('k') | Source/core/rendering/RenderFlexibleBox.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698