1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
27 ScSimpleRangeList::Range::Range(SCCOL nCol1
, SCROW nRow1
, SCCOL nCol2
, SCROW nRow2
) :
28 mnCol1(nCol1
), mnRow1(nRow1
), mnCol2(nCol2
), mnRow2(nRow2
) {}
30 ScSimpleRangeList::ScSimpleRangeList()
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
);
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
);
56 else if (nCol1
< rOld
.mnCol1
)
58 // New range sits on the left.
59 if (nCol1
- rOld
.mnCol2
<= 1)
62 rOld
.mnCol2
= max(rOld
.mnCol2
, nCol2
);
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
);
76 else if (rOld
.mnRow1
< nRow1
)
78 // Old range sits above.
79 if (nRow1
- rOld
.mnRow2
<= 1)
81 rOld
.mnRow2
= max(rOld
.mnRow2
, nRow2
);
85 else if (nRow1
< rOld
.mnRow1
)
87 // New range sits above.
88 if (nRow1
- rOld
.mnRow2
<= 1)
91 rOld
.mnRow2
= max(rOld
.mnRow2
, nRow2
);
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
);
115 // This should never happen!
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
);
128 // This should never happen!
131 list
<Range
>::iterator itr
= pRef
->begin(), itrEnd
= pRef
->end();
132 for (; itr
!= itrEnd
; ++itr
)
136 // insertion point to the right of the range.
139 if (nCol
<= r
.mnCol1
)
141 // insertion point to the left of the range.
145 else if (nCol
<= r
.mnCol2
)
147 // insertion point cuts through the range.
153 void ScSimpleRangeList::getRangeList(list
<ScRange
>& rList
) const
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
));
170 void ScSimpleRangeList::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
));
183 return RangeListRef();
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */