OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 | 42 |
43 // Recommended maximum size for both explicit and implicit grids. | 43 // Recommended maximum size for both explicit and implicit grids. |
44 const size_t kGridMaxTracks = 1000000; | 44 const size_t kGridMaxTracks = 1000000; |
45 | 45 |
46 // A span in a single direction (either rows or columns). Note that |resolvedIni tialPosition| | 46 // A span in a single direction (either rows or columns). Note that |resolvedIni tialPosition| |
47 // and |resolvedFinalPosition| are grid lines' indexes. | 47 // and |resolvedFinalPosition| are grid lines' indexes. |
48 // Iterating over the span shouldn't include |resolvedFinalPosition| to be corre ct. | 48 // Iterating over the span shouldn't include |resolvedFinalPosition| to be corre ct. |
49 struct GridSpan { | 49 struct GridSpan { |
50 USING_FAST_MALLOC(GridSpan); | 50 USING_FAST_MALLOC(GridSpan); |
51 public: | 51 public: |
52 static PassOwnPtr<GridSpan> create(const GridResolvedPosition& resolvedIniti alPosition, const GridResolvedPosition& resolvedFinalPosition) | 52 |
53 static GridSpan definiteGridSpan(const GridResolvedPosition& resolvedInitial Position, const GridResolvedPosition& resolvedFinalPosition) | |
53 { | 54 { |
54 return adoptPtr(new GridSpan(resolvedInitialPosition, resolvedFinalPosit ion)); | 55 return GridSpan(resolvedInitialPosition, resolvedFinalPosition); |
55 } | 56 } |
56 | 57 |
57 static PassOwnPtr<GridSpan> createWithSpanAgainstOpposite(const GridResolved Position& resolvedOppositePosition, const GridPosition& position, GridPositionSi de side) | 58 static GridSpan indefiniteGridSpan() |
59 { | |
60 GridSpan span = GridSpan(0, 1); | |
61 span.m_definite = false; | |
62 return span; | |
63 } | |
64 | |
65 static GridSpan definiteGridSpanWithSpanAgainstOpposite(const GridResolvedPo sition& resolvedOppositePosition, const GridPosition& position, GridPositionSide side) | |
58 { | 66 { |
59 size_t positionOffset = position.spanPosition(); | 67 size_t positionOffset = position.spanPosition(); |
60 if (side == ColumnStartSide || side == RowStartSide) { | 68 if (side == ColumnStartSide || side == RowStartSide) { |
61 if (resolvedOppositePosition == 0) | 69 if (resolvedOppositePosition == 0) |
62 return GridSpan::create(resolvedOppositePosition, resolvedOpposi tePosition.next()); | 70 return GridSpan::definiteGridSpan(resolvedOppositePosition, reso lvedOppositePosition.next()); |
63 | 71 |
64 GridResolvedPosition initialResolvedPosition = GridResolvedPosition( std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset)); | 72 GridResolvedPosition initialResolvedPosition = GridResolvedPosition( std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset)); |
65 return GridSpan::create(initialResolvedPosition, resolvedOppositePos ition); | 73 return GridSpan::definiteGridSpan(initialResolvedPosition, resolvedO ppositePosition); |
66 } | 74 } |
67 | 75 |
68 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(r esolvedOppositePosition.toInt() + positionOffset)); | 76 return GridSpan::definiteGridSpan(resolvedOppositePosition, GridResolved Position(resolvedOppositePosition.toInt() + positionOffset)); |
69 } | 77 } |
70 | 78 |
71 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridRes olvedPosition& resolvedOppositePosition, const GridPosition& position, GridPosit ionSide side, const Vector<size_t>& gridLines) | 79 static GridSpan definiteGridSpanWithNamedSpanAgainstOpposite(const GridResol vedPosition& resolvedOppositePosition, const GridPosition& position, GridPositio nSide side, const Vector<size_t>& gridLines) |
72 { | 80 { |
73 if (side == RowStartSide || side == ColumnStartSide) | 81 if (side == RowStartSide || side == ColumnStartSide) |
74 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePos ition, position, gridLines); | 82 return definiteGridSpanWithInitialNamedSpanAgainstOpposite(resolvedO ppositePosition, position, gridLines); |
75 | 83 |
76 return createWithFinalNamedSpanAgainstOpposite(resolvedOppositePosition, position, gridLines); | 84 return definiteGridSpanWithFinalNamedSpanAgainstOpposite(resolvedOpposit ePosition, position, gridLines); |
77 } | 85 } |
78 | 86 |
79 static PassOwnPtr<GridSpan> createWithInitialNamedSpanAgainstOpposite(const GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, co nst Vector<size_t>& gridLines) | 87 static GridSpan definiteGridSpanWithInitialNamedSpanAgainstOpposite(const Gr idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons t Vector<size_t>& gridLines) |
80 { | 88 { |
81 if (resolvedOppositePosition == 0) | 89 if (resolvedOppositePosition == 0) |
82 return GridSpan::create(resolvedOppositePosition, resolvedOppositePo sition.next()); | 90 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolved OppositePosition.next()); |
83 | 91 |
84 size_t firstLineBeforeOppositePositionIndex = 0; | 92 size_t firstLineBeforeOppositePositionIndex = 0; |
85 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin es.begin(), gridLines.end(), resolvedOppositePosition.toInt()); | 93 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin es.begin(), gridLines.end(), resolvedOppositePosition.toInt()); |
86 if (firstLineBeforeOppositePosition != gridLines.end()) | 94 if (firstLineBeforeOppositePosition != gridLines.end()) |
87 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi on - gridLines.begin(); | 95 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi on - gridLines.begin(); |
88 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionI ndex - position.spanPosition()); | 96 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionI ndex - position.spanPosition()); |
89 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gri dLines[gridLineIndex]); | 97 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gri dLines[gridLineIndex]); |
90 if (resolvedGridLinePosition >= resolvedOppositePosition) | 98 if (resolvedGridLinePosition >= resolvedOppositePosition) |
91 resolvedGridLinePosition = resolvedOppositePosition.prev(); | 99 resolvedGridLinePosition = resolvedOppositePosition.prev(); |
92 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePositi on); | 100 return GridSpan::definiteGridSpan(resolvedGridLinePosition, resolvedOppo sitePosition); |
93 } | 101 } |
94 | 102 |
95 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const Gr idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons t Vector<size_t>& gridLines) | 103 static GridSpan definiteGridSpanWithFinalNamedSpanAgainstOpposite(const Grid ResolvedPosition& resolvedOppositePosition, const GridPosition& position, const Vector<size_t>& gridLines) |
96 { | 104 { |
97 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; | 105 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; |
98 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine s.begin(), gridLines.end(), resolvedOppositePosition.toInt()); | 106 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine s.begin(), gridLines.end(), resolvedOppositePosition.toInt()); |
99 if (firstLineAfterOppositePosition != gridLines.end()) | 107 if (firstLineAfterOppositePosition != gridLines.end()) |
100 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - gridLines.begin(); | 108 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition - gridLines.begin(); |
101 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppo sitePositionIndex + position.spanPosition() - 1); | 109 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppo sitePositionIndex + position.spanPosition() - 1); |
102 GridResolvedPosition resolvedGridLinePosition = gridLines[gridLineIndex] ; | 110 GridResolvedPosition resolvedGridLinePosition = gridLines[gridLineIndex] ; |
103 if (resolvedGridLinePosition <= resolvedOppositePosition) | 111 if (resolvedGridLinePosition <= resolvedOppositePosition) |
104 resolvedGridLinePosition = resolvedOppositePosition.next(); | 112 resolvedGridLinePosition = resolvedOppositePosition.next(); |
105 | 113 |
106 return GridSpan::create(resolvedOppositePosition, resolvedGridLinePositi on); | 114 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolvedGrid LinePosition); |
107 } | |
108 | |
109 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridReso lvedPosition& resolvedFinalPosition) | |
110 : resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kGri dMaxTracks - 1)) | |
111 , resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridMax Tracks)) | |
112 { | |
113 ASSERT(resolvedInitialPosition < resolvedFinalPosition); | |
114 } | 115 } |
115 | 116 |
116 bool operator==(const GridSpan& o) const | 117 bool operator==(const GridSpan& o) const |
117 { | 118 { |
118 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedF inalPosition == o.resolvedFinalPosition; | 119 ASSERT(isDefinite()); |
cbiesinger
2015/11/19 22:22:08
Seems slightly weird that you can't compare a defi
Manuel Rego
2015/11/20 08:53:45
We don't need it right now, but I agree that it mi
| |
120 return m_resolvedInitialPosition == o.m_resolvedInitialPosition && m_res olvedFinalPosition == o.m_resolvedFinalPosition; | |
119 } | 121 } |
120 | 122 |
121 size_t integerSpan() const | 123 size_t integerSpan() const |
122 { | 124 { |
123 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt(); | 125 ASSERT(isDefinite()); |
126 return m_resolvedFinalPosition.toInt() - m_resolvedInitialPosition.toInt (); | |
124 } | 127 } |
125 | 128 |
126 GridResolvedPosition resolvedInitialPosition; | 129 const GridResolvedPosition& resolvedInitialPosition() const |
127 GridResolvedPosition resolvedFinalPosition; | 130 { |
131 ASSERT(isDefinite()); | |
132 return m_resolvedInitialPosition; | |
133 } | |
134 | |
135 const GridResolvedPosition& resolvedFinalPosition() const | |
136 { | |
137 ASSERT(isDefinite()); | |
138 return m_resolvedFinalPosition; | |
139 } | |
128 | 140 |
129 typedef GridResolvedPosition iterator; | 141 typedef GridResolvedPosition iterator; |
130 | 142 |
131 iterator begin() const | 143 iterator begin() const |
132 { | 144 { |
133 return resolvedInitialPosition; | 145 ASSERT(isDefinite()); |
146 return m_resolvedInitialPosition; | |
134 } | 147 } |
135 | 148 |
136 iterator end() const | 149 iterator end() const |
137 { | 150 { |
138 return resolvedFinalPosition; | 151 ASSERT(isDefinite()); |
152 return m_resolvedFinalPosition; | |
139 } | 153 } |
154 | |
155 bool isDefinite() const | |
156 { | |
157 return m_definite; | |
158 } | |
159 | |
160 private: | |
161 | |
162 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridReso lvedPosition& resolvedFinalPosition) | |
163 : m_resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kG ridMaxTracks - 1)) | |
164 , m_resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridM axTracks)) | |
cbiesinger
2015/11/19 22:22:08
Why didn't you also initialize m_definite in the c
Manuel Rego
2015/11/20 08:53:45
Ups, I forgot to do it there. Fixed now. :-)
| |
165 { | |
166 ASSERT(resolvedInitialPosition < resolvedFinalPosition); | |
167 m_definite = true; | |
168 } | |
169 | |
170 GridResolvedPosition m_resolvedInitialPosition; | |
171 GridResolvedPosition m_resolvedFinalPosition; | |
172 bool m_definite; | |
140 }; | 173 }; |
141 | 174 |
142 // This represents a grid area that spans in both rows' and columns' direction. | 175 // This represents a grid area that spans in both rows' and columns' direction. |
143 struct GridCoordinate { | 176 struct GridCoordinate { |
144 USING_FAST_MALLOC(GridCoordinate); | 177 USING_FAST_MALLOC(GridCoordinate); |
145 public: | 178 public: |
146 // HashMap requires a default constuctor. | 179 // HashMap requires a default constuctor. |
147 GridCoordinate() | 180 GridCoordinate() |
148 : columns(0, 1) | 181 : columns(GridSpan::indefiniteGridSpan()) |
149 , rows(0, 1) | 182 , rows(GridSpan::indefiniteGridSpan()) |
150 { | 183 { |
151 } | 184 } |
152 | 185 |
153 GridCoordinate(const GridSpan& r, const GridSpan& c) | 186 GridCoordinate(const GridSpan& r, const GridSpan& c) |
154 : columns(c) | 187 : columns(c) |
155 , rows(r) | 188 , rows(r) |
156 { | 189 { |
157 } | 190 } |
158 | 191 |
159 bool operator==(const GridCoordinate& o) const | 192 bool operator==(const GridCoordinate& o) const |
160 { | 193 { |
161 return columns == o.columns && rows == o.rows; | 194 return columns == o.columns && rows == o.rows; |
162 } | 195 } |
163 | 196 |
164 bool operator!=(const GridCoordinate& o) const | 197 bool operator!=(const GridCoordinate& o) const |
165 { | 198 { |
166 return !(*this == o); | 199 return !(*this == o); |
167 } | 200 } |
168 | 201 |
169 GridResolvedPosition positionForSide(GridPositionSide side) const | 202 GridResolvedPosition positionForSide(GridPositionSide side) const |
170 { | 203 { |
171 switch (side) { | 204 switch (side) { |
172 case ColumnStartSide: | 205 case ColumnStartSide: |
173 return columns.resolvedInitialPosition; | 206 return columns.resolvedInitialPosition(); |
174 case ColumnEndSide: | 207 case ColumnEndSide: |
175 return columns.resolvedFinalPosition; | 208 return columns.resolvedFinalPosition(); |
176 case RowStartSide: | 209 case RowStartSide: |
177 return rows.resolvedInitialPosition; | 210 return rows.resolvedInitialPosition(); |
178 case RowEndSide: | 211 case RowEndSide: |
179 return rows.resolvedFinalPosition; | 212 return rows.resolvedFinalPosition(); |
180 } | 213 } |
181 ASSERT_NOT_REACHED(); | 214 ASSERT_NOT_REACHED(); |
182 return 0; | 215 return 0; |
183 } | 216 } |
184 | 217 |
185 GridSpan columns; | 218 GridSpan columns; |
186 GridSpan rows; | 219 GridSpan rows; |
187 }; | 220 }; |
188 | 221 |
189 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; | 222 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; |
190 | 223 |
191 } // namespace blink | 224 } // namespace blink |
192 | 225 |
193 #endif // GridCoordinate_h | 226 #endif // GridCoordinate_h |
OLD | NEW |