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 : #ifndef INCLUDED_SC_INC_BIGRANGE_HXX
21 : #define INCLUDED_SC_INC_BIGRANGE_HXX
22 :
23 : #include "global.hxx"
24 : #include <algorithm>
25 :
26 : static const sal_Int32 nInt32Min = 0x80000000;
27 : static const sal_Int32 nInt32Max = 0x7fffffff;
28 :
29 : class ScDocument;
30 :
31 : class ScBigAddress
32 : {
33 : sal_Int32 nRow;
34 : sal_Int32 nCol;
35 : sal_Int32 nTab;
36 :
37 : public:
38 0 : ScBigAddress() : nRow(0), nCol(0), nTab(0) {}
39 104 : ScBigAddress( sal_Int32 nColP, sal_Int32 nRowP, sal_Int32 nTabP )
40 104 : : nRow( nRowP ), nCol( nColP ), nTab( nTabP ) {}
41 652 : ScBigAddress( const ScBigAddress& r )
42 652 : : nRow( r.nRow ), nCol( r.nCol ), nTab( r.nTab ) {}
43 108 : ScBigAddress( const ScAddress& r )
44 108 : : nRow( r.Row() ), nCol( r.Col() ), nTab( r.Tab() ) {}
45 :
46 384 : sal_Int32 Col() const { return nCol; }
47 670 : sal_Int32 Row() const { return nRow; }
48 96 : sal_Int32 Tab() const { return nTab; }
49 :
50 0 : void Set( sal_Int32 nColP, sal_Int32 nRowP, sal_Int32 nTabP )
51 0 : { nCol = nColP; nRow = nRowP; nTab = nTabP; }
52 144 : void SetCol( sal_Int32 nColP ) { nCol = nColP; }
53 312 : void SetRow( sal_Int32 nRowP ) { nRow = nRowP; }
54 0 : void SetTab( sal_Int32 nTabP ) { nTab = nTabP; }
55 0 : void IncCol( sal_Int32 n = 1 ) { nCol += n; }
56 0 : void IncRow( sal_Int32 n = 1 ) { nRow += n; }
57 0 : void IncTab( sal_Int32 n = 1 ) { nTab += n; }
58 :
59 576 : void GetVars( sal_Int32& nColP, sal_Int32& nRowP, sal_Int32& nTabP ) const
60 576 : { nColP = nCol; nRowP = nRow; nTabP = nTab; }
61 :
62 : inline void PutInOrder( ScBigAddress& r );
63 : bool IsValid( const ScDocument* pDoc ) const;
64 : inline ScAddress MakeAddress() const;
65 :
66 0 : ScBigAddress& operator=( const ScBigAddress& r )
67 0 : { nCol = r.nCol; nRow = r.nRow; nTab = r.nTab; return *this; }
68 : ScBigAddress& operator=( const ScAddress& r )
69 : { nCol = r.Col(); nRow = r.Row(); nTab = r.Tab(); return *this; }
70 490 : bool operator==( const ScBigAddress& r ) const
71 490 : { return nCol == r.nCol && nRow == r.nRow && nTab == r.nTab; }
72 : bool operator!=( const ScBigAddress& r ) const
73 : { return !operator==( r ); }
74 :
75 : friend inline SvStream& WriteScBigAddress( SvStream& rStream, const ScBigAddress& rAdr );
76 : friend inline SvStream& ReadScBigAddress( SvStream& rStream, ScBigAddress& rAdr );
77 : };
78 :
79 : inline void ScBigAddress::PutInOrder( ScBigAddress& r )
80 : {
81 : sal_Int32 nTmp;
82 : if ( r.nCol < nCol )
83 : {
84 : nTmp = r.nCol;
85 : r.nCol = nCol;
86 : nCol = nTmp;
87 : }
88 : if ( r.nRow < nRow )
89 : {
90 : nTmp = r.nRow;
91 : r.nRow = nRow;
92 : nRow = nTmp;
93 : }
94 : if ( r.nTab < nTab )
95 : {
96 : nTmp = r.nTab;
97 : r.nTab = nTab;
98 : nTab = nTmp;
99 : }
100 : }
101 :
102 77 : inline ScAddress ScBigAddress::MakeAddress() const
103 : {
104 : SCCOL nColA;
105 : SCROW nRowA;
106 : SCTAB nTabA;
107 :
108 77 : if ( nCol < 0 )
109 16 : nColA = 0;
110 61 : else if ( nCol > MAXCOL )
111 16 : nColA = MAXCOL;
112 : else
113 45 : nColA = (SCCOL) nCol;
114 :
115 77 : if ( nRow < 0 )
116 0 : nRowA = 0;
117 77 : else if ( nRow > MAXROW )
118 4 : nRowA = MAXROW;
119 : else
120 73 : nRowA = (SCROW) nRow;
121 :
122 77 : if ( nTab < 0 )
123 0 : nTabA = 0;
124 77 : else if ( nTab > MAXTAB )
125 0 : nTabA = MAXTAB;
126 : else
127 77 : nTabA = (SCTAB) nTab;
128 :
129 77 : return ScAddress( nColA, nRowA, nTabA );
130 : }
131 :
132 : inline SvStream& WriteScBigAddress( SvStream& rStream, const ScBigAddress& rAdr )
133 : {
134 : rStream.WriteInt32( rAdr.nCol ).WriteInt32( rAdr.nRow ).WriteInt32( rAdr.nTab );
135 : return rStream;
136 : }
137 :
138 : inline SvStream& ReadScBigAddress( SvStream& rStream, ScBigAddress& rAdr )
139 : {
140 : rStream.ReadInt32( rAdr.nCol ).ReadInt32( rAdr.nRow ).ReadInt32( rAdr.nTab );
141 : return rStream;
142 : }
143 :
144 : class ScBigRange
145 : {
146 : public:
147 :
148 : ScBigAddress aStart;
149 : ScBigAddress aEnd;
150 :
151 0 : ScBigRange() : aStart(), aEnd() {}
152 : ScBigRange( const ScBigAddress& s, const ScBigAddress& e )
153 : : aStart( s ), aEnd( e ) { aStart.PutInOrder( aEnd ); }
154 326 : ScBigRange( const ScBigRange& r )
155 326 : : aStart( r.aStart ), aEnd( r.aEnd ) {}
156 54 : ScBigRange( const ScRange& r )
157 54 : : aStart( r.aStart ), aEnd( r.aEnd ) {}
158 : ScBigRange( const ScBigAddress& r )
159 : : aStart( r ), aEnd( r ) {}
160 : ScBigRange( const ScAddress& r )
161 : : aStart( r ), aEnd( r ) {}
162 : ScBigRange( sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nTab )
163 : : aStart( nCol, nRow, nTab ), aEnd( aStart ) {}
164 52 : ScBigRange( sal_Int32 nCol1, sal_Int32 nRow1, sal_Int32 nTab1,
165 : sal_Int32 nCol2, sal_Int32 nRow2, sal_Int32 nTab2 )
166 : : aStart( nCol1, nRow1, nTab1 ),
167 52 : aEnd( nCol2, nRow2, nTab2 ) {}
168 :
169 0 : void Set( sal_Int32 nCol1, sal_Int32 nRow1, sal_Int32 nTab1,
170 : sal_Int32 nCol2, sal_Int32 nRow2, sal_Int32 nTab2 )
171 0 : { aStart.Set( nCol1, nRow1, nTab1 );
172 0 : aEnd.Set( nCol2, nRow2, nTab2 ); }
173 :
174 288 : void GetVars( sal_Int32& nCol1, sal_Int32& nRow1, sal_Int32& nTab1,
175 : sal_Int32& nCol2, sal_Int32& nRow2, sal_Int32& nTab2 ) const
176 288 : { aStart.GetVars( nCol1, nRow1, nTab1 );
177 288 : aEnd.GetVars( nCol2, nRow2, nTab2 ); }
178 :
179 1 : bool IsValid( const ScDocument* pDoc ) const
180 1 : { return aStart.IsValid( pDoc ) && aEnd.IsValid( pDoc ); }
181 30 : inline ScRange MakeRange() const
182 : { return ScRange( aStart.MakeAddress(),
183 30 : aEnd.MakeAddress() ); }
184 :
185 : inline bool In( const ScBigAddress& ) const; ///< is Address& in range?
186 : inline bool In( const ScBigRange& ) const; ///< is Range& in range?
187 : inline bool Intersects( const ScBigRange& ) const; ///< do two ranges overlap?
188 :
189 0 : ScBigRange& operator=( const ScBigRange& r )
190 0 : { aStart = r.aStart; aEnd = r.aEnd; return *this; }
191 196 : bool operator==( const ScBigRange& r ) const
192 196 : { return (aStart == r.aStart) && (aEnd == r.aEnd); }
193 144 : bool operator!=( const ScBigRange& r ) const
194 144 : { return !operator==( r ); }
195 :
196 : friend inline SvStream& WriteScBigRange( SvStream& rStream, const ScBigRange& rRange );
197 : friend inline SvStream& ReadScBigRange( SvStream& rStream, ScBigRange& rRange );
198 : };
199 :
200 0 : inline bool ScBigRange::In( const ScBigAddress& rAddr ) const
201 : {
202 : return
203 0 : aStart.Col() <= rAddr.Col() && rAddr.Col() <= aEnd.Col() &&
204 0 : aStart.Row() <= rAddr.Row() && rAddr.Row() <= aEnd.Row() &&
205 0 : aStart.Tab() <= rAddr.Tab() && rAddr.Tab() <= aEnd.Tab();
206 : }
207 :
208 0 : inline bool ScBigRange::In( const ScBigRange& r ) const
209 : {
210 : return
211 0 : aStart.Col() <= r.aStart.Col() && r.aEnd.Col() <= aEnd.Col() &&
212 0 : aStart.Row() <= r.aStart.Row() && r.aEnd.Row() <= aEnd.Row() &&
213 0 : aStart.Tab() <= r.aStart.Tab() && r.aEnd.Tab() <= aEnd.Tab();
214 : }
215 :
216 96 : inline bool ScBigRange::Intersects( const ScBigRange& r ) const
217 : {
218 : return !(
219 672 : std::min( aEnd.Col(), r.aEnd.Col() ) < std::max( aStart.Col(), r.aStart.Col() )
220 192 : || std::min( aEnd.Row(), r.aEnd.Row() ) < std::max( aStart.Row(), r.aStart.Row() )
221 216 : || std::min( aEnd.Tab(), r.aEnd.Tab() ) < std::max( aStart.Tab(), r.aStart.Tab() )
222 792 : );
223 : }
224 :
225 : inline SvStream& WriteScBigRange ( SvStream& rStream, const ScBigRange& rRange )
226 : {
227 : WriteScBigAddress( rStream, rRange.aStart );
228 : WriteScBigAddress( rStream, rRange.aEnd );
229 : return rStream;
230 : }
231 :
232 : inline SvStream& ReadScBigRange( SvStream& rStream, ScBigRange& rRange )
233 : {
234 : ReadScBigAddress( rStream, rRange.aStart );
235 : ReadScBigAddress( rStream, rRange.aEnd );
236 : return rStream;
237 : }
238 :
239 : #endif
240 :
241 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|