Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "simplerangelist.hxx"
21 : #include "rangelst.hxx"
22 :
23 : using ::std::list;
24 : using ::std::pair;
25 : using ::std::max;
26 :
27 : // ============================================================================
28 :
29 1212 : ScSimpleRangeList::Range::Range(SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2) :
30 1212 : mnCol1(nCol1), mnRow1(nRow1), mnCol2(nCol2), mnRow2(nRow2) {}
31 :
32 : // ----------------------------------------------------------------------------
33 :
34 534 : ScSimpleRangeList::ScSimpleRangeList()
35 : {
36 534 : }
37 :
38 : namespace {
39 :
40 5304 : bool maybeJoin(ScSimpleRangeList::Range& rOld, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2)
41 : {
42 5304 : if (rOld.mnRow1 == nRow1 && rOld.mnRow2 == nRow2)
43 : {
44 : // Check their column spans to see if they overlap.
45 186 : if (rOld.mnCol1 == nCol1)
46 : {
47 : // Their share the start column position.
48 0 : rOld.mnCol2 = max(rOld.mnCol2, nCol2);
49 0 : return true;
50 : }
51 186 : else if (rOld.mnCol1 < nCol1)
52 : {
53 : // Old range sits on the left.
54 186 : if (nCol1 - rOld.mnCol2 <= 1)
55 : {
56 18 : rOld.mnCol2 = max(rOld.mnCol2, nCol2);
57 18 : return true;
58 : }
59 : }
60 0 : else if (nCol1 < rOld.mnCol1)
61 : {
62 : // New range sits on the left.
63 0 : if (nCol1 - rOld.mnCol2 <= 1)
64 : {
65 0 : rOld.mnCol1 = nCol1;
66 0 : rOld.mnCol2 = max(rOld.mnCol2, nCol2);
67 0 : return true;
68 : }
69 : }
70 : }
71 :
72 5286 : if (rOld.mnCol1 == nCol1 && rOld.mnCol2 == nCol2)
73 : {
74 4624 : if (rOld.mnRow1 == nRow1)
75 : {
76 : // Their share the start row position.
77 0 : rOld.mnRow2 = max(rOld.mnRow2, nRow2);
78 0 : return true;
79 : }
80 4624 : else if (rOld.mnRow1 < nRow1)
81 : {
82 : // Old range sits above.
83 4624 : if (nRow1 - rOld.mnRow2 <= 1)
84 : {
85 4608 : rOld.mnRow2 = max(rOld.mnRow2, nRow2);
86 4608 : return true;
87 : }
88 : }
89 0 : else if (nRow1 < rOld.mnRow1)
90 : {
91 : // New range sits above.
92 0 : if (nRow1 - rOld.mnRow2 <= 1)
93 : {
94 0 : rOld.mnRow1 = nRow1;
95 0 : rOld.mnRow2 = max(rOld.mnRow2, nRow2);
96 0 : return true;
97 : }
98 : }
99 : }
100 :
101 678 : return false;
102 : }
103 :
104 : }
105 :
106 5838 : void ScSimpleRangeList::addRange(const ScRange& rRange)
107 : {
108 5838 : SCCOL nCol1 = rRange.aStart.Col();
109 5838 : SCROW nRow1 = rRange.aStart.Row();
110 5838 : SCTAB nTab1 = rRange.aStart.Tab();
111 5838 : SCCOL nCol2 = rRange.aEnd.Col();
112 5838 : SCROW nRow2 = rRange.aEnd.Row();
113 5838 : SCTAB nTab2 = rRange.aEnd.Tab();
114 :
115 11676 : for (SCTAB nTab = nTab1; nTab <= nTab2; ++nTab)
116 : {
117 5838 : RangeListRef pRef = findTab(nTab);
118 5838 : if (!pRef)
119 : // This should never happen!
120 5838 : return;
121 :
122 5838 : if (pRef->empty() || !maybeJoin(pRef->back(), nCol1, nRow1, nCol2, nRow2))
123 : // Not joinable. Append it to the list.
124 1212 : pRef->push_back(Range(nCol1, nRow1, nCol2, nRow2));
125 5838 : }
126 : }
127 :
128 102 : void ScSimpleRangeList::insertCol(SCCOL nCol, SCTAB nTab)
129 : {
130 102 : RangeListRef pRef = findTab(nTab);
131 102 : if (!pRef)
132 : // This should never happen!
133 102 : return;
134 :
135 102 : list<Range>::iterator itr = pRef->begin(), itrEnd = pRef->end();
136 212 : for (; itr != itrEnd; ++itr)
137 : {
138 110 : Range& r = *itr;
139 110 : if (r.mnCol2 < nCol)
140 : // insertion point to the right of the range.
141 110 : continue;
142 :
143 0 : if (nCol <= r.mnCol1)
144 : {
145 : // insertion point to the left of the range.
146 0 : ++r.mnCol1;
147 0 : ++r.mnCol2;
148 : }
149 0 : else if (nCol <= r.mnCol2)
150 : {
151 : // insertion point cuts through the range.
152 0 : ++r.mnCol2;
153 : }
154 102 : }
155 : }
156 :
157 534 : void ScSimpleRangeList::getRangeList(list<ScRange>& rList) const
158 : {
159 534 : list<ScRange> aList;
160 1068 : for (TabType::const_iterator itrTab = maTabs.begin(), itrTabEnd = maTabs.end(); itrTab != itrTabEnd; ++itrTab)
161 : {
162 534 : SCTAB nTab = itrTab->first;
163 534 : const RangeListRef& pRanges = itrTab->second;
164 534 : list<Range>::const_iterator itr = pRanges->begin(), itrEnd = pRanges->end();
165 1746 : for (; itr != itrEnd; ++itr)
166 : {
167 1212 : const Range& r = *itr;
168 1212 : aList.push_back(ScRange(r.mnCol1, r.mnRow1, nTab, r.mnCol2, r.mnRow2, nTab));
169 : }
170 : }
171 534 : rList.swap(aList);
172 534 : }
173 :
174 532 : void ScSimpleRangeList::clear()
175 : {
176 532 : maTabs.clear();
177 532 : }
178 :
179 5940 : ScSimpleRangeList::RangeListRef ScSimpleRangeList::findTab(SCTAB nTab)
180 : {
181 5940 : TabType::iterator itr = maTabs.find(nTab);
182 5940 : if (itr == maTabs.end())
183 : {
184 534 : RangeListRef p(new list<Range>);
185 534 : pair<TabType::iterator, bool> r = maTabs.insert(TabType::value_type(nTab, p));
186 534 : if (!r.second)
187 0 : return RangeListRef();
188 534 : itr = r.first;
189 : }
190 :
191 5940 : return itr->second;
192 : }
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|