Update to m13
[ooovba.git] / sc / source / core / data / segmenttree.cxx
blobb534de1ed5c70570ab69ee5ada5a1d338dec5377
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);
56 private:
57 ScFlatBoolSegmentsImpl();
58 ScFlatBoolSegmentsImpl(const ScFlatBoolSegmentsImpl&);
60 ::mdds::flat_segment_tree<SCCOLROW, bool> maSegments;
63 ScFlatBoolSegmentsImpl::ScFlatBoolSegmentsImpl(SCCOLROW nMax) :
64 maSegments(0, nMax+1, false)
68 ScFlatBoolSegmentsImpl::~ScFlatBoolSegmentsImpl()
72 void ScFlatBoolSegmentsImpl::setTrue(SCCOLROW nPos1, SCCOLROW nPos2)
74 maSegments.insert_segment(nPos1, nPos2+1, true);
77 void ScFlatBoolSegmentsImpl::setFalse(SCCOLROW nPos1, SCCOLROW nPos2)
79 maSegments.insert_segment(nPos1, nPos2+1, false);
82 bool ScFlatBoolSegmentsImpl::getValue(SCCOLROW nPos)
84 bool bValue = false;
85 #if USE_TREE_SEARCH
86 if (!maSegments.is_tree_valid())
87 maSegments.build_tree();
89 maSegments.search_tree(nPos, bValue);
90 #else
91 maSegments.search(nPos, bValue);
92 #endif
93 return bValue;
96 bool ScFlatBoolSegmentsImpl::getRangeData(SCCOLROW nPos, RangeData& rData)
98 #if USE_TREE_SEARCH
99 if (!maSegments.is_tree_valid())
100 maSegments.build_tree();
101 #endif
103 bool bValue;
104 SCCOLROW nPos1, nPos2;
105 #if USE_TREE_SEARCH
106 if (!maSegments.search_tree(nPos, bValue, &nPos1, &nPos2))
107 return false;
108 #else
109 if (!maSegments.search(nPos, bValue, &nPos1, &nPos2))
110 return false;
111 #endif
113 rData.mnPos1 = nPos1;
114 rData.mnPos2 = nPos2-1; // end point is not inclusive.
115 rData.mbValue = bValue;
116 return true;
119 // ============================================================================
121 ScFlatBoolRowSegments::ScFlatBoolRowSegments() :
122 mpImpl(new ScFlatBoolSegmentsImpl(static_cast<SCCOLROW>(MAXROW)))
126 ScFlatBoolRowSegments::~ScFlatBoolRowSegments()
130 void ScFlatBoolRowSegments::setTrue(SCROW nRow1, SCROW nRow2)
132 mpImpl->setTrue(static_cast<SCCOLROW>(nRow1), static_cast<SCCOLROW>(nRow2));
135 void ScFlatBoolRowSegments::setFalse(SCROW nRow1, SCROW nRow2)
137 mpImpl->setFalse(static_cast<SCCOLROW>(nRow1), static_cast<SCCOLROW>(nRow2));
140 bool ScFlatBoolRowSegments::getValue(SCROW nRow)
142 return mpImpl->getValue(static_cast<SCCOLROW>(nRow));
145 bool ScFlatBoolRowSegments::getRangeData(SCROW nRow, RangeData& rData)
147 ScFlatBoolSegmentsImpl::RangeData aData;
148 if (!mpImpl->getRangeData(static_cast<SCCOLROW>(nRow), aData))
149 return false;
151 rData.mbValue = aData.mbValue;
152 rData.mnRow1 = static_cast<SCROW>(aData.mnPos1);
153 rData.mnRow2 = static_cast<SCROW>(aData.mnPos2);
154 return true;
157 // ============================================================================
159 ScFlatBoolColSegments::ScFlatBoolColSegments() :
160 mpImpl(new ScFlatBoolSegmentsImpl(static_cast<SCCOLROW>(MAXCOL)))
164 ScFlatBoolColSegments::~ScFlatBoolColSegments()
168 void ScFlatBoolColSegments::setTrue(SCCOL nCol1, SCCOL nCol2)
170 mpImpl->setTrue(static_cast<SCCOLROW>(nCol1), static_cast<SCCOLROW>(nCol2));
173 void ScFlatBoolColSegments::setFalse(SCCOL nCol1, SCCOL nCol2)
175 mpImpl->setFalse(static_cast<SCCOLROW>(nCol1), static_cast<SCCOLROW>(nCol2));
178 bool ScFlatBoolColSegments::getValue(SCCOL nCol)
180 return mpImpl->getValue(static_cast<SCCOLROW>(nCol));
183 bool ScFlatBoolColSegments::getRangeData(SCCOL nCol, RangeData& rData)
185 ScFlatBoolSegmentsImpl::RangeData aData;
186 if (!mpImpl->getRangeData(static_cast<SCCOLROW>(nCol), aData))
187 return false;
189 rData.mbValue = aData.mbValue;
190 rData.mnCol1 = static_cast<SCCOL>(aData.mnPos1);
191 rData.mnCol2 = static_cast<SCCOL>(aData.mnPos2);
192 return true;