Update ooo320-m1
[ooovba.git] / sc / source / core / data / segmenttree.cxx
blobbe8e408b4dbcbf7e9e55a83812c16792818fb7da
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: compressedarray.hxx,v $
10 * $Revision: 1.7.32.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #include "segmenttree.hxx"
35 #include "mdds/flatsegmenttree.hxx"
37 #define USE_TREE_SEARCH 1
39 class ScFlatBoolSegmentsImpl
41 public:
42 struct RangeData
44 SCCOLROW mnPos1;
45 SCCOLROW mnPos2;
46 bool mbValue;
48 ScFlatBoolSegmentsImpl(SCCOLROW nMax);
49 ~ScFlatBoolSegmentsImpl();
51 void setTrue(SCCOLROW nPos1, SCCOLROW nPos2);
52 void setFalse(SCCOLROW nPos1, SCCOLROW nPos2);
53 bool getValue(SCCOLROW nPos);
54 bool getRangeData(SCCOLROW nPos, RangeData& rData);
55 void removeSegment(SCCOLROW nPos1, SCCOLROW nPos2);
56 void insertSegment(SCCOLROW nPos, SCCOLROW nSize, bool bSkipStartBoundary);
58 private:
59 ScFlatBoolSegmentsImpl();
60 ScFlatBoolSegmentsImpl(const ScFlatBoolSegmentsImpl&);
62 ::mdds::flat_segment_tree<SCCOLROW, bool> maSegments;
65 ScFlatBoolSegmentsImpl::ScFlatBoolSegmentsImpl(SCCOLROW nMax) :
66 maSegments(0, nMax+1, false)
70 ScFlatBoolSegmentsImpl::~ScFlatBoolSegmentsImpl()
74 void ScFlatBoolSegmentsImpl::setTrue(SCCOLROW nPos1, SCCOLROW nPos2)
76 maSegments.insert_segment(nPos1, nPos2+1, true);
79 void ScFlatBoolSegmentsImpl::setFalse(SCCOLROW nPos1, SCCOLROW nPos2)
81 maSegments.insert_segment(nPos1, nPos2+1, false);
84 bool ScFlatBoolSegmentsImpl::getValue(SCCOLROW nPos)
86 bool bValue = false;
87 #if USE_TREE_SEARCH
88 if (!maSegments.is_tree_valid())
89 maSegments.build_tree();
91 maSegments.search_tree(nPos, bValue);
92 #else
93 maSegments.search(nPos, bValue);
94 #endif
95 return bValue;
98 bool ScFlatBoolSegmentsImpl::getRangeData(SCCOLROW nPos, RangeData& rData)
100 #if USE_TREE_SEARCH
101 if (!maSegments.is_tree_valid())
102 maSegments.build_tree();
103 #endif
105 bool bValue;
106 SCCOLROW nPos1, nPos2;
107 #if USE_TREE_SEARCH
108 if (!maSegments.search_tree(nPos, bValue, &nPos1, &nPos2))
109 return false;
110 #else
111 if (!maSegments.search(nPos, bValue, &nPos1, &nPos2))
112 return false;
113 #endif
115 rData.mnPos1 = nPos1;
116 rData.mnPos2 = nPos2-1; // end point is not inclusive.
117 rData.mbValue = bValue;
118 return true;
121 void ScFlatBoolSegmentsImpl::removeSegment(SCCOLROW nPos1, SCCOLROW nPos2)
123 maSegments.shift_segment_left(nPos1, nPos2);
126 void ScFlatBoolSegmentsImpl::insertSegment(SCCOLROW nPos, SCCOLROW nSize, bool bSkipStartBoundary)
128 maSegments.shift_segment_right(nPos, nSize, bSkipStartBoundary);
131 // ============================================================================
133 ScFlatBoolRowSegments::ScFlatBoolRowSegments() :
134 mpImpl(new ScFlatBoolSegmentsImpl(static_cast<SCCOLROW>(MAXROW)))
138 ScFlatBoolRowSegments::~ScFlatBoolRowSegments()
142 void ScFlatBoolRowSegments::setTrue(SCROW nRow1, SCROW nRow2)
144 mpImpl->setTrue(static_cast<SCCOLROW>(nRow1), static_cast<SCCOLROW>(nRow2));
147 void ScFlatBoolRowSegments::setFalse(SCROW nRow1, SCROW nRow2)
149 mpImpl->setFalse(static_cast<SCCOLROW>(nRow1), static_cast<SCCOLROW>(nRow2));
152 bool ScFlatBoolRowSegments::getValue(SCROW nRow)
154 return mpImpl->getValue(static_cast<SCCOLROW>(nRow));
157 bool ScFlatBoolRowSegments::getRangeData(SCROW nRow, RangeData& rData)
159 ScFlatBoolSegmentsImpl::RangeData aData;
160 if (!mpImpl->getRangeData(static_cast<SCCOLROW>(nRow), aData))
161 return false;
163 rData.mbValue = aData.mbValue;
164 rData.mnRow1 = static_cast<SCROW>(aData.mnPos1);
165 rData.mnRow2 = static_cast<SCROW>(aData.mnPos2);
166 return true;
169 void ScFlatBoolRowSegments::removeSegment(SCROW nRow1, SCROW nRow2)
171 mpImpl->removeSegment(static_cast<SCCOLROW>(nRow1), static_cast<SCCOLROW>(nRow2));
174 void ScFlatBoolRowSegments::insertSegment(SCROW nRow, SCROW nSize, bool bSkipStartBoundary)
176 mpImpl->insertSegment(static_cast<SCCOLROW>(nRow), static_cast<SCCOLROW>(nSize), bSkipStartBoundary);
179 // ============================================================================
181 ScFlatBoolColSegments::ScFlatBoolColSegments() :
182 mpImpl(new ScFlatBoolSegmentsImpl(static_cast<SCCOLROW>(MAXCOL)))
186 ScFlatBoolColSegments::~ScFlatBoolColSegments()
190 void ScFlatBoolColSegments::setTrue(SCCOL nCol1, SCCOL nCol2)
192 mpImpl->setTrue(static_cast<SCCOLROW>(nCol1), static_cast<SCCOLROW>(nCol2));
195 void ScFlatBoolColSegments::setFalse(SCCOL nCol1, SCCOL nCol2)
197 mpImpl->setFalse(static_cast<SCCOLROW>(nCol1), static_cast<SCCOLROW>(nCol2));
200 bool ScFlatBoolColSegments::getValue(SCCOL nCol)
202 return mpImpl->getValue(static_cast<SCCOLROW>(nCol));
205 bool ScFlatBoolColSegments::getRangeData(SCCOL nCol, RangeData& rData)
207 ScFlatBoolSegmentsImpl::RangeData aData;
208 if (!mpImpl->getRangeData(static_cast<SCCOLROW>(nCol), aData))
209 return false;
211 rData.mbValue = aData.mbValue;
212 rData.mnCol1 = static_cast<SCCOL>(aData.mnPos1);
213 rData.mnCol2 = static_cast<SCCOL>(aData.mnPos2);
214 return true;
217 void ScFlatBoolColSegments::removeSegment(SCCOL nCol1, SCCOL nCol2)
219 mpImpl->removeSegment(static_cast<SCCOLROW>(nCol1), static_cast<SCCOLROW>(nCol2));
222 void ScFlatBoolColSegments::insertSegment(SCCOL nCol, SCCOL nSize, bool bSkipStartBoundary)
224 mpImpl->insertSegment(static_cast<SCCOLROW>(nCol), static_cast<SCCOLROW>(nSize), bSkipStartBoundary);