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, Definite
); |
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 return GridSpan(0, 1, Indefinite); |
| 61 } |
| 62 |
| 63 static GridSpan definiteGridSpanWithSpanAgainstOpposite(const GridResolvedPo
sition& resolvedOppositePosition, const GridPosition& position, GridPositionSide
side) |
58 { | 64 { |
59 size_t positionOffset = position.spanPosition(); | 65 size_t positionOffset = position.spanPosition(); |
60 if (side == ColumnStartSide || side == RowStartSide) { | 66 if (side == ColumnStartSide || side == RowStartSide) { |
61 if (resolvedOppositePosition == 0) | 67 if (resolvedOppositePosition == 0) |
62 return GridSpan::create(resolvedOppositePosition, resolvedOpposi
tePosition.next()); | 68 return GridSpan::definiteGridSpan(resolvedOppositePosition, reso
lvedOppositePosition.next()); |
63 | 69 |
64 GridResolvedPosition initialResolvedPosition = GridResolvedPosition(
std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset)); | 70 GridResolvedPosition initialResolvedPosition = GridResolvedPosition(
std::max<int>(0, resolvedOppositePosition.toInt() - positionOffset)); |
65 return GridSpan::create(initialResolvedPosition, resolvedOppositePos
ition); | 71 return GridSpan::definiteGridSpan(initialResolvedPosition, resolvedO
ppositePosition); |
66 } | 72 } |
67 | 73 |
68 return GridSpan::create(resolvedOppositePosition, GridResolvedPosition(r
esolvedOppositePosition.toInt() + positionOffset)); | 74 return GridSpan::definiteGridSpan(resolvedOppositePosition, GridResolved
Position(resolvedOppositePosition.toInt() + positionOffset)); |
69 } | 75 } |
70 | 76 |
71 static PassOwnPtr<GridSpan> createWithNamedSpanAgainstOpposite(const GridRes
olvedPosition& resolvedOppositePosition, const GridPosition& position, GridPosit
ionSide side, const Vector<size_t>& gridLines) | 77 static GridSpan definiteGridSpanWithNamedSpanAgainstOpposite(const GridResol
vedPosition& resolvedOppositePosition, const GridPosition& position, GridPositio
nSide side, const Vector<size_t>& gridLines) |
72 { | 78 { |
73 if (side == RowStartSide || side == ColumnStartSide) | 79 if (side == RowStartSide || side == ColumnStartSide) |
74 return createWithInitialNamedSpanAgainstOpposite(resolvedOppositePos
ition, position, gridLines); | 80 return definiteGridSpanWithInitialNamedSpanAgainstOpposite(resolvedO
ppositePosition, position, gridLines); |
75 | 81 |
76 return createWithFinalNamedSpanAgainstOpposite(resolvedOppositePosition,
position, gridLines); | 82 return definiteGridSpanWithFinalNamedSpanAgainstOpposite(resolvedOpposit
ePosition, position, gridLines); |
77 } | 83 } |
78 | 84 |
79 static PassOwnPtr<GridSpan> createWithInitialNamedSpanAgainstOpposite(const
GridResolvedPosition& resolvedOppositePosition, const GridPosition& position, co
nst Vector<size_t>& gridLines) | 85 static GridSpan definiteGridSpanWithInitialNamedSpanAgainstOpposite(const Gr
idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons
t Vector<size_t>& gridLines) |
80 { | 86 { |
81 if (resolvedOppositePosition == 0) | 87 if (resolvedOppositePosition == 0) |
82 return GridSpan::create(resolvedOppositePosition, resolvedOppositePo
sition.next()); | 88 return GridSpan::definiteGridSpan(resolvedOppositePosition, resolved
OppositePosition.next()); |
83 | 89 |
84 size_t firstLineBeforeOppositePositionIndex = 0; | 90 size_t firstLineBeforeOppositePositionIndex = 0; |
85 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin
es.begin(), gridLines.end(), resolvedOppositePosition.toInt()); | 91 const size_t* firstLineBeforeOppositePosition = std::lower_bound(gridLin
es.begin(), gridLines.end(), resolvedOppositePosition.toInt()); |
86 if (firstLineBeforeOppositePosition != gridLines.end()) | 92 if (firstLineBeforeOppositePosition != gridLines.end()) |
87 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi
on - gridLines.begin(); | 93 firstLineBeforeOppositePositionIndex = firstLineBeforeOppositePositi
on - gridLines.begin(); |
88 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionI
ndex - position.spanPosition()); | 94 size_t gridLineIndex = std::max<int>(0, firstLineBeforeOppositePositionI
ndex - position.spanPosition()); |
89 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gri
dLines[gridLineIndex]); | 95 GridResolvedPosition resolvedGridLinePosition = GridResolvedPosition(gri
dLines[gridLineIndex]); |
90 if (resolvedGridLinePosition >= resolvedOppositePosition) | 96 if (resolvedGridLinePosition >= resolvedOppositePosition) |
91 resolvedGridLinePosition = resolvedOppositePosition.prev(); | 97 resolvedGridLinePosition = resolvedOppositePosition.prev(); |
92 return GridSpan::create(resolvedGridLinePosition, resolvedOppositePositi
on); | 98 return GridSpan::definiteGridSpan(resolvedGridLinePosition, resolvedOppo
sitePosition); |
93 } | 99 } |
94 | 100 |
95 static PassOwnPtr<GridSpan> createWithFinalNamedSpanAgainstOpposite(const Gr
idResolvedPosition& resolvedOppositePosition, const GridPosition& position, cons
t Vector<size_t>& gridLines) | 101 static GridSpan definiteGridSpanWithFinalNamedSpanAgainstOpposite(const Grid
ResolvedPosition& resolvedOppositePosition, const GridPosition& position, const
Vector<size_t>& gridLines) |
96 { | 102 { |
97 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; | 103 size_t firstLineAfterOppositePositionIndex = gridLines.size() - 1; |
98 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine
s.begin(), gridLines.end(), resolvedOppositePosition.toInt()); | 104 const size_t* firstLineAfterOppositePosition = std::upper_bound(gridLine
s.begin(), gridLines.end(), resolvedOppositePosition.toInt()); |
99 if (firstLineAfterOppositePosition != gridLines.end()) | 105 if (firstLineAfterOppositePosition != gridLines.end()) |
100 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition
- gridLines.begin(); | 106 firstLineAfterOppositePositionIndex = firstLineAfterOppositePosition
- gridLines.begin(); |
101 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppo
sitePositionIndex + position.spanPosition() - 1); | 107 size_t gridLineIndex = std::min(gridLines.size() - 1, firstLineAfterOppo
sitePositionIndex + position.spanPosition() - 1); |
102 GridResolvedPosition resolvedGridLinePosition = gridLines[gridLineIndex]
; | 108 GridResolvedPosition resolvedGridLinePosition = gridLines[gridLineIndex]
; |
103 if (resolvedGridLinePosition <= resolvedOppositePosition) | 109 if (resolvedGridLinePosition <= resolvedOppositePosition) |
104 resolvedGridLinePosition = resolvedOppositePosition.next(); | 110 resolvedGridLinePosition = resolvedOppositePosition.next(); |
105 | 111 |
106 return GridSpan::create(resolvedOppositePosition, resolvedGridLinePositi
on); | 112 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 } | 113 } |
115 | 114 |
116 bool operator==(const GridSpan& o) const | 115 bool operator==(const GridSpan& o) const |
117 { | 116 { |
118 return resolvedInitialPosition == o.resolvedInitialPosition && resolvedF
inalPosition == o.resolvedFinalPosition; | 117 return m_type == o.m_type && m_resolvedInitialPosition == o.m_resolvedIn
itialPosition && m_resolvedFinalPosition == o.m_resolvedFinalPosition; |
119 } | 118 } |
120 | 119 |
121 size_t integerSpan() const | 120 size_t integerSpan() const |
122 { | 121 { |
123 return resolvedFinalPosition.toInt() - resolvedInitialPosition.toInt(); | 122 ASSERT(isDefinite()); |
| 123 return m_resolvedFinalPosition.toInt() - m_resolvedInitialPosition.toInt
(); |
124 } | 124 } |
125 | 125 |
126 GridResolvedPosition resolvedInitialPosition; | 126 const GridResolvedPosition& resolvedInitialPosition() const |
127 GridResolvedPosition resolvedFinalPosition; | 127 { |
| 128 ASSERT(isDefinite()); |
| 129 return m_resolvedInitialPosition; |
| 130 } |
| 131 |
| 132 const GridResolvedPosition& resolvedFinalPosition() const |
| 133 { |
| 134 ASSERT(isDefinite()); |
| 135 return m_resolvedFinalPosition; |
| 136 } |
128 | 137 |
129 typedef GridResolvedPosition iterator; | 138 typedef GridResolvedPosition iterator; |
130 | 139 |
131 iterator begin() const | 140 iterator begin() const |
132 { | 141 { |
133 return resolvedInitialPosition; | 142 ASSERT(isDefinite()); |
| 143 return m_resolvedInitialPosition; |
134 } | 144 } |
135 | 145 |
136 iterator end() const | 146 iterator end() const |
137 { | 147 { |
138 return resolvedFinalPosition; | 148 ASSERT(isDefinite()); |
| 149 return m_resolvedFinalPosition; |
139 } | 150 } |
| 151 |
| 152 bool isDefinite() const |
| 153 { |
| 154 return m_type == Definite; |
| 155 } |
| 156 |
| 157 private: |
| 158 |
| 159 enum GridSpanType {Definite, Indefinite}; |
| 160 |
| 161 GridSpan(const GridResolvedPosition& resolvedInitialPosition, const GridReso
lvedPosition& resolvedFinalPosition, GridSpanType type) |
| 162 : m_resolvedInitialPosition(std::min(resolvedInitialPosition.toInt(), kG
ridMaxTracks - 1)) |
| 163 , m_resolvedFinalPosition(std::min(resolvedFinalPosition.toInt(), kGridM
axTracks)) |
| 164 , m_type(type) |
| 165 { |
| 166 ASSERT(resolvedInitialPosition < resolvedFinalPosition); |
| 167 } |
| 168 |
| 169 GridResolvedPosition m_resolvedInitialPosition; |
| 170 GridResolvedPosition m_resolvedFinalPosition; |
| 171 GridSpanType m_type; |
140 }; | 172 }; |
141 | 173 |
142 // This represents a grid area that spans in both rows' and columns' direction. | 174 // This represents a grid area that spans in both rows' and columns' direction. |
143 struct GridCoordinate { | 175 struct GridCoordinate { |
144 USING_FAST_MALLOC(GridCoordinate); | 176 USING_FAST_MALLOC(GridCoordinate); |
145 public: | 177 public: |
146 // HashMap requires a default constuctor. | 178 // HashMap requires a default constuctor. |
147 GridCoordinate() | 179 GridCoordinate() |
148 : columns(0, 1) | 180 : columns(GridSpan::indefiniteGridSpan()) |
149 , rows(0, 1) | 181 , rows(GridSpan::indefiniteGridSpan()) |
150 { | 182 { |
151 } | 183 } |
152 | 184 |
153 GridCoordinate(const GridSpan& r, const GridSpan& c) | 185 GridCoordinate(const GridSpan& r, const GridSpan& c) |
154 : columns(c) | 186 : columns(c) |
155 , rows(r) | 187 , rows(r) |
156 { | 188 { |
157 } | 189 } |
158 | 190 |
159 bool operator==(const GridCoordinate& o) const | 191 bool operator==(const GridCoordinate& o) const |
160 { | 192 { |
161 return columns == o.columns && rows == o.rows; | 193 return columns == o.columns && rows == o.rows; |
162 } | 194 } |
163 | 195 |
164 bool operator!=(const GridCoordinate& o) const | 196 bool operator!=(const GridCoordinate& o) const |
165 { | 197 { |
166 return !(*this == o); | 198 return !(*this == o); |
167 } | 199 } |
168 | 200 |
169 GridResolvedPosition positionForSide(GridPositionSide side) const | 201 GridResolvedPosition positionForSide(GridPositionSide side) const |
170 { | 202 { |
171 switch (side) { | 203 switch (side) { |
172 case ColumnStartSide: | 204 case ColumnStartSide: |
173 return columns.resolvedInitialPosition; | 205 return columns.resolvedInitialPosition(); |
174 case ColumnEndSide: | 206 case ColumnEndSide: |
175 return columns.resolvedFinalPosition; | 207 return columns.resolvedFinalPosition(); |
176 case RowStartSide: | 208 case RowStartSide: |
177 return rows.resolvedInitialPosition; | 209 return rows.resolvedInitialPosition(); |
178 case RowEndSide: | 210 case RowEndSide: |
179 return rows.resolvedFinalPosition; | 211 return rows.resolvedFinalPosition(); |
180 } | 212 } |
181 ASSERT_NOT_REACHED(); | 213 ASSERT_NOT_REACHED(); |
182 return 0; | 214 return 0; |
183 } | 215 } |
184 | 216 |
185 GridSpan columns; | 217 GridSpan columns; |
186 GridSpan rows; | 218 GridSpan rows; |
187 }; | 219 }; |
188 | 220 |
189 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; | 221 typedef HashMap<String, GridCoordinate> NamedGridAreaMap; |
190 | 222 |
191 } // namespace blink | 223 } // namespace blink |
192 | 224 |
193 #endif // GridCoordinate_h | 225 #endif // GridCoordinate_h |
OLD | NEW |