fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / core / tool / simplerangelist.cxx
blob4c428e21d2406110fc79045c78ea1f57f754a3f7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "simplerangelist.hxx"
21 #include "rangelst.hxx"
23 using ::std::list;
24 using ::std::pair;
25 using ::std::max;
27 ScSimpleRangeList::Range::Range(SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2) :
28 mnCol1(nCol1), mnRow1(nRow1), mnCol2(nCol2), mnRow2(nRow2) {}
30 ScSimpleRangeList::ScSimpleRangeList()
34 namespace {
36 bool maybeJoin(ScSimpleRangeList::Range& rOld, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2)
38 if (rOld.mnRow1 == nRow1 && rOld.mnRow2 == nRow2)
40 // Check their column spans to see if they overlap.
41 if (rOld.mnCol1 == nCol1)
43 // Their share the start column position.
44 rOld.mnCol2 = max(rOld.mnCol2, nCol2);
45 return true;
47 else if (rOld.mnCol1 < nCol1)
49 // Old range sits on the left.
50 if (nCol1 - rOld.mnCol2 <= 1)
52 rOld.mnCol2 = max(rOld.mnCol2, nCol2);
53 return true;
56 else if (nCol1 < rOld.mnCol1)
58 // New range sits on the left.
59 if (nCol1 - rOld.mnCol2 <= 1)
61 rOld.mnCol1 = nCol1;
62 rOld.mnCol2 = max(rOld.mnCol2, nCol2);
63 return true;
68 if (rOld.mnCol1 == nCol1 && rOld.mnCol2 == nCol2)
70 if (rOld.mnRow1 == nRow1)
72 // Their share the start row position.
73 rOld.mnRow2 = max(rOld.mnRow2, nRow2);
74 return true;
76 else if (rOld.mnRow1 < nRow1)
78 // Old range sits above.
79 if (nRow1 - rOld.mnRow2 <= 1)
81 rOld.mnRow2 = max(rOld.mnRow2, nRow2);
82 return true;
85 else if (nRow1 < rOld.mnRow1)
87 // New range sits above.
88 if (nRow1 - rOld.mnRow2 <= 1)
90 rOld.mnRow1 = nRow1;
91 rOld.mnRow2 = max(rOld.mnRow2, nRow2);
92 return true;
97 return false;
102 void ScSimpleRangeList::addRange(const ScRange& rRange)
104 SCCOL nCol1 = rRange.aStart.Col();
105 SCROW nRow1 = rRange.aStart.Row();
106 SCTAB nTab1 = rRange.aStart.Tab();
107 SCCOL nCol2 = rRange.aEnd.Col();
108 SCROW nRow2 = rRange.aEnd.Row();
109 SCTAB nTab2 = rRange.aEnd.Tab();
111 for (SCTAB nTab = nTab1; nTab <= nTab2; ++nTab)
113 RangeListRef pRef = findTab(nTab);
114 if (!pRef)
115 // This should never happen!
116 return;
118 if (pRef->empty() || !maybeJoin(pRef->back(), nCol1, nRow1, nCol2, nRow2))
119 // Not joinable. Append it to the list.
120 pRef->push_back(Range(nCol1, nRow1, nCol2, nRow2));
124 void ScSimpleRangeList::insertCol(SCCOL nCol, SCTAB nTab)
126 RangeListRef pRef = findTab(nTab);
127 if (!pRef)
128 // This should never happen!
129 return;
131 list<Range>::iterator itr = pRef->begin(), itrEnd = pRef->end();
132 for (; itr != itrEnd; ++itr)
134 Range& r = *itr;
135 if (r.mnCol2 < nCol)
136 // insertion point to the right of the range.
137 continue;
139 if (nCol <= r.mnCol1)
141 // insertion point to the left of the range.
142 ++r.mnCol1;
143 ++r.mnCol2;
145 else if (nCol <= r.mnCol2)
147 // insertion point cuts through the range.
148 ++r.mnCol2;
153 void ScSimpleRangeList::getRangeList(list<ScRange>& rList) const
155 list<ScRange> aList;
156 for (TabType::const_iterator itrTab = maTabs.begin(), itrTabEnd = maTabs.end(); itrTab != itrTabEnd; ++itrTab)
158 SCTAB nTab = itrTab->first;
159 const RangeListRef& pRanges = itrTab->second;
160 list<Range>::const_iterator itr = pRanges->begin(), itrEnd = pRanges->end();
161 for (; itr != itrEnd; ++itr)
163 const Range& r = *itr;
164 aList.push_back(ScRange(r.mnCol1, r.mnRow1, nTab, r.mnCol2, r.mnRow2, nTab));
167 rList.swap(aList);
170 void ScSimpleRangeList::clear()
172 maTabs.clear();
175 ScSimpleRangeList::RangeListRef ScSimpleRangeList::findTab(SCTAB nTab)
177 TabType::iterator itr = maTabs.find(nTab);
178 if (itr == maTabs.end())
180 RangeListRef p(new list<Range>);
181 pair<TabType::iterator, bool> r = maTabs.insert(TabType::value_type(nTab, p));
182 if (!r.second)
183 return RangeListRef();
184 itr = r.first;
187 return itr->second;
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */