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 461 : ScSimpleRangeList::Range::Range(SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2) :
30 461 : mnCol1(nCol1), mnRow1(nRow1), mnCol2(nCol2), mnRow2(nRow2) {}
31 :
32 : // ----------------------------------------------------------------------------
33 :
34 173 : ScSimpleRangeList::ScSimpleRangeList()
35 : {
36 173 : }
37 :
38 : namespace {
39 :
40 2491 : bool maybeJoin(ScSimpleRangeList::Range& rOld, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2)
41 : {
42 2491 : if (rOld.mnRow1 == nRow1 && rOld.mnRow2 == nRow2)
43 : {
44 : // Check their column spans to see if they overlap.
45 78 : 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 78 : else if (rOld.mnCol1 < nCol1)
52 : {
53 : // Old range sits on the left.
54 78 : if (nCol1 - rOld.mnCol2 <= 1)
55 : {
56 7 : rOld.mnCol2 = max(rOld.mnCol2, nCol2);
57 7 : 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 2484 : if (rOld.mnCol1 == nCol1 && rOld.mnCol2 == nCol2)
73 : {
74 2198 : 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 2198 : else if (rOld.mnRow1 < nRow1)
81 : {
82 : // Old range sits above.
83 2198 : if (nRow1 - rOld.mnRow2 <= 1)
84 : {
85 2196 : rOld.mnRow2 = max(rOld.mnRow2, nRow2);
86 2196 : 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 288 : return false;
102 : }
103 :
104 : }
105 :
106 2664 : void ScSimpleRangeList::addRange(const ScRange& rRange)
107 : {
108 2664 : SCCOL nCol1 = rRange.aStart.Col();
109 2664 : SCROW nRow1 = rRange.aStart.Row();
110 2664 : SCTAB nTab1 = rRange.aStart.Tab();
111 2664 : SCCOL nCol2 = rRange.aEnd.Col();
112 2664 : SCROW nRow2 = rRange.aEnd.Row();
113 2664 : SCTAB nTab2 = rRange.aEnd.Tab();
114 :
115 5328 : for (SCTAB nTab = nTab1; nTab <= nTab2; ++nTab)
116 : {
117 2664 : RangeListRef pRef = findTab(nTab);
118 2664 : if (!pRef)
119 : // This should never happen!
120 2664 : return;
121 :
122 2664 : if (pRef->empty() || !maybeJoin(pRef->back(), nCol1, nRow1, nCol2, nRow2))
123 : // Not joinable. Append it to the list.
124 461 : pRef->push_back(Range(nCol1, nRow1, nCol2, nRow2));
125 2664 : }
126 : }
127 :
128 43 : void ScSimpleRangeList::insertCol(SCCOL nCol, SCTAB nTab)
129 : {
130 43 : RangeListRef pRef = findTab(nTab);
131 43 : if (!pRef)
132 : // This should never happen!
133 43 : return;
134 :
135 43 : list<Range>::iterator itr = pRef->begin(), itrEnd = pRef->end();
136 90 : for (; itr != itrEnd; ++itr)
137 : {
138 47 : Range& r = *itr;
139 47 : if (r.mnCol2 < nCol)
140 : // insertion point to the right of the range.
141 47 : 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 43 : }
155 : }
156 :
157 173 : void ScSimpleRangeList::getRangeList(list<ScRange>& rList) const
158 : {
159 173 : list<ScRange> aList;
160 346 : for (TabType::const_iterator itrTab = maTabs.begin(), itrTabEnd = maTabs.end(); itrTab != itrTabEnd; ++itrTab)
161 : {
162 173 : SCTAB nTab = itrTab->first;
163 173 : const RangeListRef& pRanges = itrTab->second;
164 173 : list<Range>::const_iterator itr = pRanges->begin(), itrEnd = pRanges->end();
165 634 : for (; itr != itrEnd; ++itr)
166 : {
167 461 : const Range& r = *itr;
168 461 : aList.push_back(ScRange(r.mnCol1, r.mnRow1, nTab, r.mnCol2, r.mnRow2, nTab));
169 : }
170 : }
171 173 : rList.swap(aList);
172 173 : }
173 :
174 172 : void ScSimpleRangeList::clear()
175 : {
176 172 : maTabs.clear();
177 172 : }
178 :
179 2707 : ScSimpleRangeList::RangeListRef ScSimpleRangeList::findTab(SCTAB nTab)
180 : {
181 2707 : TabType::iterator itr = maTabs.find(nTab);
182 2707 : if (itr == maTabs.end())
183 : {
184 173 : RangeListRef p(new list<Range>);
185 173 : pair<TabType::iterator, bool> r = maTabs.insert(TabType::value_type(nTab, p));
186 173 : if (!r.second)
187 0 : return RangeListRef();
188 173 : itr = r.first;
189 : }
190 :
191 2707 : return itr->second;
192 : }
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|